Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gltf/structures/gltf_skin.cpp
10278 views
1
/**************************************************************************/
2
/* gltf_skin.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 "gltf_skin.h"
32
33
#include "../gltf_template_convert.h"
34
35
#include "core/variant/typed_array.h"
36
#include "scene/resources/3d/skin.h"
37
38
void GLTFSkin::_bind_methods() {
39
ClassDB::bind_method(D_METHOD("get_skin_root"), &GLTFSkin::get_skin_root);
40
ClassDB::bind_method(D_METHOD("set_skin_root", "skin_root"), &GLTFSkin::set_skin_root);
41
ClassDB::bind_method(D_METHOD("get_joints_original"), &GLTFSkin::get_joints_original);
42
ClassDB::bind_method(D_METHOD("set_joints_original", "joints_original"), &GLTFSkin::set_joints_original);
43
ClassDB::bind_method(D_METHOD("get_inverse_binds"), &GLTFSkin::get_inverse_binds);
44
ClassDB::bind_method(D_METHOD("set_inverse_binds", "inverse_binds"), &GLTFSkin::set_inverse_binds);
45
ClassDB::bind_method(D_METHOD("get_joints"), &GLTFSkin::get_joints);
46
ClassDB::bind_method(D_METHOD("set_joints", "joints"), &GLTFSkin::set_joints);
47
ClassDB::bind_method(D_METHOD("get_non_joints"), &GLTFSkin::get_non_joints);
48
ClassDB::bind_method(D_METHOD("set_non_joints", "non_joints"), &GLTFSkin::set_non_joints);
49
ClassDB::bind_method(D_METHOD("get_roots"), &GLTFSkin::get_roots);
50
ClassDB::bind_method(D_METHOD("set_roots", "roots"), &GLTFSkin::set_roots);
51
ClassDB::bind_method(D_METHOD("get_skeleton"), &GLTFSkin::get_skeleton);
52
ClassDB::bind_method(D_METHOD("set_skeleton", "skeleton"), &GLTFSkin::set_skeleton);
53
ClassDB::bind_method(D_METHOD("get_joint_i_to_bone_i"), &GLTFSkin::get_joint_i_to_bone_i);
54
ClassDB::bind_method(D_METHOD("set_joint_i_to_bone_i", "joint_i_to_bone_i"), &GLTFSkin::set_joint_i_to_bone_i);
55
ClassDB::bind_method(D_METHOD("get_joint_i_to_name"), &GLTFSkin::get_joint_i_to_name);
56
ClassDB::bind_method(D_METHOD("set_joint_i_to_name", "joint_i_to_name"), &GLTFSkin::set_joint_i_to_name);
57
ClassDB::bind_method(D_METHOD("get_godot_skin"), &GLTFSkin::get_godot_skin);
58
ClassDB::bind_method(D_METHOD("set_godot_skin", "godot_skin"), &GLTFSkin::set_godot_skin);
59
60
ADD_PROPERTY(PropertyInfo(Variant::INT, "skin_root"), "set_skin_root", "get_skin_root"); // GLTFNodeIndex
61
ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "joints_original"), "set_joints_original", "get_joints_original"); // Vector<GLTFNodeIndex>
62
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "inverse_binds", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL), "set_inverse_binds", "get_inverse_binds"); // Vector<Transform3D>
63
ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "joints"), "set_joints", "get_joints"); // Vector<GLTFNodeIndex>
64
ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "non_joints"), "set_non_joints", "get_non_joints"); // Vector<GLTFNodeIndex>
65
ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "roots"), "set_roots", "get_roots"); // Vector<GLTFNodeIndex>
66
ADD_PROPERTY(PropertyInfo(Variant::INT, "skeleton"), "set_skeleton", "get_skeleton"); // int
67
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "joint_i_to_bone_i", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL), "set_joint_i_to_bone_i", "get_joint_i_to_bone_i"); // RBMap<int,
68
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "joint_i_to_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL), "set_joint_i_to_name", "get_joint_i_to_name"); // RBMap<int,
69
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "godot_skin", PROPERTY_HINT_RESOURCE_TYPE, "Skin"), "set_godot_skin", "get_godot_skin"); // Ref<Skin>
70
}
71
72
GLTFNodeIndex GLTFSkin::get_skin_root() {
73
return skin_root;
74
}
75
76
void GLTFSkin::set_skin_root(GLTFNodeIndex p_skin_root) {
77
skin_root = p_skin_root;
78
}
79
80
Vector<GLTFNodeIndex> GLTFSkin::get_joints_original() {
81
return joints_original;
82
}
83
84
void GLTFSkin::set_joints_original(Vector<GLTFNodeIndex> p_joints_original) {
85
joints_original = p_joints_original;
86
}
87
88
TypedArray<Transform3D> GLTFSkin::get_inverse_binds() {
89
return GLTFTemplateConvert::to_array(inverse_binds);
90
}
91
92
void GLTFSkin::set_inverse_binds(TypedArray<Transform3D> p_inverse_binds) {
93
GLTFTemplateConvert::set_from_array(inverse_binds, p_inverse_binds);
94
}
95
96
Vector<GLTFNodeIndex> GLTFSkin::get_joints() {
97
return joints;
98
}
99
100
void GLTFSkin::set_joints(Vector<GLTFNodeIndex> p_joints) {
101
joints = p_joints;
102
}
103
104
Vector<GLTFNodeIndex> GLTFSkin::get_non_joints() {
105
return non_joints;
106
}
107
108
void GLTFSkin::set_non_joints(Vector<GLTFNodeIndex> p_non_joints) {
109
non_joints = p_non_joints;
110
}
111
112
Vector<GLTFNodeIndex> GLTFSkin::get_roots() {
113
return roots;
114
}
115
116
void GLTFSkin::set_roots(Vector<GLTFNodeIndex> p_roots) {
117
roots = p_roots;
118
}
119
120
int GLTFSkin::get_skeleton() {
121
return skeleton;
122
}
123
124
void GLTFSkin::set_skeleton(int p_skeleton) {
125
skeleton = p_skeleton;
126
}
127
128
Dictionary GLTFSkin::get_joint_i_to_bone_i() {
129
return GLTFTemplateConvert::to_dictionary(joint_i_to_bone_i);
130
}
131
132
void GLTFSkin::set_joint_i_to_bone_i(Dictionary p_joint_i_to_bone_i) {
133
GLTFTemplateConvert::set_from_dictionary(joint_i_to_bone_i, p_joint_i_to_bone_i);
134
}
135
136
Dictionary GLTFSkin::get_joint_i_to_name() {
137
Dictionary ret;
138
HashMap<int, StringName>::Iterator elem = joint_i_to_name.begin();
139
while (elem) {
140
ret[elem->key] = String(elem->value);
141
++elem;
142
}
143
return ret;
144
}
145
146
void GLTFSkin::set_joint_i_to_name(Dictionary p_joint_i_to_name) {
147
joint_i_to_name = HashMap<int, StringName>();
148
for (const KeyValue<Variant, Variant> &kv : p_joint_i_to_name) {
149
joint_i_to_name[kv.key] = kv.value;
150
}
151
}
152
153
Ref<Skin> GLTFSkin::get_godot_skin() {
154
return godot_skin;
155
}
156
157
void GLTFSkin::set_godot_skin(Ref<Skin> p_godot_skin) {
158
godot_skin = p_godot_skin;
159
}
160
161
Error GLTFSkin::from_dictionary(const Dictionary &dict) {
162
ERR_FAIL_COND_V(!dict.has("skin_root"), ERR_INVALID_DATA);
163
skin_root = dict["skin_root"];
164
165
ERR_FAIL_COND_V(!dict.has("joints_original"), ERR_INVALID_DATA);
166
Array joints_original_array = dict["joints_original"];
167
joints_original.clear();
168
for (int i = 0; i < joints_original_array.size(); ++i) {
169
joints_original.push_back(joints_original_array[i]);
170
}
171
172
ERR_FAIL_COND_V(!dict.has("inverse_binds"), ERR_INVALID_DATA);
173
Array inverse_binds_array = dict["inverse_binds"];
174
inverse_binds.clear();
175
for (int i = 0; i < inverse_binds_array.size(); ++i) {
176
ERR_FAIL_COND_V(inverse_binds_array[i].get_type() != Variant::TRANSFORM3D, ERR_INVALID_DATA);
177
inverse_binds.push_back(inverse_binds_array[i]);
178
}
179
180
ERR_FAIL_COND_V(!dict.has("joints"), ERR_INVALID_DATA);
181
Array joints_array = dict["joints"];
182
joints.clear();
183
for (int i = 0; i < joints_array.size(); ++i) {
184
joints.push_back(joints_array[i]);
185
}
186
187
ERR_FAIL_COND_V(!dict.has("non_joints"), ERR_INVALID_DATA);
188
Array non_joints_array = dict["non_joints"];
189
non_joints.clear();
190
for (int i = 0; i < non_joints_array.size(); ++i) {
191
non_joints.push_back(non_joints_array[i]);
192
}
193
194
ERR_FAIL_COND_V(!dict.has("roots"), ERR_INVALID_DATA);
195
Array roots_array = dict["roots"];
196
roots.clear();
197
for (int i = 0; i < roots_array.size(); ++i) {
198
roots.push_back(roots_array[i]);
199
}
200
201
ERR_FAIL_COND_V(!dict.has("skeleton"), ERR_INVALID_DATA);
202
skeleton = dict["skeleton"];
203
204
ERR_FAIL_COND_V(!dict.has("joint_i_to_bone_i"), ERR_INVALID_DATA);
205
Dictionary joint_i_to_bone_i_dict = dict["joint_i_to_bone_i"];
206
joint_i_to_bone_i.clear();
207
for (const KeyValue<Variant, Variant> &kv : joint_i_to_bone_i_dict) {
208
int key = kv.key;
209
int value = kv.value;
210
joint_i_to_bone_i[key] = value;
211
}
212
213
ERR_FAIL_COND_V(!dict.has("joint_i_to_name"), ERR_INVALID_DATA);
214
Dictionary joint_i_to_name_dict = dict["joint_i_to_name"];
215
joint_i_to_name.clear();
216
for (const KeyValue<Variant, Variant> &kv : joint_i_to_name_dict) {
217
int key = kv.key;
218
StringName value = kv.value;
219
joint_i_to_name[key] = value;
220
}
221
if (dict.has("godot_skin")) {
222
godot_skin = dict["godot_skin"];
223
}
224
return OK;
225
}
226
227
Dictionary GLTFSkin::to_dictionary() {
228
Dictionary dict;
229
dict["skin_root"] = skin_root;
230
231
Array joints_original_array;
232
for (int i = 0; i < joints_original.size(); ++i) {
233
joints_original_array.push_back(joints_original[i]);
234
}
235
dict["joints_original"] = joints_original_array;
236
237
Array inverse_binds_array;
238
for (int i = 0; i < inverse_binds.size(); ++i) {
239
inverse_binds_array.push_back(inverse_binds[i]);
240
}
241
dict["inverse_binds"] = inverse_binds_array;
242
243
Array joints_array;
244
for (int i = 0; i < joints.size(); ++i) {
245
joints_array.push_back(joints[i]);
246
}
247
dict["joints"] = joints_array;
248
249
Array non_joints_array;
250
for (int i = 0; i < non_joints.size(); ++i) {
251
non_joints_array.push_back(non_joints[i]);
252
}
253
dict["non_joints"] = non_joints_array;
254
255
Array roots_array;
256
for (int i = 0; i < roots.size(); ++i) {
257
roots_array.push_back(roots[i]);
258
}
259
dict["roots"] = roots_array;
260
261
dict["skeleton"] = skeleton;
262
263
Dictionary joint_i_to_bone_i_dict;
264
for (HashMap<int, int>::Iterator E = joint_i_to_bone_i.begin(); E; ++E) {
265
joint_i_to_bone_i_dict[E->key] = E->value;
266
}
267
dict["joint_i_to_bone_i"] = joint_i_to_bone_i_dict;
268
269
Dictionary joint_i_to_name_dict;
270
for (HashMap<int, StringName>::Iterator E = joint_i_to_name.begin(); E; ++E) {
271
joint_i_to_name_dict[E->key] = E->value;
272
}
273
dict["joint_i_to_name"] = joint_i_to_name_dict;
274
275
dict["godot_skin"] = godot_skin;
276
return dict;
277
}
278
279