Blender V5.3
overlay_attribute_text.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BLI_color.hh"
13#include "BLI_string_utf8.hh"
14
15#include "DNA_curve_types.h"
17
18#include "BKE_attribute.hh"
19#include "BKE_curves.hh"
20#include "BKE_geometry_set.hh"
21#include "BKE_instances.hh"
22#include "BKE_mesh.hh"
23
24#include "DRW_render.hh"
25
26#include "ED_view3d.hh"
27
28#include "UI_interface_c.hh"
29#include "UI_resources.hh"
30
31#include "draw_manager_text.hh"
32
33#include "overlay_base.hh"
34
35namespace blender::draw::overlay {
36
42 public:
43 void begin_sync(Resources &res, const State &state) final
44 {
45 enabled_ = !res.is_selection() && state.show_attribute_viewer_text();
46 }
47
48 void object_sync(Manager & /*manager*/,
49 const ObjectRef &ob_ref,
50 Resources & /*res*/,
51 const State &state) final
52 {
53 if (!enabled_) {
54 return;
55 }
56
57 const Object &object = *ob_ref.object;
58 const bool is_preview = ob_ref.preview_base_geometry() != nullptr;
59 if (!is_preview) {
60 return;
61 }
62
63 DRWTextStore *dt = state.dt;
64 const float4x4 &object_to_world = object.object_to_world();
65
66 if (ob_ref.preview_instance_index() >= 0) {
67 const bke::Instances *instances = ob_ref.preview_base_geometry()->get_instances();
68 if (instances->attributes().contains(".viewer")) {
69 add_instance_attributes_to_text_cache(
70 dt, instances->attributes(), object_to_world, ob_ref.preview_instance_index());
71
72 return;
73 }
74 }
75
76 switch (object.type) {
77 case OB_MESH: {
79 add_mesh_attributes_to_text_cache(state, mesh, object_to_world);
80 break;
81 }
82 case OB_POINTCLOUD: {
84 add_attributes_to_text_cache(dt, pointcloud.attributes(), object_to_world);
85 break;
86 }
87 case OB_CURVES_LEGACY: {
88 const Curve &curve = DRW_object_get_data_for_drawing<Curve>(object);
89 if (curve.curve_eval) {
90 const bke::CurvesGeometry &curves = curve.curve_eval->geometry.wrap();
91 add_attributes_to_text_cache(dt, curves.attributes(), object_to_world);
92 }
93 break;
94 }
95 case OB_CURVES: {
96 const Curves &curves_id = DRW_object_get_data_for_drawing<Curves>(object);
97 const bke::CurvesGeometry &curves = curves_id.geometry.wrap();
98 add_attributes_to_text_cache(dt, curves.attributes(), object_to_world);
99 break;
100 }
101 default:
102 break;
103 }
104 }
105
106 private:
107 void add_attributes_to_text_cache(DRWTextStore *dt,
108 bke::AttributeAccessor attribute_accessor,
109 const float4x4 &object_to_world)
110 {
111 if (!attribute_accessor.contains(".viewer")) {
112 return;
113 }
114
115 const bke::GAttributeReader attribute = attribute_accessor.lookup(".viewer");
116 const VArraySpan<float3> positions = *attribute_accessor.lookup<float3>("position",
117 attribute.domain);
118
119 add_values_to_text_cache(dt, attribute.varray, positions, object_to_world);
120 }
121
122 void add_mesh_attributes_to_text_cache(const State &state,
123 const Mesh &mesh,
124 const float4x4 &object_to_world)
125 {
126 const bke::AttributeAccessor attributes = mesh.attributes();
127 if (!attributes.contains(".viewer")) {
128 return;
129 }
130
131 const bke::GAttributeReader attribute = attributes.lookup(".viewer");
132 const bke::AttrDomain domain = attribute.domain;
133 const VArraySpan<float3> positions = *attributes.lookup<float3>("position", domain);
134
135 if (domain == bke::AttrDomain::Corner) {
136 const CPPType &type = attribute.varray.type();
137 float offset_by_type = 1.0f;
138 if (type.is<int2>() || type.is<float2>() || type.is<float3>() ||
139 type.is<ColorGeometry4b>() || type.is<ColorGeometry4f>() || type.is<math::Quaternion>())
140 {
141 offset_by_type = 1.5f;
142 }
143 else if (type.is<float4x4>()) {
144 offset_by_type = 3.0f;
145 }
146
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();
152
153 threading::parallel_for(faces.index_range(), 512, [&](const IndexRange range) {
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]],
164 face_normal,
165 state.rv3d,
166 object_to_world,
167 offset_by_type);
168 }
169 }
170 });
171 add_values_to_text_cache(
172 state.dt, attribute.varray, corner_positions.as_span(), object_to_world);
173 }
174 else {
175 add_values_to_text_cache(state.dt, attribute.varray, positions, object_to_world);
176 }
177 }
178
179 void add_instance_attributes_to_text_cache(DRWTextStore *dt,
180 bke::AttributeAccessor attribute_accessor,
181 const float4x4 &object_to_world,
182 int instance_index)
183 {
184 /* Data from instances are read as a single value from a given index. The data is converted
185 * back to an array so one function can handle both instance and object data. */
186 const GVArray attribute = attribute_accessor.lookup(".viewer").varray.slice(
188
189 add_values_to_text_cache(dt, attribute, {float3(0, 0, 0)}, object_to_world);
190 }
191
192 static void add_text_to_cache(DRWTextStore *dt,
193 const float3 &position,
194 const StringRef text,
195 const uchar4 &color)
196 {
198 position,
199 text.data(),
200 text.size(),
201 0,
202 0,
204 color,
205 true,
206 true);
207 }
208
209 static void add_lines_to_cache(DRWTextStore *dt,
210 const float3 &position,
211 const Span<StringRef> lines,
212 const uchar4 &color)
213 {
214 const float text_size = ui::style_get()->widget.points;
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];
220 position,
221 line.data(),
222 line.size(),
223 0,
224 (center_offset - i) * line_height,
226 color,
227 true,
228 true);
229 }
230 }
231
232 void add_values_to_text_cache(DRWTextStore *dt,
233 const GVArray &values,
234 const Span<float3> positions,
235 const float4x4 &object_to_world)
236 {
237 uchar col[4];
239
240 bke::attribute_math::to_static_type(values.type(), [&]<typename T>() {
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];
245
246 if constexpr (std::is_same_v<T, bool>) {
247 char numstr[64];
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);
251 }
252 else if constexpr (std::is_same_v<T, int8_t>) {
253 char numstr[64];
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);
256 }
257 else if constexpr (std::is_same_v<T, int>) {
258 char numstr[64];
259 const size_t numstr_len = SNPRINTF_UTF8_RLEN(numstr, "%d", value);
260 add_text_to_cache(dt, position, StringRef(numstr, numstr_len), col);
261 }
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);
266 add_lines_to_cache(
267 dt, position, {StringRef(x_str, x_str_len), StringRef(y_str, y_str_len)}, col);
268 }
269 else if constexpr (std::is_same_v<T, float>) {
270 char numstr[64];
271 const size_t numstr_len = SNPRINTF_UTF8_RLEN(numstr, "%g", value);
272 add_text_to_cache(dt, position, StringRef(numstr, numstr_len), col);
273 }
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);
278 add_lines_to_cache(
279 dt, position, {StringRef(x_str, x_str_len), StringRef(y_str, y_str_len)}, col);
280 }
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,
287 position,
288 {StringRef(x_str, x_str_len),
289 StringRef(y_str, y_str_len),
290 StringRef(z_str, z_str_len)},
291 col);
292 }
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,
301 position,
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)},
306 col);
307 }
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,
315 position,
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)},
320 col);
321 }
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,
329 position,
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)},
334 col);
335 }
336 else if constexpr (std::is_same_v<T, float4x4>) {
337 float3 location;
338 math::EulerXYZ rotation;
339 float3 scale;
340 math::to_loc_rot_scale_safe<true>(value, location, rotation, scale);
341
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());
351 char scale_str[64];
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,
355 position,
356 {StringRef(location_str, location_str_len),
357 StringRef(rotation_str, rotation_str_len),
358 StringRef(scale_str, scale_str_len)},
359 col);
360 }
361 else {
362 BLI_assert_unreachable();
363 }
364 }
365 });
366 }
367
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,
373 const float4x4 &object_to_world,
374 const float offset_scale = 1.0f)
375 {
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);
380
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;
384
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;
389
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;
392
393 const float3 pos_o_world = math::transform_point(object_to_world, corner_pos);
394 const float pixel_size = ED_view3d_pixel_size(rv3d, pos_o_world);
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;
397
398 const float offset_distance = std::clamp(
399 screen_space_offset * sharp_multiplier * offset_scale, 0.0f, max_offset);
400
401 return corner_pos + bisector_dir * offset_distance;
402 }
403};
404
405} // namespace blender::draw::overlay
Low-level operations for curves.
#define UI_SCALE_FAC
constexpr IndexRange()=default
bool contains(StringRef name) const
GAttributeReader lookup(const StringRef name) const
bke::AttributeAccessor attributes() const
Definition instances.cc:68
void object_sync(Manager &, const ObjectRef &ob_ref, Resources &, const State &state) final
void begin_sync(Resources &res, const State &state) final
#define instance_index
#define position
#define attribute(slot)
const int state
#define T
void to_static_type(const CPPType &cpp_type, Fn &&fn)
QuaternionBase< float > Quaternion
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
void get_color_4ubv(int colorid, unsigned char col[4])
const uiStyle * style_get()
VecBase< uint8_t, 4 > uchar4
float ED_view3d_pixel_size(const RegionView3D *rv3d, const float co[3])
@ DRW_TEXT_CACHE_GLOBALSPACE
MatBase< float, 4, 4 > float4x4
unsigned char uchar
Definition BKE_text.h:58
T & DRW_object_get_data_for_drawing(const Object &object)
void DRW_text_cache_add(DRWTextStore *dt, const float co[3], const char *str, const int str_len, short xoffs, short yoffs, short flag, const uchar col[4], const bool shadow, const bool align_center)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
Point object_to_world(Point pt, float4x4 object_to_world)
PipelineCompute lines(lines_main)
const struct Curves * curve_eval
uiFontStyle widget