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