Debian Codegraph @

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

raw symbols SCIP
1 /**************************************************************************/
2 /* two_bone_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 "two_bone_ik_3d_gizmo_plugin.h"
32
33 #include "editor/settings/editor_settings.h"
34
35 TwoBoneIK3DGizmoPlugin::SelectionMaterials TwoBoneIK3DGizmoPlugin::selection_materials;
36
37 TwoBoneIK3DGizmoPlugin::TwoBoneIK3DGizmoPlugin() {
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 TwoBoneIK3DGizmoPlugin::~TwoBoneIK3DGizmoPlugin() {
72 selection_materials.unselected_mat.unref();
73 selection_materials.selected_mat.unref();
74 }
75
76 bool TwoBoneIK3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
77 return Object::cast_to<TwoBoneIK3D>(p_spatial) != nullptr;
78 }
79
80 String TwoBoneIK3DGizmoPlugin::get_gizmo_name() const {
81 return "TwoBoneIK3D";
82 }
83
84 int TwoBoneIK3DGizmoPlugin::get_priority() const {
85 return -1;
86 }
87
88 void TwoBoneIK3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
89 TwoBoneIK3D *ik = Object::cast_to<TwoBoneIK3D>(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> mesh = get_joints_mesh(skeleton, ik, p_gizmo->is_selected());
102 Transform3D skel_tr = ik->get_global_transform().inverse() * skeleton->get_global_transform();
103 p_gizmo->add_mesh(mesh, Ref<Material>(), skel_tr, skeleton->register_skin(skeleton->create_skin_from_rest_transforms()));
104 }
105
106 Ref<ArrayMesh> TwoBoneIK3DGizmoPlugin::get_joints_mesh(Skeleton3D *p_skeleton, TwoBoneIK3D *p_ik, bool p_is_selected) {
107 Color bone_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/ik_chain");
108
109 Ref<SurfaceTool> surface_tool;
110 surface_tool.instantiate();
111 surface_tool->begin(Mesh::PRIMITIVE_LINES);
112
113 if (p_is_selected) {
114 surface_tool->set_material(selection_materials.selected_mat);
115 } else {
116 selection_materials.unselected_mat->set_albedo(bone_color);
117 surface_tool->set_material(selection_materials.unselected_mat);
118 }
119
120 PackedInt32Array bones;
121 PackedFloat32Array weights;
122 bones.resize(4);
123 weights.resize(4);
124 for (int i = 0; i < 4; i++) {
125 bones.write[i] = 0;
126 weights.write[i] = 0;
127 }
128 weights.write[0] = 1;
129
130 for (int i = 0; i < p_ik->get_setting_count(); i++) {
131 if (!p_ik->is_valid(i)) {
132 continue; // Skip invalid settings.
133 }
134
135 int root_bone = p_ik->get_root_bone(i);
136 int middle_bone = p_ik->get_middle_bone(i);
137
138 Transform3D root_gp = p_skeleton->get_bone_global_rest(root_bone);
139 Transform3D mid_gp = p_skeleton->get_bone_global_rest(middle_bone);
140 Vector3 root_vec = p_ik->get_root_bone_vector(i);
141 Vector3 mid_vec = p_ik->get_middle_bone_vector(i);
142
143 bones.write[0] = root_bone;
144 surface_tool->set_bones(bones);
145 surface_tool->set_weights(weights);
146 draw_line(surface_tool, root_gp.origin, root_gp.translated_local(root_vec).origin, bone_color);
147
148 bones.write[0] = middle_bone;
149 surface_tool->set_bones(bones);
150 surface_tool->set_weights(weights);
151 draw_line(surface_tool, mid_gp.origin, mid_gp.translated_local(mid_vec).origin, bone_color);
152
153 Vector3 pole_vector = p_ik->get_pole_direction_vector(i);
154 if (pole_vector.is_zero_approx()) {
155 continue;
156 }
157
158 float pole_length = MIN(root_vec.length(), mid_vec.length()) * 0.25;
159 draw_arrow(surface_tool, mid_gp.origin, mid_gp.basis.get_rotation_quaternion().xform(pole_vector).normalized(), pole_length, bone_color);
160 }
161
162 return surface_tool->commit();
163 }
164
165 void TwoBoneIK3DGizmoPlugin::draw_line(Ref<SurfaceTool> &p_surface_tool, const Vector3 &p_begin_pos, const Vector3 &p_end_pos, const Color &p_color) {
166 p_surface_tool->set_color(p_color);
167 p_surface_tool->add_vertex(p_begin_pos);
168 p_surface_tool->set_color(p_color);
169 p_surface_tool->add_vertex(p_end_pos);
170 }
171
172 void TwoBoneIK3DGizmoPlugin::draw_arrow(Ref<SurfaceTool> &p_surface_tool, const Vector3 &p_origin, const Vector3 &p_direction, real_t p_length, const Color &p_color) {
173 static const float HALF_PI = Math::PI * 0.5;
174
175 p_surface_tool->set_color(p_color);
176
177 LocalVector<Vector3> arrow_body;
178 arrow_body.resize(2);
179 arrow_body[0] = Vector3(0, 0, 0);
180 arrow_body[1] = Vector3(0, p_length, 0);
181
182 LocalVector<Vector3> arrow_head;
183 arrow_head.resize(2);
184 arrow_head[0] = Vector3(0, p_length, 0);
185 arrow_head[1] = Vector3(p_length * 0.25, p_length * 0.75, 0);
186
187 Quaternion dir = Quaternion(Vector3(0, 1, 0), p_direction);
188
189 // Draw the arrow body.
190 p_surface_tool->add_vertex(arrow_body[0] + p_origin);
191 p_surface_tool->add_vertex(dir.xform(arrow_body[1]) + p_origin);
192
193 // Draw the arrow head 4 times, rotate around the arrow body.
194 for (int i = 0; i < 4; i++) {
195 Quaternion rotation = dir * Quaternion(Vector3(0, 1, 0), HALF_PI * i);
196 for (int j = 0; j < 2; j++) {
197 Vector3 v = arrow_head[j];
198 Vector3 rotated_v = rotation.xform(v);
199 p_surface_tool->add_vertex(rotated_v + p_origin);
200 }
201 }
202 }