45 enabled_ = !res.is_selection() &&
state.show_attribute_viewer_text();
57 const Object &
object = *ob_ref.object;
58 const bool is_preview = ob_ref.preview_base_geometry() !=
nullptr;
64 const float4x4 &object_to_world =
object.object_to_world();
66 if (ob_ref.preview_instance_index() >= 0) {
67 const bke::Instances *instances = ob_ref.preview_base_geometry()->get_instances();
69 add_instance_attributes_to_text_cache(
70 dt, instances->
attributes(), object_to_world, ob_ref.preview_instance_index());
76 switch (
object.type) {
79 add_mesh_attributes_to_text_cache(
state,
mesh, object_to_world);
84 add_attributes_to_text_cache(dt, pointcloud.attributes(), object_to_world);
91 add_attributes_to_text_cache(dt,
curves.attributes(), object_to_world);
98 add_attributes_to_text_cache(dt,
curves.attributes(), object_to_world);
111 if (!attribute_accessor.
contains(
".viewer")) {
119 add_values_to_text_cache(dt,
attribute.varray, positions, object_to_world);
122 void add_mesh_attributes_to_text_cache(
const State &
state,
127 if (!attributes.
contains(
".viewer")) {
133 const VArraySpan<float3> positions = *attributes.
lookup<
float3>(
"position", domain);
136 const CPPType &type =
attribute.varray.type();
137 float offset_by_type = 1.0f;
141 offset_by_type = 1.5f;
144 offset_by_type = 3.0f;
147 Array<float3> corner_positions(positions.size());
148 const Span<float3> positions = mesh.vert_positions();
149 const OffsetIndices<int> faces = mesh.faces();
150 const Span<int> corner_verts = mesh.corner_verts();
151 const Span<float3> face_normals = mesh.face_normals();
154 for (const int face_index : range) {
155 const float3 &face_normal = face_normals[face_index];
156 const IndexRange face = faces[face_index];
157 for (const int corner : face) {
158 const int corner_prev = bke::mesh::face_corner_prev(face, corner);
159 const int corner_next = bke::mesh::face_corner_next(face, corner);
160 corner_positions[corner] = calc_corner_text_position(
161 positions[corner_verts[corner]],
162 positions[corner_verts[corner_prev]],
163 positions[corner_verts[corner_next]],
171 add_values_to_text_cache(
172 state.dt,
attribute.varray, corner_positions.as_span(), object_to_world);
175 add_values_to_text_cache(
state.dt,
attribute.varray, positions, object_to_world);
179 void add_instance_attributes_to_text_cache(DRWTextStore *dt,
180 bke::AttributeAccessor attribute_accessor,
186 const GVArray
attribute = attribute_accessor.lookup(
".viewer").varray.slice(
192 static void add_text_to_cache(DRWTextStore *dt,
194 const StringRef text,
209 static void add_lines_to_cache(DRWTextStore *dt,
211 const Span<StringRef> lines,
215 const float line_height = text_size * 1.1f *
UI_SCALE_FAC;
216 const float center_offset = (
lines.size() - 1) / 2.0f;
217 for (
const int i :
lines.index_range()) {
218 const StringRef line =
lines[i];
224 (center_offset - i) * line_height,
232 void add_values_to_text_cache(DRWTextStore *dt,
233 const GVArray &values,
234 const Span<float3> positions,
241 const VArray<T> &values_typed = values.typed<T>();
242 for (const int i : values.index_range()) {
243 const float3 position = math::transform_point(object_to_world, positions[i]);
244 const T &value = values_typed[i];
246 if constexpr (std::is_same_v<T, bool>) {
248 const size_t numstr_len = STRNCPY_UTF8_RLEN(numstr, value ?
"True" :
"False");
249 ui::theme::get_color_blend_3ubv(TH_TEXT_HI, value ? TH_SUCCESS : TH_ERROR, 0.5f, col);
250 add_text_to_cache(dt, position, StringRef(numstr, numstr_len), col);
252 else if constexpr (std::is_same_v<T, int8_t>) {
254 const size_t numstr_len = SNPRINTF_UTF8_RLEN(numstr,
"%d", int(value));
255 add_text_to_cache(dt, position, StringRef(numstr, numstr_len), col);
257 else if constexpr (std::is_same_v<T, int>) {
259 const size_t numstr_len = SNPRINTF_UTF8_RLEN(numstr,
"%d", value);
260 add_text_to_cache(dt, position, StringRef(numstr, numstr_len), col);
262 else if constexpr (std::is_same_v<T, int2>) {
263 char x_str[64], y_str[64];
264 const size_t x_str_len = SNPRINTF_UTF8_RLEN(x_str,
"X: %d", value.x);
265 const size_t y_str_len = SNPRINTF_UTF8_RLEN(y_str,
"Y: %d", value.y);
267 dt, position, {StringRef(x_str, x_str_len), StringRef(y_str, y_str_len)}, col);
269 else if constexpr (std::is_same_v<T, float>) {
271 const size_t numstr_len = SNPRINTF_UTF8_RLEN(numstr,
"%g", value);
272 add_text_to_cache(dt, position, StringRef(numstr, numstr_len), col);
274 else if constexpr (std::is_same_v<T, float2>) {
275 char x_str[64], y_str[64];
276 const size_t x_str_len = SNPRINTF_UTF8_RLEN(x_str,
"X: %g", value.x);
277 const size_t y_str_len = SNPRINTF_UTF8_RLEN(y_str,
"Y: %g", value.y);
279 dt, position, {StringRef(x_str, x_str_len), StringRef(y_str, y_str_len)}, col);
281 else if constexpr (std::is_same_v<T, float3>) {
282 char x_str[64], y_str[64], z_str[64];
283 const size_t x_str_len = SNPRINTF_UTF8_RLEN(x_str,
"X: %g", value.x);
284 const size_t y_str_len = SNPRINTF_UTF8_RLEN(y_str,
"Y: %g", value.y);
285 const size_t z_str_len = SNPRINTF_UTF8_RLEN(z_str,
"Z: %g", value.z);
286 add_lines_to_cache(dt,
288 {StringRef(x_str, x_str_len),
289 StringRef(y_str, y_str_len),
290 StringRef(z_str, z_str_len)},
293 else if constexpr (std::is_same_v<T, ColorGeometry4b>) {
294 const ColorGeometry4f color = color::decode(value);
295 char r_str[64], g_str[64], b_str[64], a_str[64];
296 const size_t r_str_len = SNPRINTF_UTF8_RLEN(r_str,
"R: %.3f", color.r);
297 const size_t g_str_len = SNPRINTF_UTF8_RLEN(g_str,
"G: %.3f", color.g);
298 const size_t b_str_len = SNPRINTF_UTF8_RLEN(b_str,
"B: %.3f", color.b);
299 const size_t a_str_len = SNPRINTF_UTF8_RLEN(a_str,
"A: %.3f", color.a);
300 add_lines_to_cache(dt,
302 {StringRef(r_str, r_str_len),
303 StringRef(g_str, g_str_len),
304 StringRef(b_str, b_str_len),
305 StringRef(a_str, a_str_len)},
308 else if constexpr (std::is_same_v<T, ColorGeometry4f>) {
309 char r_str[64], g_str[64], b_str[64], a_str[64];
310 const size_t r_str_len = SNPRINTF_UTF8_RLEN(r_str,
"R: %.3f", value.r);
311 const size_t g_str_len = SNPRINTF_UTF8_RLEN(g_str,
"G: %.3f", value.g);
312 const size_t b_str_len = SNPRINTF_UTF8_RLEN(b_str,
"B: %.3f", value.b);
313 const size_t a_str_len = SNPRINTF_UTF8_RLEN(a_str,
"A: %.3f", value.a);
314 add_lines_to_cache(dt,
316 {StringRef(r_str, r_str_len),
317 StringRef(g_str, g_str_len),
318 StringRef(b_str, b_str_len),
319 StringRef(a_str, a_str_len)},
322 else if constexpr (std::is_same_v<T, math::Quaternion>) {
323 char w_str[64], x_str[64], y_str[64], z_str[64];
324 const size_t w_str_len = SNPRINTF_UTF8_RLEN(w_str,
"W: %.3f", value.w);
325 const size_t x_str_len = SNPRINTF_UTF8_RLEN(x_str,
"X: %.3f", value.x);
326 const size_t y_str_len = SNPRINTF_UTF8_RLEN(y_str,
"Y: %.3f", value.y);
327 const size_t z_str_len = SNPRINTF_UTF8_RLEN(z_str,
"Z: %.3f", value.z);
328 add_lines_to_cache(dt,
330 {StringRef(w_str, w_str_len),
331 StringRef(x_str, x_str_len),
332 StringRef(y_str, y_str_len),
333 StringRef(z_str, z_str_len)},
336 else if constexpr (std::is_same_v<T, float4x4>) {
338 math::EulerXYZ rotation;
340 math::to_loc_rot_scale_safe<true>(value, location, rotation, scale);
342 char location_str[64];
343 const size_t location_str_len = SNPRINTF_UTF8_RLEN(
344 location_str,
"Location: %.3f, %.3f, %.3f", location.x, location.y, location.z);
345 char rotation_str[64];
346 const size_t rotation_str_len = SNPRINTF_UTF8_RLEN(rotation_str,
347 "Rotation: %.3f°, %.3f°, %.3f°",
348 rotation.x().degree(),
349 rotation.y().degree(),
350 rotation.z().degree());
352 const size_t scale_str_len = SNPRINTF_UTF8_RLEN(
353 scale_str,
"Scale: %.3f, %.3f, %.3f", scale.x, scale.y, scale.z);
354 add_lines_to_cache(dt,
356 {StringRef(location_str, location_str_len),
357 StringRef(rotation_str, rotation_str_len),
358 StringRef(scale_str, scale_str_len)},
362 BLI_assert_unreachable();
368 static float3 calc_corner_text_position(
const float3 &corner_pos,
369 const float3 &prev_corner_pos,
370 const float3 &next_corner_pos,
371 const float3 &face_normal,
372 const RegionView3D *rv3d,
374 const float offset_scale = 1.0f)
376 const float3 prev_edge_vec = prev_corner_pos - corner_pos;
377 const float3 next_edge_vec = next_corner_pos - corner_pos;
378 const float3 prev_edge_dir = math::normalize(prev_edge_vec);
379 const float3 next_edge_dir = math::normalize(next_edge_vec);
381 const float pre_edge_len = math::length(prev_edge_vec);
382 const float next_edge_len = math::length(next_edge_vec);
383 const float max_offset = math::min(pre_edge_len, next_edge_len) / 2;
385 const float3 corner_normal = math::cross(next_edge_dir, prev_edge_dir);
386 const float concavity_check = math::dot(corner_normal, face_normal);
387 const float direction_correct = concavity_check > 0.0f ? 1.0f : -1.0f;
388 const float3 bisector_dir = (prev_edge_dir + next_edge_dir) / 2 * direction_correct;
390 const float sharp_factor = std::clamp(math::dot(prev_edge_dir, next_edge_dir), 0.0f, 1.0f);
391 const float sharp_multiplier = math::pow(sharp_factor, 4.0f) * 2 + 1;
393 const float3 pos_o_world = math::transform_point(object_to_world, corner_pos);
395 const float pixel_offset = ui::style_get()->widget.points * 7.0f *
UI_SCALE_FAC;
396 const float screen_space_offset = pixel_size * pixel_offset;
398 const float offset_distance = std::clamp(
399 screen_space_offset * sharp_multiplier * offset_scale, 0.0f, max_offset);
401 return corner_pos + bisector_dir * offset_distance;