1 /*************************************************************************/
2 /*  editor_scene_importer_gltf.cpp                                       */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 "editor_scene_importer_gltf.h"
32 #include "core/crypto/crypto_core.h"
33 #include "core/io/json.h"
34 #include "core/math/disjoint_set.h"
35 #include "core/math/math_defs.h"
36 #include "core/os/file_access.h"
37 #include "core/os/os.h"
38 #include "modules/regex/regex.h"
39 #include "scene/3d/bone_attachment.h"
40 #include "scene/3d/camera.h"
41 #include "scene/3d/mesh_instance.h"
42 #include "scene/animation/animation_player.h"
43 #include "scene/resources/surface_tool.h"
44 
get_import_flags() const45 uint32_t EditorSceneImporterGLTF::get_import_flags() const {
46 
47 	return IMPORT_SCENE | IMPORT_ANIMATION;
48 }
get_extensions(List<String> * r_extensions) const49 void EditorSceneImporterGLTF::get_extensions(List<String> *r_extensions) const {
50 
51 	r_extensions->push_back("gltf");
52 	r_extensions->push_back("glb");
53 }
54 
_parse_json(const String & p_path,GLTFState & state)55 Error EditorSceneImporterGLTF::_parse_json(const String &p_path, GLTFState &state) {
56 
57 	Error err;
58 	FileAccessRef f = FileAccess::open(p_path, FileAccess::READ, &err);
59 	if (!f) {
60 		return err;
61 	}
62 
63 	Vector<uint8_t> array;
64 	array.resize(f->get_len());
65 	f->get_buffer(array.ptrw(), array.size());
66 	String text;
67 	text.parse_utf8((const char *)array.ptr(), array.size());
68 
69 	String err_txt;
70 	int err_line;
71 	Variant v;
72 	err = JSON::parse(text, v, err_txt, err_line);
73 	if (err != OK) {
74 		_err_print_error("", p_path.utf8().get_data(), err_line, err_txt.utf8().get_data(), ERR_HANDLER_SCRIPT);
75 		return err;
76 	}
77 	state.json = v;
78 
79 	return OK;
80 }
81 
_parse_glb(const String & p_path,GLTFState & state)82 Error EditorSceneImporterGLTF::_parse_glb(const String &p_path, GLTFState &state) {
83 
84 	Error err;
85 	FileAccessRef f = FileAccess::open(p_path, FileAccess::READ, &err);
86 	if (!f) {
87 		return err;
88 	}
89 
90 	uint32_t magic = f->get_32();
91 	ERR_FAIL_COND_V(magic != 0x46546C67, ERR_FILE_UNRECOGNIZED); //glTF
92 	f->get_32(); // version
93 	f->get_32(); // length
94 
95 	uint32_t chunk_length = f->get_32();
96 	uint32_t chunk_type = f->get_32();
97 
98 	ERR_FAIL_COND_V(chunk_type != 0x4E4F534A, ERR_PARSE_ERROR); //JSON
99 	Vector<uint8_t> json_data;
100 	json_data.resize(chunk_length);
101 	uint32_t len = f->get_buffer(json_data.ptrw(), chunk_length);
102 	ERR_FAIL_COND_V(len != chunk_length, ERR_FILE_CORRUPT);
103 
104 	String text;
105 	text.parse_utf8((const char *)json_data.ptr(), json_data.size());
106 
107 	String err_txt;
108 	int err_line;
109 	Variant v;
110 	err = JSON::parse(text, v, err_txt, err_line);
111 	if (err != OK) {
112 		_err_print_error("", p_path.utf8().get_data(), err_line, err_txt.utf8().get_data(), ERR_HANDLER_SCRIPT);
113 		return err;
114 	}
115 
116 	state.json = v;
117 
118 	//data?
119 
120 	chunk_length = f->get_32();
121 	chunk_type = f->get_32();
122 
123 	if (f->eof_reached()) {
124 		return OK; //all good
125 	}
126 
127 	ERR_FAIL_COND_V(chunk_type != 0x004E4942, ERR_PARSE_ERROR); //BIN
128 
129 	state.glb_data.resize(chunk_length);
130 	len = f->get_buffer(state.glb_data.ptrw(), chunk_length);
131 	ERR_FAIL_COND_V(len != chunk_length, ERR_FILE_CORRUPT);
132 
133 	return OK;
134 }
135 
_arr_to_vec3(const Array & p_array)136 static Vector3 _arr_to_vec3(const Array &p_array) {
137 	ERR_FAIL_COND_V(p_array.size() != 3, Vector3());
138 	return Vector3(p_array[0], p_array[1], p_array[2]);
139 }
140 
_arr_to_quat(const Array & p_array)141 static Quat _arr_to_quat(const Array &p_array) {
142 	ERR_FAIL_COND_V(p_array.size() != 4, Quat());
143 	return Quat(p_array[0], p_array[1], p_array[2], p_array[3]);
144 }
145 
_arr_to_xform(const Array & p_array)146 static Transform _arr_to_xform(const Array &p_array) {
147 	ERR_FAIL_COND_V(p_array.size() != 16, Transform());
148 
149 	Transform xform;
150 	xform.basis.set_axis(Vector3::AXIS_X, Vector3(p_array[0], p_array[1], p_array[2]));
151 	xform.basis.set_axis(Vector3::AXIS_Y, Vector3(p_array[4], p_array[5], p_array[6]));
152 	xform.basis.set_axis(Vector3::AXIS_Z, Vector3(p_array[8], p_array[9], p_array[10]));
153 	xform.set_origin(Vector3(p_array[12], p_array[13], p_array[14]));
154 
155 	return xform;
156 }
157 
_sanitize_scene_name(const String & name)158 String EditorSceneImporterGLTF::_sanitize_scene_name(const String &name) {
159 	RegEx regex("([^a-zA-Z0-9_ -]+)");
160 	String p_name = regex.sub(name, "", true);
161 	return p_name;
162 }
163 
_gen_unique_name(GLTFState & state,const String & p_name)164 String EditorSceneImporterGLTF::_gen_unique_name(GLTFState &state, const String &p_name) {
165 
166 	const String s_name = _sanitize_scene_name(p_name);
167 
168 	String name;
169 	int index = 1;
170 	while (true) {
171 		name = s_name;
172 
173 		if (index > 1) {
174 			name += " " + itos(index);
175 		}
176 		if (!state.unique_names.has(name)) {
177 			break;
178 		}
179 		index++;
180 	}
181 
182 	state.unique_names.insert(name);
183 
184 	return name;
185 }
186 
_sanitize_bone_name(const String & name)187 String EditorSceneImporterGLTF::_sanitize_bone_name(const String &name) {
188 	String p_name = name.camelcase_to_underscore(true);
189 
190 	RegEx pattern_del("([^a-zA-Z0-9_ ])+");
191 	p_name = pattern_del.sub(p_name, "", true);
192 
193 	RegEx pattern_nospace(" +");
194 	p_name = pattern_nospace.sub(p_name, "_", true);
195 
196 	RegEx pattern_multiple("_+");
197 	p_name = pattern_multiple.sub(p_name, "_", true);
198 
199 	RegEx pattern_padded("0+(\\d+)");
200 	p_name = pattern_padded.sub(p_name, "$1", true);
201 
202 	return p_name;
203 }
204 
_gen_unique_bone_name(GLTFState & state,const GLTFSkeletonIndex skel_i,const String & p_name)205 String EditorSceneImporterGLTF::_gen_unique_bone_name(GLTFState &state, const GLTFSkeletonIndex skel_i, const String &p_name) {
206 
207 	const String s_name = _sanitize_bone_name(p_name);
208 
209 	String name;
210 	int index = 1;
211 	while (true) {
212 		name = s_name;
213 
214 		if (index > 1) {
215 			name += "_" + itos(index);
216 		}
217 		if (!state.skeletons[skel_i].unique_names.has(name)) {
218 			break;
219 		}
220 		index++;
221 	}
222 
223 	state.skeletons.write[skel_i].unique_names.insert(name);
224 
225 	return name;
226 }
227 
_parse_scenes(GLTFState & state)228 Error EditorSceneImporterGLTF::_parse_scenes(GLTFState &state) {
229 
230 	ERR_FAIL_COND_V(!state.json.has("scenes"), ERR_FILE_CORRUPT);
231 	const Array &scenes = state.json["scenes"];
232 	int loaded_scene = 0;
233 	if (state.json.has("scene")) {
234 		loaded_scene = state.json["scene"];
235 	} else {
236 		WARN_PRINT("The load-time scene is not defined in the glTF2 file. Picking the first scene.")
237 	}
238 
239 	if (scenes.size()) {
240 		ERR_FAIL_COND_V(loaded_scene >= scenes.size(), ERR_FILE_CORRUPT);
241 		const Dictionary &s = scenes[loaded_scene];
242 		ERR_FAIL_COND_V(!s.has("nodes"), ERR_UNAVAILABLE);
243 		const Array &nodes = s["nodes"];
244 		for (int j = 0; j < nodes.size(); j++) {
245 			state.root_nodes.push_back(nodes[j]);
246 		}
247 
248 		if (s.has("name") && s["name"] != "") {
249 			state.scene_name = _gen_unique_name(state, s["name"]);
250 		} else {
251 			state.scene_name = _gen_unique_name(state, "Scene");
252 		}
253 	}
254 
255 	return OK;
256 }
257 
_parse_nodes(GLTFState & state)258 Error EditorSceneImporterGLTF::_parse_nodes(GLTFState &state) {
259 
260 	ERR_FAIL_COND_V(!state.json.has("nodes"), ERR_FILE_CORRUPT);
261 	const Array &nodes = state.json["nodes"];
262 	for (int i = 0; i < nodes.size(); i++) {
263 
264 		GLTFNode *node = memnew(GLTFNode);
265 		const Dictionary &n = nodes[i];
266 
267 		if (n.has("name")) {
268 			node->name = n["name"];
269 		}
270 		if (n.has("camera")) {
271 			node->camera = n["camera"];
272 		}
273 		if (n.has("mesh")) {
274 			node->mesh = n["mesh"];
275 		}
276 		if (n.has("skin")) {
277 			node->skin = n["skin"];
278 		}
279 		if (n.has("matrix")) {
280 			node->xform = _arr_to_xform(n["matrix"]);
281 
282 		} else {
283 
284 			if (n.has("translation")) {
285 				node->translation = _arr_to_vec3(n["translation"]);
286 			}
287 			if (n.has("rotation")) {
288 				node->rotation = _arr_to_quat(n["rotation"]);
289 			}
290 			if (n.has("scale")) {
291 				node->scale = _arr_to_vec3(n["scale"]);
292 			}
293 
294 			node->xform.basis.set_quat_scale(node->rotation, node->scale);
295 			node->xform.origin = node->translation;
296 		}
297 		if (n.has("extensions")) {
298 			Dictionary extensions = n["extensions"];
299 			if (extensions.has("KHR_lights_punctual")) {
300 				Dictionary lights_punctual = extensions["KHR_lights_punctual"];
301 				if (lights_punctual.has("light")) {
302 					GLTFLightIndex light = lights_punctual["light"];
303 					node->light = light;
304 				}
305 			}
306 		}
307 		if (n.has("children")) {
308 			const Array &children = n["children"];
309 			for (int j = 0; j < children.size(); j++) {
310 				node->children.push_back(children[j]);
311 			}
312 		}
313 
314 		state.nodes.push_back(node);
315 	}
316 
317 	// build the hierarchy
318 	for (GLTFNodeIndex node_i = 0; node_i < state.nodes.size(); node_i++) {
319 
320 		for (int j = 0; j < state.nodes[node_i]->children.size(); j++) {
321 			GLTFNodeIndex child_i = state.nodes[node_i]->children[j];
322 
323 			ERR_FAIL_INDEX_V(child_i, state.nodes.size(), ERR_FILE_CORRUPT);
324 			ERR_CONTINUE(state.nodes[child_i]->parent != -1); //node already has a parent, wtf.
325 
326 			state.nodes[child_i]->parent = node_i;
327 		}
328 	}
329 
330 	_compute_node_heights(state);
331 
332 	return OK;
333 }
334 
_compute_node_heights(GLTFState & state)335 void EditorSceneImporterGLTF::_compute_node_heights(GLTFState &state) {
336 
337 	state.root_nodes.clear();
338 	for (GLTFNodeIndex node_i = 0; node_i < state.nodes.size(); ++node_i) {
339 		GLTFNode *node = state.nodes[node_i];
340 		node->height = 0;
341 
342 		GLTFNodeIndex current_i = node_i;
343 		while (current_i >= 0) {
344 			const GLTFNodeIndex parent_i = state.nodes[current_i]->parent;
345 			if (parent_i >= 0) {
346 				++node->height;
347 			}
348 			current_i = parent_i;
349 		}
350 
351 		if (node->height == 0) {
352 			state.root_nodes.push_back(node_i);
353 		}
354 	}
355 }
356 
_parse_base64_uri(const String & uri)357 static Vector<uint8_t> _parse_base64_uri(const String &uri) {
358 
359 	int start = uri.find(",");
360 	ERR_FAIL_COND_V(start == -1, Vector<uint8_t>());
361 
362 	CharString substr = uri.right(start + 1).ascii();
363 
364 	int strlen = substr.length();
365 
366 	Vector<uint8_t> buf;
367 	buf.resize(strlen / 4 * 3 + 1 + 1);
368 
369 	size_t len = 0;
370 	ERR_FAIL_COND_V(CryptoCore::b64_decode(buf.ptrw(), buf.size(), &len, (unsigned char *)substr.get_data(), strlen) != OK, Vector<uint8_t>());
371 
372 	buf.resize(len);
373 
374 	return buf;
375 }
376 
_parse_buffers(GLTFState & state,const String & p_base_path)377 Error EditorSceneImporterGLTF::_parse_buffers(GLTFState &state, const String &p_base_path) {
378 
379 	if (!state.json.has("buffers"))
380 		return OK;
381 
382 	const Array &buffers = state.json["buffers"];
383 	for (GLTFBufferIndex i = 0; i < buffers.size(); i++) {
384 
385 		if (i == 0 && state.glb_data.size()) {
386 			state.buffers.push_back(state.glb_data);
387 
388 		} else {
389 			const Dictionary &buffer = buffers[i];
390 			if (buffer.has("uri")) {
391 
392 				Vector<uint8_t> buffer_data;
393 				String uri = buffer["uri"];
394 
395 				if (uri.findn("data:application/octet-stream;base64") == 0) {
396 					//embedded data
397 					buffer_data = _parse_base64_uri(uri);
398 				} else {
399 
400 					uri = p_base_path.plus_file(uri).replace("\\", "/"); //fix for windows
401 					buffer_data = FileAccess::get_file_as_array(uri);
402 					ERR_FAIL_COND_V(buffer.size() == 0, ERR_PARSE_ERROR);
403 				}
404 
405 				ERR_FAIL_COND_V(!buffer.has("byteLength"), ERR_PARSE_ERROR);
406 				int byteLength = buffer["byteLength"];
407 				ERR_FAIL_COND_V(byteLength < buffer_data.size(), ERR_PARSE_ERROR);
408 				state.buffers.push_back(buffer_data);
409 			}
410 		}
411 	}
412 
413 	print_verbose("glTF: Total buffers: " + itos(state.buffers.size()));
414 
415 	return OK;
416 }
417 
_parse_buffer_views(GLTFState & state)418 Error EditorSceneImporterGLTF::_parse_buffer_views(GLTFState &state) {
419 
420 	ERR_FAIL_COND_V(!state.json.has("bufferViews"), ERR_FILE_CORRUPT);
421 	const Array &buffers = state.json["bufferViews"];
422 	for (GLTFBufferViewIndex i = 0; i < buffers.size(); i++) {
423 
424 		const Dictionary &d = buffers[i];
425 
426 		GLTFBufferView buffer_view;
427 
428 		ERR_FAIL_COND_V(!d.has("buffer"), ERR_PARSE_ERROR);
429 		buffer_view.buffer = d["buffer"];
430 		ERR_FAIL_COND_V(!d.has("byteLength"), ERR_PARSE_ERROR);
431 		buffer_view.byte_length = d["byteLength"];
432 
433 		if (d.has("byteOffset")) {
434 			buffer_view.byte_offset = d["byteOffset"];
435 		}
436 
437 		if (d.has("byteStride")) {
438 			buffer_view.byte_stride = d["byteStride"];
439 		}
440 
441 		if (d.has("target")) {
442 			const int target = d["target"];
443 			buffer_view.indices = target == ELEMENT_ARRAY_BUFFER;
444 		}
445 
446 		state.buffer_views.push_back(buffer_view);
447 	}
448 
449 	print_verbose("glTF: Total buffer views: " + itos(state.buffer_views.size()));
450 
451 	return OK;
452 }
453 
_get_type_from_str(const String & p_string)454 EditorSceneImporterGLTF::GLTFType EditorSceneImporterGLTF::_get_type_from_str(const String &p_string) {
455 
456 	if (p_string == "SCALAR")
457 		return TYPE_SCALAR;
458 
459 	if (p_string == "VEC2")
460 		return TYPE_VEC2;
461 	if (p_string == "VEC3")
462 		return TYPE_VEC3;
463 	if (p_string == "VEC4")
464 		return TYPE_VEC4;
465 
466 	if (p_string == "MAT2")
467 		return TYPE_MAT2;
468 	if (p_string == "MAT3")
469 		return TYPE_MAT3;
470 	if (p_string == "MAT4")
471 		return TYPE_MAT4;
472 
473 	ERR_FAIL_V(TYPE_SCALAR);
474 }
475 
_parse_accessors(GLTFState & state)476 Error EditorSceneImporterGLTF::_parse_accessors(GLTFState &state) {
477 
478 	ERR_FAIL_COND_V(!state.json.has("accessors"), ERR_FILE_CORRUPT);
479 	const Array &accessors = state.json["accessors"];
480 	for (GLTFAccessorIndex i = 0; i < accessors.size(); i++) {
481 
482 		const Dictionary &d = accessors[i];
483 
484 		GLTFAccessor accessor;
485 
486 		ERR_FAIL_COND_V(!d.has("componentType"), ERR_PARSE_ERROR);
487 		accessor.component_type = d["componentType"];
488 		ERR_FAIL_COND_V(!d.has("count"), ERR_PARSE_ERROR);
489 		accessor.count = d["count"];
490 		ERR_FAIL_COND_V(!d.has("type"), ERR_PARSE_ERROR);
491 		accessor.type = _get_type_from_str(d["type"]);
492 
493 		if (d.has("bufferView")) {
494 			accessor.buffer_view = d["bufferView"]; //optional because it may be sparse...
495 		}
496 
497 		if (d.has("byteOffset")) {
498 			accessor.byte_offset = d["byteOffset"];
499 		}
500 
501 		if (d.has("max")) {
502 			accessor.max = d["max"];
503 		}
504 
505 		if (d.has("min")) {
506 			accessor.min = d["min"];
507 		}
508 
509 		if (d.has("sparse")) {
510 			//eeh..
511 
512 			const Dictionary &s = d["sparse"];
513 
514 			ERR_FAIL_COND_V(!s.has("count"), ERR_PARSE_ERROR);
515 			accessor.sparse_count = s["count"];
516 			ERR_FAIL_COND_V(!s.has("indices"), ERR_PARSE_ERROR);
517 			const Dictionary &si = s["indices"];
518 
519 			ERR_FAIL_COND_V(!si.has("bufferView"), ERR_PARSE_ERROR);
520 			accessor.sparse_indices_buffer_view = si["bufferView"];
521 			ERR_FAIL_COND_V(!si.has("componentType"), ERR_PARSE_ERROR);
522 			accessor.sparse_indices_component_type = si["componentType"];
523 
524 			if (si.has("byteOffset")) {
525 				accessor.sparse_indices_byte_offset = si["byteOffset"];
526 			}
527 
528 			ERR_FAIL_COND_V(!s.has("values"), ERR_PARSE_ERROR);
529 			const Dictionary &sv = s["values"];
530 
531 			ERR_FAIL_COND_V(!sv.has("bufferView"), ERR_PARSE_ERROR);
532 			accessor.sparse_values_buffer_view = sv["bufferView"];
533 			if (sv.has("byteOffset")) {
534 				accessor.sparse_values_byte_offset = sv["byteOffset"];
535 			}
536 		}
537 
538 		state.accessors.push_back(accessor);
539 	}
540 
541 	print_verbose("glTF: Total accessors: " + itos(state.accessors.size()));
542 
543 	return OK;
544 }
545 
_get_component_type_name(const uint32_t p_component)546 String EditorSceneImporterGLTF::_get_component_type_name(const uint32_t p_component) {
547 
548 	switch (p_component) {
549 		case COMPONENT_TYPE_BYTE: return "Byte";
550 		case COMPONENT_TYPE_UNSIGNED_BYTE: return "UByte";
551 		case COMPONENT_TYPE_SHORT: return "Short";
552 		case COMPONENT_TYPE_UNSIGNED_SHORT: return "UShort";
553 		case COMPONENT_TYPE_INT: return "Int";
554 		case COMPONENT_TYPE_FLOAT: return "Float";
555 	}
556 
557 	return "<Error>";
558 }
559 
_get_type_name(const GLTFType p_component)560 String EditorSceneImporterGLTF::_get_type_name(const GLTFType p_component) {
561 
562 	static const char *names[] = {
563 		"float",
564 		"vec2",
565 		"vec3",
566 		"vec4",
567 		"mat2",
568 		"mat3",
569 		"mat4"
570 	};
571 
572 	return names[p_component];
573 }
574 
_decode_buffer_view(GLTFState & state,double * dst,const GLTFBufferViewIndex p_buffer_view,const int skip_every,const int skip_bytes,const int element_size,const int count,const GLTFType type,const int component_count,const int component_type,const int component_size,const bool normalized,const int byte_offset,const bool for_vertex)575 Error EditorSceneImporterGLTF::_decode_buffer_view(GLTFState &state, double *dst, const GLTFBufferViewIndex p_buffer_view, const int skip_every, const int skip_bytes, const int element_size, const int count, const GLTFType type, const int component_count, const int component_type, const int component_size, const bool normalized, const int byte_offset, const bool for_vertex) {
576 
577 	const GLTFBufferView &bv = state.buffer_views[p_buffer_view];
578 
579 	int stride = bv.byte_stride ? bv.byte_stride : element_size;
580 	if (for_vertex && stride % 4) {
581 		stride += 4 - (stride % 4); //according to spec must be multiple of 4
582 	}
583 
584 	ERR_FAIL_INDEX_V(bv.buffer, state.buffers.size(), ERR_PARSE_ERROR);
585 
586 	const uint32_t offset = bv.byte_offset + byte_offset;
587 	Vector<uint8_t> buffer = state.buffers[bv.buffer]; //copy on write, so no performance hit
588 	const uint8_t *bufptr = buffer.ptr();
589 
590 	//use to debug
591 	print_verbose("glTF: type " + _get_type_name(type) + " component type: " + _get_component_type_name(component_type) + " stride: " + itos(stride) + " amount " + itos(count));
592 	print_verbose("glTF: accessor offset" + itos(byte_offset) + " view offset: " + itos(bv.byte_offset) + " total buffer len: " + itos(buffer.size()) + " view len " + itos(bv.byte_length));
593 
594 	const int buffer_end = (stride * (count - 1)) + element_size;
595 	ERR_FAIL_COND_V(buffer_end > bv.byte_length, ERR_PARSE_ERROR);
596 
597 	ERR_FAIL_COND_V((int)(offset + buffer_end) > buffer.size(), ERR_PARSE_ERROR);
598 
599 	//fill everything as doubles
600 
601 	for (int i = 0; i < count; i++) {
602 
603 		const uint8_t *src = &bufptr[offset + i * stride];
604 
605 		for (int j = 0; j < component_count; j++) {
606 
607 			if (skip_every && j > 0 && (j % skip_every) == 0) {
608 				src += skip_bytes;
609 			}
610 
611 			double d = 0;
612 
613 			switch (component_type) {
614 				case COMPONENT_TYPE_BYTE: {
615 					int8_t b = int8_t(*src);
616 					if (normalized) {
617 						d = (double(b) / 128.0);
618 					} else {
619 						d = double(b);
620 					}
621 				} break;
622 				case COMPONENT_TYPE_UNSIGNED_BYTE: {
623 					uint8_t b = *src;
624 					if (normalized) {
625 						d = (double(b) / 255.0);
626 					} else {
627 						d = double(b);
628 					}
629 				} break;
630 				case COMPONENT_TYPE_SHORT: {
631 					int16_t s = *(int16_t *)src;
632 					if (normalized) {
633 						d = (double(s) / 32768.0);
634 					} else {
635 						d = double(s);
636 					}
637 				} break;
638 				case COMPONENT_TYPE_UNSIGNED_SHORT: {
639 					uint16_t s = *(uint16_t *)src;
640 					if (normalized) {
641 						d = (double(s) / 65535.0);
642 					} else {
643 						d = double(s);
644 					}
645 
646 				} break;
647 				case COMPONENT_TYPE_INT: {
648 					d = *(int *)src;
649 				} break;
650 				case COMPONENT_TYPE_FLOAT: {
651 					d = *(float *)src;
652 				} break;
653 			}
654 
655 			*dst++ = d;
656 			src += component_size;
657 		}
658 	}
659 
660 	return OK;
661 }
662 
_get_component_type_size(const int component_type)663 int EditorSceneImporterGLTF::_get_component_type_size(const int component_type) {
664 
665 	switch (component_type) {
666 		case COMPONENT_TYPE_BYTE: return 1; break;
667 		case COMPONENT_TYPE_UNSIGNED_BYTE: return 1; break;
668 		case COMPONENT_TYPE_SHORT: return 2; break;
669 		case COMPONENT_TYPE_UNSIGNED_SHORT: return 2; break;
670 		case COMPONENT_TYPE_INT: return 4; break;
671 		case COMPONENT_TYPE_FLOAT: return 4; break;
672 		default: {
673 			ERR_FAIL_V(0);
674 		}
675 	}
676 	return 0;
677 }
678 
_decode_accessor(GLTFState & state,const GLTFAccessorIndex p_accessor,const bool p_for_vertex)679 Vector<double> EditorSceneImporterGLTF::_decode_accessor(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex) {
680 
681 	//spec, for reference:
682 	//https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#data-alignment
683 
684 	ERR_FAIL_INDEX_V(p_accessor, state.accessors.size(), Vector<double>());
685 
686 	const GLTFAccessor &a = state.accessors[p_accessor];
687 
688 	const int component_count_for_type[7] = {
689 		1, 2, 3, 4, 4, 9, 16
690 	};
691 
692 	const int component_count = component_count_for_type[a.type];
693 	const int component_size = _get_component_type_size(a.component_type);
694 	ERR_FAIL_COND_V(component_size == 0, Vector<double>());
695 	int element_size = component_count * component_size;
696 
697 	int skip_every = 0;
698 	int skip_bytes = 0;
699 	//special case of alignments, as described in spec
700 	switch (a.component_type) {
701 		case COMPONENT_TYPE_BYTE:
702 		case COMPONENT_TYPE_UNSIGNED_BYTE: {
703 
704 			if (a.type == TYPE_MAT2) {
705 				skip_every = 2;
706 				skip_bytes = 2;
707 				element_size = 8; //override for this case
708 			}
709 			if (a.type == TYPE_MAT3) {
710 				skip_every = 3;
711 				skip_bytes = 1;
712 				element_size = 12; //override for this case
713 			}
714 
715 		} break;
716 		case COMPONENT_TYPE_SHORT:
717 		case COMPONENT_TYPE_UNSIGNED_SHORT: {
718 			if (a.type == TYPE_MAT3) {
719 				skip_every = 6;
720 				skip_bytes = 4;
721 				element_size = 16; //override for this case
722 			}
723 		} break;
724 		default: {
725 		}
726 	}
727 
728 	Vector<double> dst_buffer;
729 	dst_buffer.resize(component_count * a.count);
730 	double *dst = dst_buffer.ptrw();
731 
732 	if (a.buffer_view >= 0) {
733 
734 		ERR_FAIL_INDEX_V(a.buffer_view, state.buffer_views.size(), Vector<double>());
735 
736 		const Error err = _decode_buffer_view(state, dst, a.buffer_view, skip_every, skip_bytes, element_size, a.count, a.type, component_count, a.component_type, component_size, a.normalized, a.byte_offset, p_for_vertex);
737 		if (err != OK)
738 			return Vector<double>();
739 
740 	} else {
741 		//fill with zeros, as bufferview is not defined.
742 		for (int i = 0; i < (a.count * component_count); i++) {
743 			dst_buffer.write[i] = 0;
744 		}
745 	}
746 
747 	if (a.sparse_count > 0) {
748 		// I could not find any file using this, so this code is so far untested
749 		Vector<double> indices;
750 		indices.resize(a.sparse_count);
751 		const int indices_component_size = _get_component_type_size(a.sparse_indices_component_type);
752 
753 		Error err = _decode_buffer_view(state, indices.ptrw(), a.sparse_indices_buffer_view, 0, 0, indices_component_size, a.sparse_count, TYPE_SCALAR, 1, a.sparse_indices_component_type, indices_component_size, false, a.sparse_indices_byte_offset, false);
754 		if (err != OK)
755 			return Vector<double>();
756 
757 		Vector<double> data;
758 		data.resize(component_count * a.sparse_count);
759 		err = _decode_buffer_view(state, data.ptrw(), a.sparse_values_buffer_view, skip_every, skip_bytes, element_size, a.sparse_count, a.type, component_count, a.component_type, component_size, a.normalized, a.sparse_values_byte_offset, p_for_vertex);
760 		if (err != OK)
761 			return Vector<double>();
762 
763 		for (int i = 0; i < indices.size(); i++) {
764 			const int write_offset = int(indices[i]) * component_count;
765 
766 			for (int j = 0; j < component_count; j++) {
767 				dst[write_offset + j] = data[i * component_count + j];
768 			}
769 		}
770 	}
771 
772 	return dst_buffer;
773 }
774 
_decode_accessor_as_ints(GLTFState & state,const GLTFAccessorIndex p_accessor,const bool p_for_vertex)775 PoolVector<int> EditorSceneImporterGLTF::_decode_accessor_as_ints(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex) {
776 
777 	const Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex);
778 	PoolVector<int> ret;
779 
780 	if (attribs.size() == 0)
781 		return ret;
782 
783 	const double *attribs_ptr = attribs.ptr();
784 	const int ret_size = attribs.size();
785 	ret.resize(ret_size);
786 	{
787 		PoolVector<int>::Write w = ret.write();
788 		for (int i = 0; i < ret_size; i++) {
789 			w[i] = int(attribs_ptr[i]);
790 		}
791 	}
792 	return ret;
793 }
794 
_decode_accessor_as_floats(GLTFState & state,const GLTFAccessorIndex p_accessor,const bool p_for_vertex)795 PoolVector<float> EditorSceneImporterGLTF::_decode_accessor_as_floats(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex) {
796 
797 	const Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex);
798 	PoolVector<float> ret;
799 
800 	if (attribs.size() == 0)
801 		return ret;
802 
803 	const double *attribs_ptr = attribs.ptr();
804 	const int ret_size = attribs.size();
805 	ret.resize(ret_size);
806 	{
807 		PoolVector<float>::Write w = ret.write();
808 		for (int i = 0; i < ret_size; i++) {
809 			w[i] = float(attribs_ptr[i]);
810 		}
811 	}
812 	return ret;
813 }
814 
_decode_accessor_as_vec2(GLTFState & state,const GLTFAccessorIndex p_accessor,const bool p_for_vertex)815 PoolVector<Vector2> EditorSceneImporterGLTF::_decode_accessor_as_vec2(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex) {
816 
817 	const Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex);
818 	PoolVector<Vector2> ret;
819 
820 	if (attribs.size() == 0)
821 		return ret;
822 
823 	ERR_FAIL_COND_V(attribs.size() % 2 != 0, ret);
824 	const double *attribs_ptr = attribs.ptr();
825 	const int ret_size = attribs.size() / 2;
826 	ret.resize(ret_size);
827 	{
828 		PoolVector<Vector2>::Write w = ret.write();
829 		for (int i = 0; i < ret_size; i++) {
830 			w[i] = Vector2(attribs_ptr[i * 2 + 0], attribs_ptr[i * 2 + 1]);
831 		}
832 	}
833 	return ret;
834 }
835 
_decode_accessor_as_vec3(GLTFState & state,const GLTFAccessorIndex p_accessor,const bool p_for_vertex)836 PoolVector<Vector3> EditorSceneImporterGLTF::_decode_accessor_as_vec3(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex) {
837 
838 	const Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex);
839 	PoolVector<Vector3> ret;
840 
841 	if (attribs.size() == 0)
842 		return ret;
843 
844 	ERR_FAIL_COND_V(attribs.size() % 3 != 0, ret);
845 	const double *attribs_ptr = attribs.ptr();
846 	const int ret_size = attribs.size() / 3;
847 	ret.resize(ret_size);
848 	{
849 		PoolVector<Vector3>::Write w = ret.write();
850 		for (int i = 0; i < ret_size; i++) {
851 			w[i] = Vector3(attribs_ptr[i * 3 + 0], attribs_ptr[i * 3 + 1], attribs_ptr[i * 3 + 2]);
852 		}
853 	}
854 	return ret;
855 }
856 
_decode_accessor_as_color(GLTFState & state,const GLTFAccessorIndex p_accessor,const bool p_for_vertex)857 PoolVector<Color> EditorSceneImporterGLTF::_decode_accessor_as_color(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex) {
858 
859 	const Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex);
860 	PoolVector<Color> ret;
861 
862 	if (attribs.size() == 0)
863 		return ret;
864 
865 	const int type = state.accessors[p_accessor].type;
866 	ERR_FAIL_COND_V(!(type == TYPE_VEC3 || type == TYPE_VEC4), ret);
867 	int vec_len = 3;
868 	if (type == TYPE_VEC4) {
869 		vec_len = 4;
870 	}
871 
872 	ERR_FAIL_COND_V(attribs.size() % vec_len != 0, ret);
873 	const double *attribs_ptr = attribs.ptr();
874 	const int ret_size = attribs.size() / vec_len;
875 	ret.resize(ret_size);
876 	{
877 		PoolVector<Color>::Write w = ret.write();
878 		for (int i = 0; i < ret_size; i++) {
879 			w[i] = Color(attribs_ptr[i * vec_len + 0], attribs_ptr[i * vec_len + 1], attribs_ptr[i * vec_len + 2], vec_len == 4 ? attribs_ptr[i * 4 + 3] : 1.0);
880 		}
881 	}
882 	return ret;
883 }
884 
_decode_accessor_as_quat(GLTFState & state,const GLTFAccessorIndex p_accessor,const bool p_for_vertex)885 Vector<Quat> EditorSceneImporterGLTF::_decode_accessor_as_quat(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex) {
886 
887 	const Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex);
888 	Vector<Quat> ret;
889 
890 	if (attribs.size() == 0)
891 		return ret;
892 
893 	ERR_FAIL_COND_V(attribs.size() % 4 != 0, ret);
894 	const double *attribs_ptr = attribs.ptr();
895 	const int ret_size = attribs.size() / 4;
896 	ret.resize(ret_size);
897 	{
898 		for (int i = 0; i < ret_size; i++) {
899 			ret.write[i] = Quat(attribs_ptr[i * 4 + 0], attribs_ptr[i * 4 + 1], attribs_ptr[i * 4 + 2], attribs_ptr[i * 4 + 3]).normalized();
900 		}
901 	}
902 	return ret;
903 }
_decode_accessor_as_xform2d(GLTFState & state,const GLTFAccessorIndex p_accessor,const bool p_for_vertex)904 Vector<Transform2D> EditorSceneImporterGLTF::_decode_accessor_as_xform2d(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex) {
905 
906 	const Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex);
907 	Vector<Transform2D> ret;
908 
909 	if (attribs.size() == 0)
910 		return ret;
911 
912 	ERR_FAIL_COND_V(attribs.size() % 4 != 0, ret);
913 	ret.resize(attribs.size() / 4);
914 	for (int i = 0; i < ret.size(); i++) {
915 		ret.write[i][0] = Vector2(attribs[i * 4 + 0], attribs[i * 4 + 1]);
916 		ret.write[i][1] = Vector2(attribs[i * 4 + 2], attribs[i * 4 + 3]);
917 	}
918 	return ret;
919 }
920 
_decode_accessor_as_basis(GLTFState & state,const GLTFAccessorIndex p_accessor,bool p_for_vertex)921 Vector<Basis> EditorSceneImporterGLTF::_decode_accessor_as_basis(GLTFState &state, const GLTFAccessorIndex p_accessor, bool p_for_vertex) {
922 
923 	const Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex);
924 	Vector<Basis> ret;
925 
926 	if (attribs.size() == 0)
927 		return ret;
928 
929 	ERR_FAIL_COND_V(attribs.size() % 9 != 0, ret);
930 	ret.resize(attribs.size() / 9);
931 	for (int i = 0; i < ret.size(); i++) {
932 		ret.write[i].set_axis(0, Vector3(attribs[i * 9 + 0], attribs[i * 9 + 1], attribs[i * 9 + 2]));
933 		ret.write[i].set_axis(1, Vector3(attribs[i * 9 + 3], attribs[i * 9 + 4], attribs[i * 9 + 5]));
934 		ret.write[i].set_axis(2, Vector3(attribs[i * 9 + 6], attribs[i * 9 + 7], attribs[i * 9 + 8]));
935 	}
936 	return ret;
937 }
938 
_decode_accessor_as_xform(GLTFState & state,const GLTFAccessorIndex p_accessor,const bool p_for_vertex)939 Vector<Transform> EditorSceneImporterGLTF::_decode_accessor_as_xform(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex) {
940 
941 	const Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex);
942 	Vector<Transform> ret;
943 
944 	if (attribs.size() == 0)
945 		return ret;
946 
947 	ERR_FAIL_COND_V(attribs.size() % 16 != 0, ret);
948 	ret.resize(attribs.size() / 16);
949 	for (int i = 0; i < ret.size(); i++) {
950 		ret.write[i].basis.set_axis(0, Vector3(attribs[i * 16 + 0], attribs[i * 16 + 1], attribs[i * 16 + 2]));
951 		ret.write[i].basis.set_axis(1, Vector3(attribs[i * 16 + 4], attribs[i * 16 + 5], attribs[i * 16 + 6]));
952 		ret.write[i].basis.set_axis(2, Vector3(attribs[i * 16 + 8], attribs[i * 16 + 9], attribs[i * 16 + 10]));
953 		ret.write[i].set_origin(Vector3(attribs[i * 16 + 12], attribs[i * 16 + 13], attribs[i * 16 + 14]));
954 	}
955 	return ret;
956 }
957 
_parse_meshes(GLTFState & state)958 Error EditorSceneImporterGLTF::_parse_meshes(GLTFState &state) {
959 
960 	if (!state.json.has("meshes"))
961 		return OK;
962 
963 	bool compress_vert_data = state.import_flags & IMPORT_USE_COMPRESSION;
964 	uint32_t mesh_flags = compress_vert_data ? Mesh::ARRAY_COMPRESS_DEFAULT : 0;
965 
966 	Array meshes = state.json["meshes"];
967 	for (GLTFMeshIndex i = 0; i < meshes.size(); i++) {
968 
969 		print_verbose("glTF: Parsing mesh: " + itos(i));
970 		Dictionary d = meshes[i];
971 
972 		GLTFMesh mesh;
973 		mesh.mesh.instance();
974 
975 		ERR_FAIL_COND_V(!d.has("primitives"), ERR_PARSE_ERROR);
976 
977 		Array primitives = d["primitives"];
978 		const Dictionary &extras = d.has("extras") ? (Dictionary)d["extras"] : Dictionary();
979 
980 		for (int j = 0; j < primitives.size(); j++) {
981 
982 			Dictionary p = primitives[j];
983 
984 			Array array;
985 			array.resize(Mesh::ARRAY_MAX);
986 
987 			ERR_FAIL_COND_V(!p.has("attributes"), ERR_PARSE_ERROR);
988 
989 			Dictionary a = p["attributes"];
990 
991 			Mesh::PrimitiveType primitive = Mesh::PRIMITIVE_TRIANGLES;
992 			if (p.has("mode")) {
993 				const int mode = p["mode"];
994 				ERR_FAIL_INDEX_V(mode, 7, ERR_FILE_CORRUPT);
995 				static const Mesh::PrimitiveType primitives2[7] = {
996 					Mesh::PRIMITIVE_POINTS,
997 					Mesh::PRIMITIVE_LINES,
998 					Mesh::PRIMITIVE_LINE_LOOP,
999 					Mesh::PRIMITIVE_LINE_STRIP,
1000 					Mesh::PRIMITIVE_TRIANGLES,
1001 					Mesh::PRIMITIVE_TRIANGLE_STRIP,
1002 					Mesh::PRIMITIVE_TRIANGLE_FAN,
1003 				};
1004 
1005 				primitive = primitives2[mode];
1006 			}
1007 
1008 			ERR_FAIL_COND_V(!a.has("POSITION"), ERR_PARSE_ERROR);
1009 			if (a.has("POSITION")) {
1010 				array[Mesh::ARRAY_VERTEX] = _decode_accessor_as_vec3(state, a["POSITION"], true);
1011 			}
1012 			if (a.has("NORMAL")) {
1013 				array[Mesh::ARRAY_NORMAL] = _decode_accessor_as_vec3(state, a["NORMAL"], true);
1014 			}
1015 			if (a.has("TANGENT")) {
1016 				array[Mesh::ARRAY_TANGENT] = _decode_accessor_as_floats(state, a["TANGENT"], true);
1017 			}
1018 			if (a.has("TEXCOORD_0")) {
1019 				array[Mesh::ARRAY_TEX_UV] = _decode_accessor_as_vec2(state, a["TEXCOORD_0"], true);
1020 			}
1021 			if (a.has("TEXCOORD_1")) {
1022 				array[Mesh::ARRAY_TEX_UV2] = _decode_accessor_as_vec2(state, a["TEXCOORD_1"], true);
1023 			}
1024 			if (a.has("COLOR_0")) {
1025 				array[Mesh::ARRAY_COLOR] = _decode_accessor_as_color(state, a["COLOR_0"], true);
1026 			}
1027 			if (a.has("JOINTS_0")) {
1028 				array[Mesh::ARRAY_BONES] = _decode_accessor_as_ints(state, a["JOINTS_0"], true);
1029 			}
1030 			if (a.has("WEIGHTS_0")) {
1031 				PoolVector<float> weights = _decode_accessor_as_floats(state, a["WEIGHTS_0"], true);
1032 				{ //gltf does not seem to normalize the weights for some reason..
1033 					int wc = weights.size();
1034 					PoolVector<float>::Write w = weights.write();
1035 
1036 					for (int k = 0; k < wc; k += 4) {
1037 						float total = 0.0;
1038 						total += w[k + 0];
1039 						total += w[k + 1];
1040 						total += w[k + 2];
1041 						total += w[k + 3];
1042 						if (total > 0.0) {
1043 							w[k + 0] /= total;
1044 							w[k + 1] /= total;
1045 							w[k + 2] /= total;
1046 							w[k + 3] /= total;
1047 						}
1048 					}
1049 				}
1050 				array[Mesh::ARRAY_WEIGHTS] = weights;
1051 			}
1052 
1053 			if (p.has("indices")) {
1054 				PoolVector<int> indices = _decode_accessor_as_ints(state, p["indices"], false);
1055 
1056 				if (primitive == Mesh::PRIMITIVE_TRIANGLES) {
1057 					//swap around indices, convert ccw to cw for front face
1058 
1059 					const int is = indices.size();
1060 					const PoolVector<int>::Write w = indices.write();
1061 					for (int k = 0; k < is; k += 3) {
1062 						SWAP(w[k + 1], w[k + 2]);
1063 					}
1064 				}
1065 				array[Mesh::ARRAY_INDEX] = indices;
1066 
1067 			} else if (primitive == Mesh::PRIMITIVE_TRIANGLES) {
1068 				//generate indices because they need to be swapped for CW/CCW
1069 				const PoolVector<Vector3> &vertices = array[Mesh::ARRAY_VERTEX];
1070 				ERR_FAIL_COND_V(vertices.size() == 0, ERR_PARSE_ERROR);
1071 				PoolVector<int> indices;
1072 				const int vs = vertices.size();
1073 				indices.resize(vs);
1074 				{
1075 					const PoolVector<int>::Write w = indices.write();
1076 					for (int k = 0; k < vs; k += 3) {
1077 						w[k] = k;
1078 						w[k + 1] = k + 2;
1079 						w[k + 2] = k + 1;
1080 					}
1081 				}
1082 				array[Mesh::ARRAY_INDEX] = indices;
1083 			}
1084 
1085 			bool generate_tangents = (primitive == Mesh::PRIMITIVE_TRIANGLES && !a.has("TANGENT") && a.has("TEXCOORD_0") && a.has("NORMAL"));
1086 
1087 			if (generate_tangents) {
1088 				//must generate mikktspace tangents.. ergh..
1089 				Ref<SurfaceTool> st;
1090 				st.instance();
1091 				st->create_from_triangle_arrays(array);
1092 				st->generate_tangents();
1093 				array = st->commit_to_arrays();
1094 			}
1095 
1096 			Array morphs;
1097 			//blend shapes
1098 			if (p.has("targets")) {
1099 				print_verbose("glTF: Mesh has targets");
1100 				const Array &targets = p["targets"];
1101 
1102 				//ideally BLEND_SHAPE_MODE_RELATIVE since gltf2 stores in displacement
1103 				//but it could require a larger refactor?
1104 				mesh.mesh->set_blend_shape_mode(Mesh::BLEND_SHAPE_MODE_NORMALIZED);
1105 
1106 				if (j == 0) {
1107 					const Array &target_names = extras.has("targetNames") ? (Array)extras["targetNames"] : Array();
1108 					for (int k = 0; k < targets.size(); k++) {
1109 						const String name = k < target_names.size() ? (String)target_names[k] : String("morph_") + itos(k);
1110 						mesh.mesh->add_blend_shape(name);
1111 					}
1112 				}
1113 
1114 				for (int k = 0; k < targets.size(); k++) {
1115 
1116 					const Dictionary &t = targets[k];
1117 
1118 					Array array_copy;
1119 					array_copy.resize(Mesh::ARRAY_MAX);
1120 
1121 					for (int l = 0; l < Mesh::ARRAY_MAX; l++) {
1122 						array_copy[l] = array[l];
1123 					}
1124 
1125 					array_copy[Mesh::ARRAY_INDEX] = Variant();
1126 
1127 					if (t.has("POSITION")) {
1128 						PoolVector<Vector3> varr = _decode_accessor_as_vec3(state, t["POSITION"], true);
1129 						const PoolVector<Vector3> src_varr = array[Mesh::ARRAY_VERTEX];
1130 						const int size = src_varr.size();
1131 						ERR_FAIL_COND_V(size == 0, ERR_PARSE_ERROR);
1132 						{
1133 
1134 							const int max_idx = varr.size();
1135 							varr.resize(size);
1136 
1137 							const PoolVector<Vector3>::Write w_varr = varr.write();
1138 							const PoolVector<Vector3>::Read r_varr = varr.read();
1139 							const PoolVector<Vector3>::Read r_src_varr = src_varr.read();
1140 							for (int l = 0; l < size; l++) {
1141 								if (l < max_idx) {
1142 									w_varr[l] = r_varr[l] + r_src_varr[l];
1143 								} else {
1144 									w_varr[l] = r_src_varr[l];
1145 								}
1146 							}
1147 						}
1148 						array_copy[Mesh::ARRAY_VERTEX] = varr;
1149 					}
1150 					if (t.has("NORMAL")) {
1151 						PoolVector<Vector3> narr = _decode_accessor_as_vec3(state, t["NORMAL"], true);
1152 						const PoolVector<Vector3> src_narr = array[Mesh::ARRAY_NORMAL];
1153 						int size = src_narr.size();
1154 						ERR_FAIL_COND_V(size == 0, ERR_PARSE_ERROR);
1155 						{
1156 							int max_idx = narr.size();
1157 							narr.resize(size);
1158 
1159 							const PoolVector<Vector3>::Write w_narr = narr.write();
1160 							const PoolVector<Vector3>::Read r_narr = narr.read();
1161 							const PoolVector<Vector3>::Read r_src_narr = src_narr.read();
1162 							for (int l = 0; l < size; l++) {
1163 								if (l < max_idx) {
1164 									w_narr[l] = r_narr[l] + r_src_narr[l];
1165 								} else {
1166 									w_narr[l] = r_src_narr[l];
1167 								}
1168 							}
1169 						}
1170 						array_copy[Mesh::ARRAY_NORMAL] = narr;
1171 					}
1172 					if (t.has("TANGENT")) {
1173 						const PoolVector<Vector3> tangents_v3 = _decode_accessor_as_vec3(state, t["TANGENT"], true);
1174 						const PoolVector<float> src_tangents = array[Mesh::ARRAY_TANGENT];
1175 						ERR_FAIL_COND_V(src_tangents.size() == 0, ERR_PARSE_ERROR);
1176 
1177 						PoolVector<float> tangents_v4;
1178 
1179 						{
1180 
1181 							int max_idx = tangents_v3.size();
1182 
1183 							int size4 = src_tangents.size();
1184 							tangents_v4.resize(size4);
1185 							const PoolVector<float>::Write w4 = tangents_v4.write();
1186 
1187 							const PoolVector<Vector3>::Read r3 = tangents_v3.read();
1188 							const PoolVector<float>::Read r4 = src_tangents.read();
1189 
1190 							for (int l = 0; l < size4 / 4; l++) {
1191 
1192 								if (l < max_idx) {
1193 									w4[l * 4 + 0] = r3[l].x + r4[l * 4 + 0];
1194 									w4[l * 4 + 1] = r3[l].y + r4[l * 4 + 1];
1195 									w4[l * 4 + 2] = r3[l].z + r4[l * 4 + 2];
1196 								} else {
1197 									w4[l * 4 + 0] = r4[l * 4 + 0];
1198 									w4[l * 4 + 1] = r4[l * 4 + 1];
1199 									w4[l * 4 + 2] = r4[l * 4 + 2];
1200 								}
1201 								w4[l * 4 + 3] = r4[l * 4 + 3]; //copy flip value
1202 							}
1203 						}
1204 
1205 						array_copy[Mesh::ARRAY_TANGENT] = tangents_v4;
1206 					}
1207 
1208 					if (generate_tangents) {
1209 						Ref<SurfaceTool> st;
1210 						st.instance();
1211 						st->create_from_triangle_arrays(array_copy);
1212 						st->deindex();
1213 						st->generate_tangents();
1214 						array_copy = st->commit_to_arrays();
1215 					}
1216 
1217 					morphs.push_back(array_copy);
1218 				}
1219 			}
1220 
1221 			//just add it
1222 			mesh.mesh->add_surface_from_arrays(primitive, array, morphs, mesh_flags);
1223 
1224 			if (p.has("material")) {
1225 				const int material = p["material"];
1226 				ERR_FAIL_INDEX_V(material, state.materials.size(), ERR_FILE_CORRUPT);
1227 				const Ref<Material> &mat = state.materials[material];
1228 
1229 				mesh.mesh->surface_set_material(mesh.mesh->get_surface_count() - 1, mat);
1230 			}
1231 		}
1232 
1233 		mesh.blend_weights.resize(mesh.mesh->get_blend_shape_count());
1234 		for (int32_t weight_i = 0; weight_i < mesh.blend_weights.size(); weight_i++) {
1235 			mesh.blend_weights.write[weight_i] = 0.0f;
1236 		}
1237 
1238 		if (d.has("weights")) {
1239 			const Array &weights = d["weights"];
1240 			ERR_FAIL_COND_V(mesh.blend_weights.size() != weights.size(), ERR_PARSE_ERROR);
1241 			for (int j = 0; j < weights.size(); j++) {
1242 				mesh.blend_weights.write[j] = weights[j];
1243 			}
1244 		}
1245 
1246 		state.meshes.push_back(mesh);
1247 	}
1248 
1249 	print_verbose("glTF: Total meshes: " + itos(state.meshes.size()));
1250 
1251 	return OK;
1252 }
1253 
_parse_images(GLTFState & state,const String & p_base_path)1254 Error EditorSceneImporterGLTF::_parse_images(GLTFState &state, const String &p_base_path) {
1255 
1256 	if (!state.json.has("images"))
1257 		return OK;
1258 
1259 	const Array &images = state.json["images"];
1260 	for (int i = 0; i < images.size(); i++) {
1261 
1262 		const Dictionary &d = images[i];
1263 
1264 		String mimetype;
1265 		if (d.has("mimeType")) {
1266 			mimetype = d["mimeType"];
1267 		}
1268 
1269 		Vector<uint8_t> data;
1270 		const uint8_t *data_ptr = NULL;
1271 		int data_size = 0;
1272 
1273 		if (d.has("uri")) {
1274 			String uri = d["uri"];
1275 
1276 			if (uri.findn("data:application/octet-stream;base64") == 0 ||
1277 					uri.findn("data:" + mimetype + ";base64") == 0) {
1278 				//embedded data
1279 				data = _parse_base64_uri(uri);
1280 				data_ptr = data.ptr();
1281 				data_size = data.size();
1282 			} else {
1283 
1284 				uri = p_base_path.plus_file(uri).replace("\\", "/"); //fix for windows
1285 				Ref<Texture> texture = ResourceLoader::load(uri);
1286 				state.images.push_back(texture);
1287 				continue;
1288 			}
1289 		}
1290 
1291 		if (d.has("bufferView")) {
1292 			const GLTFBufferViewIndex bvi = d["bufferView"];
1293 
1294 			ERR_FAIL_INDEX_V(bvi, state.buffer_views.size(), ERR_PARAMETER_RANGE_ERROR);
1295 
1296 			const GLTFBufferView &bv = state.buffer_views[bvi];
1297 
1298 			const GLTFBufferIndex bi = bv.buffer;
1299 			ERR_FAIL_INDEX_V(bi, state.buffers.size(), ERR_PARAMETER_RANGE_ERROR);
1300 
1301 			ERR_FAIL_COND_V(bv.byte_offset + bv.byte_length > state.buffers[bi].size(), ERR_FILE_CORRUPT);
1302 
1303 			data_ptr = &state.buffers[bi][bv.byte_offset];
1304 			data_size = bv.byte_length;
1305 		}
1306 
1307 		ERR_FAIL_COND_V(mimetype == "", ERR_FILE_CORRUPT);
1308 
1309 		if (mimetype.findn("png") != -1) {
1310 			//is a png
1311 			ERR_FAIL_COND_V(Image::_png_mem_loader_func == NULL, ERR_UNAVAILABLE);
1312 
1313 			const Ref<Image> img = Image::_png_mem_loader_func(data_ptr, data_size);
1314 
1315 			ERR_FAIL_COND_V(img.is_null(), ERR_FILE_CORRUPT);
1316 
1317 			Ref<ImageTexture> t;
1318 			t.instance();
1319 			t->create_from_image(img);
1320 
1321 			state.images.push_back(t);
1322 			continue;
1323 		}
1324 
1325 		if (mimetype.findn("jpeg") != -1) {
1326 			//is a jpg
1327 			ERR_FAIL_COND_V(Image::_jpg_mem_loader_func == NULL, ERR_UNAVAILABLE);
1328 
1329 			const Ref<Image> img = Image::_jpg_mem_loader_func(data_ptr, data_size);
1330 
1331 			ERR_FAIL_COND_V(img.is_null(), ERR_FILE_CORRUPT);
1332 
1333 			Ref<ImageTexture> t;
1334 			t.instance();
1335 			t->create_from_image(img);
1336 
1337 			state.images.push_back(t);
1338 
1339 			continue;
1340 		}
1341 
1342 		ERR_FAIL_V(ERR_FILE_CORRUPT);
1343 	}
1344 
1345 	print_verbose("Total images: " + itos(state.images.size()));
1346 
1347 	return OK;
1348 }
1349 
_parse_textures(GLTFState & state)1350 Error EditorSceneImporterGLTF::_parse_textures(GLTFState &state) {
1351 
1352 	if (!state.json.has("textures"))
1353 		return OK;
1354 
1355 	const Array &textures = state.json["textures"];
1356 	for (GLTFTextureIndex i = 0; i < textures.size(); i++) {
1357 
1358 		const Dictionary &d = textures[i];
1359 
1360 		ERR_FAIL_COND_V(!d.has("source"), ERR_PARSE_ERROR);
1361 
1362 		GLTFTexture t;
1363 		t.src_image = d["source"];
1364 		state.textures.push_back(t);
1365 	}
1366 
1367 	return OK;
1368 }
1369 
_get_texture(GLTFState & state,const GLTFTextureIndex p_texture)1370 Ref<Texture> EditorSceneImporterGLTF::_get_texture(GLTFState &state, const GLTFTextureIndex p_texture) {
1371 	ERR_FAIL_INDEX_V(p_texture, state.textures.size(), Ref<Texture>());
1372 	const GLTFImageIndex image = state.textures[p_texture].src_image;
1373 
1374 	ERR_FAIL_INDEX_V(image, state.images.size(), Ref<Texture>());
1375 
1376 	return state.images[image];
1377 }
1378 
_parse_materials(GLTFState & state)1379 Error EditorSceneImporterGLTF::_parse_materials(GLTFState &state) {
1380 
1381 	if (!state.json.has("materials"))
1382 		return OK;
1383 
1384 	const Array &materials = state.json["materials"];
1385 	for (GLTFMaterialIndex i = 0; i < materials.size(); i++) {
1386 
1387 		const Dictionary &d = materials[i];
1388 
1389 		Ref<SpatialMaterial> material;
1390 		material.instance();
1391 		if (d.has("name")) {
1392 			material->set_name(d["name"]);
1393 		}
1394 
1395 		if (d.has("pbrMetallicRoughness")) {
1396 
1397 			const Dictionary &mr = d["pbrMetallicRoughness"];
1398 			if (mr.has("baseColorFactor")) {
1399 				const Array &arr = mr["baseColorFactor"];
1400 				ERR_FAIL_COND_V(arr.size() != 4, ERR_PARSE_ERROR);
1401 				const Color c = Color(arr[0], arr[1], arr[2], arr[3]).to_srgb();
1402 
1403 				material->set_albedo(c);
1404 			}
1405 
1406 			if (mr.has("baseColorTexture")) {
1407 				const Dictionary &bct = mr["baseColorTexture"];
1408 				if (bct.has("index")) {
1409 					material->set_texture(SpatialMaterial::TEXTURE_ALBEDO, _get_texture(state, bct["index"]));
1410 				}
1411 				if (!mr.has("baseColorFactor")) {
1412 					material->set_albedo(Color(1, 1, 1));
1413 				}
1414 			}
1415 
1416 			if (mr.has("metallicFactor")) {
1417 				material->set_metallic(mr["metallicFactor"]);
1418 			} else {
1419 				material->set_metallic(1.0);
1420 			}
1421 
1422 			if (mr.has("roughnessFactor")) {
1423 				material->set_roughness(mr["roughnessFactor"]);
1424 			} else {
1425 				material->set_roughness(1.0);
1426 			}
1427 
1428 			if (mr.has("metallicRoughnessTexture")) {
1429 				const Dictionary &bct = mr["metallicRoughnessTexture"];
1430 				if (bct.has("index")) {
1431 					const Ref<Texture> t = _get_texture(state, bct["index"]);
1432 					material->set_texture(SpatialMaterial::TEXTURE_METALLIC, t);
1433 					material->set_metallic_texture_channel(SpatialMaterial::TEXTURE_CHANNEL_BLUE);
1434 					material->set_texture(SpatialMaterial::TEXTURE_ROUGHNESS, t);
1435 					material->set_roughness_texture_channel(SpatialMaterial::TEXTURE_CHANNEL_GREEN);
1436 					if (!mr.has("metallicFactor")) {
1437 						material->set_metallic(1);
1438 					}
1439 					if (!mr.has("roughnessFactor")) {
1440 						material->set_roughness(1);
1441 					}
1442 				}
1443 			}
1444 		}
1445 
1446 		if (d.has("normalTexture")) {
1447 			const Dictionary &bct = d["normalTexture"];
1448 			if (bct.has("index")) {
1449 				material->set_texture(SpatialMaterial::TEXTURE_NORMAL, _get_texture(state, bct["index"]));
1450 				material->set_feature(SpatialMaterial::FEATURE_NORMAL_MAPPING, true);
1451 			}
1452 			if (bct.has("scale")) {
1453 				material->set_normal_scale(bct["scale"]);
1454 			}
1455 		}
1456 		if (d.has("occlusionTexture")) {
1457 			const Dictionary &bct = d["occlusionTexture"];
1458 			if (bct.has("index")) {
1459 				material->set_texture(SpatialMaterial::TEXTURE_AMBIENT_OCCLUSION, _get_texture(state, bct["index"]));
1460 				material->set_ao_texture_channel(SpatialMaterial::TEXTURE_CHANNEL_RED);
1461 				material->set_feature(SpatialMaterial::FEATURE_AMBIENT_OCCLUSION, true);
1462 			}
1463 		}
1464 
1465 		if (d.has("emissiveFactor")) {
1466 			const Array &arr = d["emissiveFactor"];
1467 			ERR_FAIL_COND_V(arr.size() != 3, ERR_PARSE_ERROR);
1468 			const Color c = Color(arr[0], arr[1], arr[2]).to_srgb();
1469 			material->set_feature(SpatialMaterial::FEATURE_EMISSION, true);
1470 
1471 			material->set_emission(c);
1472 		}
1473 
1474 		if (d.has("emissiveTexture")) {
1475 			const Dictionary &bct = d["emissiveTexture"];
1476 			if (bct.has("index")) {
1477 				material->set_texture(SpatialMaterial::TEXTURE_EMISSION, _get_texture(state, bct["index"]));
1478 				material->set_feature(SpatialMaterial::FEATURE_EMISSION, true);
1479 				material->set_emission(Color(0, 0, 0));
1480 			}
1481 		}
1482 
1483 		if (d.has("doubleSided")) {
1484 			const bool ds = d["doubleSided"];
1485 			if (ds) {
1486 				material->set_cull_mode(SpatialMaterial::CULL_DISABLED);
1487 			}
1488 		}
1489 
1490 		if (d.has("alphaMode")) {
1491 			const String &am = d["alphaMode"];
1492 			if (am == "BLEND") {
1493 				material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
1494 				material->set_depth_draw_mode(SpatialMaterial::DEPTH_DRAW_ALPHA_OPAQUE_PREPASS);
1495 			} else if (am == "MASK") {
1496 				material->set_flag(SpatialMaterial::FLAG_USE_ALPHA_SCISSOR, true);
1497 				if (d.has("alphaCutoff")) {
1498 					material->set_alpha_scissor_threshold(d["alphaCutoff"]);
1499 				} else {
1500 					material->set_alpha_scissor_threshold(0.5f);
1501 				}
1502 			}
1503 		}
1504 
1505 		state.materials.push_back(material);
1506 	}
1507 
1508 	print_verbose("Total materials: " + itos(state.materials.size()));
1509 
1510 	return OK;
1511 }
1512 
_find_highest_node(GLTFState & state,const Vector<GLTFNodeIndex> & subset)1513 EditorSceneImporterGLTF::GLTFNodeIndex EditorSceneImporterGLTF::_find_highest_node(GLTFState &state, const Vector<GLTFNodeIndex> &subset) {
1514 	int highest = -1;
1515 	GLTFNodeIndex best_node = -1;
1516 
1517 	for (int i = 0; i < subset.size(); ++i) {
1518 		const GLTFNodeIndex node_i = subset[i];
1519 		const GLTFNode *node = state.nodes[node_i];
1520 
1521 		if (highest == -1 || node->height < highest) {
1522 			highest = node->height;
1523 			best_node = node_i;
1524 		}
1525 	}
1526 
1527 	return best_node;
1528 }
1529 
_capture_nodes_in_skin(GLTFState & state,GLTFSkin & skin,const GLTFNodeIndex node_index)1530 bool EditorSceneImporterGLTF::_capture_nodes_in_skin(GLTFState &state, GLTFSkin &skin, const GLTFNodeIndex node_index) {
1531 
1532 	bool found_joint = false;
1533 
1534 	for (int i = 0; i < state.nodes[node_index]->children.size(); ++i) {
1535 		found_joint |= _capture_nodes_in_skin(state, skin, state.nodes[node_index]->children[i]);
1536 	}
1537 
1538 	if (found_joint) {
1539 		// Mark it if we happen to find another skins joint...
1540 		if (state.nodes[node_index]->joint && skin.joints.find(node_index) < 0) {
1541 			skin.joints.push_back(node_index);
1542 		} else if (skin.non_joints.find(node_index) < 0) {
1543 			skin.non_joints.push_back(node_index);
1544 		}
1545 	}
1546 
1547 	if (skin.joints.find(node_index) > 0) {
1548 		return true;
1549 	}
1550 
1551 	return false;
1552 }
1553 
_capture_nodes_for_multirooted_skin(GLTFState & state,GLTFSkin & skin)1554 void EditorSceneImporterGLTF::_capture_nodes_for_multirooted_skin(GLTFState &state, GLTFSkin &skin) {
1555 
1556 	DisjointSet<GLTFNodeIndex> disjoint_set;
1557 
1558 	for (int i = 0; i < skin.joints.size(); ++i) {
1559 		const GLTFNodeIndex node_index = skin.joints[i];
1560 		const GLTFNodeIndex parent = state.nodes[node_index]->parent;
1561 		disjoint_set.insert(node_index);
1562 
1563 		if (skin.joints.find(parent) >= 0) {
1564 			disjoint_set.create_union(parent, node_index);
1565 		}
1566 	}
1567 
1568 	Vector<GLTFNodeIndex> roots;
1569 	disjoint_set.get_representatives(roots);
1570 
1571 	if (roots.size() <= 1) {
1572 		return;
1573 	}
1574 
1575 	int maxHeight = -1;
1576 
1577 	// Determine the max height rooted tree
1578 	for (int i = 0; i < roots.size(); ++i) {
1579 		const GLTFNodeIndex root = roots[i];
1580 
1581 		if (maxHeight == -1 || state.nodes[root]->height < maxHeight) {
1582 			maxHeight = state.nodes[root]->height;
1583 		}
1584 	}
1585 
1586 	// Go up the tree till all of the multiple roots of the skin are at the same hierarchy level.
1587 	// This sucks, but 99% of all game engines (not just Godot) would have this same issue.
1588 	for (int i = 0; i < roots.size(); ++i) {
1589 
1590 		GLTFNodeIndex current_node = roots[i];
1591 		while (state.nodes[current_node]->height > maxHeight) {
1592 			GLTFNodeIndex parent = state.nodes[current_node]->parent;
1593 
1594 			if (state.nodes[parent]->joint && skin.joints.find(parent) < 0) {
1595 				skin.joints.push_back(parent);
1596 			} else if (skin.non_joints.find(parent) < 0) {
1597 				skin.non_joints.push_back(parent);
1598 			}
1599 
1600 			current_node = parent;
1601 		}
1602 
1603 		// replace the roots
1604 		roots.write[i] = current_node;
1605 	}
1606 
1607 	// Climb up the tree until they all have the same parent
1608 	bool all_same;
1609 
1610 	do {
1611 		all_same = true;
1612 		const GLTFNodeIndex first_parent = state.nodes[roots[0]]->parent;
1613 
1614 		for (int i = 1; i < roots.size(); ++i) {
1615 			all_same &= (first_parent == state.nodes[roots[i]]->parent);
1616 		}
1617 
1618 		if (!all_same) {
1619 			for (int i = 0; i < roots.size(); ++i) {
1620 				const GLTFNodeIndex current_node = roots[i];
1621 				const GLTFNodeIndex parent = state.nodes[current_node]->parent;
1622 
1623 				if (state.nodes[parent]->joint && skin.joints.find(parent) < 0) {
1624 					skin.joints.push_back(parent);
1625 				} else if (skin.non_joints.find(parent) < 0) {
1626 					skin.non_joints.push_back(parent);
1627 				}
1628 
1629 				roots.write[i] = parent;
1630 			}
1631 		}
1632 
1633 	} while (!all_same);
1634 }
1635 
_expand_skin(GLTFState & state,GLTFSkin & skin)1636 Error EditorSceneImporterGLTF::_expand_skin(GLTFState &state, GLTFSkin &skin) {
1637 
1638 	_capture_nodes_for_multirooted_skin(state, skin);
1639 
1640 	// Grab all nodes that lay in between skin joints/nodes
1641 	DisjointSet<GLTFNodeIndex> disjoint_set;
1642 
1643 	Vector<GLTFNodeIndex> all_skin_nodes;
1644 	all_skin_nodes.append_array(skin.joints);
1645 	all_skin_nodes.append_array(skin.non_joints);
1646 
1647 	for (int i = 0; i < all_skin_nodes.size(); ++i) {
1648 		const GLTFNodeIndex node_index = all_skin_nodes[i];
1649 		const GLTFNodeIndex parent = state.nodes[node_index]->parent;
1650 		disjoint_set.insert(node_index);
1651 
1652 		if (all_skin_nodes.find(parent) >= 0) {
1653 			disjoint_set.create_union(parent, node_index);
1654 		}
1655 	}
1656 
1657 	Vector<GLTFNodeIndex> out_owners;
1658 	disjoint_set.get_representatives(out_owners);
1659 
1660 	Vector<GLTFNodeIndex> out_roots;
1661 
1662 	for (int i = 0; i < out_owners.size(); ++i) {
1663 		Vector<GLTFNodeIndex> set;
1664 		disjoint_set.get_members(set, out_owners[i]);
1665 
1666 		const GLTFNodeIndex root = _find_highest_node(state, set);
1667 		ERR_FAIL_COND_V(root < 0, FAILED);
1668 		out_roots.push_back(root);
1669 	}
1670 
1671 	out_roots.sort();
1672 
1673 	for (int i = 0; i < out_roots.size(); ++i) {
1674 		_capture_nodes_in_skin(state, skin, out_roots[i]);
1675 	}
1676 
1677 	skin.roots = out_roots;
1678 
1679 	return OK;
1680 }
1681 
_verify_skin(GLTFState & state,GLTFSkin & skin)1682 Error EditorSceneImporterGLTF::_verify_skin(GLTFState &state, GLTFSkin &skin) {
1683 
1684 	// This may seem duplicated from expand_skins, but this is really a sanity check! (so it kinda is)
1685 	// In case additional interpolating logic is added to the skins, this will help ensure that you
1686 	// do not cause it to self implode into a fiery blaze
1687 
1688 	// We are going to re-calculate the root nodes and compare them to the ones saved in the skin,
1689 	// then ensure the multiple trees (if they exist) are on the same sublevel
1690 
1691 	// Grab all nodes that lay in between skin joints/nodes
1692 	DisjointSet<GLTFNodeIndex> disjoint_set;
1693 
1694 	Vector<GLTFNodeIndex> all_skin_nodes;
1695 	all_skin_nodes.append_array(skin.joints);
1696 	all_skin_nodes.append_array(skin.non_joints);
1697 
1698 	for (int i = 0; i < all_skin_nodes.size(); ++i) {
1699 		const GLTFNodeIndex node_index = all_skin_nodes[i];
1700 		const GLTFNodeIndex parent = state.nodes[node_index]->parent;
1701 		disjoint_set.insert(node_index);
1702 
1703 		if (all_skin_nodes.find(parent) >= 0) {
1704 			disjoint_set.create_union(parent, node_index);
1705 		}
1706 	}
1707 
1708 	Vector<GLTFNodeIndex> out_owners;
1709 	disjoint_set.get_representatives(out_owners);
1710 
1711 	Vector<GLTFNodeIndex> out_roots;
1712 
1713 	for (int i = 0; i < out_owners.size(); ++i) {
1714 		Vector<GLTFNodeIndex> set;
1715 		disjoint_set.get_members(set, out_owners[i]);
1716 
1717 		const GLTFNodeIndex root = _find_highest_node(state, set);
1718 		ERR_FAIL_COND_V(root < 0, FAILED);
1719 		out_roots.push_back(root);
1720 	}
1721 
1722 	out_roots.sort();
1723 
1724 	ERR_FAIL_COND_V(out_roots.size() == 0, FAILED);
1725 
1726 	// Make sure the roots are the exact same (they better be)
1727 	ERR_FAIL_COND_V(out_roots.size() != skin.roots.size(), FAILED);
1728 	for (int i = 0; i < out_roots.size(); ++i) {
1729 		ERR_FAIL_COND_V(out_roots[i] != skin.roots[i], FAILED);
1730 	}
1731 
1732 	// Single rooted skin? Perfectly ok!
1733 	if (out_roots.size() == 1) {
1734 		return OK;
1735 	}
1736 
1737 	// Make sure all parents of a multi-rooted skin are the SAME
1738 	const GLTFNodeIndex parent = state.nodes[out_roots[0]]->parent;
1739 	for (int i = 1; i < out_roots.size(); ++i) {
1740 		if (state.nodes[out_roots[i]]->parent != parent) {
1741 			return FAILED;
1742 		}
1743 	}
1744 
1745 	return OK;
1746 }
1747 
_parse_skins(GLTFState & state)1748 Error EditorSceneImporterGLTF::_parse_skins(GLTFState &state) {
1749 
1750 	if (!state.json.has("skins"))
1751 		return OK;
1752 
1753 	const Array &skins = state.json["skins"];
1754 
1755 	// Create the base skins, and mark nodes that are joints
1756 	for (int i = 0; i < skins.size(); i++) {
1757 
1758 		const Dictionary &d = skins[i];
1759 
1760 		GLTFSkin skin;
1761 
1762 		ERR_FAIL_COND_V(!d.has("joints"), ERR_PARSE_ERROR);
1763 
1764 		const Array &joints = d["joints"];
1765 
1766 		if (d.has("inverseBindMatrices")) {
1767 			skin.inverse_binds = _decode_accessor_as_xform(state, d["inverseBindMatrices"], false);
1768 			ERR_FAIL_COND_V(skin.inverse_binds.size() != joints.size(), ERR_PARSE_ERROR);
1769 		}
1770 
1771 		for (int j = 0; j < joints.size(); j++) {
1772 			const GLTFNodeIndex node = joints[j];
1773 			ERR_FAIL_INDEX_V(node, state.nodes.size(), ERR_PARSE_ERROR);
1774 
1775 			skin.joints.push_back(node);
1776 			skin.joints_original.push_back(node);
1777 
1778 			state.nodes[node]->joint = true;
1779 		}
1780 
1781 		if (d.has("name")) {
1782 			skin.name = d["name"];
1783 		}
1784 
1785 		if (d.has("skeleton")) {
1786 			skin.skin_root = d["skeleton"];
1787 		}
1788 
1789 		state.skins.push_back(skin);
1790 	}
1791 
1792 	for (GLTFSkinIndex i = 0; i < state.skins.size(); ++i) {
1793 		GLTFSkin &skin = state.skins.write[i];
1794 
1795 		// Expand the skin to capture all the extra non-joints that lie in between the actual joints,
1796 		// and expand the hierarchy to ensure multi-rooted trees lie on the same height level
1797 		ERR_FAIL_COND_V(_expand_skin(state, skin), ERR_PARSE_ERROR);
1798 		ERR_FAIL_COND_V(_verify_skin(state, skin), ERR_PARSE_ERROR);
1799 	}
1800 
1801 	print_verbose("glTF: Total skins: " + itos(state.skins.size()));
1802 
1803 	return OK;
1804 }
1805 
_determine_skeletons(GLTFState & state)1806 Error EditorSceneImporterGLTF::_determine_skeletons(GLTFState &state) {
1807 
1808 	// Using a disjoint set, we are going to potentially combine all skins that are actually branches
1809 	// of a main skeleton, or treat skins defining the same set of nodes as ONE skeleton.
1810 	// This is another unclear issue caused by the current glTF specification.
1811 
1812 	DisjointSet<GLTFNodeIndex> skeleton_sets;
1813 
1814 	for (GLTFSkinIndex skin_i = 0; skin_i < state.skins.size(); ++skin_i) {
1815 		const GLTFSkin &skin = state.skins[skin_i];
1816 
1817 		Vector<GLTFNodeIndex> all_skin_nodes;
1818 		all_skin_nodes.append_array(skin.joints);
1819 		all_skin_nodes.append_array(skin.non_joints);
1820 
1821 		for (int i = 0; i < all_skin_nodes.size(); ++i) {
1822 			const GLTFNodeIndex node_index = all_skin_nodes[i];
1823 			const GLTFNodeIndex parent = state.nodes[node_index]->parent;
1824 			skeleton_sets.insert(node_index);
1825 
1826 			if (all_skin_nodes.find(parent) >= 0) {
1827 				skeleton_sets.create_union(parent, node_index);
1828 			}
1829 		}
1830 
1831 		// We are going to connect the separate skin subtrees in each skin together
1832 		// so that the final roots are entire sets of valid skin trees
1833 		for (int i = 1; i < skin.roots.size(); ++i) {
1834 			skeleton_sets.create_union(skin.roots[0], skin.roots[i]);
1835 		}
1836 	}
1837 
1838 	{ // attempt to joint all touching subsets (siblings/parent are part of another skin)
1839 		Vector<GLTFNodeIndex> groups_representatives;
1840 		skeleton_sets.get_representatives(groups_representatives);
1841 
1842 		Vector<GLTFNodeIndex> highest_group_members;
1843 		Vector<Vector<GLTFNodeIndex> > groups;
1844 		for (int i = 0; i < groups_representatives.size(); ++i) {
1845 			Vector<GLTFNodeIndex> group;
1846 			skeleton_sets.get_members(group, groups_representatives[i]);
1847 			highest_group_members.push_back(_find_highest_node(state, group));
1848 			groups.push_back(group);
1849 		}
1850 
1851 		for (int i = 0; i < highest_group_members.size(); ++i) {
1852 			const GLTFNodeIndex node_i = highest_group_members[i];
1853 
1854 			// Attach any siblings together (this needs to be done n^2/2 times)
1855 			for (int j = i + 1; j < highest_group_members.size(); ++j) {
1856 				const GLTFNodeIndex node_j = highest_group_members[j];
1857 
1858 				// Even if they are siblings under the root! :)
1859 				if (state.nodes[node_i]->parent == state.nodes[node_j]->parent) {
1860 					skeleton_sets.create_union(node_i, node_j);
1861 				}
1862 			}
1863 
1864 			// Attach any parenting going on together (we need to do this n^2 times)
1865 			const GLTFNodeIndex node_i_parent = state.nodes[node_i]->parent;
1866 			if (node_i_parent >= 0) {
1867 				for (int j = 0; j < groups.size() && i != j; ++j) {
1868 					const Vector<GLTFNodeIndex> &group = groups[j];
1869 
1870 					if (group.find(node_i_parent) >= 0) {
1871 						const GLTFNodeIndex node_j = highest_group_members[j];
1872 						skeleton_sets.create_union(node_i, node_j);
1873 					}
1874 				}
1875 			}
1876 		}
1877 	}
1878 
1879 	// At this point, the skeleton groups should be finalized
1880 	Vector<GLTFNodeIndex> skeleton_owners;
1881 	skeleton_sets.get_representatives(skeleton_owners);
1882 
1883 	// Mark all the skins actual skeletons, after we have merged them
1884 	for (GLTFSkeletonIndex skel_i = 0; skel_i < skeleton_owners.size(); ++skel_i) {
1885 
1886 		const GLTFNodeIndex skeleton_owner = skeleton_owners[skel_i];
1887 		GLTFSkeleton skeleton;
1888 
1889 		Vector<GLTFNodeIndex> skeleton_nodes;
1890 		skeleton_sets.get_members(skeleton_nodes, skeleton_owner);
1891 
1892 		for (GLTFSkinIndex skin_i = 0; skin_i < state.skins.size(); ++skin_i) {
1893 			GLTFSkin &skin = state.skins.write[skin_i];
1894 
1895 			// If any of the the skeletons nodes exist in a skin, that skin now maps to the skeleton
1896 			for (int i = 0; i < skeleton_nodes.size(); ++i) {
1897 				GLTFNodeIndex skel_node_i = skeleton_nodes[i];
1898 				if (skin.joints.find(skel_node_i) >= 0 || skin.non_joints.find(skel_node_i) >= 0) {
1899 					skin.skeleton = skel_i;
1900 					continue;
1901 				}
1902 			}
1903 		}
1904 
1905 		Vector<GLTFNodeIndex> non_joints;
1906 		for (int i = 0; i < skeleton_nodes.size(); ++i) {
1907 			const GLTFNodeIndex node_i = skeleton_nodes[i];
1908 
1909 			if (state.nodes[node_i]->joint) {
1910 				skeleton.joints.push_back(node_i);
1911 			} else {
1912 				non_joints.push_back(node_i);
1913 			}
1914 		}
1915 
1916 		state.skeletons.push_back(skeleton);
1917 
1918 		_reparent_non_joint_skeleton_subtrees(state, state.skeletons.write[skel_i], non_joints);
1919 	}
1920 
1921 	for (GLTFSkeletonIndex skel_i = 0; skel_i < state.skeletons.size(); ++skel_i) {
1922 		GLTFSkeleton &skeleton = state.skeletons.write[skel_i];
1923 
1924 		for (int i = 0; i < skeleton.joints.size(); ++i) {
1925 			const GLTFNodeIndex node_i = skeleton.joints[i];
1926 			GLTFNode *node = state.nodes[node_i];
1927 
1928 			ERR_FAIL_COND_V(!node->joint, ERR_PARSE_ERROR);
1929 			ERR_FAIL_COND_V(node->skeleton >= 0, ERR_PARSE_ERROR);
1930 			node->skeleton = skel_i;
1931 		}
1932 
1933 		ERR_FAIL_COND_V(_determine_skeleton_roots(state, skel_i), ERR_PARSE_ERROR);
1934 	}
1935 
1936 	return OK;
1937 }
1938 
_reparent_non_joint_skeleton_subtrees(GLTFState & state,GLTFSkeleton & skeleton,const Vector<GLTFNodeIndex> & non_joints)1939 Error EditorSceneImporterGLTF::_reparent_non_joint_skeleton_subtrees(GLTFState &state, GLTFSkeleton &skeleton, const Vector<GLTFNodeIndex> &non_joints) {
1940 
1941 	DisjointSet<GLTFNodeIndex> subtree_set;
1942 
1943 	// Populate the disjoint set with ONLY non joints that are in the skeleton hierarchy (non_joints vector)
1944 	// This way we can find any joints that lie in between joints, as the current glTF specification
1945 	// mentions nothing about non-joints being in between joints of the same skin. Hopefully one day we
1946 	// can remove this code.
1947 
1948 	// skinD depicted here explains this issue:
1949 	// https://github.com/KhronosGroup/glTF-Asset-Generator/blob/master/Output/Positive/Animation_Skin
1950 
1951 	for (int i = 0; i < non_joints.size(); ++i) {
1952 		const GLTFNodeIndex node_i = non_joints[i];
1953 
1954 		subtree_set.insert(node_i);
1955 
1956 		const GLTFNodeIndex parent_i = state.nodes[node_i]->parent;
1957 		if (parent_i >= 0 && non_joints.find(parent_i) >= 0 && !state.nodes[parent_i]->joint) {
1958 			subtree_set.create_union(parent_i, node_i);
1959 		}
1960 	}
1961 
1962 	// Find all the non joint subtrees and re-parent them to a new "fake" joint
1963 
1964 	Vector<GLTFNodeIndex> non_joint_subtree_roots;
1965 	subtree_set.get_representatives(non_joint_subtree_roots);
1966 
1967 	for (int root_i = 0; root_i < non_joint_subtree_roots.size(); ++root_i) {
1968 		const GLTFNodeIndex subtree_root = non_joint_subtree_roots[root_i];
1969 
1970 		Vector<GLTFNodeIndex> subtree_nodes;
1971 		subtree_set.get_members(subtree_nodes, subtree_root);
1972 
1973 		for (int subtree_i = 0; subtree_i < subtree_nodes.size(); ++subtree_i) {
1974 			ERR_FAIL_COND_V(_reparent_to_fake_joint(state, skeleton, subtree_nodes[subtree_i]), FAILED);
1975 
1976 			// We modified the tree, recompute all the heights
1977 			_compute_node_heights(state);
1978 		}
1979 	}
1980 
1981 	return OK;
1982 }
1983 
_reparent_to_fake_joint(GLTFState & state,GLTFSkeleton & skeleton,const GLTFNodeIndex node_index)1984 Error EditorSceneImporterGLTF::_reparent_to_fake_joint(GLTFState &state, GLTFSkeleton &skeleton, const GLTFNodeIndex node_index) {
1985 	GLTFNode *node = state.nodes[node_index];
1986 
1987 	// Can we just "steal" this joint if it is just a spatial node?
1988 	if (node->skin < 0 && node->mesh < 0 && node->camera < 0) {
1989 		node->joint = true;
1990 		// Add the joint to the skeletons joints
1991 		skeleton.joints.push_back(node_index);
1992 		return OK;
1993 	}
1994 
1995 	GLTFNode *fake_joint = memnew(GLTFNode);
1996 	const GLTFNodeIndex fake_joint_index = state.nodes.size();
1997 	state.nodes.push_back(fake_joint);
1998 
1999 	// We better not be a joint, or we messed up in our logic
2000 	if (node->joint)
2001 		return FAILED;
2002 
2003 	fake_joint->translation = node->translation;
2004 	fake_joint->rotation = node->rotation;
2005 	fake_joint->scale = node->scale;
2006 	fake_joint->xform = node->xform;
2007 	fake_joint->joint = true;
2008 
2009 	// We can use the exact same name here, because the joint will be inside a skeleton and not the scene
2010 	fake_joint->name = node->name;
2011 
2012 	// Clear the nodes transforms, since it will be parented to the fake joint
2013 	node->translation = Vector3(0, 0, 0);
2014 	node->rotation = Quat();
2015 	node->scale = Vector3(1, 1, 1);
2016 	node->xform = Transform();
2017 
2018 	// Transfer the node children to the fake joint
2019 	for (int child_i = 0; child_i < node->children.size(); ++child_i) {
2020 		GLTFNode *child = state.nodes[node->children[child_i]];
2021 		child->parent = fake_joint_index;
2022 	}
2023 
2024 	fake_joint->children = node->children;
2025 	node->children.clear();
2026 
2027 	// add the fake joint to the parent and remove the original joint
2028 	if (node->parent >= 0) {
2029 		GLTFNode *parent = state.nodes[node->parent];
2030 		parent->children.erase(node_index);
2031 		parent->children.push_back(fake_joint_index);
2032 		fake_joint->parent = node->parent;
2033 	}
2034 
2035 	// Add the node to the fake joint
2036 	fake_joint->children.push_back(node_index);
2037 	node->parent = fake_joint_index;
2038 	node->fake_joint_parent = fake_joint_index;
2039 
2040 	// Add the fake joint to the skeletons joints
2041 	skeleton.joints.push_back(fake_joint_index);
2042 
2043 	// Replace skin_skeletons with fake joints if we must.
2044 	for (GLTFSkinIndex skin_i = 0; skin_i < state.skins.size(); ++skin_i) {
2045 		GLTFSkin &skin = state.skins.write[skin_i];
2046 		if (skin.skin_root == node_index) {
2047 			skin.skin_root = fake_joint_index;
2048 		}
2049 	}
2050 
2051 	return OK;
2052 }
2053 
_determine_skeleton_roots(GLTFState & state,const GLTFSkeletonIndex skel_i)2054 Error EditorSceneImporterGLTF::_determine_skeleton_roots(GLTFState &state, const GLTFSkeletonIndex skel_i) {
2055 
2056 	DisjointSet<GLTFNodeIndex> disjoint_set;
2057 
2058 	for (GLTFNodeIndex i = 0; i < state.nodes.size(); ++i) {
2059 		const GLTFNode *node = state.nodes[i];
2060 
2061 		if (node->skeleton != skel_i) {
2062 			continue;
2063 		}
2064 
2065 		disjoint_set.insert(i);
2066 
2067 		if (node->parent >= 0 && state.nodes[node->parent]->skeleton == skel_i) {
2068 			disjoint_set.create_union(node->parent, i);
2069 		}
2070 	}
2071 
2072 	GLTFSkeleton &skeleton = state.skeletons.write[skel_i];
2073 
2074 	Vector<GLTFNodeIndex> owners;
2075 	disjoint_set.get_representatives(owners);
2076 
2077 	Vector<GLTFNodeIndex> roots;
2078 
2079 	for (int i = 0; i < owners.size(); ++i) {
2080 		Vector<GLTFNodeIndex> set;
2081 		disjoint_set.get_members(set, owners[i]);
2082 		const GLTFNodeIndex root = _find_highest_node(state, set);
2083 		ERR_FAIL_COND_V(root < 0, FAILED);
2084 		roots.push_back(root);
2085 	}
2086 
2087 	roots.sort();
2088 
2089 	skeleton.roots = roots;
2090 
2091 	if (roots.size() == 0) {
2092 		return FAILED;
2093 	} else if (roots.size() == 1) {
2094 		return OK;
2095 	}
2096 
2097 	// Check that the subtrees have the same parent root
2098 	const GLTFNodeIndex parent = state.nodes[roots[0]]->parent;
2099 	for (int i = 1; i < roots.size(); ++i) {
2100 		if (state.nodes[roots[i]]->parent != parent) {
2101 			return FAILED;
2102 		}
2103 	}
2104 
2105 	return OK;
2106 }
2107 
_create_skeletons(GLTFState & state)2108 Error EditorSceneImporterGLTF::_create_skeletons(GLTFState &state) {
2109 	for (GLTFSkeletonIndex skel_i = 0; skel_i < state.skeletons.size(); ++skel_i) {
2110 
2111 		GLTFSkeleton &gltf_skeleton = state.skeletons.write[skel_i];
2112 
2113 		Skeleton *skeleton = memnew(Skeleton);
2114 		gltf_skeleton.godot_skeleton = skeleton;
2115 
2116 		// Make a unique name, no gltf node represents this skeleton
2117 		skeleton->set_name(_gen_unique_name(state, "Skeleton"));
2118 
2119 		List<GLTFNodeIndex> bones;
2120 
2121 		for (int i = 0; i < gltf_skeleton.roots.size(); ++i) {
2122 			bones.push_back(gltf_skeleton.roots[i]);
2123 		}
2124 
2125 		// Make the skeleton creation deterministic by going through the roots in
2126 		// a sorted order, and DEPTH FIRST
2127 		bones.sort();
2128 
2129 		while (!bones.empty()) {
2130 			const GLTFNodeIndex node_i = bones.front()->get();
2131 			bones.pop_front();
2132 
2133 			GLTFNode *node = state.nodes[node_i];
2134 			ERR_FAIL_COND_V(node->skeleton != skel_i, FAILED);
2135 
2136 			{ // Add all child nodes to the stack (deterministically)
2137 				Vector<GLTFNodeIndex> child_nodes;
2138 				for (int i = 0; i < node->children.size(); ++i) {
2139 					const GLTFNodeIndex child_i = node->children[i];
2140 					if (state.nodes[child_i]->skeleton == skel_i) {
2141 						child_nodes.push_back(child_i);
2142 					}
2143 				}
2144 
2145 				// Depth first insertion
2146 				child_nodes.sort();
2147 				for (int i = child_nodes.size() - 1; i >= 0; --i) {
2148 					bones.push_front(child_nodes[i]);
2149 				}
2150 			}
2151 
2152 			const int bone_index = skeleton->get_bone_count();
2153 
2154 			if (node->name.empty()) {
2155 				node->name = "bone";
2156 			}
2157 
2158 			node->name = _gen_unique_bone_name(state, skel_i, node->name);
2159 
2160 			skeleton->add_bone(node->name);
2161 			skeleton->set_bone_rest(bone_index, node->xform);
2162 
2163 			if (node->parent >= 0 && state.nodes[node->parent]->skeleton == skel_i) {
2164 				const int bone_parent = skeleton->find_bone(state.nodes[node->parent]->name);
2165 				ERR_FAIL_COND_V(bone_parent < 0, FAILED);
2166 				skeleton->set_bone_parent(bone_index, skeleton->find_bone(state.nodes[node->parent]->name));
2167 			}
2168 
2169 			state.scene_nodes.insert(node_i, skeleton);
2170 		}
2171 	}
2172 
2173 	ERR_FAIL_COND_V(_map_skin_joints_indices_to_skeleton_bone_indices(state), ERR_PARSE_ERROR);
2174 
2175 	return OK;
2176 }
2177 
_map_skin_joints_indices_to_skeleton_bone_indices(GLTFState & state)2178 Error EditorSceneImporterGLTF::_map_skin_joints_indices_to_skeleton_bone_indices(GLTFState &state) {
2179 	for (GLTFSkinIndex skin_i = 0; skin_i < state.skins.size(); ++skin_i) {
2180 		GLTFSkin &skin = state.skins.write[skin_i];
2181 
2182 		const GLTFSkeleton &skeleton = state.skeletons[skin.skeleton];
2183 
2184 		for (int joint_index = 0; joint_index < skin.joints_original.size(); ++joint_index) {
2185 			const GLTFNodeIndex node_i = skin.joints_original[joint_index];
2186 			const GLTFNode *node = state.nodes[node_i];
2187 
2188 			skin.joint_i_to_name.insert(joint_index, node->name);
2189 
2190 			const int bone_index = skeleton.godot_skeleton->find_bone(node->name);
2191 			ERR_FAIL_COND_V(bone_index < 0, FAILED);
2192 
2193 			skin.joint_i_to_bone_i.insert(joint_index, bone_index);
2194 		}
2195 	}
2196 
2197 	return OK;
2198 }
2199 
_create_skins(GLTFState & state)2200 Error EditorSceneImporterGLTF::_create_skins(GLTFState &state) {
2201 	for (GLTFSkinIndex skin_i = 0; skin_i < state.skins.size(); ++skin_i) {
2202 		GLTFSkin &gltf_skin = state.skins.write[skin_i];
2203 
2204 		Ref<Skin> skin;
2205 		skin.instance();
2206 
2207 		// Some skins don't have IBM's! What absolute monsters!
2208 		const bool has_ibms = !gltf_skin.inverse_binds.empty();
2209 
2210 		for (int joint_i = 0; joint_i < gltf_skin.joints_original.size(); ++joint_i) {
2211 
2212 			Transform xform;
2213 			if (has_ibms) {
2214 				xform = gltf_skin.inverse_binds[joint_i];
2215 			}
2216 
2217 			if (state.use_named_skin_binds) {
2218 				StringName name = gltf_skin.joint_i_to_name[joint_i];
2219 				skin->add_named_bind(name, xform);
2220 			} else {
2221 				int bone_i = gltf_skin.joint_i_to_bone_i[joint_i];
2222 				skin->add_bind(bone_i, xform);
2223 			}
2224 		}
2225 
2226 		gltf_skin.godot_skin = skin;
2227 	}
2228 
2229 	// Purge the duplicates!
2230 	_remove_duplicate_skins(state);
2231 
2232 	// Create unique names now, after removing duplicates
2233 	for (GLTFSkinIndex skin_i = 0; skin_i < state.skins.size(); ++skin_i) {
2234 		Ref<Skin> skin = state.skins[skin_i].godot_skin;
2235 		if (skin->get_name().empty()) {
2236 			// Make a unique name, no gltf node represents this skin
2237 			skin->set_name(_gen_unique_name(state, "Skin"));
2238 		}
2239 	}
2240 
2241 	return OK;
2242 }
2243 
_skins_are_same(const Ref<Skin> & skin_a,const Ref<Skin> & skin_b)2244 bool EditorSceneImporterGLTF::_skins_are_same(const Ref<Skin> &skin_a, const Ref<Skin> &skin_b) {
2245 	if (skin_a->get_bind_count() != skin_b->get_bind_count()) {
2246 		return false;
2247 	}
2248 
2249 	for (int i = 0; i < skin_a->get_bind_count(); ++i) {
2250 		if (skin_a->get_bind_bone(i) != skin_b->get_bind_bone(i)) {
2251 			return false;
2252 		}
2253 
2254 		Transform a_xform = skin_a->get_bind_pose(i);
2255 		Transform b_xform = skin_b->get_bind_pose(i);
2256 
2257 		if (a_xform != b_xform) {
2258 			return false;
2259 		}
2260 	}
2261 
2262 	return true;
2263 }
2264 
_remove_duplicate_skins(GLTFState & state)2265 void EditorSceneImporterGLTF::_remove_duplicate_skins(GLTFState &state) {
2266 	for (int i = 0; i < state.skins.size(); ++i) {
2267 		for (int j = i + 1; j < state.skins.size(); ++j) {
2268 			const Ref<Skin> &skin_i = state.skins[i].godot_skin;
2269 			const Ref<Skin> &skin_j = state.skins[j].godot_skin;
2270 
2271 			if (_skins_are_same(skin_i, skin_j)) {
2272 				// replace it and delete the old
2273 				state.skins.write[j].godot_skin = skin_i;
2274 			}
2275 		}
2276 	}
2277 }
2278 
_parse_lights(GLTFState & state)2279 Error EditorSceneImporterGLTF::_parse_lights(GLTFState &state) {
2280 	if (!state.json.has("extensions")) {
2281 		return OK;
2282 	}
2283 	Dictionary extensions = state.json["extensions"];
2284 	if (!extensions.has("KHR_lights_punctual")) {
2285 		return OK;
2286 	}
2287 	Dictionary lights_punctual = extensions["KHR_lights_punctual"];
2288 	if (!lights_punctual.has("lights")) {
2289 		return OK;
2290 	}
2291 
2292 	const Array &lights = lights_punctual["lights"];
2293 
2294 	for (GLTFLightIndex light_i = 0; light_i < lights.size(); light_i++) {
2295 		const Dictionary &d = lights[light_i];
2296 
2297 		GLTFLight light;
2298 		ERR_FAIL_COND_V(!d.has("type"), ERR_PARSE_ERROR);
2299 		const String &type = d["type"];
2300 		light.type = type;
2301 
2302 		if (d.has("color")) {
2303 			const Array &arr = d["color"];
2304 			ERR_FAIL_COND_V(arr.size() != 3, ERR_PARSE_ERROR);
2305 			const Color c = Color(arr[0], arr[1], arr[2]).to_srgb();
2306 			light.color = c;
2307 		}
2308 		if (d.has("intensity")) {
2309 			light.intensity = d["intensity"];
2310 		}
2311 		if (d.has("range")) {
2312 			light.range = d["range"];
2313 		}
2314 		if (type == "spot") {
2315 			const Dictionary &spot = d["spot"];
2316 			light.inner_cone_angle = spot["innerConeAngle"];
2317 			light.outer_cone_angle = spot["outerConeAngle"];
2318 			ERR_FAIL_COND_V_MSG(light.inner_cone_angle >= light.outer_cone_angle, ERR_PARSE_ERROR, "The inner angle must be smaller than the outer angle.");
2319 		} else if (type != "point" && type != "directional") {
2320 			ERR_FAIL_V_MSG(ERR_PARSE_ERROR, "Light type is unknown.");
2321 		}
2322 
2323 		state.lights.push_back(light);
2324 	}
2325 
2326 	print_verbose("glTF: Total lights: " + itos(state.lights.size()));
2327 
2328 	return OK;
2329 }
2330 
_parse_cameras(GLTFState & state)2331 Error EditorSceneImporterGLTF::_parse_cameras(GLTFState &state) {
2332 
2333 	if (!state.json.has("cameras"))
2334 		return OK;
2335 
2336 	const Array &cameras = state.json["cameras"];
2337 
2338 	for (GLTFCameraIndex i = 0; i < cameras.size(); i++) {
2339 
2340 		const Dictionary &d = cameras[i];
2341 
2342 		GLTFCamera camera;
2343 		ERR_FAIL_COND_V(!d.has("type"), ERR_PARSE_ERROR);
2344 		const String &type = d["type"];
2345 		if (type == "orthographic") {
2346 
2347 			camera.perspective = false;
2348 			if (d.has("orthographic")) {
2349 				const Dictionary &og = d["orthographic"];
2350 				camera.fov_size = og["ymag"];
2351 				camera.zfar = og["zfar"];
2352 				camera.znear = og["znear"];
2353 			} else {
2354 				camera.fov_size = 10;
2355 			}
2356 
2357 		} else if (type == "perspective") {
2358 
2359 			camera.perspective = true;
2360 			if (d.has("perspective")) {
2361 				const Dictionary &ppt = d["perspective"];
2362 				// GLTF spec is in radians, Godot's camera is in degrees.
2363 				camera.fov_size = (double)ppt["yfov"] * 180.0 / Math_PI;
2364 				camera.zfar = ppt["zfar"];
2365 				camera.znear = ppt["znear"];
2366 			} else {
2367 				camera.fov_size = 10;
2368 			}
2369 		} else {
2370 			ERR_FAIL_V_MSG(ERR_PARSE_ERROR, "Camera should be in 'orthographic' or 'perspective'");
2371 		}
2372 
2373 		state.cameras.push_back(camera);
2374 	}
2375 
2376 	print_verbose("glTF: Total cameras: " + itos(state.cameras.size()));
2377 
2378 	return OK;
2379 }
2380 
_parse_animations(GLTFState & state)2381 Error EditorSceneImporterGLTF::_parse_animations(GLTFState &state) {
2382 
2383 	if (!state.json.has("animations"))
2384 		return OK;
2385 
2386 	const Array &animations = state.json["animations"];
2387 
2388 	for (GLTFAnimationIndex i = 0; i < animations.size(); i++) {
2389 
2390 		const Dictionary &d = animations[i];
2391 
2392 		GLTFAnimation animation;
2393 
2394 		if (!d.has("channels") || !d.has("samplers"))
2395 			continue;
2396 
2397 		Array channels = d["channels"];
2398 		Array samplers = d["samplers"];
2399 
2400 		if (d.has("name")) {
2401 			String name = d["name"];
2402 			if (name.begins_with("loop") || name.ends_with("loop") || name.begins_with("cycle") || name.ends_with("cycle")) {
2403 				animation.loop = true;
2404 			}
2405 			animation.name = _sanitize_scene_name(name);
2406 		}
2407 
2408 		for (int j = 0; j < channels.size(); j++) {
2409 
2410 			const Dictionary &c = channels[j];
2411 			if (!c.has("target"))
2412 				continue;
2413 
2414 			const Dictionary &t = c["target"];
2415 			if (!t.has("node") || !t.has("path")) {
2416 				continue;
2417 			}
2418 
2419 			ERR_FAIL_COND_V(!c.has("sampler"), ERR_PARSE_ERROR);
2420 			const int sampler = c["sampler"];
2421 			ERR_FAIL_INDEX_V(sampler, samplers.size(), ERR_PARSE_ERROR);
2422 
2423 			GLTFNodeIndex node = t["node"];
2424 			String path = t["path"];
2425 
2426 			ERR_FAIL_INDEX_V(node, state.nodes.size(), ERR_PARSE_ERROR);
2427 
2428 			GLTFAnimation::Track *track = nullptr;
2429 
2430 			if (!animation.tracks.has(node)) {
2431 				animation.tracks[node] = GLTFAnimation::Track();
2432 			}
2433 
2434 			track = &animation.tracks[node];
2435 
2436 			const Dictionary &s = samplers[sampler];
2437 
2438 			ERR_FAIL_COND_V(!s.has("input"), ERR_PARSE_ERROR);
2439 			ERR_FAIL_COND_V(!s.has("output"), ERR_PARSE_ERROR);
2440 
2441 			const int input = s["input"];
2442 			const int output = s["output"];
2443 
2444 			GLTFAnimation::Interpolation interp = GLTFAnimation::INTERP_LINEAR;
2445 			int output_count = 1;
2446 			if (s.has("interpolation")) {
2447 				const String &in = s["interpolation"];
2448 				if (in == "STEP") {
2449 					interp = GLTFAnimation::INTERP_STEP;
2450 				} else if (in == "LINEAR") {
2451 					interp = GLTFAnimation::INTERP_LINEAR;
2452 				} else if (in == "CATMULLROMSPLINE") {
2453 					interp = GLTFAnimation::INTERP_CATMULLROMSPLINE;
2454 					output_count = 3;
2455 				} else if (in == "CUBICSPLINE") {
2456 					interp = GLTFAnimation::INTERP_CUBIC_SPLINE;
2457 					output_count = 3;
2458 				}
2459 			}
2460 
2461 			const PoolVector<float> times = _decode_accessor_as_floats(state, input, false);
2462 			if (path == "translation") {
2463 				const PoolVector<Vector3> translations = _decode_accessor_as_vec3(state, output, false);
2464 				track->translation_track.interpolation = interp;
2465 				track->translation_track.times = Variant(times); //convert via variant
2466 				track->translation_track.values = Variant(translations); //convert via variant
2467 			} else if (path == "rotation") {
2468 				const Vector<Quat> rotations = _decode_accessor_as_quat(state, output, false);
2469 				track->rotation_track.interpolation = interp;
2470 				track->rotation_track.times = Variant(times); //convert via variant
2471 				track->rotation_track.values = rotations; //convert via variant
2472 			} else if (path == "scale") {
2473 				const PoolVector<Vector3> scales = _decode_accessor_as_vec3(state, output, false);
2474 				track->scale_track.interpolation = interp;
2475 				track->scale_track.times = Variant(times); //convert via variant
2476 				track->scale_track.values = Variant(scales); //convert via variant
2477 			} else if (path == "weights") {
2478 				const PoolVector<float> weights = _decode_accessor_as_floats(state, output, false);
2479 
2480 				ERR_FAIL_INDEX_V(state.nodes[node]->mesh, state.meshes.size(), ERR_PARSE_ERROR);
2481 				const GLTFMesh *mesh = &state.meshes[state.nodes[node]->mesh];
2482 				ERR_FAIL_COND_V(mesh->blend_weights.size() == 0, ERR_PARSE_ERROR);
2483 				const int wc = mesh->blend_weights.size();
2484 
2485 				track->weight_tracks.resize(wc);
2486 
2487 				const int expected_value_count = times.size() * output_count * wc;
2488 				ERR_FAIL_COND_V_MSG(weights.size() != expected_value_count, ERR_PARSE_ERROR, "Invalid weight data, expected " + itos(expected_value_count) + " weight values, got " + itos(weights.size()) + " instead.");
2489 
2490 				const int wlen = weights.size() / wc;
2491 				PoolVector<float>::Read r = weights.read();
2492 				for (int k = 0; k < wc; k++) { //separate tracks, having them together is not such a good idea
2493 					GLTFAnimation::Channel<float> cf;
2494 					cf.interpolation = interp;
2495 					cf.times = Variant(times);
2496 					Vector<float> wdata;
2497 					wdata.resize(wlen);
2498 					for (int l = 0; l < wlen; l++) {
2499 						wdata.write[l] = r[l * wc + k];
2500 					}
2501 
2502 					cf.values = wdata;
2503 					track->weight_tracks.write[k] = cf;
2504 				}
2505 			} else {
2506 				WARN_PRINTS("Invalid path '" + path + "'.");
2507 			}
2508 		}
2509 
2510 		state.animations.push_back(animation);
2511 	}
2512 
2513 	print_verbose("glTF: Total animations '" + itos(state.animations.size()) + "'.");
2514 
2515 	return OK;
2516 }
2517 
_assign_scene_names(GLTFState & state)2518 void EditorSceneImporterGLTF::_assign_scene_names(GLTFState &state) {
2519 
2520 	for (int i = 0; i < state.nodes.size(); i++) {
2521 		GLTFNode *n = state.nodes[i];
2522 
2523 		// Any joints get unique names generated when the skeleton is made, unique to the skeleton
2524 		if (n->skeleton >= 0)
2525 			continue;
2526 
2527 		if (n->name.empty()) {
2528 			if (n->mesh >= 0) {
2529 				n->name = "Mesh";
2530 			} else if (n->camera >= 0) {
2531 				n->name = "Camera";
2532 			} else {
2533 				n->name = "Node";
2534 			}
2535 		}
2536 
2537 		n->name = _gen_unique_name(state, n->name);
2538 	}
2539 }
2540 
_generate_bone_attachment(GLTFState & state,Skeleton * skeleton,const GLTFNodeIndex node_index)2541 BoneAttachment *EditorSceneImporterGLTF::_generate_bone_attachment(GLTFState &state, Skeleton *skeleton, const GLTFNodeIndex node_index) {
2542 
2543 	const GLTFNode *gltf_node = state.nodes[node_index];
2544 	const GLTFNode *bone_node = state.nodes[gltf_node->parent];
2545 
2546 	BoneAttachment *bone_attachment = memnew(BoneAttachment);
2547 	print_verbose("glTF: Creating bone attachment for: " + gltf_node->name);
2548 
2549 	ERR_FAIL_COND_V(!bone_node->joint, nullptr);
2550 
2551 	bone_attachment->set_bone_name(bone_node->name);
2552 
2553 	return bone_attachment;
2554 }
2555 
_generate_mesh_instance(GLTFState & state,Node * scene_parent,const GLTFNodeIndex node_index)2556 MeshInstance *EditorSceneImporterGLTF::_generate_mesh_instance(GLTFState &state, Node *scene_parent, const GLTFNodeIndex node_index) {
2557 	const GLTFNode *gltf_node = state.nodes[node_index];
2558 
2559 	ERR_FAIL_INDEX_V(gltf_node->mesh, state.meshes.size(), nullptr);
2560 
2561 	MeshInstance *mi = memnew(MeshInstance);
2562 	print_verbose("glTF: Creating mesh for: " + gltf_node->name);
2563 
2564 	GLTFMesh &mesh = state.meshes.write[gltf_node->mesh];
2565 	mi->set_mesh(mesh.mesh);
2566 
2567 	if (mesh.mesh->get_name() == "") {
2568 		mesh.mesh->set_name(gltf_node->name);
2569 	}
2570 
2571 	for (int i = 0; i < mesh.blend_weights.size(); i++) {
2572 		mi->set("blend_shapes/" + mesh.mesh->get_blend_shape_name(i), mesh.blend_weights[i]);
2573 	}
2574 
2575 	return mi;
2576 }
2577 
_generate_light(GLTFState & state,Node * scene_parent,const GLTFNodeIndex node_index)2578 Light *EditorSceneImporterGLTF::_generate_light(GLTFState &state, Node *scene_parent, const GLTFNodeIndex node_index) {
2579 	const GLTFNode *gltf_node = state.nodes[node_index];
2580 
2581 	ERR_FAIL_INDEX_V(gltf_node->light, state.lights.size(), nullptr);
2582 
2583 	print_verbose("glTF: Creating light for: " + gltf_node->name);
2584 
2585 	const GLTFLight &l = state.lights[gltf_node->light];
2586 
2587 	float intensity = l.intensity;
2588 	if (intensity > 10) {
2589 		// GLTF spec has the default around 1, but Blender defaults lights to 100.
2590 		// The only sane way to handle this is to check where it came from and
2591 		// handle it accordingly. If it's over 10, it probably came from Blender.
2592 		intensity /= 100;
2593 	}
2594 
2595 	if (l.type == "directional") {
2596 		DirectionalLight *light = memnew(DirectionalLight);
2597 		light->set_param(Light::PARAM_ENERGY, intensity);
2598 		light->set_color(l.color);
2599 		return light;
2600 	}
2601 
2602 	const float range = CLAMP(l.range, 0, 4096);
2603 	// Doubling the range will double the effective brightness, so we need double attenuation (half brightness).
2604 	// We want to have double intensity give double brightness, so we need half the attenuation.
2605 	const float attenuation = range / intensity;
2606 	if (l.type == "point") {
2607 		OmniLight *light = memnew(OmniLight);
2608 		light->set_param(OmniLight::PARAM_ATTENUATION, attenuation);
2609 		light->set_param(OmniLight::PARAM_RANGE, range);
2610 		light->set_color(l.color);
2611 		return light;
2612 	}
2613 	if (l.type == "spot") {
2614 		SpotLight *light = memnew(SpotLight);
2615 		light->set_param(SpotLight::PARAM_ATTENUATION, attenuation);
2616 		light->set_param(SpotLight::PARAM_RANGE, range);
2617 		light->set_param(SpotLight::PARAM_SPOT_ANGLE, Math::rad2deg(l.outer_cone_angle));
2618 		light->set_color(l.color);
2619 
2620 		// Line of best fit derived from guessing, see https://www.desmos.com/calculator/biiflubp8b
2621 		// The points in desmos are not exact, except for (1, infinity).
2622 		float angle_ratio = l.inner_cone_angle / l.outer_cone_angle;
2623 		float angle_attenuation = 0.2 / (1 - angle_ratio) - 0.1;
2624 		light->set_param(SpotLight::PARAM_SPOT_ATTENUATION, angle_attenuation);
2625 		return light;
2626 	}
2627 	return nullptr;
2628 }
2629 
_generate_camera(GLTFState & state,Node * scene_parent,const GLTFNodeIndex node_index)2630 Camera *EditorSceneImporterGLTF::_generate_camera(GLTFState &state, Node *scene_parent, const GLTFNodeIndex node_index) {
2631 	const GLTFNode *gltf_node = state.nodes[node_index];
2632 
2633 	ERR_FAIL_INDEX_V(gltf_node->camera, state.cameras.size(), nullptr);
2634 
2635 	Camera *camera = memnew(Camera);
2636 	print_verbose("glTF: Creating camera for: " + gltf_node->name);
2637 
2638 	const GLTFCamera &c = state.cameras[gltf_node->camera];
2639 	if (c.perspective) {
2640 		camera->set_perspective(c.fov_size, c.znear, c.zfar);
2641 	} else {
2642 		camera->set_orthogonal(c.fov_size, c.znear, c.zfar);
2643 	}
2644 
2645 	return camera;
2646 }
2647 
_generate_spatial(GLTFState & state,Node * scene_parent,const GLTFNodeIndex node_index)2648 Spatial *EditorSceneImporterGLTF::_generate_spatial(GLTFState &state, Node *scene_parent, const GLTFNodeIndex node_index) {
2649 	const GLTFNode *gltf_node = state.nodes[node_index];
2650 
2651 	Spatial *spatial = memnew(Spatial);
2652 	print_verbose("glTF: Creating spatial for: " + gltf_node->name);
2653 
2654 	return spatial;
2655 }
2656 
_generate_scene_node(GLTFState & state,Node * scene_parent,Spatial * scene_root,const GLTFNodeIndex node_index)2657 void EditorSceneImporterGLTF::_generate_scene_node(GLTFState &state, Node *scene_parent, Spatial *scene_root, const GLTFNodeIndex node_index) {
2658 
2659 	const GLTFNode *gltf_node = state.nodes[node_index];
2660 
2661 	Spatial *current_node = nullptr;
2662 
2663 	// Is our parent a skeleton
2664 	Skeleton *active_skeleton = Object::cast_to<Skeleton>(scene_parent);
2665 
2666 	if (gltf_node->skeleton >= 0) {
2667 		Skeleton *skeleton = state.skeletons[gltf_node->skeleton].godot_skeleton;
2668 
2669 		if (active_skeleton != skeleton) {
2670 			ERR_FAIL_COND_MSG(active_skeleton != nullptr, "glTF: Generating scene detected direct parented Skeletons");
2671 
2672 			// Add it to the scene if it has not already been added
2673 			if (skeleton->get_parent() == nullptr) {
2674 				scene_parent->add_child(skeleton);
2675 				skeleton->set_owner(scene_root);
2676 			}
2677 		}
2678 
2679 		active_skeleton = skeleton;
2680 		current_node = skeleton;
2681 	}
2682 
2683 	// If we have an active skeleton, and the node is node skinned, we need to create a bone attachment
2684 	if (current_node == nullptr && active_skeleton != nullptr && gltf_node->skin < 0) {
2685 		BoneAttachment *bone_attachment = _generate_bone_attachment(state, active_skeleton, node_index);
2686 
2687 		scene_parent->add_child(bone_attachment);
2688 		bone_attachment->set_owner(scene_root);
2689 
2690 		// There is no gltf_node that represent this, so just directly create a unique name
2691 		bone_attachment->set_name(_gen_unique_name(state, "BoneAttachment"));
2692 
2693 		// We change the scene_parent to our bone attachment now. We do not set current_node because we want to make the node
2694 		// and attach it to the bone_attachment
2695 		scene_parent = bone_attachment;
2696 	}
2697 
2698 	// We still have not managed to make a node
2699 	if (current_node == nullptr) {
2700 		if (gltf_node->mesh >= 0) {
2701 			current_node = _generate_mesh_instance(state, scene_parent, node_index);
2702 		} else if (gltf_node->camera >= 0) {
2703 			current_node = _generate_camera(state, scene_parent, node_index);
2704 		} else if (gltf_node->light >= 0) {
2705 			current_node = _generate_light(state, scene_parent, node_index);
2706 		} else {
2707 			current_node = _generate_spatial(state, scene_parent, node_index);
2708 		}
2709 
2710 		scene_parent->add_child(current_node);
2711 		current_node->set_owner(scene_root);
2712 		current_node->set_transform(gltf_node->xform);
2713 		current_node->set_name(gltf_node->name);
2714 	}
2715 
2716 	state.scene_nodes.insert(node_index, current_node);
2717 
2718 	for (int i = 0; i < gltf_node->children.size(); ++i) {
2719 		_generate_scene_node(state, current_node, scene_root, gltf_node->children[i]);
2720 	}
2721 }
2722 
2723 template <class T>
2724 struct EditorSceneImporterGLTFInterpolate {
2725 
lerpEditorSceneImporterGLTFInterpolate2726 	T lerp(const T &a, const T &b, float c) const {
2727 
2728 		return a + (b - a) * c;
2729 	}
2730 
catmull_romEditorSceneImporterGLTFInterpolate2731 	T catmull_rom(const T &p0, const T &p1, const T &p2, const T &p3, float t) {
2732 
2733 		const float t2 = t * t;
2734 		const float t3 = t2 * t;
2735 
2736 		return 0.5f * ((2.0f * p1) + (-p0 + p2) * t + (2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 + (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
2737 	}
2738 
bezierEditorSceneImporterGLTFInterpolate2739 	T bezier(T start, T control_1, T control_2, T end, float t) {
2740 		/* Formula from Wikipedia article on Bezier curves. */
2741 		const real_t omt = (1.0 - t);
2742 		const real_t omt2 = omt * omt;
2743 		const real_t omt3 = omt2 * omt;
2744 		const real_t t2 = t * t;
2745 		const real_t t3 = t2 * t;
2746 
2747 		return start * omt3 + control_1 * omt2 * t * 3.0 + control_2 * omt * t2 * 3.0 + end * t3;
2748 	}
2749 };
2750 
2751 // thank you for existing, partial specialization
2752 template <>
2753 struct EditorSceneImporterGLTFInterpolate<Quat> {
2754 
lerpEditorSceneImporterGLTFInterpolate2755 	Quat lerp(const Quat &a, const Quat &b, const float c) const {
2756 		ERR_FAIL_COND_V_MSG(!a.is_normalized(), Quat(), "The quaternion \"a\" must be normalized.");
2757 		ERR_FAIL_COND_V_MSG(!b.is_normalized(), Quat(), "The quaternion \"b\" must be normalized.");
2758 
2759 		return a.slerp(b, c).normalized();
2760 	}
2761 
catmull_romEditorSceneImporterGLTFInterpolate2762 	Quat catmull_rom(const Quat &p0, const Quat &p1, const Quat &p2, const Quat &p3, const float c) {
2763 		ERR_FAIL_COND_V_MSG(!p1.is_normalized(), Quat(), "The quaternion \"p1\" must be normalized.");
2764 		ERR_FAIL_COND_V_MSG(!p2.is_normalized(), Quat(), "The quaternion \"p2\" must be normalized.");
2765 
2766 		return p1.slerp(p2, c).normalized();
2767 	}
2768 
bezierEditorSceneImporterGLTFInterpolate2769 	Quat bezier(const Quat start, const Quat control_1, const Quat control_2, const Quat end, const float t) {
2770 		ERR_FAIL_COND_V_MSG(!start.is_normalized(), Quat(), "The start quaternion must be normalized.");
2771 		ERR_FAIL_COND_V_MSG(!end.is_normalized(), Quat(), "The end quaternion must be normalized.");
2772 
2773 		return start.slerp(end, t).normalized();
2774 	}
2775 };
2776 
2777 template <class T>
_interpolate_track(const Vector<float> & p_times,const Vector<T> & p_values,const float p_time,const GLTFAnimation::Interpolation p_interp)2778 T EditorSceneImporterGLTF::_interpolate_track(const Vector<float> &p_times, const Vector<T> &p_values, const float p_time, const GLTFAnimation::Interpolation p_interp) {
2779 
2780 	//could use binary search, worth it?
2781 	int idx = -1;
2782 	for (int i = 0; i < p_times.size(); i++) {
2783 		if (p_times[i] > p_time)
2784 			break;
2785 		idx++;
2786 	}
2787 
2788 	EditorSceneImporterGLTFInterpolate<T> interp;
2789 
2790 	switch (p_interp) {
2791 		case GLTFAnimation::INTERP_LINEAR: {
2792 
2793 			if (idx == -1) {
2794 				return p_values[0];
2795 			} else if (idx >= p_times.size() - 1) {
2796 				return p_values[p_times.size() - 1];
2797 			}
2798 
2799 			const float c = (p_time - p_times[idx]) / (p_times[idx + 1] - p_times[idx]);
2800 
2801 			return interp.lerp(p_values[idx], p_values[idx + 1], c);
2802 
2803 		} break;
2804 		case GLTFAnimation::INTERP_STEP: {
2805 
2806 			if (idx == -1) {
2807 				return p_values[0];
2808 			} else if (idx >= p_times.size() - 1) {
2809 				return p_values[p_times.size() - 1];
2810 			}
2811 
2812 			return p_values[idx];
2813 
2814 		} break;
2815 		case GLTFAnimation::INTERP_CATMULLROMSPLINE: {
2816 
2817 			if (idx == -1) {
2818 				return p_values[1];
2819 			} else if (idx >= p_times.size() - 1) {
2820 				return p_values[1 + p_times.size() - 1];
2821 			}
2822 
2823 			const float c = (p_time - p_times[idx]) / (p_times[idx + 1] - p_times[idx]);
2824 
2825 			return interp.catmull_rom(p_values[idx - 1], p_values[idx], p_values[idx + 1], p_values[idx + 3], c);
2826 
2827 		} break;
2828 		case GLTFAnimation::INTERP_CUBIC_SPLINE: {
2829 
2830 			if (idx == -1) {
2831 				return p_values[1];
2832 			} else if (idx >= p_times.size() - 1) {
2833 				return p_values[(p_times.size() - 1) * 3 + 1];
2834 			}
2835 
2836 			const float c = (p_time - p_times[idx]) / (p_times[idx + 1] - p_times[idx]);
2837 
2838 			const T from = p_values[idx * 3 + 1];
2839 			const T c1 = from + p_values[idx * 3 + 2];
2840 			const T to = p_values[idx * 3 + 4];
2841 			const T c2 = to + p_values[idx * 3 + 3];
2842 
2843 			return interp.bezier(from, c1, c2, to, c);
2844 
2845 		} break;
2846 	}
2847 
2848 	ERR_FAIL_V(p_values[0]);
2849 }
2850 
_import_animation(GLTFState & state,AnimationPlayer * ap,const GLTFAnimationIndex index,const int bake_fps)2851 void EditorSceneImporterGLTF::_import_animation(GLTFState &state, AnimationPlayer *ap, const GLTFAnimationIndex index, const int bake_fps) {
2852 
2853 	const GLTFAnimation &anim = state.animations[index];
2854 
2855 	String name = anim.name;
2856 	if (name.empty()) {
2857 		// No node represent these, and they are not in the hierarchy, so just make a unique name
2858 		name = _gen_unique_name(state, "Animation");
2859 	}
2860 
2861 	Ref<Animation> animation;
2862 	animation.instance();
2863 	animation->set_name(name);
2864 
2865 	if (anim.loop) {
2866 		animation->set_loop(true);
2867 	}
2868 
2869 	float length = 0;
2870 
2871 	for (Map<int, GLTFAnimation::Track>::Element *E = anim.tracks.front(); E; E = E->next()) {
2872 
2873 		const GLTFAnimation::Track &track = E->get();
2874 		//need to find the path
2875 		NodePath node_path;
2876 
2877 		GLTFNodeIndex node_index = E->key();
2878 		if (state.nodes[node_index]->fake_joint_parent >= 0) {
2879 			// Should be same as parent
2880 			node_index = state.nodes[node_index]->fake_joint_parent;
2881 		}
2882 
2883 		const GLTFNode *node = state.nodes[E->key()];
2884 
2885 		if (node->skeleton >= 0) {
2886 			const Skeleton *sk = Object::cast_to<Skeleton>(state.scene_nodes.find(node_index)->get());
2887 			ERR_FAIL_COND(sk == nullptr);
2888 
2889 			const String path = ap->get_parent()->get_path_to(sk);
2890 			const String bone = node->name;
2891 			node_path = path + ":" + bone;
2892 		} else {
2893 			node_path = ap->get_parent()->get_path_to(state.scene_nodes.find(node_index)->get());
2894 		}
2895 
2896 		for (int i = 0; i < track.rotation_track.times.size(); i++) {
2897 			length = MAX(length, track.rotation_track.times[i]);
2898 		}
2899 		for (int i = 0; i < track.translation_track.times.size(); i++) {
2900 			length = MAX(length, track.translation_track.times[i]);
2901 		}
2902 		for (int i = 0; i < track.scale_track.times.size(); i++) {
2903 			length = MAX(length, track.scale_track.times[i]);
2904 		}
2905 
2906 		for (int i = 0; i < track.weight_tracks.size(); i++) {
2907 			for (int j = 0; j < track.weight_tracks[i].times.size(); j++) {
2908 				length = MAX(length, track.weight_tracks[i].times[j]);
2909 			}
2910 		}
2911 
2912 		if (track.rotation_track.values.size() || track.translation_track.values.size() || track.scale_track.values.size()) {
2913 			//make transform track
2914 			int track_idx = animation->get_track_count();
2915 			animation->add_track(Animation::TYPE_TRANSFORM);
2916 			animation->track_set_path(track_idx, node_path);
2917 			animation->track_set_imported(track_idx, true);
2918 			//first determine animation length
2919 
2920 			const float increment = 1.0 / float(bake_fps);
2921 			float time = 0.0;
2922 
2923 			Vector3 base_pos;
2924 			Quat base_rot;
2925 			Vector3 base_scale = Vector3(1, 1, 1);
2926 
2927 			if (!track.rotation_track.values.size()) {
2928 				base_rot = state.nodes[E->key()]->rotation.normalized();
2929 			}
2930 
2931 			if (!track.translation_track.values.size()) {
2932 				base_pos = state.nodes[E->key()]->translation;
2933 			}
2934 
2935 			if (!track.scale_track.values.size()) {
2936 				base_scale = state.nodes[E->key()]->scale;
2937 			}
2938 
2939 			bool last = false;
2940 			while (true) {
2941 
2942 				Vector3 pos = base_pos;
2943 				Quat rot = base_rot;
2944 				Vector3 scale = base_scale;
2945 
2946 				if (track.translation_track.times.size()) {
2947 					pos = _interpolate_track<Vector3>(track.translation_track.times, track.translation_track.values, time, track.translation_track.interpolation);
2948 				}
2949 
2950 				if (track.rotation_track.times.size()) {
2951 					rot = _interpolate_track<Quat>(track.rotation_track.times, track.rotation_track.values, time, track.rotation_track.interpolation);
2952 				}
2953 
2954 				if (track.scale_track.times.size()) {
2955 					scale = _interpolate_track<Vector3>(track.scale_track.times, track.scale_track.values, time, track.scale_track.interpolation);
2956 				}
2957 
2958 				if (node->skeleton >= 0) {
2959 
2960 					Transform xform;
2961 					xform.basis.set_quat_scale(rot, scale);
2962 					xform.origin = pos;
2963 
2964 					const Skeleton *skeleton = state.skeletons[node->skeleton].godot_skeleton;
2965 					const int bone_idx = skeleton->find_bone(node->name);
2966 					xform = skeleton->get_bone_rest(bone_idx).affine_inverse() * xform;
2967 
2968 					rot = xform.basis.get_rotation_quat();
2969 					rot.normalize();
2970 					scale = xform.basis.get_scale();
2971 					pos = xform.origin;
2972 				}
2973 
2974 				animation->transform_track_insert_key(track_idx, time, pos, rot, scale);
2975 
2976 				if (last) {
2977 					break;
2978 				}
2979 				time += increment;
2980 				if (time >= length) {
2981 					last = true;
2982 					time = length;
2983 				}
2984 			}
2985 		}
2986 
2987 		for (int i = 0; i < track.weight_tracks.size(); i++) {
2988 			ERR_CONTINUE(node->mesh < 0 || node->mesh >= state.meshes.size());
2989 			const GLTFMesh &mesh = state.meshes[node->mesh];
2990 			const String prop = "blend_shapes/" + mesh.mesh->get_blend_shape_name(i);
2991 
2992 			const String blend_path = String(node_path) + ":" + prop;
2993 
2994 			const int track_idx = animation->get_track_count();
2995 			animation->add_track(Animation::TYPE_VALUE);
2996 			animation->track_set_path(track_idx, blend_path);
2997 
2998 			// Only LINEAR and STEP (NEAREST) can be supported out of the box by Godot's Animation,
2999 			// the other modes have to be baked.
3000 			GLTFAnimation::Interpolation gltf_interp = track.weight_tracks[i].interpolation;
3001 			if (gltf_interp == GLTFAnimation::INTERP_LINEAR || gltf_interp == GLTFAnimation::INTERP_STEP) {
3002 				animation->track_set_interpolation_type(track_idx, gltf_interp == GLTFAnimation::INTERP_STEP ? Animation::INTERPOLATION_NEAREST : Animation::INTERPOLATION_LINEAR);
3003 				for (int j = 0; j < track.weight_tracks[i].times.size(); j++) {
3004 					const float t = track.weight_tracks[i].times[j];
3005 					const float w = track.weight_tracks[i].values[j];
3006 					animation->track_insert_key(track_idx, t, w);
3007 				}
3008 			} else {
3009 				// CATMULLROMSPLINE or CUBIC_SPLINE have to be baked, apologies.
3010 				const float increment = 1.0 / float(bake_fps);
3011 				float time = 0.0;
3012 				bool last = false;
3013 				while (true) {
3014 					_interpolate_track<float>(track.weight_tracks[i].times, track.weight_tracks[i].values, time, gltf_interp);
3015 					if (last) {
3016 						break;
3017 					}
3018 					time += increment;
3019 					if (time >= length) {
3020 						last = true;
3021 						time = length;
3022 					}
3023 				}
3024 			}
3025 		}
3026 	}
3027 
3028 	animation->set_length(length);
3029 
3030 	ap->add_animation(name, animation);
3031 }
3032 
_process_mesh_instances(GLTFState & state,Spatial * scene_root)3033 void EditorSceneImporterGLTF::_process_mesh_instances(GLTFState &state, Spatial *scene_root) {
3034 	for (GLTFNodeIndex node_i = 0; node_i < state.nodes.size(); ++node_i) {
3035 		const GLTFNode *node = state.nodes[node_i];
3036 
3037 		if (node->skin >= 0 && node->mesh >= 0) {
3038 			const GLTFSkinIndex skin_i = node->skin;
3039 
3040 			Map<GLTFNodeIndex, Node *>::Element *mi_element = state.scene_nodes.find(node_i);
3041 			MeshInstance *mi = Object::cast_to<MeshInstance>(mi_element->get());
3042 			ERR_FAIL_COND(mi == nullptr);
3043 
3044 			const GLTFSkeletonIndex skel_i = state.skins[node->skin].skeleton;
3045 			const GLTFSkeleton &gltf_skeleton = state.skeletons[skel_i];
3046 			Skeleton *skeleton = gltf_skeleton.godot_skeleton;
3047 			ERR_FAIL_COND(skeleton == nullptr);
3048 
3049 			mi->get_parent()->remove_child(mi);
3050 			skeleton->add_child(mi);
3051 			mi->set_owner(scene_root);
3052 
3053 			mi->set_skin(state.skins[skin_i].godot_skin);
3054 			mi->set_skeleton_path(mi->get_path_to(skeleton));
3055 			mi->set_transform(Transform());
3056 		}
3057 	}
3058 }
3059 
_generate_scene(GLTFState & state,const int p_bake_fps)3060 Spatial *EditorSceneImporterGLTF::_generate_scene(GLTFState &state, const int p_bake_fps) {
3061 
3062 	Spatial *root = memnew(Spatial);
3063 
3064 	// scene_name is already unique
3065 	root->set_name(state.scene_name);
3066 
3067 	for (int i = 0; i < state.root_nodes.size(); ++i) {
3068 		_generate_scene_node(state, root, root, state.root_nodes[i]);
3069 	}
3070 
3071 	_process_mesh_instances(state, root);
3072 
3073 	if (state.animations.size()) {
3074 		AnimationPlayer *ap = memnew(AnimationPlayer);
3075 		ap->set_name("AnimationPlayer");
3076 		root->add_child(ap);
3077 		ap->set_owner(root);
3078 
3079 		for (int i = 0; i < state.animations.size(); i++) {
3080 			_import_animation(state, ap, i, p_bake_fps);
3081 		}
3082 	}
3083 
3084 	return root;
3085 }
3086 
import_scene(const String & p_path,uint32_t p_flags,int p_bake_fps,List<String> * r_missing_deps,Error * r_err)3087 Node *EditorSceneImporterGLTF::import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps, Error *r_err) {
3088 
3089 	GLTFState state;
3090 
3091 	if (p_path.to_lower().ends_with("glb")) {
3092 		//binary file
3093 		//text file
3094 		Error err = _parse_glb(p_path, state);
3095 		if (err) {
3096 			return NULL;
3097 		}
3098 	} else {
3099 		//text file
3100 		Error err = _parse_json(p_path, state);
3101 		if (err) {
3102 			return NULL;
3103 		}
3104 	}
3105 
3106 	ERR_FAIL_COND_V(!state.json.has("asset"), NULL);
3107 
3108 	Dictionary asset = state.json["asset"];
3109 
3110 	ERR_FAIL_COND_V(!asset.has("version"), NULL);
3111 
3112 	String version = asset["version"];
3113 
3114 	state.import_flags = p_flags;
3115 	state.major_version = version.get_slice(".", 0).to_int();
3116 	state.minor_version = version.get_slice(".", 1).to_int();
3117 	state.use_named_skin_binds = p_flags & IMPORT_USE_NAMED_SKIN_BINDS;
3118 
3119 	/* STEP 0 PARSE SCENE */
3120 	Error err = _parse_scenes(state);
3121 	if (err != OK) {
3122 		return NULL;
3123 	}
3124 
3125 	/* STEP 1 PARSE NODES */
3126 	err = _parse_nodes(state);
3127 	if (err != OK) {
3128 		return NULL;
3129 	}
3130 
3131 	/* STEP 2 PARSE BUFFERS */
3132 	err = _parse_buffers(state, p_path.get_base_dir());
3133 	if (err != OK) {
3134 		return NULL;
3135 	}
3136 
3137 	/* STEP 3 PARSE BUFFER VIEWS */
3138 	err = _parse_buffer_views(state);
3139 	if (err != OK) {
3140 		return NULL;
3141 	}
3142 
3143 	/* STEP 4 PARSE ACCESSORS */
3144 	err = _parse_accessors(state);
3145 	if (err != OK) {
3146 		return NULL;
3147 	}
3148 
3149 	/* STEP 5 PARSE IMAGES */
3150 	err = _parse_images(state, p_path.get_base_dir());
3151 	if (err != OK) {
3152 		return NULL;
3153 	}
3154 
3155 	/* STEP 6 PARSE TEXTURES */
3156 	err = _parse_textures(state);
3157 	if (err != OK) {
3158 		return NULL;
3159 	}
3160 
3161 	/* STEP 7 PARSE TEXTURES */
3162 	err = _parse_materials(state);
3163 	if (err != OK) {
3164 		return NULL;
3165 	}
3166 
3167 	/* STEP 9 PARSE SKINS */
3168 	err = _parse_skins(state);
3169 	if (err != OK) {
3170 		return NULL;
3171 	}
3172 
3173 	/* STEP 10 DETERMINE SKELETONS */
3174 	err = _determine_skeletons(state);
3175 	if (err != OK) {
3176 		return NULL;
3177 	}
3178 
3179 	/* STEP 11 CREATE SKELETONS */
3180 	err = _create_skeletons(state);
3181 	if (err != OK) {
3182 		return NULL;
3183 	}
3184 
3185 	/* STEP 12 CREATE SKINS */
3186 	err = _create_skins(state);
3187 	if (err != OK) {
3188 		return NULL;
3189 	}
3190 
3191 	/* STEP 13 PARSE MESHES (we have enough info now) */
3192 	err = _parse_meshes(state);
3193 	if (err != OK) {
3194 		return NULL;
3195 	}
3196 
3197 	/* STEP 14 PARSE LIGHTS */
3198 	err = _parse_lights(state);
3199 	if (err != OK) {
3200 		return NULL;
3201 	}
3202 
3203 	/* STEP 15 PARSE CAMERAS */
3204 	err = _parse_cameras(state);
3205 	if (err != OK) {
3206 		return NULL;
3207 	}
3208 
3209 	/* STEP 16 PARSE ANIMATIONS */
3210 	err = _parse_animations(state);
3211 	if (err != OK) {
3212 		return NULL;
3213 	}
3214 
3215 	/* STEP 17 ASSIGN SCENE NAMES */
3216 	_assign_scene_names(state);
3217 
3218 	/* STEP 18 MAKE SCENE! */
3219 	Spatial *scene = _generate_scene(state, p_bake_fps);
3220 
3221 	return scene;
3222 }
3223 
import_animation(const String & p_path,uint32_t p_flags,int p_bake_fps)3224 Ref<Animation> EditorSceneImporterGLTF::import_animation(const String &p_path, uint32_t p_flags, int p_bake_fps) {
3225 
3226 	return Ref<Animation>();
3227 }
3228 
EditorSceneImporterGLTF()3229 EditorSceneImporterGLTF::EditorSceneImporterGLTF() {
3230 }
3231