Debian Codegraph @

editor/scene/3d/gizmos/chain_ik_3d_gizmo_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* chain_ik_3d_gizmo_plugin.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "chain_ik_3d_gizmo_plugin.h"
32
33 #include "editor/settings/editor_settings.h"
34
35 ChainIK3DGizmoPlugin::SelectionMaterials ChainIK3DGizmoPlugin::selection_materials;
36
37 ChainIK3DGizmoPlugin::ChainIK3DGizmoPlugin() {
38 selection_materials.unselected_mat.instantiate();
39 selection_materials.unselected_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
40 selection_materials.unselected_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
41 selection_materials.unselected_mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
42 selection_materials.unselected_mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
43 selection_materials.unselected_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
44
45 selection_materials.selected_mat.instantiate();
46 Ref<Shader> sh;
47 sh.instantiate();
48 sh->set_code(R"(
49 // Skeleton 3D gizmo bones shader.
50
51 shader_type spatial;
52 render_mode unshaded, shadows_disabled;
53
54 void vertex() {
55 if (!OUTPUT_IS_SRGB) {
56 COLOR.rgb = mix(pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb * (1.0 / 12.92), lessThan(COLOR.rgb,vec3(0.04045)));
57 }
58 VERTEX = VERTEX;
59 POSITION = PROJECTION_MATRIX * VIEW_MATRIX * MODEL_MATRIX * vec4(VERTEX.xyz, 1.0);
60 POSITION.z = mix(POSITION.z, POSITION.w, 0.998);
61 }
62
63 void fragment() {
64 ALBEDO = COLOR.rgb;
65 ALPHA = COLOR.a;
66 }
67 )");
68 selection_materials.selected_mat->set_shader(sh);
69 }
70
71 ChainIK3DGizmoPlugin::~ChainIK3DGizmoPlugin() {
72 selection_materials.unselected_mat.unref();
73 selection_materials.selected_mat.unref();
74 }
75
76 bool ChainIK3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
77 return Object::cast_to<ChainIK3D>(p_spatial) != nullptr;
78 }
79
80 String ChainIK3DGizmoPlugin::get_gizmo_name() const {
81 return "ChainIK3D";
82 }
83
84 int ChainIK3DGizmoPlugin::get_priority() const {
85 return -1;
86 }
87
88 void ChainIK3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
89 ChainIK3D *ik = Object::cast_to<ChainIK3D>(p_gizmo->get_node_3d());
90 p_gizmo->clear();
91
92 if (!ik->get_setting_count()) {
93 return;
94 }
95
96 Skeleton3D *skeleton = ik->get_skeleton();
97 if (!skeleton) {
98 return;
99 }
100
101 Ref<ArrayMesh> skeleton_mesh;
102 Ref<ArrayMesh> mesh;
103 get_joints_mesh(skeleton, ik, p_gizmo->is_selected(), skeleton_mesh, mesh);
104 Transform3D skel_tr = ik->get_global_transform().inverse() * skeleton->get_global_transform();
105 p_gizmo->add_mesh(skeleton_mesh, Ref<Material>(), skel_tr, skeleton->register_skin(skeleton->create_skin_from_rest_transforms()));
106 p_gizmo->add_mesh(mesh, Ref<Material>(), skel_tr);
107 }
108
109 void ChainIK3DGizmoPlugin::get_joints_mesh(Skeleton3D *p_skeleton, ChainIK3D *p_ik, bool p_is_selected, Ref<ArrayMesh> &r_skinned_mesh, Ref<ArrayMesh> &r_mesh) {
110 Color bone_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/ik_chain");
111 static const Color limitation_x_axis_color = Color(1, 0, 0, 1);
112 static const Color limitation_z_axis_color = Color(0, 0, 1, 1);
113
114 IterateIK3D *it_ik = Object::cast_to<IterateIK3D>(p_ik);
115
116 Ref<SurfaceTool> surface_tool;
117 surface_tool.instantiate();
118 surface_tool->begin(Mesh::PRIMITIVE_LINES);
119
120 Ref<SurfaceTool> surface_tool_without_skin;
121 surface_tool_without_skin.instantiate();
122 surface_tool_without_skin->begin(Mesh::PRIMITIVE_LINES);
123
124 if (p_is_selected) {
125 surface_tool->set_material(selection_materials.selected_mat);
126 surface_tool_without_skin->set_material(selection_materials.selected_mat);
127 } else {
128 selection_materials.unselected_mat->set_albedo(bone_color);
129 surface_tool->set_material(selection_materials.unselected_mat);
130 surface_tool_without_skin->set_material(selection_materials.unselected_mat);
131 }
132
133 PackedInt32Array bones;
134 PackedFloat32Array weights;
135 bones.resize(4);
136 weights.resize(4);
137 for (int i = 0; i < 4; i++) {
138 bones.write[i] = 0;
139 weights.write[i] = 0;
140 }
141 weights.write[0] = 1;
142
143 for (int i = 0; i < p_ik->get_setting_count(); i++) {
144 int current_bone = -1;
145 int prev_bone = -1;
146 int joint_end = p_ik->get_joint_count(i) - 1;
147 float prev_length = INFINITY;
148 bool is_extended = p_ik->is_end_bone_extended(i) && p_ik->get_end_bone_length(i) > 0;
149 Transform3D anc_global_pose = p_ik->get_chain_root_global_rest(i);
150 for (int j = 0; j <= joint_end; j++) {
151 current_bone = p_ik->get_joint_bone(i, j);
152 if (j > 0) {
153 int prev_joint = j - 1;
154 Transform3D parent_global_pose = p_skeleton->get_bone_global_rest(prev_bone);
155 Vector3 bone_vector = p_ik->get_bone_vector(i, prev_joint);
156 float current_length = bone_vector.length();
157 Vector3 center = parent_global_pose.translated_local(bone_vector).origin;
158 draw_line(surface_tool, parent_global_pose.origin, center, bone_color);
159
160 if (it_ik) {
161 // Draw rotation axis vector if not ROTATION_AXIS_ALL.
162 if (j != joint_end || (j == joint_end && is_extended)) {
163 SkeletonModifier3D::RotationAxis rotation_axis = it_ik->get_joint_rotation_axis(i, j);
164 if (rotation_axis != SkeletonModifier3D::ROTATION_AXIS_ALL) {
165 Vector3 axis_vector = it_ik->get_joint_rotation_axis_vector(i, j);
166 if (!axis_vector.is_zero_approx()) {
167 float rot_axis_length = bone_vector.length() * 0.2; // Use 20% of the bone length for the rotation axis vector.
168 Vector3 axis = parent_global_pose.basis.xform(axis_vector.normalized()) * rot_axis_length;
169 draw_line(surface_tool, center - axis, center + axis, bone_color);
170 }
171 }
172 }
173
174 // Draw parent limitation shape.
175 Ref<JointLimitation3D> lim = it_ik->get_joint_limitation(i, prev_joint);
176 if (lim.is_valid() && prev_bone >= 0) {
177 // Limitation space should bind parent bone rest.
178 int parent = p_skeleton->get_bone_parent(prev_bone);
179 Ref<SurfaceTool> limitation_surface_tool = parent >= 0 ? surface_tool : surface_tool_without_skin;
180 if (parent >= 0) {
181 bones.write[0] = parent;
182 limitation_surface_tool->set_bones(bones);
183 limitation_surface_tool->set_weights(weights);
184 }
185 Transform3D tr = anc_global_pose;
186 tr.basis *= it_ik->get_joint_limitation_space(i, prev_joint, bone_vector.normalized());
187 float sl = MIN(current_length, prev_length);
188 lim->draw_shape(limitation_surface_tool, tr, sl, bone_color);
189 sl *= 0.1;
190 Vector3 x_axis = tr.basis.get_column(Vector3::AXIS_X).normalized() * sl;
191 Vector3 z_axis = tr.basis.get_column(Vector3::AXIS_Z).normalized() * sl;
192 draw_line(limitation_surface_tool, tr.origin + x_axis * 2, tr.origin + x_axis * 3, limitation_x_axis_color); // Offset 20%.
193 draw_line(limitation_surface_tool, tr.origin + z_axis * 2, tr.origin + z_axis * 3, limitation_z_axis_color); // Offset 20%.
194 }
195 }
196 prev_length = current_length;
197 Transform3D tr = p_skeleton->get_bone_rest(current_bone);
198 tr.origin = bone_vector;
199 parent_global_pose *= tr;
200 anc_global_pose = parent_global_pose;
201 }
202 if (j == joint_end && is_extended) {
203 Transform3D current_global_pose = p_skeleton->get_bone_global_rest(current_bone);
204 Vector3 bone_vector = p_ik->get_bone_vector(i, j);
205 if (bone_vector.is_zero_approx()) {
206 continue;
207 }
208 float current_length = bone_vector.length();
209 bones.write[0] = current_bone;
210 surface_tool->set_bones(bones);
211 surface_tool->set_weights(weights);
212 Vector3 center = current_global_pose.translated_local(bone_vector).origin;
213 draw_line(surface_tool, current_global_pose.origin, center, bone_color);
214
215 if (it_ik) {
216 // Draw limitation shape.
217 Ref<JointLimitation3D> lim = it_ik->get_joint_limitation(i, j);
218 if (lim.is_valid() && current_bone >= 0) {
219 // Limitation space should bind parent bone rest.
220 int parent = p_skeleton->get_bone_parent(current_bone);
221 Ref<SurfaceTool> limitation_surface_tool = parent >= 0 ? surface_tool : surface_tool_without_skin;
222 if (parent >= 0) {
223 bones.write[0] = parent;
224 limitation_surface_tool->set_bones(bones);
225 limitation_surface_tool->set_weights(weights);
226 }
227 Transform3D tr = anc_global_pose;
228 tr.basis *= it_ik->get_joint_limitation_space(i, j, bone_vector.normalized());
229 float sl = MIN(current_length, prev_length);
230 lim->draw_shape(limitation_surface_tool, tr, sl, bone_color);
231 sl *= 0.1;
232 Vector3 x_axis = tr.basis.get_column(Vector3::AXIS_X).normalized() * sl;
233 Vector3 z_axis = tr.basis.get_column(Vector3::AXIS_Z).normalized() * sl;
234 draw_line(limitation_surface_tool, tr.origin + x_axis * 2, tr.origin + x_axis * 3, limitation_x_axis_color); // Offset 20%.
235 draw_line(limitation_surface_tool, tr.origin + z_axis * 2, tr.origin + z_axis * 3, limitation_z_axis_color); // Offset 20%.
236 }
237 }
238 } else {
239 bones.write[0] = current_bone;
240 surface_tool->set_bones(bones);
241 surface_tool->set_weights(weights);
242 if (j == 0) {
243 // Check if the next bone exists.
244 int count = p_ik->get_joint_count(i);
245 if (count < 2) {
246 continue;
247 }
248 if (it_ik) {
249 // Draw rotation axis vector if not ROTATION_AXIS_ALL.
250 SkeletonModifier3D::RotationAxis rotation_axis = it_ik->get_joint_rotation_axis(i, j);
251 if (rotation_axis != SkeletonModifier3D::ROTATION_AXIS_ALL) {
252 Vector3 axis_vector = it_ik->get_joint_rotation_axis_vector(i, j);
253 if (!axis_vector.is_zero_approx()) {
254 Vector3 bone_vector = p_ik->get_bone_vector(i, j);
255 float rot_axis_length = bone_vector.length() * 0.2; // Use 20% of the bone length for the rotation axis vector.
256 Vector3 axis = anc_global_pose.basis.xform(axis_vector.normalized()) * rot_axis_length;
257 draw_line(surface_tool, anc_global_pose.origin - axis, anc_global_pose.origin + axis, bone_color);
258 }
259 }
260 }
261 }
262 }
263 prev_bone = current_bone;
264 }
265 }
266
267 r_skinned_mesh = surface_tool->commit();
268 r_mesh = surface_tool_without_skin->commit();
269 }
270
271 void ChainIK3DGizmoPlugin::draw_line(Ref<SurfaceTool> &p_surface_tool, const Vector3 &p_begin_pos, const Vector3 &p_end_pos, const Color &p_color) {
272 p_surface_tool->set_color(p_color);
273 p_surface_tool->add_vertex(p_begin_pos);
274 p_surface_tool->set_color(p_color);
275 p_surface_tool->add_vertex(p_end_pos);
276 }