Blender V5.3
overlay_armature.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10#include <cstdlib>
11#include <cstring>
12
13#include "DNA_armature_types.h"
15#include "DNA_mesh_types.h"
16#include "DNA_object_types.h"
17#include "DNA_scene_types.h"
18
19#include "DRW_render.hh"
20
22#include "BLI_math_color_c.hh"
23#include "BLI_math_matrix_c.hh"
26#include "BLI_math_vector_c.hh"
27#include "BLI_utildefines.hh"
28
29#include "BKE_action.hh"
30#include "BKE_armature.hh"
31#include "BKE_deform.hh"
32#include "BKE_object.hh"
33#include "BKE_pose.hh"
34
36
37#include "ED_armature.hh"
38#include "ED_view3d.hh"
39
40#include "ANIM_armature.hh"
41#include "ANIM_bonecolor.hh"
42
43#include "UI_resources.hh"
44
45#include "draw_cache.hh"
47#include "draw_manager_text.hh"
48
49#include "overlay_armature.hh"
50
51#include "draw_cache_impl.hh"
52
53namespace blender {
54
55#define PT_DEFAULT_RAD 0.05f /* radius of the point batch. */
56
57using namespace blender::draw::overlay;
58
63 private:
64 union {
65 EditBone *eBone_;
66 bPoseChannel *pchan_;
67 };
68 Bone *bone_; /* Only set when pchan_ is set. */
69 bool is_editbone_; /* Discriminator for the above union. */
70
71 public:
72 UnifiedBonePtr(const UnifiedBonePtr &ptr) = default;
74
75 UnifiedBonePtr(EditBone *eBone) : eBone_(eBone), is_editbone_(true) {}
76 UnifiedBonePtr(bPoseChannel *pchan, Bone *bone) : pchan_(pchan), bone_(bone), is_editbone_(false)
77 {
78 }
79
80 const char *name() const
81 {
82 return is_editbone_ ? eBone_->name : pchan_->name;
83 }
84
85 const EditBone *as_editbone() const
86 {
87 BLI_assert_msg(is_editbone_,
88 "conversion to EditBone* only possible when "
89 "UnifiedBonePtr contains an edit bone");
90 return eBone_;
91 }
93 {
94 BLI_assert_msg(is_editbone_,
95 "conversion to EditBone* only possible when "
96 "UnifiedBonePtr contains an edit bone");
97 return eBone_;
98 }
99
101 {
102 BLI_assert_msg(!is_editbone_,
103 "conversion to bPoseChannel* only possible when "
104 "UnifiedBonePtr contains a pose channel");
105 return pchan_;
106 }
108 {
109 BLI_assert_msg(!is_editbone_,
110 "conversion to bPoseChannel* only possible when "
111 "UnifiedBonePtr contains a pose channel");
112 return pchan_;
113 }
114
115 const Bone *posebone_bone() const
116 {
117 BLI_assert_msg(!is_editbone_,
118 "bPoseChannel's armature Bone* only set when "
119 "UnifiedBonePtr contains a pose channel");
120 return bone_;
121 }
123 {
124 BLI_assert_msg(!is_editbone_,
125 "bPoseChannel's armature Bone* only set when "
126 "UnifiedBonePtr contains a pose channel");
127 return bone_;
128 }
130 {
131 BLI_assert_msg(!is_editbone_,
132 "bPoseChannel's armature Bone* only set when "
133 "UnifiedBonePtr contains a pose channel");
134 return {pchan_, bone_};
135 }
137 {
138 BLI_assert_msg(!is_editbone_,
139 "bPoseChannel's armature Bone* only set when "
140 "UnifiedBonePtr contains a pose channel");
141 return {pchan_, bone_};
142 }
143
144 bool is_editbone() const
145 {
146 return is_editbone_;
147 };
148 bool is_posebone() const
149 {
150 return !is_editbone();
151 };
152
153 void get(const EditBone **eBone, const bPoseChannel **pchan) const
154 {
155 *eBone = eBone_;
156 *pchan = pchan_;
157 }
158 void get(EditBone **eBone, bPoseChannel **pchan)
159 {
160 *eBone = eBone_;
161 *pchan = pchan_;
162 }
163
165 {
166 if (is_editbone_) {
167 return static_cast<eBone_Flag>(eBone_->flag);
168 }
169 /* Making sure the select flag is set correctly since it moved to the pose channel. */
170 eBone_Flag flag = static_cast<eBone_Flag>(bone_->flag);
171 if (pchan_->flag & POSE_SELECTED) {
173 }
174 else {
176 }
177 return flag;
178 }
179
182 {
183 return ePchan_ConstFlag(is_editbone_ ? 0 : pchan_->constflag);
184 }
185
186 bool has_parent() const
187 {
188 return is_editbone_ ? eBone_->parent != nullptr : bone_->parent != nullptr;
189 }
190
191 using f44 = float[4][4];
192 const f44 &disp_mat() const
193 {
194 return is_editbone_ ? eBone_->disp_mat : pchan_->disp_mat;
195 }
197 {
198 return is_editbone_ ? eBone_->disp_mat : pchan_->disp_mat;
199 }
200
201 const f44 &disp_tail_mat() const
202 {
203 return is_editbone_ ? eBone_->disp_tail_mat : pchan_->disp_tail_mat;
204 }
205
207 {
208 return is_editbone_ ? eBone_->disp_tail_mat : pchan_->disp_tail_mat;
209 }
210
211 /* For some, to me unknown, reason, the drawing code passes these around as pointers. This is the
212 * reason that these are returned as references. I'll leave refactoring that for another time. */
213 const float &rad_head() const
214 {
215 return is_editbone_ ? eBone_->rad_head : bone_->rad_head;
216 }
217
218 const float &rad_tail() const
219 {
220 return is_editbone_ ? eBone_->rad_tail : bone_->rad_tail;
221 }
222
224 {
225 if (is_editbone_) {
226 return eBone_->color.wrap();
227 }
228
229 if (pchan_->color.palette_index == 0) {
230 /* If the pchan has the 'default' color, treat it as a signal to use the underlying bone
231 * color. */
232 return bone_->color.wrap();
233 }
234 return pchan_->color.wrap();
235 }
236};
237
238/* -------------------------------------------------------------------- */
241
242/* Stick */
244 const float (*bone_mat)[4],
245 const float col_wire[4],
246 const float col_bone[4],
247 const float col_head[4],
248 const float col_tail[4],
249 const int select_id)
250{
251 float4x4 bmat = float4x4(bone_mat);
252 float3 head = math::transform_point(ctx->ob->object_to_world(), bmat.location());
253 float3 tail = math::transform_point(ctx->ob->object_to_world(), bmat.location() + bmat.y_axis());
254
255 auto sel_id = ctx->res->select_id(*ctx->ob_ref, select_id);
256
257 ctx->bone_buf->stick_buf.append({head,
258 tail,
259 *reinterpret_cast<float4 *>(const_cast<float *>(col_wire)),
260 *reinterpret_cast<float4 *>(const_cast<float *>(col_bone)),
261 *reinterpret_cast<float4 *>(const_cast<float *>(col_head)),
262 *reinterpret_cast<float4 *>(const_cast<float *>(col_tail))},
263 sel_id);
264}
265
266/* Envelope */
268 const float (*bone_mat)[4],
269 const float *radius_head,
270 const float *radius_tail,
271 const float *distance)
272{
273 if (ctx->draw_envelope_distance) {
274 float head_sph[4] = {0.0f, 0.0f, 0.0f, 1.0f}, tail_sph[4] = {0.0f, 1.0f, 0.0f, 1.0f};
275 float xaxis[4] = {1.0f, 0.0f, 0.0f, 1.0f};
276 /* Still less operation than m4 multiplication. */
277 mul_m4_v4(bone_mat, head_sph);
278 mul_m4_v4(bone_mat, tail_sph);
279 mul_m4_v4(bone_mat, xaxis);
280 mul_m4_v4(ctx->ob->object_to_world().ptr(), head_sph);
281 mul_m4_v4(ctx->ob->object_to_world().ptr(), tail_sph);
282 mul_m4_v4(ctx->ob->object_to_world().ptr(), xaxis);
283 sub_v3_v3(xaxis, head_sph);
284 float obscale = mat4_to_scale(ctx->ob->object_to_world().ptr());
285 head_sph[3] = *radius_head * obscale;
286 head_sph[3] += *distance * obscale;
287 tail_sph[3] = *radius_tail * obscale;
288 tail_sph[3] += *distance * obscale;
289 /* TODO(fclem): Cleanup these casts when Overlay Next is shipped. */
290 ctx->bone_buf->envelope_distance_buf.append({*reinterpret_cast<float4 *>(head_sph),
291 *reinterpret_cast<float4 *>(tail_sph),
292 *reinterpret_cast<float3 *>(xaxis)},
294 }
295}
296
298 const float (*bone_mat)[4],
299 const float bone_col[4],
300 const float hint_col[4],
301 const float outline_col[4],
302 const float *radius_head,
303 const float *radius_tail,
304 const int select_id)
305{
306 float head_sph[4] = {0.0f, 0.0f, 0.0f, 1.0f}, tail_sph[4] = {0.0f, 1.0f, 0.0f, 1.0f};
307 float xaxis[4] = {1.0f, 0.0f, 0.0f, 1.0f};
308 /* Still less operation than m4 multiplication. */
309 mul_m4_v4(bone_mat, head_sph);
310 mul_m4_v4(bone_mat, tail_sph);
311 mul_m4_v4(bone_mat, xaxis);
312 mul_m4_v4(ctx->ob->object_to_world().ptr(), head_sph);
313 mul_m4_v4(ctx->ob->object_to_world().ptr(), tail_sph);
314 mul_m4_v4(ctx->ob->object_to_world().ptr(), xaxis);
315 float obscale = mat4_to_scale(ctx->ob->object_to_world().ptr());
316 head_sph[3] = *radius_head * obscale;
317 tail_sph[3] = *radius_tail * obscale;
318
319 auto sel_id = (ctx->bone_buf) ? ctx->res->select_id(*ctx->ob_ref, select_id) :
321
322 if (head_sph[3] < 0.0f || tail_sph[3] < 0.0f) {
324 if (head_sph[3] < 0.0f) {
325 /* Draw Tail only */
326 scale_m4_fl(inst_data.mat, tail_sph[3] / PT_DEFAULT_RAD);
327 copy_v3_v3(inst_data.mat[3], tail_sph);
328 }
329 else {
330 /* Draw Head only */
331 scale_m4_fl(inst_data.mat, head_sph[3] / PT_DEFAULT_RAD);
332 copy_v3_v3(inst_data.mat[3], head_sph);
333 }
334
335 if (ctx->is_filled) {
336 ctx->bone_buf->sphere_fill_buf.append({inst_data.mat44, bone_col, hint_col}, sel_id);
337 }
338 if (outline_col[3] > 0.0f) {
339 ctx->bone_buf->sphere_outline_buf.append({inst_data.mat44, outline_col}, sel_id);
340 }
341 }
342 else {
343 /* Draw Body */
344 float tmp_sph[4];
345 float len = len_v3v3(tail_sph, head_sph);
346 float fac_head = (len - head_sph[3]) / len;
347 float fac_tail = (len - tail_sph[3]) / len;
348 /* Small epsilon to avoid problem with float precision in shader. */
349 if (len > (tail_sph[3] + head_sph[3]) + 1e-8f) {
350 copy_v4_v4(tmp_sph, head_sph);
351 interp_v4_v4v4(head_sph, tail_sph, head_sph, fac_head);
352 interp_v4_v4v4(tail_sph, tmp_sph, tail_sph, fac_tail);
353
354 if (ctx->is_filled) {
355 /* TODO(fclem): Cleanup these casts when Overlay Next is shipped. */
356 ctx->bone_buf->envelope_fill_buf.append(
357 {*reinterpret_cast<float4 *>(head_sph),
358 *reinterpret_cast<float4 *>(tail_sph),
359 *reinterpret_cast<float3 *>(const_cast<float *>(bone_col)),
360 *reinterpret_cast<float3 *>(const_cast<float *>(hint_col)),
361 *reinterpret_cast<float3 *>(xaxis)},
362 sel_id);
363 }
364 if (outline_col[3] > 0.0f) {
365 ctx->bone_buf->envelope_outline_buf.append(
366 {*reinterpret_cast<float4 *>(head_sph),
367 *reinterpret_cast<float4 *>(tail_sph),
368 *reinterpret_cast<float4 *>(const_cast<float *>(outline_col)),
369 *reinterpret_cast<float3 *>(xaxis)},
370 sel_id);
371 }
372 }
373 else {
374 /* Distance between endpoints is too small for a capsule. Draw a Sphere instead. */
375 float fac = max_ff(fac_head, 1.0f - fac_tail);
376 interp_v4_v4v4(tmp_sph, tail_sph, head_sph, clamp_f(fac, 0.0f, 1.0f));
377
379 scale_m4_fl(inst_data.mat, tmp_sph[3] / PT_DEFAULT_RAD);
380 copy_v3_v3(inst_data.mat[3], tmp_sph);
381
382 if (ctx->is_filled) {
383 ctx->bone_buf->sphere_fill_buf.append({inst_data.mat44, bone_col, hint_col}, sel_id);
384 }
385 if (outline_col[3] > 0.0f) {
386 ctx->bone_buf->sphere_outline_buf.append({inst_data.mat44, outline_col}, sel_id);
387 }
388 }
389 }
390}
391
392/* Custom (geometry) */
393
395 Mesh &mesh,
396 const float (*bone_mat)[4],
397 const float bone_color[4],
398 const float hint_color[4],
399 const float outline_color[4],
400 const float wire_width,
401 const draw::select::ID select_id,
402 Object &custom)
403{
404 using namespace blender::draw;
405 /* TODO(fclem): arg... less than ideal but we never iter on this object
406 * to assure batch cache is valid. */
408
409 gpu::Batch *surf = DRW_mesh_batch_cache_get_surface(mesh);
410 gpu::Batch *edges = DRW_mesh_batch_cache_get_edge_detection(mesh, nullptr);
411 gpu::Batch *loose_edges = DRW_mesh_batch_cache_get_loose_edges(mesh);
413
414 if (surf || edges || loose_edges) {
415 inst_data.mat44 = ctx->ob->object_to_world() * float4x4(bone_mat);
416 }
417
418 if (surf) {
419 inst_data.set_hint_color(hint_color);
420 inst_data.set_color(bone_color);
421 if (ctx->is_filled) {
422 ctx->bone_buf->custom_shape_fill_get_buffer(surf).append(inst_data, select_id);
423 }
424 }
425
426 if (edges) {
427 inst_data.set_color(outline_color);
428 ctx->bone_buf->custom_shape_outline_get_buffer(edges).append(inst_data, select_id);
429 }
430
431 if (loose_edges) {
432 inst_data.set_hint_color(outline_color);
433 inst_data.set_color(float4(UNPACK3(outline_color), wire_width / WIRE_WIDTH_COMPRESSION));
434 ctx->bone_buf->custom_shape_wire_get_buffer(loose_edges).append(inst_data, select_id);
435 }
436
437 /* TODO(fclem): needs to be moved elsewhere. */
439}
440
442 Mesh &mesh,
443 const float (*bone_mat)[4],
444 const float color[4],
445 const float wire_width,
446 const draw::select::ID select_id,
447 Object &custom)
448{
449 using namespace blender::draw;
450 /* TODO(fclem): arg... less than ideal but we never iter on this object
451 * to assure batch cache is valid. */
453
454 gpu::Batch *geom = DRW_mesh_batch_cache_get_all_edges(mesh);
455 if (geom) {
457 inst_data.mat44 = ctx->ob->object_to_world() * float4x4(bone_mat);
458 inst_data.set_hint_color(color);
459 inst_data.set_color(float4(UNPACK3(color), wire_width / WIRE_WIDTH_COMPRESSION));
460
461 ctx->bone_buf->custom_shape_wire_get_buffer(geom).append(inst_data, select_id);
462 }
463
464 /* TODO(fclem): needs to be moved elsewhere. */
466}
467
469 Curve *curve,
470 const float (*bone_mat)[4],
471 const float outline_color[4],
472 const float wire_width,
473 const draw::select::ID select_id,
474 Object *custom)
475{
476 using namespace blender::draw;
477 /* TODO(fclem): arg... less than ideal but we never iter on this object
478 * to assure batch cache is valid. */
480
481 /* This only handles curves without any surface. The other curve types should have been converted
482 * to meshes and rendered in the mesh drawing function. */
483 gpu::Batch *loose_edges = nullptr;
484 if (custom->type == OB_FONT) {
485 loose_edges = DRW_cache_text_edge_wire_get(custom);
486 }
487 else {
488 loose_edges = DRW_cache_curve_edge_wire_get(custom);
489 }
490
491 if (loose_edges) {
493 inst_data.mat44 = ctx->ob->object_to_world() * float4x4(bone_mat);
494 inst_data.set_hint_color(outline_color);
495 inst_data.set_color(float4(UNPACK3(outline_color), wire_width / WIRE_WIDTH_COMPRESSION));
496
497 ctx->bone_buf->custom_shape_wire_get_buffer(loose_edges).append(inst_data, select_id);
498 }
499
500 /* TODO(fclem): needs to be moved elsewhere. */
502}
503
505 const float (*bone_mat)[4],
506 const float bone_color[4],
507 const float hint_color[4],
508 const float outline_color[4],
509 const float wire_width,
510 const draw::select::ID select_id,
511 Object *custom)
512{
513 /* The custom object is not an evaluated object, so its object->data field hasn't been replaced
514 * by #data_eval. This is bad since it gives preference to an object's evaluated mesh over any
515 * other data type, but supporting all evaluated geometry components would require a much
516 * larger refactor of this area. */
518 if (mesh != nullptr) {
521 bone_mat,
522 bone_color,
523 hint_color,
524 outline_color,
525 wire_width,
526 select_id,
527 *custom);
528 return;
529 }
530
531 if (ELEM(custom->type, OB_CURVES_LEGACY, OB_FONT, OB_SURF)) {
534 bone_mat,
535 outline_color,
536 wire_width,
537 select_id,
538 custom);
539 }
540}
541
543 const float (*bone_mat)[4],
544 const float color[4],
545 const float wire_width,
546 const draw::select::ID select_id,
547 Object *custom)
548{
549 /* See comments in #drw_shgroup_bone_custom_solid. */
551 if (mesh != nullptr) {
553 ctx, DRW_mesh_get_for_drawing(*mesh), bone_mat, color, wire_width, select_id, *custom);
554 return;
555 }
556
557 if (ELEM(custom->type, OB_CURVES_LEGACY, OB_FONT, OB_SURF)) {
560 bone_mat,
561 color,
562 wire_width,
563 select_id,
564 custom);
565 }
566}
567
569 const float (*bone_mat)[4],
570 const float color[4],
571 const float wire_width,
572 const draw::select::ID select_id,
573 Object *custom)
574{
575 using namespace blender::draw;
576
577 gpu::Batch *geom = nullptr;
578 switch (custom->empty_drawtype) {
579 case OB_PLAINAXES:
580 geom = ctx->res->shapes.plain_axes.get();
581 break;
582 case OB_SINGLE_ARROW:
583 geom = ctx->res->shapes.single_arrow.get();
584 break;
585 case OB_CUBE:
586 geom = ctx->res->shapes.cube.get();
587 break;
588 case OB_CIRCLE:
589 geom = ctx->res->shapes.circle.get();
590 break;
591 case OB_EMPTY_SPHERE:
592 geom = ctx->res->shapes.empty_sphere.get();
593 break;
594 case OB_EMPTY_CONE:
595 geom = ctx->res->shapes.empty_cone.get();
596 break;
597 case OB_ARROWS:
598 geom = ctx->res->shapes.arrows.get();
599 break;
600 case OB_EMPTY_IMAGE:
601 /* Not supported. */
602 return;
603 }
604 BLI_assert(geom);
605
606 const float4 final_color(UNPACK3(color), 1.0f);
607
609 inst_data.mat44 = ctx->ob->object_to_world() * float4x4(bone_mat) *
611 inst_data.set_hint_color(final_color);
612 inst_data.set_color(float4(UNPACK3(final_color), wire_width / WIRE_WIDTH_COMPRESSION));
613
614 ctx->bone_buf->custom_shape_wire_get_buffer(geom).append(inst_data, select_id);
615}
616
617/* Head and tail sphere */
619 const float (*bone_mat)[4],
620 const float bone_color[4],
621 const float hint_color[4],
622 const float outline_color[4],
623 const int select_id)
624{
625 auto sel_id = (ctx->bone_buf) ? ctx->res->select_id(*ctx->ob_ref, select_id) :
627 float4x4 mat = ctx->ob->object_to_world() * float4x4(bone_mat);
628
629 if (ctx->is_filled) {
630 ctx->bone_buf->sphere_fill_buf.append({mat, bone_color, hint_color}, sel_id);
631 }
632 if (outline_color[3] > 0.0f) {
633 ctx->bone_buf->sphere_outline_buf.append({mat, outline_color}, sel_id);
634 }
635}
636
637/* Axes */
639 const float (*bone_mat)[4],
640 const float color[4])
641{
642 float4x4 mat = ctx->ob->object_to_world() * float4x4(bone_mat);
643 /* Move to bone tail. */
644 mat[3] += mat[1];
645 ExtraInstanceData data(mat, color, 0.25f);
646 /* NOTE: Axes are not drawn in bone selection (pose or edit mode).
647 * They are only drawn and selectable in object mode. So only load the object select ID. */
648 ctx->bone_buf->arrows_buf.append(data, ctx->res->select_id(*ctx->ob_ref));
649}
650
651/* Relationship lines */
653 const float start[3],
654 const float end[3],
655 const float color[4])
656{
657 float3 start_pt = math::transform_point(ctx->ob->object_to_world(), float3(start));
658 float3 end_pt = math::transform_point(ctx->ob->object_to_world(), float3(end));
659
660 /* Reverse order to have less stipple overlap. */
661 ctx->bone_buf->relations_buf.append(end_pt, start_pt, float4(color));
662}
663
665 const float start[3],
666 const float end[3])
667{
668 const UniformData &theme = ctx->res->theme;
669 drw_shgroup_bone_relationship_lines_ex(ctx, start, end, theme.colors.wire);
670}
671
673 const float start[3],
674 const float end[3])
675{
676 const UniformData &theme = ctx->res->theme;
677 drw_shgroup_bone_relationship_lines_ex(ctx, start, end, theme.colors.bone_ik_line);
678}
679
681 const float start[3],
682 const float end[3])
683{
684 const UniformData &theme = ctx->res->theme;
685 drw_shgroup_bone_relationship_lines_ex(ctx, start, end, theme.colors.bone_ik_line_no_target);
686}
687
689 const float start[3],
690 const float end[3])
691{
692 const UniformData &theme = ctx->res->theme;
693 drw_shgroup_bone_relationship_lines_ex(ctx, start, end, theme.colors.bone_ik_line_spline);
694}
695
697
698/* -------------------------------------------------------------------- */
704
705/* This function sets the color-set for coloring a certain bone */
707{
709
710 if ((arm.flag & ARM_COL_CUSTOM) == 0) {
711 /* Only set a custom color if that's enabled on this armature. */
712 ctx->bcolor = nullptr;
713 return;
714 }
715
716 const animrig::BoneColor &bone_color = bone.effective_bonecolor();
717 ctx->bcolor = bone_color.effective_color();
718}
719
720/* This function is for brightening/darkening a given color (like
721 * ui::theme::get_color_shade_3ubv())
722 */
723static void cp_shade_color3ub(uchar cp[3], const int offset)
724{
725 int r, g, b;
726
727 r = offset + int(cp[0]);
728 CLAMP(r, 0, 255);
729 g = offset + int(cp[1]);
730 CLAMP(g, 0, 255);
731 b = offset + int(cp[2]);
732 CLAMP(b, 0, 255);
733
734 cp[0] = r;
735 cp[1] = g;
736 cp[2] = b;
737}
738
744static void use_bone_color(float *r_color, const uint8_t *color_from_theme, const int shade_offset)
745{
746 uint8_t srgb_color[4] = {255, 255, 255, 255};
747 /* Only copy RGB, not alpha. The "alpha" channel in the bone theme colors is
748 * essentially just padding, and should be ignored. */
749 copy_v3_v3_uchar(srgb_color, color_from_theme);
750 if (shade_offset != 0) {
751 cp_shade_color3ub(srgb_color, shade_offset);
752 }
753 rgba_uchar_to_float(r_color, srgb_color);
754 /* Meh, hardcoded srgb transform here. */
755 srgb_to_linearrgb_v4(r_color, r_color);
756};
757
758static void get_pchan_color_wire(const UniformData &theme,
759 const ThemeWireColor *bcolor,
760 const eArmatureDrawMode draw_mode,
761 const eBone_Flag boneflag,
762 float r_color[4])
763{
764 const bool draw_active = boneflag & BONE_DRAW_ACTIVE;
765 const bool draw_selected = boneflag & BONE_SELECTED;
766 const bool is_edit = draw_mode == ARM_DRAW_MODE_EDIT;
767 float4 wire_color;
768
769 if (bcolor) {
770 if (draw_active && draw_selected) {
771 use_bone_color(r_color, bcolor->active, 0);
772 }
773 else if (draw_active) {
774 use_bone_color(r_color, bcolor->active, -80);
775 }
776 else if (draw_selected) {
777 use_bone_color(r_color, bcolor->select, 0);
778 }
779 else {
780 use_bone_color(r_color, bcolor->solid, -50);
781 }
782 }
783 else {
784 if (draw_active && draw_selected) {
785 wire_color = is_edit ? theme.colors.bone_active : theme.colors.bone_pose_active;
786 }
787 else if (draw_active) {
788 wire_color = is_edit ? theme.colors.bone_active_unsel : theme.colors.bone_pose_active_unsel;
789 }
790 else if (draw_selected) {
791 wire_color = is_edit ? theme.colors.bone_select : theme.colors.bone_pose;
792 }
793 else {
794 wire_color = is_edit ? theme.colors.wire_edit : theme.colors.wire;
795 }
796 copy_v4_v4(r_color, wire_color);
797 }
798}
799
800static void get_pchan_color_solid(const UniformData &theme,
801 const ThemeWireColor *bcolor,
802 float r_color[4])
803{
804
805 if (bcolor) {
806 use_bone_color(r_color, bcolor->solid, 0);
807 }
808 else {
809 copy_v4_v4(r_color, theme.colors.bone_solid);
810 }
811}
812
814 const ThemeWireColor *bcolor,
815 const UnifiedBonePtr bone,
816 float r_color[4])
817{
818 const ePchan_ConstFlag constflag = bone.constflag();
819 /* Not all flags should result in a different bone color. */
822 if ((constflag & flags_to_color) == 0 ||
823 (bcolor && (bcolor->flag & TH_WIRECOLOR_CONSTCOLS) == 0))
824 {
825 get_pchan_color_solid(theme, bcolor, r_color);
826 return;
827 }
828
829 /* The constraint color needs to be blended with the solid color. */
830 float solid_color[4];
831 get_pchan_color_solid(theme, bcolor, solid_color);
832
833 float4 constraint_color;
834 if (constflag & PCHAN_HAS_NO_TARGET) {
835 constraint_color = theme.colors.bone_pose_no_target;
836 }
837 else if (constflag & PCHAN_HAS_IK) {
838 constraint_color = theme.colors.bone_pose_ik;
839 }
840 else if (constflag & PCHAN_HAS_SPLINEIK) {
841 constraint_color = theme.colors.bone_pose_spline_ik;
842 }
843 else if (constflag & PCHAN_HAS_CONST) {
844 constraint_color = theme.colors.bone_pose_constraint;
845 }
846 interp_v4_v4v4(r_color, solid_color, constraint_color, 0.5f);
847}
848
850
851/* -------------------------------------------------------------------- */
854
855static void bone_locked_color_shade(const UniformData &theme, float color[4])
856{
857 const float *locked_color = theme.colors.bone_locked;
858
859 interp_v3_v3v3(color, color, locked_color, locked_color[3]);
860}
861
862static const float *get_bone_solid_color(const Armatures::DrawContext *ctx,
863 const eBone_Flag boneflag)
864{
865 const UniformData &theme = ctx->res->theme;
866 if (ctx->const_color) {
867 return theme.colors.bone_solid;
868 }
869
870 static float disp_color[4];
871 get_pchan_color_solid(theme, ctx->bcolor, disp_color);
872
873 if (ctx->draw_mode == ARM_DRAW_MODE_POSE && (boneflag & BONE_DRAW_LOCKED_WEIGHT)) {
874 bone_locked_color_shade(theme, disp_color);
875 }
876
877 return disp_color;
878}
879
881 const UnifiedBonePtr bone,
882 const eBone_Flag boneflag)
883{
884 const UniformData &theme = ctx->res->theme;
885 if (ctx->const_color) {
886 return theme.colors.bone_solid;
887 }
888
889 const float *col = get_bone_solid_color(ctx, boneflag);
890
891 if (ctx->draw_mode != ARM_DRAW_MODE_POSE || (boneflag & BONE_DRAW_LOCKED_WEIGHT)) {
892 return col;
893 }
894
895 static float consts_color[4];
896 get_pchan_color_constraint(theme, ctx->bcolor, bone, consts_color);
897 return consts_color;
898}
899
900static float get_bone_wire_thickness(const Armatures::DrawContext *ctx, int boneflag)
901{
902 if (ctx->const_color) {
903 return ctx->const_wire;
904 }
905 if (boneflag & (BONE_DRAW_ACTIVE | BONE_SELECTED)) {
906 return 2.0f;
907 }
908
909 return 1.0f;
910}
911
912static const float *get_bone_wire_color(const Armatures::DrawContext *ctx,
913 const eBone_Flag boneflag)
914{
915 static float disp_color[4];
916
917 if (ctx->const_color) {
918 copy_v3_v3(disp_color, ctx->const_color);
919 }
920 else {
921 const UniformData &theme = ctx->res->theme;
922 switch (ctx->draw_mode) {
924 get_pchan_color_wire(theme, ctx->bcolor, ctx->draw_mode, boneflag, disp_color);
925 break;
927 get_pchan_color_wire(theme, ctx->bcolor, ctx->draw_mode, boneflag, disp_color);
928
929 if (boneflag & BONE_DRAW_LOCKED_WEIGHT) {
930 bone_locked_color_shade(theme, disp_color);
931 }
932 break;
934 copy_v3_v3(disp_color, theme.colors.vert);
935 break;
936 }
937 }
938
939 disp_color[3] = get_bone_wire_thickness(ctx, boneflag);
940
941 return disp_color;
942}
943
944static void bone_hint_color_shade(float hint_color[4], const float color[4])
945{
946 /* Increase contrast. */
947 mul_v3_v3v3(hint_color, color, color);
948 /* Decrease value to add mode shading to the shape. */
949 mul_v3_fl(hint_color, 0.1f);
950 hint_color[3] = 1.0f;
951}
952
953static const float *get_bone_hint_color(const Armatures::DrawContext *ctx,
954 const eBone_Flag boneflag)
955{
956 static float hint_color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
957
958 if (ctx->const_color) {
959 bone_hint_color_shade(hint_color, ctx->res->theme.colors.bone_solid);
960 }
961 else {
962 const float *wire_color = get_bone_wire_color(ctx, boneflag);
963 bone_hint_color_shade(hint_color, wire_color);
964 }
965
966 return hint_color;
967}
968
970
971/* -------------------------------------------------------------------- */
974
975static void pchan_draw_data_init(bPoseChannel *pchan, Bone &bone)
976{
977 if (pchan->draw_data != nullptr) {
978 if (pchan->draw_data->bbone_matrix_len != bone.segments) {
979 MEM_SAFE_DELETE(pchan->draw_data);
980 }
981 }
982
983 if (pchan->draw_data == nullptr) {
984 pchan->draw_data = static_cast<bPoseChannelDrawData *>(
985 MEM_new_uninitialized(sizeof(*pchan->draw_data) + sizeof(Mat4) * bone.segments, __func__));
986 pchan->draw_data->bbone_matrix_len = bone.segments;
987 }
988}
989
991{
992 float ebmat[4][4];
993 float bone_scale[3];
994 float (*bone_mat)[4];
995 float (*disp_mat)[4] = bone.disp_mat();
996 float (*disp_tail_mat)[4] = bone.disp_tail_mat();
997
998 /* TODO: This should be moved to depsgraph or armature refresh
999 * and not be tied to the draw pass creation.
1000 * This would refresh armature without invalidating the draw cache */
1001 if (bone.is_posebone()) {
1002 bPoseChannel *pchan = bone.as_posebone();
1003 Bone *pchan_bone = bone.posebone_bone();
1004 bone_mat = pchan->pose_mat;
1005 copy_v3_fl(bone_scale, pchan_bone->length);
1006 }
1007 else {
1008 EditBone *eBone = bone.as_editbone();
1009 eBone->length = len_v3v3(eBone->tail, eBone->head);
1010 ED_armature_ebone_to_mat4(eBone, ebmat);
1011
1012 copy_v3_fl(bone_scale, eBone->length);
1013 bone_mat = ebmat;
1014 }
1015
1016 copy_m4_m4(disp_mat, bone_mat);
1017 rescale_m4(disp_mat, bone_scale);
1018 copy_m4_m4(disp_tail_mat, disp_mat);
1019 translate_m4(disp_tail_mat, 0.0f, 1.0f, 0.0f);
1020}
1021
1023{
1024 float bone_scale[3];
1025 float (*bone_mat)[4];
1026 float (*disp_mat)[4];
1027 float (*disp_tail_mat)[4];
1028 float rot_mat[3][3];
1029
1030 /* Custom bone shapes are only supported in pose mode for now. */
1031 bPoseChannel *pchan = bone.as_posebone();
1032
1033 /* TODO: This should be moved to depsgraph or armature refresh
1034 * and not be tied to the draw pass creation.
1035 * This would refresh armature without invalidating the draw cache. */
1036 const bke::PChanBoneConst pchanbone{pchan, bone.posebone_bone()};
1037 mul_v3_v3fl(bone_scale, pchan->custom_scale_xyz, PCHAN_CUSTOM_BONE_LENGTH(pchanbone));
1038 bone_mat = pchan->custom_tx ? pchan->custom_tx->pose_mat : pchan->pose_mat;
1039 disp_mat = bone.disp_mat();
1040 disp_tail_mat = pchan->disp_tail_mat;
1041
1043
1044 copy_m4_m4(disp_mat, bone_mat);
1045 translate_m4(disp_mat,
1046 pchan->custom_translation[0],
1047 pchan->custom_translation[1],
1048 pchan->custom_translation[2]);
1049 mul_m4_m4m3(disp_mat, disp_mat, rot_mat);
1050 rescale_m4(disp_mat, bone_scale);
1051 copy_m4_m4(disp_tail_mat, disp_mat);
1052 translate_m4(disp_tail_mat, 0.0f, 1.0f, 0.0f);
1053}
1054
1055/* compute connected child pointer for B-Bone drawing */
1057{
1058 for (EditBone &eBone : *arm->edbo) {
1059 eBone.bbone_child = nullptr;
1060 }
1061
1062 for (EditBone &eBone : *arm->edbo) {
1063 if (eBone.parent && (eBone.flag & BONE_CONNECTED)) {
1064 eBone.parent->bbone_child = &eBone;
1065 }
1066 }
1067}
1068
1069/* A version of BKE_pchan_bbone_spline_setup() for previewing editmode curve settings. */
1070static void ebone_spline_preview(EditBone *ebone, const float result_array[MAX_BBONE_SUBDIV][4][4])
1071{
1073 EditBone *prev, *next;
1074 float imat[4][4], bonemat[4][4];
1075 float tmp[3];
1076
1077 memset(&param, 0, sizeof(param));
1078
1079 param.segments = ebone->segments;
1080 param.length = ebone->length;
1081
1082 /* Get "next" and "prev" bones - these are used for handle calculations. */
1083 if (ebone->bbone_prev_type == BBONE_HANDLE_AUTO) {
1084 /* Use connected parent. */
1085 if (ebone->flag & BONE_CONNECTED) {
1086 prev = ebone->parent;
1087 }
1088 else {
1089 prev = nullptr;
1090 }
1091 }
1092 else {
1093 prev = ebone->bbone_prev;
1094 }
1095
1096 if (ebone->bbone_next_type == BBONE_HANDLE_AUTO) {
1097 /* Use connected child. */
1098 next = ebone->bbone_child;
1099 }
1100 else {
1101 next = ebone->bbone_next;
1102 }
1103
1104 /* compute handles from connected bones */
1105 if (prev || next) {
1106 ED_armature_ebone_to_mat4(ebone, imat);
1107 invert_m4(imat);
1108
1109 if (prev) {
1110 param.use_prev = true;
1111
1112 if (ebone->bbone_prev_type == BBONE_HANDLE_RELATIVE) {
1113 zero_v3(param.prev_h);
1114 }
1115 else if (ebone->bbone_prev_type == BBONE_HANDLE_TANGENT) {
1116 sub_v3_v3v3(tmp, prev->tail, prev->head);
1117 sub_v3_v3v3(tmp, ebone->head, tmp);
1118 mul_v3_m4v3(param.prev_h, imat, tmp);
1119 }
1120 else {
1121 param.prev_bbone = (prev->segments > 1);
1122
1123 mul_v3_m4v3(param.prev_h, imat, prev->head);
1124 }
1125
1126 if (!param.prev_bbone) {
1127 ED_armature_ebone_to_mat4(prev, bonemat);
1128 mul_m4_m4m4(param.prev_mat, imat, bonemat);
1129 }
1130 }
1131
1132 if (next) {
1133 param.use_next = true;
1134
1135 if (ebone->bbone_next_type == BBONE_HANDLE_RELATIVE) {
1136 copy_v3_fl3(param.next_h, 0.0f, param.length, 0.0);
1137 }
1138 else if (ebone->bbone_next_type == BBONE_HANDLE_TANGENT) {
1139 sub_v3_v3v3(tmp, next->tail, next->head);
1140 add_v3_v3v3(tmp, ebone->tail, tmp);
1141 mul_v3_m4v3(param.next_h, imat, tmp);
1142 }
1143 else {
1144 param.next_bbone = (next->segments > 1);
1145
1146 mul_v3_m4v3(param.next_h, imat, next->tail);
1147 }
1148
1149 ED_armature_ebone_to_mat4(next, bonemat);
1150 mul_m4_m4m4(param.next_mat, imat, bonemat);
1151 }
1152 }
1153
1154 param.ease1 = ebone->ease1;
1155 param.ease2 = ebone->ease2;
1156 param.roll1 = ebone->roll1;
1157 param.roll2 = ebone->roll2;
1158
1159 if (prev && (ebone->bbone_flag & BBONE_ADD_PARENT_END_ROLL)) {
1160 param.roll1 += prev->roll2;
1161 }
1162
1163 copy_v3_v3(param.scale_in, ebone->scale_in);
1164 copy_v3_v3(param.scale_out, ebone->scale_out);
1165
1166 param.curve_in_x = ebone->curve_in_x;
1167 param.curve_in_z = ebone->curve_in_z;
1168
1169 param.curve_out_x = ebone->curve_out_x;
1170 param.curve_out_z = ebone->curve_out_z;
1171
1172 if (ebone->bbone_flag & BBONE_SCALE_EASING) {
1173 param.ease1 *= param.scale_in[1];
1174 param.curve_in_x *= param.scale_in[1];
1175 param.curve_in_z *= param.scale_in[1];
1176
1177 param.ease2 *= param.scale_out[1];
1178 param.curve_out_x *= param.scale_out[1];
1179 param.curve_out_z *= param.scale_out[1];
1180 }
1181
1182 ebone->segments = BKE_pchan_bbone_spline_compute(&param, false, (Mat4 *)result_array);
1183}
1184
1185/* This function is used for both B-Bone and Wire matrix updates. */
1187{
1188 float s[4][4], ebmat[4][4];
1189 float length, xwidth, zwidth;
1190 float (*bone_mat)[4];
1191 short bbone_segments;
1192
1193 /* TODO: This should be moved to depsgraph or armature refresh
1194 * and not be tied to the draw pass creation.
1195 * This would refresh armature without invalidating the draw cache. */
1196 if (bone.is_posebone()) {
1197 bPoseChannel *pchan = bone.as_posebone();
1198 const Bone *pchan_bone = bone.posebone_bone();
1199 length = pchan_bone->length;
1200 xwidth = pchan_bone->xwidth;
1201 zwidth = pchan_bone->zwidth;
1202 bone_mat = pchan->pose_mat;
1203 bbone_segments = pchan_bone->segments;
1204 }
1205 else {
1206 EditBone *eBone = bone.as_editbone();
1207 eBone->length = len_v3v3(eBone->tail, eBone->head);
1208 ED_armature_ebone_to_mat4(eBone, ebmat);
1209
1210 length = eBone->length;
1211 xwidth = eBone->xwidth;
1212 zwidth = eBone->zwidth;
1213 bone_mat = ebmat;
1214 bbone_segments = eBone->segments;
1215 }
1216
1217 const float3 size_vec = {xwidth, length / bbone_segments, zwidth};
1218 size_to_mat4(s, size_vec);
1219
1220 /* Compute BBones segment matrices... */
1221 /* Note that we need this even for one-segment bones, because box drawing need specific weirdo
1222 * matrix for the box, that we cannot use to draw end points & co. */
1223 if (bone.is_posebone()) {
1224 bPoseChannel *pchan = bone.as_posebone();
1225 Mat4 *bbones_mat = reinterpret_cast<Mat4 *>(pchan->draw_data->bbone_matrix);
1226 if (bbone_segments > 1) {
1227 BKE_pchan_bbone_spline_setup(bone.as_pchanbone(), armature, false, false, bbones_mat);
1228
1229 for (int i = bbone_segments; i--; bbones_mat++) {
1230 mul_m4_m4m4(bbones_mat->mat, bbones_mat->mat, s);
1231 mul_m4_m4m4(bbones_mat->mat, bone_mat, bbones_mat->mat);
1232 }
1233 }
1234 else {
1235 mul_m4_m4m4(bbones_mat->mat, bone_mat, s);
1236 }
1237 }
1238 else {
1239 EditBone *eBone = bone.as_editbone();
1240 float (*bbones_mat)[4][4] = eBone->disp_bbone_mat;
1241
1242 if (bbone_segments > 1) {
1243 ebone_spline_preview(eBone, bbones_mat);
1244
1245 for (int i = bbone_segments; i--; bbones_mat++) {
1246 mul_m4_m4m4(*bbones_mat, *bbones_mat, s);
1247 mul_m4_m4m4(*bbones_mat, bone_mat, *bbones_mat);
1248 }
1249 }
1250 else {
1251 mul_m4_m4m4(*bbones_mat, bone_mat, s);
1252 }
1253 }
1254
1255 /* Grrr... We need default display matrix to draw end points, axes, etc. :( */
1257}
1258
1259static void draw_axes(const Armatures::DrawContext *ctx,
1260 const UnifiedBonePtr bone,
1261 const bArmature &arm)
1262{
1263 float final_col[4];
1264 const float *col = (ctx->const_color) ? ctx->const_color :
1265 (bone.flag() & BONE_SELECTED) ? &ctx->res->theme.colors.text_hi.x :
1266 &ctx->res->theme.colors.text.x;
1267 copy_v4_v4(final_col, col);
1268 /* Mix with axes color. */
1269 final_col[3] = (ctx->const_color) ? 1.0 : (bone.flag() & BONE_SELECTED) ? 0.1 : 0.65;
1270
1271 if (bone.is_posebone() && bone.as_posebone()->custom && !(arm.flag & ARM_NO_CUSTOM)) {
1272 const bPoseChannel *pchan = bone.as_posebone();
1273 const Bone *pchan_bone = bone.posebone_bone();
1274 /* Special case: Custom bones can have different scale than the bone.
1275 * Recompute display matrix without the custom scaling applied. (#65640). */
1276 float axis_mat[4][4];
1277 float length = pchan_bone->length;
1278 copy_m4_m4(axis_mat, pchan->custom_tx ? pchan->custom_tx->pose_mat : pchan->pose_mat);
1279 const float3 length_vec = {length, length, length};
1280 rescale_m4(axis_mat, length_vec);
1281 translate_m4(axis_mat, 0.0, arm.axes_position - 1.0, 0.0);
1282
1283 drw_shgroup_bone_axes(ctx, axis_mat, final_col);
1284 }
1285 else {
1286 float disp_mat[4][4];
1287 copy_m4_m4(disp_mat, bone.disp_mat());
1288 translate_m4(disp_mat, 0.0, arm.axes_position - 1.0, 0.0);
1289 drw_shgroup_bone_axes(ctx, disp_mat, final_col);
1290 }
1291}
1292
1294 const UnifiedBonePtr bone,
1295 const eBone_Flag boneflag,
1296 const float col_solid[4],
1297 const int select_id)
1298{
1299 float col_wire_root[4], col_wire_tail[4];
1300 float col_hint_root[4], col_hint_tail[4];
1301
1302 const UniformData &theme = ctx->res->theme;
1303
1304 copy_v4_v4(col_wire_root, (ctx->const_color) ? ctx->const_color : &theme.colors.vert.x);
1305 copy_v4_v4(col_wire_tail, (ctx->const_color) ? ctx->const_color : &theme.colors.vert.x);
1306
1307 const bool is_envelope_draw = (ctx->drawtype == ARM_DRAW_TYPE_ENVELOPE);
1308 const float envelope_ignore = -1.0f;
1309
1310 col_wire_tail[3] = col_wire_root[3] = get_bone_wire_thickness(ctx, boneflag);
1311
1312 /* Edit bone points can be selected */
1313 if (ctx->draw_mode == ARM_DRAW_MODE_EDIT) {
1314 const EditBone *eBone = bone.as_editbone();
1315 if (eBone->flag & BONE_ROOTSEL) {
1316 copy_v3_v3(col_wire_root, theme.colors.vert_select);
1317 }
1318 if (eBone->flag & BONE_TIPSEL) {
1319 copy_v3_v3(col_wire_tail, theme.colors.vert_select);
1320 }
1321 }
1322 else if (ctx->draw_mode == ARM_DRAW_MODE_POSE) {
1323 const float *wire_color = get_bone_wire_color(ctx, boneflag);
1324 copy_v4_v4(col_wire_tail, wire_color);
1325 copy_v4_v4(col_wire_root, wire_color);
1326 }
1327
1328 const float *hint_color_shade_root = (ctx->const_color) ?
1329 static_cast<const float *>(theme.colors.bone_solid) :
1330 col_wire_root;
1331 const float *hint_color_shade_tail = (ctx->const_color) ?
1332 static_cast<const float *>(theme.colors.bone_solid) :
1333 col_wire_tail;
1334 bone_hint_color_shade(col_hint_root, hint_color_shade_root);
1335 bone_hint_color_shade(col_hint_tail, hint_color_shade_tail);
1336
1337 /* Draw root point if we are not connected to our parent */
1338
1339 if (!(bone.has_parent() && (boneflag & BONE_CONNECTED))) {
1340 if (is_envelope_draw) {
1342 bone.disp_mat(),
1343 col_solid,
1344 col_hint_root,
1345 col_wire_root,
1346 &bone.rad_head(),
1347 &envelope_ignore,
1348 select_id | BONESEL_ROOT);
1349 }
1350 else {
1352 ctx, bone.disp_mat(), col_solid, col_hint_root, col_wire_root, select_id | BONESEL_ROOT);
1353 }
1354 }
1355
1356 /* Draw tip point. */
1357 if (is_envelope_draw) {
1359 bone.disp_mat(),
1360 col_solid,
1361 col_hint_tail,
1362 col_wire_tail,
1363 &envelope_ignore,
1364 &bone.rad_tail(),
1365 select_id | BONESEL_TIP);
1366 }
1367 else {
1369 bone.disp_tail_mat(),
1370 col_solid,
1371 col_hint_tail,
1372 col_wire_tail,
1373 select_id | BONESEL_TIP);
1374 }
1375}
1376
1378 const UnifiedBonePtr bone,
1379 const eBone_Flag boneflag,
1380 const int select_id)
1381{
1382 const float *col_solid = get_bone_solid_color(ctx, boneflag);
1383 const float *col_wire = get_bone_wire_color(ctx, boneflag);
1384 const float *col_hint = get_bone_hint_color(ctx, boneflag);
1385 const float (*disp_mat)[4] = bone.disp_mat();
1386
1387 auto sel_id = ctx->res->select_id(*ctx->ob_ref, select_id | BONESEL_BONE);
1388
1389 /* Custom bone shapes are only supported in pose mode for now. */
1390 const bPoseChannel *pchan = bone.as_posebone();
1391 Object *custom_shape_ob = pchan->custom;
1392
1393 if (custom_shape_ob->type == OB_EMPTY) {
1394 if (custom_shape_ob->empty_drawtype != OB_EMPTY_IMAGE) {
1396 ctx, disp_mat, col_wire, pchan->custom_shape_wire_width, sel_id, pchan->custom);
1397 }
1398 }
1399 else if (boneflag & (BONE_DRAWWIRE | BONE_DRAW_LOCKED_WEIGHT)) {
1401 ctx, disp_mat, col_wire, pchan->custom_shape_wire_width, sel_id, pchan->custom);
1402 }
1403 else {
1405 disp_mat,
1406 col_solid,
1407 col_hint,
1408 col_wire,
1410 sel_id,
1411 pchan->custom);
1412 }
1413}
1414
1416 const UnifiedBonePtr bone,
1417 const eBone_Flag boneflag,
1418 const int select_id)
1419{
1420 const float *col_solid = get_bone_solid_with_consts_color(ctx, bone, boneflag);
1421 const float *col_wire = get_bone_wire_color(ctx, boneflag);
1422 const float *col_hint = get_bone_hint_color(ctx, boneflag);
1423
1424 auto sel_id = ctx->res->select_id(*ctx->ob_ref, select_id | BONESEL_BONE);
1425 float4x4 bone_mat = ctx->ob->object_to_world() * float4x4(bone.disp_mat());
1426
1427 if (ctx->is_filled) {
1428 ctx->bone_buf->octahedral_fill_buf.append({bone_mat, col_solid, col_hint}, sel_id);
1429 }
1430 if (col_wire[3] > 0.0f) {
1431 ctx->bone_buf->octahedral_outline_buf.append({bone_mat, col_wire}, sel_id);
1432 }
1433
1434 draw_points(ctx, bone, boneflag, col_solid, select_id);
1435}
1436
1438 const UnifiedBonePtr bone,
1439 const eBone_Flag boneflag,
1440 const int select_id)
1441{
1442 const float *col_bone = get_bone_solid_with_consts_color(ctx, bone, boneflag);
1443 const float *col_wire = get_bone_wire_color(ctx, boneflag);
1444 const float no_display[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1445 const float *col_head = no_display;
1446 const float *col_tail = col_bone;
1447
1448 if (ctx->const_color != nullptr) {
1449 col_wire = no_display; /* actually shrink the display. */
1450 col_bone = col_head = col_tail = ctx->const_color;
1451 }
1452 else {
1453 const UniformData &theme = ctx->res->theme;
1454
1455 if (bone.is_editbone() && bone.flag() & BONE_TIPSEL) {
1456 col_tail = &theme.colors.vert_select.x;
1457 }
1458
1459 /* Draw root point if we are not connected to our parent. */
1460 if (!(bone.has_parent() && (boneflag & BONE_CONNECTED))) {
1461
1462 if (bone.is_editbone()) {
1463 col_head = (bone.flag() & BONE_ROOTSEL) ? &theme.colors.vert_select.x : col_bone;
1464 }
1465 else {
1466 col_head = col_bone;
1467 }
1468 }
1469 }
1470
1471 if (select_id == -1) {
1472 /* Not in bone selection mode (can still be object select mode), draw everything at once.
1473 */
1475 ctx, bone.disp_mat(), col_wire, col_bone, col_head, col_tail, select_id);
1476 }
1477 else {
1478 /* In selection mode, draw bone, root and tip separately. */
1480 bone.disp_mat(),
1481 col_wire,
1482 col_bone,
1483 no_display,
1484 no_display,
1485 select_id | BONESEL_BONE);
1486
1487 if (col_head[3] > 0.0f) {
1489 bone.disp_mat(),
1490 col_wire,
1491 no_display,
1492 col_head,
1493 no_display,
1494 select_id | BONESEL_ROOT);
1495 }
1496
1498 ctx, bone.disp_mat(), col_wire, no_display, no_display, col_tail, select_id | BONESEL_TIP);
1499 }
1500}
1501
1503 const UnifiedBonePtr bone,
1504 const eBone_Flag boneflag,
1505 const int select_id)
1506{
1507 const float *col_solid = get_bone_solid_with_consts_color(ctx, bone, boneflag);
1508 const float *col_wire = get_bone_wire_color(ctx, boneflag);
1509 const float *col_hint = get_bone_hint_color(ctx, boneflag);
1510
1511 /* NOTE: Cannot reinterpret as float4x4 because of alignment requirement of float4x4.
1512 * This would require a deeper refactor. */
1513 Span<Mat4> bbone_matrices;
1514 if (bone.is_posebone()) {
1515 bbone_matrices = {reinterpret_cast<Mat4 *>(bone.as_posebone()->draw_data->bbone_matrix),
1516 bone.posebone_bone()->segments};
1517 }
1518 else {
1519 bbone_matrices = {
1520 reinterpret_cast<Mat4 *>(const_cast<float (*)[4][4]>(bone.as_editbone()->disp_bbone_mat)),
1521 bone.as_editbone()->segments};
1522 }
1523
1524 auto sel_id = ctx->res->select_id(*ctx->ob_ref, select_id | BONESEL_BONE);
1525
1526 for (const Mat4 &in_bone_mat : bbone_matrices) {
1527 float4x4 bone_mat = ctx->ob->object_to_world() * float4x4(in_bone_mat.mat);
1528
1529 if (ctx->is_filled) {
1530 ctx->bone_buf->bbones_fill_buf.append({bone_mat, col_solid, col_hint}, sel_id);
1531 }
1532 if (col_wire[3] > 0.0f) {
1533 ctx->bone_buf->bbones_outline_buf.append({bone_mat, col_wire}, sel_id);
1534 }
1535 }
1536
1537 if (ctx->draw_mode == ARM_DRAW_MODE_EDIT) {
1538 draw_points(ctx, bone, boneflag, col_solid, select_id);
1539 }
1540}
1541
1543 const UnifiedBonePtr bone,
1544 const eBone_Flag boneflag,
1545 const int select_id)
1546{
1547 const float *col_solid = get_bone_solid_with_consts_color(ctx, bone, boneflag);
1548 const float *col_wire = get_bone_wire_color(ctx, boneflag);
1549 const float *col_hint = get_bone_hint_color(ctx, boneflag);
1550
1551 const float *rad_head, *rad_tail, *distance;
1552 if (bone.is_editbone()) {
1553 const EditBone *eBone = bone.as_editbone();
1554 rad_tail = &eBone->rad_tail;
1555 distance = &eBone->dist;
1556 rad_head = (eBone->parent && (boneflag & BONE_CONNECTED)) ? &eBone->parent->rad_tail :
1557 &eBone->rad_head;
1558 }
1559 else {
1560 const bPoseChannel *pchan = bone.as_posebone();
1561 const Bone *pchan_bone = bone.posebone_bone();
1562 rad_tail = &pchan_bone->rad_tail;
1563 distance = &pchan_bone->dist;
1564 rad_head = (pchan->parent && (boneflag & BONE_CONNECTED)) ?
1565 &pchan->parent->bone_get(*ctx->armature)->rad_tail :
1566 &pchan_bone->rad_head;
1567 }
1568
1569 if ((select_id == -1) && (boneflag & BONE_NO_DEFORM) == 0 &&
1570 ((boneflag & BONE_SELECTED) ||
1571 (bone.is_editbone() && (boneflag & (BONE_ROOTSEL | BONE_TIPSEL)))))
1572 {
1573 drw_shgroup_bone_envelope_distance(ctx, bone.disp_mat(), rad_head, rad_tail, distance);
1574 }
1575
1577 bone.disp_mat(),
1578 col_solid,
1579 col_hint,
1580 col_wire,
1581 rad_head,
1582 rad_tail,
1583 select_id | BONESEL_BONE);
1584
1585 draw_points(ctx, bone, boneflag, col_solid, select_id);
1586}
1587
1589 const UnifiedBonePtr bone,
1590 const eBone_Flag boneflag,
1591 const int select_id)
1592{
1593 using namespace blender::math;
1594
1595 const float *col_wire = get_bone_wire_color(ctx, boneflag);
1596
1597 auto sel_id = (ctx->bone_buf) ? ctx->res->select_id(*ctx->ob_ref, select_id | BONESEL_BONE) :
1599
1600 /* NOTE: Cannot reinterpret as float4x4 because of alignment requirement of float4x4.
1601 * This would require a deeper refactor. */
1602 Span<Mat4> bbone_matrices;
1603 if (bone.is_posebone()) {
1604 bbone_matrices = {reinterpret_cast<Mat4 *>(bone.as_posebone()->draw_data->bbone_matrix),
1605 bone.posebone_bone()->segments};
1606 }
1607 else {
1608 bbone_matrices = {
1609 reinterpret_cast<Mat4 *>(const_cast<float (*)[4][4]>(bone.as_editbone()->disp_bbone_mat)),
1610 bone.as_editbone()->segments};
1611 }
1612
1613 for (const Mat4 &in_bone_mat : bbone_matrices) {
1614 float4x4 bmat = float4x4(in_bone_mat.mat);
1615 float3 head = transform_point(ctx->ob->object_to_world(), bmat.location());
1616 float3 tail = transform_point(ctx->ob->object_to_world(), bmat.location() + bmat.y_axis());
1617
1618 ctx->bone_buf->wire_buf.append(head, tail, float4(col_wire), sel_id);
1619 }
1620
1621 if (bone.is_editbone()) {
1622 const float *col_solid = get_bone_solid_with_consts_color(ctx, bone, boneflag);
1623 draw_points(ctx, bone, boneflag, col_solid, select_id);
1624 }
1625}
1626
1627static void bone_draw(const eArmature_Drawtype drawtype,
1628 const bool use_custom_shape,
1629 const Armatures::DrawContext *ctx,
1630 const UnifiedBonePtr bone,
1631 const eBone_Flag boneflag,
1632 const int select_id)
1633{
1634 if (use_custom_shape) {
1635 bone_draw_custom_shape(ctx, bone, boneflag, select_id);
1636 return;
1637 }
1638
1639 switch (drawtype) {
1640 case ARM_DRAW_TYPE_OCTA:
1641 bone_draw_octa(ctx, bone, boneflag, select_id);
1642 break;
1644 bone_draw_line(ctx, bone, boneflag, select_id);
1645 break;
1647 bone_draw_b_bone(ctx, bone, boneflag, select_id);
1648 break;
1650 bone_draw_envelope(ctx, bone, boneflag, select_id);
1651 break;
1652 case ARM_DRAW_TYPE_WIRE:
1653 bone_draw_wire(ctx, bone, boneflag, select_id);
1654 break;
1655 default:
1657 break;
1658 }
1659}
1660
1662
1663/* -------------------------------------------------------------------- */
1666
1668 const bPoseChannel *pchan)
1669{
1671 float tmp[4][4], posetrans[4][4];
1672 float xminmax[2], zminmax[2];
1673
1674 /* *0.5f here comes from M_PI/360.0f when rotations were still in degrees */
1675 xminmax[0] = sinf(pchan->limitmin[0] * 0.5f);
1676 xminmax[1] = sinf(pchan->limitmax[0] * 0.5f);
1677 zminmax[0] = sinf(pchan->limitmin[2] * 0.5f);
1678 zminmax[1] = sinf(pchan->limitmax[2] * 0.5f);
1679
1680 unit_m4(posetrans);
1681 translate_m4(posetrans, pchan->pose_mat[3][0], pchan->pose_mat[3][1], pchan->pose_mat[3][2]);
1682 /* In parent-bone pose space... */
1683 if (pchan->parent) {
1684 copy_m4_m4(tmp, pchan->parent->pose_mat);
1685 zero_v3(tmp[3]);
1686 mul_m4_m4m4(posetrans, posetrans, tmp);
1687 }
1688 /* ... but its own rest-space. */
1689 const Bone *bone = pchan->bone_get(*ctx->armature);
1690 mul_m4_m4m3(posetrans, posetrans, bone->bone_mat);
1691
1692 const float scale = bone->length * pchan->scale[1];
1693 scale_m4_fl(tmp, scale);
1694 tmp[1][1] = -tmp[1][1];
1695 mul_m4_m4m4(posetrans, posetrans, tmp);
1696
1697 /* into world space. */
1698 inst_data.mat44 = ctx->ob->object_to_world() * float4x4(posetrans);
1699
1700 /* Not selectable. */
1702
1703 if ((pchan->ikflag & BONE_IK_XLIMIT) && (pchan->ikflag & BONE_IK_ZLIMIT)) {
1705 inst_data.mat44, float4(0.25f), xminmax[0], zminmax[0], xminmax[1], zminmax[1]);
1706
1707 ctx->bone_buf->degrees_of_freedom_fill_buf.append(data, sel_id);
1708 ctx->bone_buf->degrees_of_freedom_wire_buf.append(data.with_color({0.0f, 0.0f, 0.0f, 1.0f}),
1709 sel_id);
1710 }
1711 if (pchan->ikflag & BONE_IK_XLIMIT) {
1713 inst_data.mat44, float4(1.0f, 0.0f, 0.0f, 1.0f), xminmax[0], 0.0f, xminmax[1], 0.0f);
1714 ctx->bone_buf->degrees_of_freedom_wire_buf.append(data, sel_id);
1715 }
1716 if (pchan->ikflag & BONE_IK_ZLIMIT) {
1718 inst_data.mat44, float4(0.0f, 0.0f, 1.0f, 1.0f), 0.0f, zminmax[0], 0.0f, zminmax[1]);
1719 ctx->bone_buf->degrees_of_freedom_wire_buf.append(data, sel_id);
1720 }
1721}
1722
1724
1725/* -------------------------------------------------------------------- */
1728
1730static bool should_draw_relation_to_parent(const UnifiedBonePtr bone, const eBone_Flag boneflag)
1731{
1732 const bool has_parent = bone.has_parent();
1733
1734 if (bone.is_editbone() && has_parent) {
1735 /* Always draw for unconnected bones, regardless of selection,
1736 * since riggers will want to know about the links between bones
1737 */
1738 return (boneflag & BONE_CONNECTED) == 0;
1739 }
1740
1741 if (bone.is_posebone() && has_parent) {
1742 /* Only draw between unconnected bones. */
1743 if (boneflag & BONE_CONNECTED) {
1744 return false;
1745 }
1746
1747 /* Only draw if bone or its parent is selected - reduces viewport
1748 * complexity with complex rigs */
1749 const bPoseChannel *pchan = bone.as_posebone();
1750 return (pchan->flag & POSE_SELECTED) ||
1751 (pchan->parent && (pchan->parent->flag & POSE_SELECTED));
1752 }
1753
1754 return false;
1755}
1756
1758 const bPoseChannel *pchan,
1759 const bool only_temp)
1760{
1761 const bPoseChannel *parchan;
1762 const float *line_start = nullptr, *line_end = nullptr;
1763 const ePchan_ConstFlag constflag = ePchan_ConstFlag(pchan->constflag);
1764
1765 for (bConstraint &con : pchan->constraints) {
1766 if (con.enforce == 0.0f) {
1767 continue;
1768 }
1769
1770 switch (con.type) {
1772 bKinematicConstraint *data = static_cast<bKinematicConstraint *>(con.data);
1773 int segcount = 0;
1774
1775 /* if only_temp, only draw if it is a temporary ik-chain */
1776 if (only_temp && !(data->flag & CONSTRAINT_IK_TEMP)) {
1777 continue;
1778 }
1779
1780 /* exclude tip from chain? */
1781 parchan = ((data->flag & CONSTRAINT_IK_TIP) == 0) ? pchan->parent : pchan;
1782 line_start = parchan->pose_tail;
1783
1784 /* Find the chain's root */
1785 while (parchan->parent) {
1786 segcount++;
1787 if (segcount == data->rootbone || segcount > 255) {
1788 break; /* 255 is weak */
1789 }
1790 parchan = parchan->parent;
1791 }
1792
1793 if (parchan) {
1794 line_end = parchan->pose_head;
1795
1796 if (constflag & PCHAN_HAS_NO_TARGET) {
1797 drw_shgroup_bone_ik_no_target_lines(ctx, line_start, line_end);
1798 }
1799 else {
1800 drw_shgroup_bone_ik_lines(ctx, line_start, line_end);
1801 }
1802 }
1803 break;
1804 }
1806 bSplineIKConstraint *data = static_cast<bSplineIKConstraint *>(con.data);
1807 int segcount = 0;
1808
1809 /* don't draw if only_temp, as Spline IK chains cannot be temporary */
1810 if (only_temp) {
1811 continue;
1812 }
1813
1814 parchan = pchan;
1815 line_start = parchan->pose_tail;
1816
1817 /* Find the chain's root */
1818 while (parchan->parent) {
1819 segcount++;
1820
1821 if (segcount == data->chainlen) {
1822 break;
1823 }
1824 parchan = parchan->parent;
1825 }
1826 /* Only draw line in case our chain is more than one bone long! */
1827 if (parchan != pchan) { /* XXX revise the breaking conditions to only stop at the tail? */
1828 line_end = parchan->pose_head;
1829 drw_shgroup_bone_ik_spline_lines(ctx, line_start, line_end);
1830 }
1831 break;
1832 }
1833 default:
1834 break;
1835 }
1836 }
1837}
1838
1840 const float bone_head[3],
1841 const float parent_head[3],
1842 const float parent_tail[3])
1843{
1844 if (ctx->draw_relation_from_head) {
1845 drw_shgroup_bone_relationship_lines(ctx, bone_head, parent_head);
1846 }
1847 else {
1848 drw_shgroup_bone_relationship_lines(ctx, bone_head, parent_tail);
1849 }
1850}
1851
1853 const UnifiedBonePtr bone,
1854 const eBone_Flag boneflag)
1855{
1856 if (ctx->draw_mode == ARM_DRAW_MODE_EDIT) {
1857 const EditBone *ebone = bone.as_editbone();
1858 if (ebone->parent) {
1859 if (ctx->do_relations && should_draw_relation_to_parent(bone, boneflag)) {
1861 ctx, ebone->head, ebone->parent->head, ebone->parent->tail);
1862 }
1863 }
1864 }
1865 else {
1866 const bPoseChannel *pchan = bone.as_posebone();
1867 if (pchan->parent) {
1868 if (ctx->do_relations && should_draw_relation_to_parent(bone, boneflag)) {
1870 ctx, pchan->pose_head, pchan->parent->pose_head, pchan->parent->pose_tail);
1871 }
1872
1873 /* Draw a line to IK root bone if bone is selected. */
1874 if (ctx->draw_mode == ARM_DRAW_MODE_POSE) {
1875 if (pchan->constflag & (PCHAN_HAS_IK | PCHAN_HAS_SPLINEIK)) {
1876 if (pchan->flag & POSE_SELECTED) {
1877 pchan_draw_ik_lines(ctx, pchan, !ctx->do_relations);
1878 }
1879 }
1880 }
1881 }
1882 }
1883}
1884
1885static void draw_bone_name(const Armatures::DrawContext *ctx, const UnifiedBonePtr bone)
1886{
1887 uchar color[4];
1888 float vec[3];
1889
1890 const bool is_pose = bone.is_posebone();
1891 const EditBone *eBone = nullptr;
1892 const bPoseChannel *pchan = nullptr;
1893 bone.get(&eBone, &pchan);
1894
1895 /* TODO: make this look at `boneflag` only. */
1896 bool highlight = (is_pose && ctx->draw_mode == ARM_DRAW_MODE_POSE &&
1897 (pchan->flag & POSE_SELECTED)) ||
1898 (!is_pose && (eBone->flag & BONE_SELECTED));
1899
1900 /* Color Management: Exception here as texts are drawn in sRGB space directly. */
1902
1903 const float *head = is_pose ? pchan->pose_head : eBone->head;
1904 const float *tail = is_pose ? pchan->pose_tail : eBone->tail;
1905 mid_v3_v3v3(vec, head, tail);
1906 mul_m4_v3(ctx->ob->object_to_world().ptr(), vec);
1907
1909 vec,
1910 is_pose ? pchan->name : eBone->name,
1911 is_pose ? strlen(pchan->name) : strlen(eBone->name),
1912 10,
1913 0,
1915 color,
1916 true);
1917}
1918
1920
1921/* -------------------------------------------------------------------- */
1924
1926 const bool use_custom_shape,
1927 UnifiedBonePtr bone,
1928 bArmature &armature)
1929{
1930 if (use_custom_shape) {
1932 }
1933 else if (ELEM(drawtype, ARM_DRAW_TYPE_B_BONE, ARM_DRAW_TYPE_WIRE)) {
1934 draw_bone_update_disp_matrix_bbone(bone, armature);
1935 }
1936 else {
1938 }
1939}
1940
1942{
1943 Object *ob = ctx->ob;
1944 EditBone *eBone;
1945 int index;
1946 const bool is_select = ctx->res->is_selection();
1947 const bool show_text = ctx->show_text;
1948
1949 const Object *ob_orig = DEG_get_original(ob);
1950 /* FIXME(@ideasman42): We should be able to use the evaluated object,
1951 * however the active bone isn't updated. Long term solution is an 'EditArmature' struct.
1952 * for now we can draw from the original armature. See: #66773. */
1953 // bArmature *arm = ob->data;
1955
1957
1958 const eArmature_Drawtype arm_drawtype = eArmature_Drawtype(arm.drawtype);
1959
1960 for (eBone = arm.edbo->first(),
1961 /* Note: Selection Next handles the object id merging later. */
1962 index = ctx->bone_buf ? 0x0 : ob_orig->runtime->select_id;
1963 eBone;
1964 eBone = eBone->next, index += 0x10000)
1965 {
1966 if (!animrig::bone_is_visible(&arm, eBone)) {
1967 continue;
1968 }
1969
1970 const int select_id = is_select ? index : uint(-1);
1971
1972 /* catch exception for bone with hidden parent */
1973 eBone_Flag boneflag = eBone_Flag(eBone->flag);
1974 if ((eBone->parent) && !animrig::bone_is_visible(&arm, eBone->parent)) {
1975 boneflag &= ~BONE_CONNECTED;
1976 }
1977
1978 /* set temporary flag for drawing bone as active, but only if selected */
1979 if (eBone == arm.act_edbone) {
1980 boneflag |= BONE_DRAW_ACTIVE;
1981 }
1982
1983 boneflag &= ~BONE_DRAW_LOCKED_WEIGHT;
1984
1985 UnifiedBonePtr bone = eBone;
1986 if (!ctx->const_color) {
1987 set_ctx_bcolor(ctx, bone);
1988 }
1989
1990 if (!is_select) {
1991 draw_bone_relations(ctx, bone, boneflag);
1992 }
1993
1995 arm_drawtype :
1997 bone_draw_update_display_matrix(drawtype, false, bone, arm);
1998 bone_draw(drawtype, false, ctx, bone, boneflag, select_id);
1999
2000 if (!is_select) {
2001 if (show_text && (arm.flag & ARM_DRAWNAMES)) {
2002 draw_bone_name(ctx, bone);
2003 }
2004
2005 if (arm.flag & ARM_DRAWAXES) {
2006 draw_axes(ctx, bone, arm);
2007 }
2008 }
2009 }
2010}
2011
2013{
2014 Object *ob = ctx->ob;
2015 const DRWContext *draw_ctx = DRW_context_get();
2016 const Scene *scene = draw_ctx->scene;
2018 int index = -1;
2019 const bool show_text = ctx->show_text;
2020 bool draw_locked_weights = false;
2021
2022 /* We can't safely draw non-updated pose, might contain nullptr bone pointers... */
2023 if (ob->pose->flag & POSE_RECALC) {
2024 return;
2025 }
2026
2027 ctx->draw_mode = ARM_DRAW_MODE_OBJECT; /* Will likely be set to ARM_DRAW_MODE_POSE below. */
2028
2029 bool is_pose_select = false;
2030 /* Object can be edited in the scene. */
2031 if (!is_from_dupli_or_set(ob)) {
2032 if ((draw_ctx->object_mode & OB_MODE_POSE) || (ob == draw_ctx->object_pose)) {
2034 }
2035 is_pose_select =
2036 /* If we're in pose-mode or object-mode with the ability to enter pose mode. */
2037 (
2038 /* Draw as if in pose mode (when selection is possible). */
2039 (ctx->draw_mode == ARM_DRAW_MODE_POSE) ||
2040 /* When we're in object mode, which may select bones. */
2041 ((ob->mode & OB_MODE_POSE) &&
2042 (
2043 /* Switch from object mode when object lock is disabled. */
2044 ((draw_ctx->object_mode == OB_MODE_OBJECT) &&
2045 (scene->toolsettings->object_flag & SCE_OBJECT_MODE_LOCK) == 0) ||
2046 /* Allow selection when in weight-paint mode
2047 * (selection code ensures this won't become active). */
2048 ((draw_ctx->object_mode & OB_MODE_ALL_WEIGHT_PAINT) &&
2049 (draw_ctx->object_pose != nullptr))))) &&
2050 ctx->res->is_selection();
2051
2052 if (is_pose_select) {
2053 const Object *ob_orig = DEG_get_original(ob);
2054 /* Note: Selection Next handles the object id merging later. */
2055 index = ctx->bone_buf ? 0x0 : ob_orig->runtime->select_id;
2056 }
2057 }
2058
2059 /* In weight paint mode retrieve the vertex group lock status. */
2060 if ((draw_ctx->object_mode & OB_MODE_ALL_WEIGHT_PAINT) && (draw_ctx->object_pose == ob) &&
2061 (draw_ctx->obact != nullptr))
2062 {
2063 draw_locked_weights = true;
2064
2066 pchan->bone_get(arm)->flag &= ~BONE_DRAW_LOCKED_WEIGHT;
2067 }
2068
2069 const Object *obact_orig = DEG_get_original(draw_ctx->obact);
2070
2071 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(obact_orig);
2072 for (const bDeformGroup *dg : ConstListBaseWrapper<bDeformGroup>(defbase)) {
2073 if ((dg->flag & DG_LOCK_WEIGHT) == 0) {
2074 continue;
2075 }
2076
2077 bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, dg->name);
2078 if (!pchan) {
2079 continue;
2080 }
2081
2082 pchan->bone_get(arm)->flag |= BONE_DRAW_LOCKED_WEIGHT;
2083 }
2084 }
2085
2086 const eArmature_Drawtype arm_drawtype = eArmature_Drawtype(arm.drawtype);
2087
2088 for (bPoseChannel *pchan = ob->pose->chanbase.first(); pchan;
2089 pchan = pchan->next, index += 0x10000)
2090 {
2091 if (!animrig::bone_is_visible(&arm, {pchan, pchan->bone_get(*ob)})) {
2092 continue;
2093 }
2094
2095 Bone *bone = pchan->bone_get(arm);
2096 const bool draw_dofs = !is_pose_select && ctx->do_relations &&
2097 (ctx->draw_mode == ARM_DRAW_MODE_POSE) &&
2098 (pchan->flag & POSE_SELECTED) &&
2099 ((ob->base_flag & BASE_FROM_DUPLI) == 0) &&
2100 (pchan->ikflag & (BONE_IK_XLIMIT | BONE_IK_ZLIMIT));
2101 const int select_id = is_pose_select ? index : uint(-1);
2102
2103 pchan_draw_data_init(pchan, *bone);
2104
2105 UnifiedBonePtr bone_ptr(pchan, bone);
2106 if (!ctx->const_color) {
2107 set_ctx_bcolor(ctx, bone_ptr);
2108 }
2109
2110 eBone_Flag boneflag = bone_ptr.flag();
2111 if (pchan->parent &&
2112 !animrig::bone_is_visible(&arm, {pchan->parent, pchan->parent->bone_get(*ob)}))
2113 {
2114 /* Avoid drawing connection line to hidden parent. */
2115 boneflag &= ~BONE_CONNECTED;
2116 }
2117 if (bone == arm.act_bone) {
2118 /* Draw bone as active, but only if selected. */
2119 boneflag |= BONE_DRAW_ACTIVE;
2120 }
2121 if (!draw_locked_weights) {
2122 boneflag &= ~BONE_DRAW_LOCKED_WEIGHT;
2123 }
2124
2125 const bool use_custom_shape = (pchan->custom) && !(arm.flag & ARM_NO_CUSTOM);
2126 if (!is_pose_select) {
2127 draw_bone_relations(ctx, bone_ptr, boneflag);
2128 }
2129
2131 arm_drawtype :
2133 bone_draw_update_display_matrix(drawtype, use_custom_shape, bone_ptr, arm);
2134 bone_draw(drawtype, use_custom_shape, ctx, bone_ptr, boneflag, select_id);
2135
2136 /* Below this point nothing is used for selection queries. */
2137 if (is_pose_select) {
2138 continue;
2139 }
2140
2141 if (draw_dofs) {
2142 draw_bone_degrees_of_freedom(ctx, pchan);
2143 }
2144 if (show_text && (arm.flag & ARM_DRAWNAMES)) {
2145 draw_bone_name(ctx, bone_ptr);
2146 }
2147 if (arm.flag & ARM_DRAWAXES) {
2148 draw_axes(ctx, bone_ptr, arm);
2149 }
2150 }
2151}
2152
2154
2155} // namespace blender
Functions to deal with Armatures.
C++ part of the BoneColor DNA struct.
Blender kernel action and pose functionality.
#define MAX_BBONE_SUBDIV
support for deformation groups and hooks.
General operations, lookup, etc. for blender objects.
#define PCHAN_CUSTOM_BONE_LENGTH(pchanbone)
#define OB_MODE_ALL_WEIGHT_PAINT
Object is a sort of wrapper for general info.
@ TH_WIRECOLOR_CONSTCOLS
#define BONESEL_ROOT
#define BONESEL_TIP
#define BONESEL_BONE
#define CLAMP(a, b, c)
#define BLI_assert_unreachable()
#define UNPACK3(a)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
UnifiedBonePtr(bPoseChannel *pchan, Bone *bone)
void get(const EditBone **eBone, const bPoseChannel **pchan) const
UnifiedBonePtr(const UnifiedBonePtr &ptr)=default
const EditBone * as_editbone() const
const f44 & disp_mat() const
const f44 & disp_tail_mat() const
const float & rad_head() const
bke::PChanBoneConst as_pchanbone() const
ePchan_ConstFlag constflag() const
const bPoseChannel * as_posebone() const
const char * name() const
UnifiedBonePtr(EditBone *eBone)
const float & rad_tail() const
const animrig::BoneColor & effective_bonecolor() const
bke::PChanBone as_pchanbone()
void get(EditBone **eBone, bPoseChannel **pchan)
const Bone * posebone_bone() const
const ThemeWireColor * effective_color() const
Definition bonecolor.cc:32
static void draw_armature_pose(Armatures::DrawContext *ctx)
static void draw_armature_edit(Armatures::DrawContext *ctx)
float distance(float, float) RET
float length(VecOp< float, D >) RET
float3x3 scale(float3x3 mat, float2 scale)
float3 transform_point(float3x3 mat, float3 point)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
bool bone_is_visible(const bArmature *armature, const Bone *bone)
PChanBoneT< bPoseChannel, Bone > PChanBone
Definition BKE_pose.hh:88
PChanBoneT< const bPoseChannel, const Bone > PChanBoneConst
Definition BKE_pose.hh:89
void DRW_mesh_batch_cache_validate(Mesh &mesh)
gpu::Batch * DRW_mesh_batch_cache_get_loose_edges(Mesh &mesh)
gpu::Batch * DRW_cache_text_edge_wire_get(Object *ob)
void DRW_curve_batch_cache_validate(Curve *cu)
gpu::Batch * DRW_mesh_batch_cache_get_surface(Mesh &mesh)
gpu::Batch * DRW_mesh_batch_cache_get_all_edges(Mesh &mesh)
gpu::Batch * DRW_mesh_batch_cache_get_edge_detection(Mesh &mesh, bool *r_is_manifold)
void drw_batch_cache_generate_requested_delayed(Object *ob)
gpu::Batch * DRW_cache_curve_edge_wire_get(Object *ob)
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
void get_color_4ubv(int colorid, unsigned char col[4])
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
static void edbo_compute_bbone_child(bArmature *arm)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void draw_bone_update_disp_matrix_default(UnifiedBonePtr bone)
static const float * get_bone_solid_color(const Armatures::DrawContext *ctx, const eBone_Flag boneflag)
static void drw_shgroup_bone_envelope(const Armatures::DrawContext *ctx, const float(*bone_mat)[4], const float bone_col[4], const float hint_col[4], const float outline_col[4], const float *radius_head, const float *radius_tail, const int select_id)
MINLINE void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
static void get_pchan_color_wire(const UniformData &theme, const ThemeWireColor *bcolor, const eArmatureDrawMode draw_mode, const eBone_Flag boneflag, float r_color[4])
const ListBaseT< bDeformGroup > * BKE_object_defgroup_list(const Object *ob)
Definition deform.cc:591
Mesh * BKE_object_get_evaluated_mesh_no_subsurf_unchecked(const Object *object)
Definition object.cc:4392
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
static void use_bone_color(float *r_color, const uint8_t *color_from_theme, const int shade_offset)
static void bone_draw_wire(const Armatures::DrawContext *ctx, const UnifiedBonePtr bone, const eBone_Flag boneflag, const int select_id)
static void drw_shgroup_bone_custom_mesh_wire(const Armatures::DrawContext *ctx, Mesh &mesh, const float(*bone_mat)[4], const float color[4], const float wire_width, const draw::select::ID select_id, Object &custom)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
void interp_v4_v4v4(float r[4], const float a[4], const float b[4], float t)
float mat4_to_scale(const float mat[4][4])
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
static void drw_shgroup_bone_ik_no_target_lines(const Armatures::DrawContext *ctx, const float start[3], const float end[3])
static void bone_draw_envelope(const Armatures::DrawContext *ctx, const UnifiedBonePtr bone, const eBone_Flag boneflag, const int select_id)
@ DRW_TEXT_CACHE_GLOBALSPACE
@ DRW_TEXT_CACHE_STRING_PTR
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
bool invert_m4(float mat[4][4])
MINLINE void srgb_to_linearrgb_v4(float linear[4], const float srgb[4])
void translate_m4(float mat[4][4], float Tx, float Ty, float Tz)
static void draw_points(const Armatures::DrawContext *ctx, const UnifiedBonePtr bone, const eBone_Flag boneflag, const float col_solid[4], const int select_id)
Mesh & DRW_mesh_get_for_drawing(Mesh &mesh)
MatBase< float, 4, 4 > float4x4
static float get_bone_wire_thickness(const Armatures::DrawContext *ctx, int boneflag)
static bool should_draw_relation_to_parent(const UnifiedBonePtr bone, const eBone_Flag boneflag)
static void drw_shgroup_bone_custom_wire(const Armatures::DrawContext *ctx, const float(*bone_mat)[4], const float color[4], const float wire_width, const draw::select::ID select_id, Object *custom)
static void bone_hint_color_shade(float hint_color[4], const float color[4])
static void drw_shgroup_bone_custom_empty(const Armatures::DrawContext *ctx, const float(*bone_mat)[4], const float color[4], const float wire_width, const draw::select::ID select_id, Object *custom)
static void drw_shgroup_bone_custom_solid(const Armatures::DrawContext *ctx, const float(*bone_mat)[4], const float bone_color[4], const float hint_color[4], const float outline_color[4], const float wire_width, const draw::select::ID select_id, Object *custom)
MINLINE void mul_v3_fl(float r[3], float f)
ListBaseWrapperTemplate< const ListBase, const T > ConstListBaseWrapper
static void ebone_spline_preview(EditBone *ebone, const float result_array[MAX_BBONE_SUBDIV][4][4])
MINLINE void sub_v3_v3(float r[3], const float a[3])
VecBase< float, 4 > float4
void eulO_to_mat3(float M[3][3], const float e[3], short order)
static void bone_draw_b_bone(const Armatures::DrawContext *ctx, const UnifiedBonePtr bone, const eBone_Flag boneflag, const int select_id)
static void drw_shgroup_bone_sphere(const Armatures::DrawContext *ctx, const float(*bone_mat)[4], const float bone_color[4], const float hint_color[4], const float outline_color[4], const int select_id)
void mul_m4_v3(const float M[4][4], float r[3])
static void bone_draw_line(const Armatures::DrawContext *ctx, const UnifiedBonePtr bone, const eBone_Flag boneflag, const int select_id)
static void bone_draw(const eArmature_Drawtype drawtype, const bool use_custom_shape, const Armatures::DrawContext *ctx, const UnifiedBonePtr bone, const eBone_Flag boneflag, const int select_id)
void BKE_pchan_bbone_spline_setup(bke::PChanBone pchanbone, const bArmature &armature, bool rest, bool for_deform, Mat4 *result_array)
static void drw_shgroup_bone_axes(const Armatures::DrawContext *ctx, const float(*bone_mat)[4], const float color[4])
void size_to_mat4(float R[4][4], const float size[3])
static void drw_shgroup_bone_relationship_lines_ex(const Armatures::DrawContext *ctx, const float start[3], const float end[3], const float color[4])
unsigned char uchar
Definition BKE_text.h:58
T & DRW_object_get_data_for_drawing(const Object &object)
ListBaseWrapperTemplate< ListBase, T > ListBaseWrapper
static void get_pchan_color_constraint(const UniformData &theme, const ThemeWireColor *bcolor, const UnifiedBonePtr bone, float r_color[4])
void mul_m4_m4m3(float R[4][4], const float A[4][4], const float B[3][3])
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)
BMesh const char void * data
int BKE_pchan_bbone_spline_compute(BBoneSplineParameters *param, bool for_deform, Mat4 *result_array)
static void drw_shgroup_bone_envelope_distance(const Armatures::DrawContext *ctx, const float(*bone_mat)[4], const float *radius_head, const float *radius_tail, const float *distance)
unsigned char uint8_t
Definition BKE_text.h:79
@ ARM_DRAW_TYPE_ARMATURE_DEFINED
static void draw_bone_degrees_of_freedom(const Armatures::DrawContext *ctx, const bPoseChannel *pchan)
void unit_m4(float m[4][4])
void ED_armature_ebone_to_mat4(EditBone *ebone, float r_mat[4][4])
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
static void drw_shgroup_bone_stick(const Armatures::DrawContext *ctx, const float(*bone_mat)[4], const float col_wire[4], const float col_bone[4], const float col_head[4], const float col_tail[4], const int select_id)
static void bone_draw_custom_shape(const Armatures::DrawContext *ctx, const UnifiedBonePtr bone, const eBone_Flag boneflag, const int select_id)
static void set_ctx_bcolor(Armatures::DrawContext *ctx, const UnifiedBonePtr bone)
const DRWContext * DRW_context_get()
MINLINE void zero_v3(float r[3])
void mul_m4_v4(const float mat[4][4], float r[4])
static void draw_bone_update_disp_matrix_bbone(UnifiedBonePtr bone, bArmature &armature)
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
static void drw_shgroup_bone_ik_spline_lines(const Armatures::DrawContext *ctx, const float start[3], const float end[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
T * DEG_get_original(T *id)
static void pchan_draw_ik_lines(const Armatures::DrawContext *ctx, const bPoseChannel *pchan, const bool only_temp)
MINLINE void copy_v3_fl(float r[3], float f)
static void draw_bone_relations(const Armatures::DrawContext *ctx, const UnifiedBonePtr bone, const eBone_Flag boneflag)
static void drw_shgroup_bone_relationship_lines(const Armatures::DrawContext *ctx, const float start[3], const float end[3])
MINLINE float max_ff(float a, float b)
MINLINE void copy_v3_v3_uchar(unsigned char r[3], const unsigned char a[3])
static void drw_shgroup_custom_bone_curve(const Armatures::DrawContext *ctx, Curve *curve, const float(*bone_mat)[4], const float outline_color[4], const float wire_width, const draw::select::ID select_id, Object *custom)
static void bone_draw_octa(const Armatures::DrawContext *ctx, const UnifiedBonePtr bone, const eBone_Flag boneflag, const int select_id)
void rescale_m4(float mat[4][4], const float scale[3])
static void draw_axes(const Armatures::DrawContext *ctx, const UnifiedBonePtr bone, const bArmature &arm)
static const float * get_bone_solid_with_consts_color(const Armatures::DrawContext *ctx, const UnifiedBonePtr bone, const eBone_Flag boneflag)
static void drw_shgroup_bone_custom_solid_mesh(const Armatures::DrawContext *ctx, Mesh &mesh, const float(*bone_mat)[4], const float bone_color[4], const float hint_color[4], const float outline_color[4], const float wire_width, const draw::select::ID select_id, Object &custom)
static void drw_shgroup_bone_ik_lines(const Armatures::DrawContext *ctx, const float start[3], const float end[3])
MINLINE void copy_v4_v4(float r[4], const float a[4])
static void bone_draw_update_display_matrix(const eArmature_Drawtype drawtype, const bool use_custom_shape, UnifiedBonePtr bone, bArmature &armature)
void copy_m4_m4(float m1[4][4], const float m2[4][4])
static const float * get_bone_wire_color(const Armatures::DrawContext *ctx, const eBone_Flag boneflag)
VecBase< float, 3 > float3
void scale_m4_fl(float R[4][4], float scale)
static void draw_bone_update_disp_matrix_custom_shape(UnifiedBonePtr bone)
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void mul_v3_v3v3(float r[3], const float v1[3], const float v2[3])
static void get_pchan_color_solid(const UniformData &theme, const ThemeWireColor *bcolor, float r_color[4])
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
static void cp_shade_color3ub(uchar cp[3], const int offset)
static void draw_bone_name(const Armatures::DrawContext *ctx, const UnifiedBonePtr bone)
static const float * get_bone_hint_color(const Armatures::DrawContext *ctx, const eBone_Flag boneflag)
static void draw_bone_bone_relationship_line(const Armatures::DrawContext *ctx, const float bone_head[3], const float parent_head[3], const float parent_tail[3])
MINLINE float clamp_f(float value, float min, float max)
static void bone_locked_color_shade(const UniformData &theme, float color[4])
static void pchan_draw_data_init(bPoseChannel *pchan, Bone &bone)
#define PT_DEFAULT_RAD
#define WIRE_WIDTH_COMPRESSION
#define sinf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
unsigned char select[4]
unsigned char solid[4]
unsigned char active[4]
eObjectMode object_mode
float disp_bbone_mat[32][4][4]
eBone_BBoneFlag bbone_flag
EditBone * bbone_child
eBone_BBoneHandleType bbone_next_type
EditBone * bbone_prev
EditBone * bbone_next
eBone_BBoneHandleType bbone_prev_type
float mat[4][4]
bke::ObjectRuntime * runtime
eObject_EmptyDrawType empty_drawtype
struct bPose * pose
struct ToolSettings * toolsettings
ListBaseT< EditBone > * edbo
struct EditBone * act_edbone
eArmature_Drawtype drawtype
struct bPoseChannel * parent
bPoseChannelDrawData * draw_data
ListBaseT< struct bConstraint > constraints
struct bPoseChannel * custom_tx
ePchan_ConstFlag constflag
ListBaseT< bPoseChannel > chanbase
void set_color(const float4 &bone_color)
void set_hint_color(const float4 &hint_color)
void append(const float3 &start, const float3 &end, const float4 &color, select::ID select_id=select::SelectMap::select_invalid_id())
void append(const InstanceDataT &data, select::ID select_id)
const ID select_id(const ObjectRef &ob_ref, uint sub_object_id=0)