Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/3d/ik_modifier_3d.cpp
14709 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
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::_make_all_joints_dirty))) {
75
p_old->disconnect(SNAME("rest_updated"), callable_mp(this, &IKModifier3D::_make_all_joints_dirty));
76
}
77
if (p_new && !p_new->is_connected(SNAME("rest_updated"), callable_mp(this, &IKModifier3D::_make_all_joints_dirty))) {
78
p_new->connect(SNAME("rest_updated"), callable_mp(this, &IKModifier3D::_make_all_joints_dirty));
79
}
80
_make_all_joints_dirty();
81
}
82
83
void IKModifier3D::_validate_bone_names() {
84
//
85
}
86
87
void IKModifier3D::_make_all_joints_dirty() {
88
//
89
}
90
91
void IKModifier3D::_init_joints(Skeleton3D *p_skeleton, int p_index) {
92
//
93
}
94
95
void IKModifier3D::_update_joints(int p_index) {
96
//
97
}
98
99
void IKModifier3D::_make_simulation_dirty(int p_index) {
100
//
101
}
102
103
void IKModifier3D::_process_modification(double p_delta) {
104
Skeleton3D *skeleton = get_skeleton();
105
if (!skeleton) {
106
return;
107
}
108
109
_process_ik(skeleton, p_delta);
110
}
111
112
void IKModifier3D::_process_ik(Skeleton3D *p_skeleton, double p_delta) {
113
//
114
}
115
116
void IKModifier3D::_update_bone_axis(Skeleton3D *p_skeleton, int p_index) {
117
//
118
}
119
120
#ifdef TOOLS_ENABLED
121
void IKModifier3D::_make_gizmo_dirty() {
122
if (gizmo_dirty) {
123
return;
124
}
125
gizmo_dirty = true;
126
127
callable_mp(this, &IKModifier3D::_redraw_gizmo).call_deferred();
128
}
129
130
void IKModifier3D::_update_mutable_info() {
131
//
132
}
133
134
void IKModifier3D::_redraw_gizmo() {
135
update_gizmos();
136
gizmo_dirty = false;
137
}
138
#endif // TOOLS_ENABLED
139
140
void IKModifier3D::set_mutable_bone_axes(bool p_enabled) {
141
mutable_bone_axes = p_enabled;
142
for (uint32_t i = 0; i < settings.size(); i++) {
143
_make_simulation_dirty(i);
144
}
145
#ifdef TOOLS_ENABLED
146
_update_mutable_info();
147
#endif // TOOLS_ENABLED
148
}
149
150
bool IKModifier3D::are_bone_axes_mutable() const {
151
return mutable_bone_axes;
152
}
153
154
Quaternion IKModifier3D::get_local_pose_rotation(Skeleton3D *p_skeleton, int p_bone, const Quaternion &p_global_pose_rotation) {
155
int parent = p_skeleton->get_bone_parent(p_bone);
156
if (parent < 0) {
157
return p_global_pose_rotation;
158
}
159
return p_skeleton->get_bone_global_pose(parent).basis.get_rotation_quaternion().inverse() * p_global_pose_rotation;
160
}
161
162
Vector3 IKModifier3D::get_bone_axis(Skeleton3D *p_skeleton, int p_end_bone, BoneDirection p_direction, bool p_mutable_bone_axes) {
163
if (!p_skeleton->is_inside_tree()) {
164
return Vector3();
165
}
166
Vector3 axis;
167
if (p_direction == BONE_DIRECTION_FROM_PARENT) {
168
if (p_skeleton) {
169
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);
170
axis.normalize();
171
}
172
} else {
173
axis = get_vector_from_bone_axis(static_cast<BoneAxis>((int)p_direction));
174
}
175
return axis;
176
}
177
178
int IKModifier3D::get_setting_count() const {
179
return settings.size();
180
}
181
182
void IKModifier3D::reset() {
183
Skeleton3D *skeleton = get_skeleton();
184
if (!skeleton) {
185
return;
186
}
187
for (uint32_t i = 0; i < settings.size(); i++) {
188
_make_simulation_dirty(i);
189
_init_joints(skeleton, i);
190
}
191
}
192
193
IKModifier3D::~IKModifier3D() {
194
clear_settings();
195
}
196
197