Blender V5.3
overlay_motion_path.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_math_matrix.hh"
12#include "BLI_string.hh"
13
15
17
18#include "draw_manager_text.hh"
19
20#include "overlay_armature.hh"
21#include "overlay_base.hh"
22
23namespace blender::draw::overlay {
24
30
31 private:
32 PassSimple motion_path_ps_ = {"motion_path_ps_"};
33
34 PassSimple::Sub *line_ps_ = nullptr;
35 PassSimple::Sub *vert_ps_ = nullptr;
36 PassSimple::Sub *preview_ps_ = nullptr;
37
38 public:
39 void begin_sync(Resources &res, const State &state) final
40 {
41 enabled_ = state.v3d && state.show_motion_paths() && !res.is_selection();
42 if (!enabled_) {
43 /* Not used. But release the data. */
44 motion_path_ps_.init();
45 return;
46 }
47
48 {
49 PassSimple &pass = motion_path_ps_;
50 pass.init();
51 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
52 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
53 pass.state_set(DRW_STATE_WRITE_COLOR, state.clipping_plane_count);
54 {
55 PassSimple::Sub &sub = pass.sub("Lines");
56 sub.shader_set(res.shaders->motion_path_line.get());
57 line_ps_ = ⊂
58 }
59 {
60 PassSimple::Sub &sub = pass.sub("Points");
61 sub.shader_set(res.shaders->motion_path_vert.get());
62 vert_ps_ = ⊂
63 }
64 {
65 PassSimple::Sub &sub = pass.sub("Preview");
66 /* We use the same shader for line drawing here. */
67 sub.shader_set(res.shaders->motion_path_line.get());
68 preview_ps_ = ⊂
69 }
70 }
71 }
72
73 void object_sync(Manager & /*manager*/,
74 const ObjectRef &ob_ref,
75 Resources & /*res*/,
76 const State &state) final
77 {
78 if (!enabled_) {
79 return;
80 }
81
82 const Object *object = ob_ref.object;
83
84 if (object->type == OB_ARMATURE) {
85 if (Armatures::is_pose_mode(object, state)) {
86 for (bPoseChannel *pchan : ListBaseWrapper<bPoseChannel>(&object->pose->chanbase)) {
87 if (pchan->mpath) {
88 motion_path_sync(state, object, pchan, object->pose->avs, pchan->mpath);
89 }
90 }
91 }
92 }
93
94 if (object->mpath) {
95 motion_path_sync(state, object, nullptr, object->avs, object->mpath);
96 }
97 }
98
99 void draw_color_only(Framebuffer &framebuffer, Manager &manager, View &view) final
100 {
101 if (!enabled_) {
102 return;
103 }
104 GPU_framebuffer_bind(framebuffer);
105 manager.submit(motion_path_ps_, view);
106 }
107
108 private:
109 void motion_path_sync(const State &state,
110 const Object *ob,
111 const bPoseChannel *pchan,
112 const bAnimVizSettings &avs,
113 bMotionPath *mpath)
114 {
115 /* Avoid 0 size allocations. Current code to calculate motion paths should
116 * sanitize this already [see animviz_ensure_motionpath()], we might however
117 * encounter an older file where this was still possible. */
118 if (mpath->length == 0) {
119 return;
120 }
121
122 const bool show_keyframes = (avs.path_viewflag & MOTIONPATH_VIEW_KFRAS);
123 const bool show_keyframes_number = (avs.path_viewflag & MOTIONPATH_VIEW_KFNOS);
124 const bool show_frame_number = (avs.path_viewflag & MOTIONPATH_VIEW_FNUMS);
125 const bool show_lines = (mpath->flag & MOTIONPATH_FLAG_LINES);
126 const bool custom_color = (mpath->flag & MOTIONPATH_FLAG_CUSTOM);
127 const bool selected = (pchan) ? (pchan->flag & POSE_SELECTED) :
128 (ob->base_flag & BASE_SELECTED);
129
130 const float3 color_pre = custom_color ? float3(mpath->color) : float3(-1.0f);
131 const float3 color_post = custom_color ? float3(mpath->color_post) : float3(-1.0f);
132
133 int stride = max_ii(avs.path_step, 1);
134 int current_frame = state.cfra;
135
136 /* Range of frames to draw the motion path on. Exclusive at the end. */
137 IndexRange frame_range;
138 {
139 int start, end;
140 if (avs.path_type == MOTIONPATH_TYPE_ACFRA) {
141 start = current_frame - avs.path_bc;
142 end = current_frame + avs.path_ac + 1;
143 }
144 else {
145 start = avs.path_sf;
146 end = avs.path_ef + 1;
147 }
148
149 if (start > end) {
150 std::swap(start, end);
151 }
152 start = math::clamp(start, mpath->start_frame, mpath->end_frame);
153 end = math::clamp(end, mpath->start_frame, mpath->end_frame);
154
155 frame_range = IndexRange::from_begin_end(start, end);
156 }
157
158 if (frame_range.is_empty()) {
159 return;
160 }
161
162 int start_index = frame_range.start() - mpath->start_frame;
163
164 Object *camera_eval = nullptr;
165 float4x4 camera_matrix = float4x4::identity();
167 state.v3d->camera)
168 {
169 camera_eval = DEG_get_evaluated(state.depsgraph, state.v3d->camera);
170 Scene *scene = DEG_get_input_scene(state.depsgraph);
171 float4x4 window_matrix;
172 BKE_camera_multiview_window_matrix(&scene->r, camera_eval, nullptr, window_matrix.ptr());
173 /* Storing the inverse perspective matrix of the current camera to convert the verts stored
174 * in NDC space, back into world space from the point of view of the current camera. See
175 * `anim_motion_paths.cc/motionpaths_calc_bake_target`. */
176 camera_matrix = math::invert(window_matrix * camera_eval->world_to_object());
177 }
178
179 /* Show a preview of where the motion path will end up during editing. */
180 if (mpath->runtime->show_preview && current_frame >= mpath->start_frame &&
181 current_frame < mpath->start_frame + mpath->length)
182 {
183 auto &sub = *preview_ps_;
184 /* The current frame has to be out of range for the custom colors to be used correctly. */
185 const int4 motion_path_settings(
186 /* current frame= */ -1, /* range start= */ 0, /* range end */ 1, /* start frame */ 0);
187
188 sub.push_constant("mpath_line_settings", motion_path_settings);
189 sub.push_constant("line_thickness", mpath->line_thickness);
190 sub.push_constant("selected", selected);
191 float3 preview_line_color;
192 ui::theme::get_color_3fv(TH_CFRAME, preview_line_color);
193 sub.push_constant("custom_color_pre", preview_line_color);
194 sub.push_constant("custom_color_post", preview_line_color);
195 sub.push_constant("camera_object_persinv", float4x4::identity());
196
197 gpu::Batch *geom = preview_batch_points_get(*mpath, current_frame, camera_matrix);
198 sub.draw_expand(geom, GPU_PRIM_TRIS, 2, 1, /* vertex_len= */ 1, /* vertex_first= */ 0);
199 }
200
201 /* Draw curve-line of path. */
202 if (show_lines) {
203 const int4 motion_path_settings(
204 current_frame, int(frame_range.start()), int(frame_range.last()), mpath->start_frame);
205
206 auto &sub = *line_ps_;
207 sub.push_constant("mpath_line_settings", motion_path_settings);
208 sub.push_constant("line_thickness", mpath->line_thickness);
209 sub.push_constant("selected", selected);
210 sub.push_constant("custom_color_pre", color_pre);
211 sub.push_constant("custom_color_post", color_post);
212 sub.push_constant("camera_object_persinv", camera_matrix);
213
214 gpu::Batch *geom = mpath_batch_points_get(mpath);
215 /* Only draw the required range. */
216 sub.draw_expand(geom, GPU_PRIM_TRIS, 2, 1, frame_range.size() - 1, start_index);
217 }
218
219 /* Draw points. */
220 {
221 int pt_size = max_ii(mpath->line_thickness - 1, 1);
222 const int4 motion_path_settings = {pt_size, current_frame, mpath->start_frame, stride};
223
224 auto &sub = *vert_ps_;
225 sub.push_constant("mpath_point_settings", motion_path_settings);
226 sub.push_constant("show_key_frames", show_keyframes);
227 sub.push_constant("custom_color_pre", color_pre);
228 sub.push_constant("custom_color_post", color_post);
229 sub.push_constant("camera_object_persinv", camera_matrix);
230
231 gpu::Batch *geom = mpath_batch_points_get(mpath);
232 /* Only draw the required range. */
233 sub.draw(geom, 1, frame_range.size(), start_index);
234 }
235
236 /* Draw frame numbers at each frame-step value. */
237 if (show_frame_number || (show_keyframes_number && show_keyframes)) {
238 uchar4 col, col_kf;
239 /* Color Management: Exception here as texts are drawn in sRGB space directly. */
242 col.w = col_kf.w = 255;
243
244 auto safe_index = [&](int index) { return math::clamp(index, 0, mpath->length - 1); };
245
246 Span<bMotionPathVert> mpv(mpath->points, mpath->length);
247 for (int i = 0; i < frame_range.size(); i += stride) {
248 const bMotionPathVert &mpv_curr = mpv[safe_index(start_index + i)];
249
250 int frame = frame_range.start() + i;
251 bool is_keyframe = (mpv_curr.flag & MOTIONPATH_VERT_KEY) != 0;
252
253 /* Projecting the point into world space from the camera's POV. */
254 float3 vert_world_space = project_point(camera_matrix, float3(mpv_curr.co));
255
256 if ((show_keyframes && show_keyframes_number && is_keyframe) ||
257 (show_frame_number && (i == 0)))
258 {
259 char numstr[32];
260 size_t numstr_len = SNPRINTF_RLEN(numstr, " %d", frame);
262 vert_world_space,
263 numstr,
264 numstr_len,
265 0,
266 0,
268 (is_keyframe) ? col_kf : col);
269 }
270 else if (show_frame_number) {
271 const bMotionPathVert &mpv_prev = mpv[safe_index(start_index + i - stride)];
272 const bMotionPathVert &mpv_next = mpv[safe_index(start_index + i + stride)];
273 /* Only draw frame number if several consecutive highlighted points
274 * don't occur on same point. */
275 if (!math::is_equal(float3(mpv_curr.co), float3(mpv_prev.co)) ||
276 !math::is_equal(float3(mpv_curr.co), float3(mpv_next.co)))
277 {
278 char numstr[32];
279 size_t numstr_len = SNPRINTF_RLEN(numstr, " %d", frame);
281 vert_world_space,
282 numstr,
283 numstr_len,
284 0,
285 0,
287 col);
288 }
289 }
290 }
291 }
292 }
293
294 /* Just convert the CPU cache to GPU cache. */
295 /* TODO(fclem) This should go into a draw_cache_impl_motionpath. */
296 gpu::VertBuf *mpath_vbo_get(bMotionPath *mpath)
297 {
298 if (!mpath->points_vbo) {
299 GPUVertFormat format = {0};
300 /* Match structure of #bMotionPathVert. */
301 GPU_vertformat_attr_add(&format, "pos", gpu::VertAttrType::SFLOAT_32_32_32);
302 GPU_vertformat_attr_add(&format, "flag", gpu::VertAttrType::SINT_32);
303 mpath->points_vbo = GPU_vertbuf_create_with_format(format);
304 GPU_vertbuf_data_alloc(*mpath->points_vbo, mpath->length);
305 /* meh... a useless `memcpy`. */
306 mpath->points_vbo->data<bMotionPathVert>().copy_from({mpath->points, mpath->length});
307 }
308 return mpath->points_vbo;
309 }
310
311 gpu::Batch *mpath_batch_points_get(bMotionPath *mpath)
312 {
313 if (!mpath->batch_points) {
314 mpath->batch_points = GPU_batch_create(GPU_PRIM_POINTS, mpath_vbo_get(mpath), nullptr);
315 }
316 return mpath->batch_points;
317 }
318
319 gpu::VertBuf *preview_vbo_get(bMotionPath &mpath,
320 const int current_frame,
321 const float4x4 &camera_matrix)
322 {
323 if (mpath.runtime->vbo_preview) {
324 return mpath.runtime->vbo_preview;
325 }
326 GPUVertFormat format = {0};
327 GPU_vertformat_attr_add(&format, "pos", gpu::VertAttrType::SFLOAT_32_32_32);
328 GPU_vertformat_attr_add(&format, "flag", gpu::VertAttrType::SINT_32);
329 mpath.runtime->vbo_preview = GPU_vertbuf_create_with_format(format);
330 GPU_vertbuf_data_alloc(*mpath.runtime->vbo_preview, 2);
331
332 bMotionPathVert zero_vert;
333 mpath.runtime->vbo_preview->data<bMotionPathVert>().fill(zero_vert);
334
335 const int mpath_index = current_frame - mpath.start_frame;
336 /* Only draw the preview if the current frame is within the range of the motion path.
337 * Otherwise there is no good point to draw the line towards.*/
338 if (mpath_index >= 0 && mpath_index < mpath.length) {
339 copy_v3_v3(mpath.runtime->vbo_preview->data<bMotionPathVert>()[0].co,
340 mpath.runtime->preview_location);
341 /* The given matrix is expected to project the point into world space. If the point is
342 * already in world space, the matrix should be an identity matrix. */
343 const float3 mpath_point = float3(mpath.points[mpath_index].co);
344 const float3 projected_point = math::project_point(camera_matrix, mpath_point);
345 copy_v3_v3(mpath.runtime->vbo_preview->data<bMotionPathVert>()[1].co, projected_point);
346 }
347 return mpath.runtime->vbo_preview;
348 }
349
350 gpu::Batch *preview_batch_points_get(bMotionPath &mpath,
351 const int current_frame,
352 const float4x4 &camera_matrix)
353 {
354 if (!mpath.runtime->batch_preview) {
355 mpath.runtime->batch_preview = GPU_batch_create(
356 GPU_PRIM_POINTS, preview_vbo_get(mpath, current_frame, camera_matrix), nullptr);
357 }
358 return mpath.runtime->batch_preview;
359 }
360};
361
362} // namespace blender::draw::overlay
#define SNPRINTF_RLEN(dst, format,...)
#define GPU_batch_create(primitive_type, vertex_buf, index_buf)
Definition GPU_batch.hh:143
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr bool is_empty() const
static constexpr IndexRange from_begin_end(const int64_t begin, const int64_t end)
constexpr int64_t start() const
void shader_set(gpu::Shader *shader)
PassBase< DrawCommandBufType > & sub(const char *name)
Definition draw_pass.hh:701
void state_set(DRWState state, int clip_plane_count=0)
void bind_ubo(const char *name, gpu::UniformBuf *buffer)
detail::PassBase< command::DrawCommandBuf > Sub
Definition draw_pass.hh:509
static bool is_pose_mode(const Object *armature_ob, const State &state)
void object_sync(Manager &, const ObjectRef &ob_ref, Resources &, const State &state) final
void begin_sync(Resources &res, const State &state) final
void draw_color_only(Framebuffer &framebuffer, Manager &manager, View &view) final
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
#define index(i)
float2 project_point(float3x3 mat, float2 point)
const int state
detail::Pass< command::DrawCommandBuf > PassSimple
BLI_NOINLINE void fill(T *__restrict data, const T &value, const IndexMask &mask)
T clamp(const T &a, const T &min, const T &max)
CartesianBasis invert(const CartesianBasis &basis)
VectorT project_point(const MatT &mat, const VectorT &point)
bool is_equal(const MatBase< T, NumCol, NumRow > &a, const MatBase< T, NumCol, NumRow > &b, const T epsilon=T(0))
void get_color_3fv(int colorid, float col[3])
void get_color_3ubv(int colorid, unsigned char col[3])
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
VecBase< uint8_t, 4 > uchar4
MINLINE void copy_v3_v3(float r[3], const float a[3])
@ MOTIONPATH_FLAG_CUSTOM
@ MOTIONPATH_FLAG_LINES
@ DRW_TEXT_CACHE_GLOBALSPACE
MINLINE int max_ii(int a, int b)
@ TH_VERTEX_SELECT
void BKE_camera_multiview_window_matrix(const struct RenderData *rd, const struct Object *camera, const char *viewname, float r_winmat[4][4])
ListBaseWrapperTemplate< ListBase, T > ListBaseWrapper
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)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
Scene * DEG_get_input_scene(const Depsgraph *graph)
VecBase< float, 3 > float3
@ MOTIONPATH_BAKE_CAMERA_SPACE
std::string format(const std::string &fmt, Args &&...args)
Definition diagnostic.hh:43
const c_style_mat & ptr() const
eMotionPath_BakeFlag path_bakeflag
eMotionPaths_ViewFlag path_viewflag
eMotionPaths_Types path_type
bke::motionpath::Runtime * runtime
eMotionPath_Flag flag
bMotionPathVert * points
uchar w