Debian Codegraph @

scene/3d/ik_modifier_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* ik_modifier_3d.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 "ik_modifier_3d.h"
32
33 void IKModifier3D::_notification(int p_what) {
34 switch (p_what) {
35 case NOTIFICATION_ENTER_TREE: {
36 #ifdef TOOLS_ENABLED
37 if (Engine::get_singleton()->is_editor_hint()) {
38 set_notify_local_transform(true); // Used for updating gizmo in editor.
39 }
40 _update_mutable_info();
41 _make_gizmo_dirty();
42 #endif // TOOLS_ENABLED
43 _make_all_joints_dirty();
44 } break;
45 #ifdef TOOLS_ENABLED
46 case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
47 _make_gizmo_dirty();
48 } break;
49 #endif // TOOLS_ENABLED
50 }
51 }
52
53 void IKModifier3D::_bind_methods() {
54 ClassDB::bind_method(D_METHOD("set_setting_count", "count"), &IKModifier3D::set_setting_count);
55 ClassDB::bind_method(D_METHOD("get_setting_count"), &IKModifier3D::get_setting_count);
56 ClassDB::bind_method(D_METHOD("clear_settings"), &IKModifier3D::clear_settings);
57
58 ClassDB::bind_method(D_METHOD("set_mutable_bone_axes", "enabled"), &IKModifier3D::set_mutable_bone_axes);
59 ClassDB::bind_method(D_METHOD("are_bone_axes_mutable"), &IKModifier3D::are_bone_axes_mutable);
60
61 // To process manually.
62 ClassDB::bind_method(D_METHOD("reset"), &IKModifier3D::reset);
63
64 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mutable_bone_axes"), "set_mutable_bone_axes", "are_bone_axes_mutable");
65 }
66
67 void IKModifier3D::_set_active(bool p_active) {
68 if (p_active) {
69 reset();
70 }
71 }
72
73 void IKModifier3D::_skeleton_changed(Skeleton3D *p_old, Skeleton3D *p_new) {
74 if (p_old && p_old->is_connected(SNAME("rest_updated"), callable_mp(this, &IKModifier3D::_rest_updated))) {
75 p_old->disconnect(SNAME("rest_updated"), callable_mp(this, &IKModifier3D::_rest_updated));
76 }
77 if (p_new && !p_new->is_connected(SNAME("rest_updated"), callable_mp(this, &IKModifier3D::_rest_updated))) {
78 p_new->connect(SNAME("rest_updated"), callable_mp(this, &IKModifier3D::_rest_updated));
79 }
80 _make_all_joints_dirty();
81 }
82
83 void IKModifier3D::_validate_bone_names() {
84 //
85 }
86
87 void IKModifier3D::_rest_updated() {
88 _make_all_joints_dirty();
89 if (is_inside_tree()) {
90 Skeleton3D *skeleton = get_skeleton();
91 if (skeleton) {
92 for (uint32_t i = 0; i < settings.size(); i++) {
93 _init_joints(skeleton, i);
94 }
95 }
96 }
97 #ifdef TOOLS_ENABLED
98 _update_mutable_info();
99 _make_gizmo_dirty();
100 #endif // TOOLS_ENABLED
101 }
102
103 void IKModifier3D::_make_all_joints_dirty() {
104 //
105 }
106
107 void IKModifier3D::_init_joints(Skeleton3D *p_skeleton, int p_index) {
108 //
109 }
110
111 void IKModifier3D::_update_joints(int p_index) {
112 //
113 }
114
115 void IKModifier3D::_make_simulation_dirty(int p_index) {
116 //
117 }
118
119 void IKModifier3D::_process_modification(double p_delta) {
120 Skeleton3D *skeleton = get_skeleton();
121 if (!skeleton) {
122 return;
123 }
124
125 _process_ik(skeleton, p_delta);
126 }
127
128 void IKModifier3D::_process_ik(Skeleton3D *p_skeleton, double p_delta) {
129 //
130 }
131
132 void IKModifier3D::_update_bone_axis(Skeleton3D *p_skeleton, int p_index) {
133 //
134 }
135
136 #ifdef TOOLS_ENABLED
137 void IKModifier3D::_make_gizmo_dirty() {
138 if (gizmo_dirty) {
139 return;
140 }
141 gizmo_dirty = true;
142
143 callable_mp(this, &IKModifier3D::_redraw_gizmo).call_deferred();
144 }
145
146 void IKModifier3D::_update_mutable_info() {
147 //
148 }
149
150 void IKModifier3D::_redraw_gizmo() {
151 update_gizmos();
152 gizmo_dirty = false;
153 }
154 #endif // TOOLS_ENABLED
155
156 void IKModifier3D::set_mutable_bone_axes(bool p_enabled) {
157 mutable_bone_axes = p_enabled;
158 for (uint32_t i = 0; i < settings.size(); i++) {
159 _make_simulation_dirty(i);
160 }
161 #ifdef TOOLS_ENABLED
162 _update_mutable_info();
163 #endif // TOOLS_ENABLED
164 }
165
166 bool IKModifier3D::are_bone_axes_mutable() const {
167 return mutable_bone_axes;
168 }
169
170 Quaternion IKModifier3D::get_local_pose_rotation(Skeleton3D *p_skeleton, int p_bone, const Quaternion &p_global_pose_rotation) {
171 int parent = p_skeleton->get_bone_parent(p_bone);
172 if (parent < 0) {
173 return p_global_pose_rotation;
174 }
175 return p_skeleton->get_bone_global_pose(parent).basis.get_rotation_quaternion().inverse() * p_global_pose_rotation;
176 }
177
178 Vector3 IKModifier3D::get_bone_axis(Skeleton3D *p_skeleton, int p_end_bone, BoneDirection p_direction, bool p_mutable_bone_axes) {
179 if (!p_skeleton || !p_skeleton->is_inside_tree()) {
180 return Vector3();
181 }
182 Vector3 axis;
183 if (p_direction == BONE_DIRECTION_FROM_PARENT) {
184 if (p_skeleton) {
185 axis = p_skeleton->get_bone_rest(p_end_bone).basis.xform_inv(p_mutable_bone_axes ? p_skeleton->get_bone_pose(p_end_bone).origin : p_skeleton->get_bone_rest(p_end_bone).origin);
186 axis.normalize();
187 }
188 } else {
189 axis = get_vector_from_bone_axis(static_cast<BoneAxis>((int)p_direction));
190 }
191 return axis;
192 }
193
194 int IKModifier3D::get_setting_count() const {
195 return settings.size();
196 }
197
198 void IKModifier3D::reset() {
199 Skeleton3D *skeleton = get_skeleton();
200 if (!skeleton) {
201 return;
202 }
203 for (uint32_t i = 0; i < settings.size(); i++) {
204 _make_simulation_dirty(i);
205 _init_joints(skeleton, i);
206 }
207 }
208
209 IKModifier3D::~IKModifier3D() {
210 clear_settings();
211 }