1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
#ifndef EDITOR_SCENE_IMPORTER_GLTF_H
#define EDITOR_SCENE_IMPORTER_GLTF_H
#include "editor/import/resource_importer_scene.h"
#include "scene/3d/skeleton.h"
#include "scene/3d/spatial.h"
class AnimationPlayer;
class EditorSceneImporterGLTF : public EditorSceneImporter {
GDCLASS(EditorSceneImporterGLTF, EditorSceneImporter);
enum {
ARRAY_BUFFER = 34962,
ELEMENT_ARRAY_BUFFER = 34963,
TYPE_BYTE = 5120,
TYPE_UNSIGNED_BYTE = 5121,
TYPE_SHORT = 5122,
TYPE_UNSIGNED_SHORT = 5123,
TYPE_UNSIGNED_INT = 5125,
TYPE_FLOAT = 5126,
COMPONENT_TYPE_BYTE = 5120,
COMPONENT_TYPE_UNSIGNED_BYTE = 5121,
COMPONENT_TYPE_SHORT = 5122,
COMPONENT_TYPE_UNSIGNED_SHORT = 5123,
COMPONENT_TYPE_INT = 5125,
COMPONENT_TYPE_FLOAT = 5126,
};
String _get_component_type_name(uint32_t p_component);
int _get_component_type_size(int component_type);
enum GLTFType {
TYPE_SCALAR,
TYPE_VEC2,
TYPE_VEC3,
TYPE_VEC4,
TYPE_MAT2,
TYPE_MAT3,
TYPE_MAT4,
};
String _get_type_name(GLTFType p_component);
struct GLTFNode {
//matrices need to be transformed to this
int parent;
Transform xform;
String name;
Node *godot_node;
int godot_bone_index;
int mesh;
int camera;
int skin;
int skeleton_skin;
int child_of_skeleton; // put as children of skeleton
Vector<int> skeleton_children; //skeleton put as children of this
int joint_skin;
int joint_bone;
//keep them for animation
Vector3 translation;
Quat rotation;
Vector3 scale;
Vector<int> children;
GLTFNode() {
godot_node = NULL;
godot_bone_index = -1;
joint_skin = -1;
joint_bone = -1;
child_of_skeleton = -1;
skeleton_skin = -1;
mesh = -1;
camera = -1;
parent = -1;
scale = Vector3(1, 1, 1);
}
};
struct GLTFBufferView {
int buffer;
int byte_offset;
int byte_length;
int byte_stride;
bool indices;
//matrices need to be transformed to this
GLTFBufferView() {
buffer = 0;
byte_offset = 0;
byte_length = 0;
byte_stride = 0;
indices = false;
}
};
struct GLTFAccessor {
int buffer_view;
int byte_offset;
int component_type;
bool normalized;
int count;
GLTFType type;
float min;
float max;
int sparse_count;
int sparse_indices_buffer_view;
int sparse_indices_byte_offset;
int sparse_indices_component_type;
int sparse_values_buffer_view;
int sparse_values_byte_offset;
//matrices need to be transformed to this
GLTFAccessor() {
buffer_view = 0;
byte_offset = 0;
component_type = 0;
normalized = false;
count = 0;
min = 0;
max = 0;
sparse_count = 0;
sparse_indices_byte_offset = 0;
sparse_values_byte_offset = 0;
}
};
struct GLTFTexture {
int src_image;
};
struct GLTFSkin {
String name;
struct Bone {
Transform inverse_bind;
int node;
};
int skeleton;
Vector<Bone> bones;
//matrices need to be transformed to this
GLTFSkin() {
skeleton = -1;
}
};
struct GLTFMesh {
Ref<ArrayMesh> mesh;
Vector<float> blend_weights;
};
struct GLTFCamera {
bool perspective;
float fov_size;
float zfar;
float znear;
GLTFCamera() {
perspective = true;
fov_size = 65;
zfar = 500;
znear = 0.1;
}
};
struct GLTFAnimation {
enum Interpolation {
INTERP_LINEAR,
INTERP_STEP,
INTERP_CATMULLROMSPLINE,
INTERP_CUBIC_SPLINE
};
template <class T>
struct Channel {
Interpolation interpolation;
Vector<float> times;
Vector<T> values;
};
struct Track {
Channel<Vector3> translation_track;
Channel<Quat> rotation_track;
Channel<Vector3> scale_track;
Vector<Channel<float> > weight_tracks;
};
String name;
Map<int, Track> tracks;
};
struct GLTFState {
Dictionary json;
int major_version;
int minor_version;
Vector<uint8_t> glb_data;
Vector<GLTFNode *> nodes;
Vector<Vector<uint8_t> > buffers;
Vector<GLTFBufferView> buffer_views;
Vector<GLTFAccessor> accessors;
Vector<GLTFMesh> meshes; //meshes are loaded directly, no reason not to.
Vector<Ref<Material> > materials;
String scene_name;
Vector<int> root_nodes;
Vector<GLTFTexture> textures;
Vector<Ref<Texture> > images;
Vector<GLTFSkin> skins;
Vector<GLTFCamera> cameras;
Set<String> unique_names;
Vector<GLTFAnimation> animations;
Map<int, Vector<int> > skin_users; //cache skin users
~GLTFState() {
for (int i = 0; i < nodes.size(); i++) {
memdelete(nodes[i]);
}
}
};
String _gen_unique_name(GLTFState &state, const String &p_name);
Ref<Texture> _get_texture(GLTFState &state, int p_texture);
Error _parse_json(const String &p_path, GLTFState &state);
Error _parse_glb(const String &p_path, GLTFState &state);
Error _parse_scenes(GLTFState &state);
Error _parse_nodes(GLTFState &state);
Error _parse_buffers(GLTFState &state, const String &p_base_path);
Error _parse_buffer_views(GLTFState &state);
GLTFType _get_type_from_str(const String &p_string);
Error _parse_accessors(GLTFState &state);
Error _decode_buffer_view(GLTFState &state, int p_buffer_view, double *dst, int skip_every, int skip_bytes, int element_size, int count, GLTFType type, int component_count, int component_type, int component_size, bool normalized, int byte_offset, bool for_vertex);
Vector<double> _decode_accessor(GLTFState &state, int p_accessor, bool p_for_vertex);
PoolVector<float> _decode_accessor_as_floats(GLTFState &state, int p_accessor, bool p_for_vertex);
PoolVector<int> _decode_accessor_as_ints(GLTFState &state, int p_accessor, bool p_for_vertex);
PoolVector<Vector2> _decode_accessor_as_vec2(GLTFState &state, int p_accessor, bool p_for_vertex);
PoolVector<Vector3> _decode_accessor_as_vec3(GLTFState &state, int p_accessor, bool p_for_vertex);
PoolVector<Color> _decode_accessor_as_color(GLTFState &state, int p_accessor, bool p_for_vertex);
Vector<Quat> _decode_accessor_as_quat(GLTFState &state, int p_accessor, bool p_for_vertex);
Vector<Transform2D> _decode_accessor_as_xform2d(GLTFState &state, int p_accessor, bool p_for_vertex);
Vector<Basis> _decode_accessor_as_basis(GLTFState &state, int p_accessor, bool p_for_vertex);
Vector<Transform> _decode_accessor_as_xform(GLTFState &state, int p_accessor, bool p_for_vertex);
void _generate_bone(GLTFState &state, int p_node, Vector<Skeleton *> &skeletons, int p_parent_bone);
void _generate_node(GLTFState &state, int p_node, Node *p_parent, Node *p_owner, Vector<Skeleton *> &skeletons);
void _import_animation(GLTFState &state, AnimationPlayer *ap, int index, int bake_fps, Vector<Skeleton *> skeletons);
Spatial *_generate_scene(GLTFState &state, int p_bake_fps);
Error _parse_meshes(GLTFState &state);
Error _parse_images(GLTFState &state, const String &p_base_path);
Error _parse_textures(GLTFState &state);
Error _parse_materials(GLTFState &state);
Error _parse_skins(GLTFState &state);
Error _parse_cameras(GLTFState &state);
Error _parse_animations(GLTFState &state);
void _assign_scene_names(GLTFState &state);
template <class T>
T _interpolate_track(const Vector<float> &p_times, const Vector<T> &p_values, float p_time, GLTFAnimation::Interpolation p_interp);
public:
virtual uint32_t get_import_flags() const;
virtual void get_extensions(List<String> *r_extensions) const;
virtual Node *import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps = NULL, Error *r_err = NULL);
virtual Ref<Animation> import_animation(const String &p_path, uint32_t p_flags, int p_bake_fps);
EditorSceneImporterGLTF();
};
#endif // EDITOR_SCENE_IMPORTER_GLTF_H
|