| 1 | /**************************************************************************/ | 
| 2 | /*  gltf_document_extension_convert_importer_mesh.cpp                     */ | 
| 3 | /**************************************************************************/ | 
| 4 | /*                         This file is part of:                          */ | 
| 5 | /*                             GODOT ENGINE                               */ | 
| 6 | /*                        https://godotengine.org                         */ | 
| 7 | /**************************************************************************/ | 
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
| 10 | /*                                                                        */ | 
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
| 12 | /* a copy of this software and associated documentation files (the        */ | 
| 13 | /* "Software"), to deal in the Software without restriction, including    */ | 
| 14 | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
| 16 | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
| 17 | /* the following conditions:                                              */ | 
| 18 | /*                                                                        */ | 
| 19 | /* The above copyright notice and this permission notice shall be         */ | 
| 20 | /* included in all copies or substantial portions of the Software.        */ | 
| 21 | /*                                                                        */ | 
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
| 29 | /**************************************************************************/ | 
| 30 |  | 
| 31 | #include "gltf_document_extension_convert_importer_mesh.h" | 
| 32 |  | 
| 33 | #include "scene/3d/importer_mesh_instance_3d.h" | 
| 34 | #include "scene/3d/mesh_instance_3d.h" | 
| 35 | #include "scene/resources/importer_mesh.h" | 
| 36 |  | 
| 37 | void GLTFDocumentExtensionConvertImporterMesh::_bind_methods() { | 
| 38 | } | 
| 39 |  | 
| 40 | Error GLTFDocumentExtensionConvertImporterMesh::import_post(Ref<GLTFState> p_state, Node *p_root) { | 
| 41 | 	ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER); | 
| 42 | 	ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER); | 
| 43 | 	List<Node *> queue; | 
| 44 | 	queue.push_back(p_root); | 
| 45 | 	List<Node *> delete_queue; | 
| 46 | 	while (!queue.is_empty()) { | 
| 47 | 		List<Node *>::Element *E = queue.front(); | 
| 48 | 		Node *node = E->get(); | 
| 49 | 		ImporterMeshInstance3D *mesh_3d = cast_to<ImporterMeshInstance3D>(node); | 
| 50 | 		if (mesh_3d) { | 
| 51 | 			MeshInstance3D *mesh_instance_node_3d = memnew(MeshInstance3D); | 
| 52 | 			Ref<ImporterMesh> mesh = mesh_3d->get_mesh(); | 
| 53 | 			if (mesh.is_valid()) { | 
| 54 | 				Ref<ArrayMesh> array_mesh = mesh->get_mesh(); | 
| 55 | 				mesh_instance_node_3d->set_name(node->get_name()); | 
| 56 | 				mesh_instance_node_3d->set_transform(mesh_3d->get_transform()); | 
| 57 | 				mesh_instance_node_3d->set_mesh(array_mesh); | 
| 58 | 				mesh_instance_node_3d->set_skin(mesh_3d->get_skin()); | 
| 59 | 				mesh_instance_node_3d->set_skeleton_path(mesh_3d->get_skeleton_path()); | 
| 60 | 				node->replace_by(mesh_instance_node_3d); | 
| 61 | 				delete_queue.push_back(node); | 
| 62 | 				node = mesh_instance_node_3d; | 
| 63 | 			} else { | 
| 64 | 				memdelete(mesh_instance_node_3d); | 
| 65 | 			} | 
| 66 | 		} | 
| 67 | 		int child_count = node->get_child_count(); | 
| 68 | 		for (int i = 0; i < child_count; i++) { | 
| 69 | 			queue.push_back(node->get_child(i)); | 
| 70 | 		} | 
| 71 | 		queue.pop_front(); | 
| 72 | 	} | 
| 73 | 	while (!delete_queue.is_empty()) { | 
| 74 | 		List<Node *>::Element *E = delete_queue.front(); | 
| 75 | 		Node *node = E->get(); | 
| 76 | 		memdelete(node); | 
| 77 | 		delete_queue.pop_front(); | 
| 78 | 	} | 
| 79 | 	return OK; | 
| 80 | } | 
| 81 |  |