Debian Codegraph @

modules/gltf/extensions/gltf_document_extension_convert_importer_mesh.cpp

raw symbols SCIP
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/3d/importer_mesh.h"
36
37 void GLTFDocumentExtensionConvertImporterMesh::_copy_meta(Object *p_src_object, Object *p_dst_object) {
38 List<StringName> meta_list;
39 p_src_object->get_meta_list(&meta_list);
40 for (const StringName &meta_key : meta_list) {
41 Variant meta_value = p_src_object->get_meta(meta_key);
42 p_dst_object->set_meta(meta_key, meta_value);
43 }
44 }
45
46 MeshInstance3D *GLTFDocumentExtensionConvertImporterMesh::convert_importer_mesh_instance_3d(ImporterMeshInstance3D *p_importer_mesh_instance_3d) {
47 // Convert the node itself first.
48 MeshInstance3D *mesh_instance_node_3d = memnew(MeshInstance3D);
49 ERR_FAIL_NULL_V(p_importer_mesh_instance_3d, mesh_instance_node_3d);
50 mesh_instance_node_3d->set_name(p_importer_mesh_instance_3d->get_name());
51 mesh_instance_node_3d->set_transform(p_importer_mesh_instance_3d->get_transform());
52 mesh_instance_node_3d->set_skin(p_importer_mesh_instance_3d->get_skin());
53 mesh_instance_node_3d->set_skeleton_path(p_importer_mesh_instance_3d->get_skeleton_path());
54 mesh_instance_node_3d->set_visible(p_importer_mesh_instance_3d->is_visible());
55 p_importer_mesh_instance_3d->replace_by(mesh_instance_node_3d);
56 _copy_meta(p_importer_mesh_instance_3d, mesh_instance_node_3d);
57 // Convert the mesh data in the mesh resource.
58 Ref<ImporterMesh> importer_mesh = p_importer_mesh_instance_3d->get_mesh();
59 if (importer_mesh.is_valid()) {
60 Ref<ArrayMesh> array_mesh = importer_mesh->get_mesh();
61 mesh_instance_node_3d->set_mesh(array_mesh);
62 _copy_meta(importer_mesh.ptr(), array_mesh.ptr());
63 } else {
64 WARN_PRINT("glTF: ImporterMeshInstance3D does not have a valid mesh. This should not happen. Continuing anyway.");
65 }
66 return mesh_instance_node_3d;
67 }
68
69 Error GLTFDocumentExtensionConvertImporterMesh::import_post(Ref<GLTFState> p_state, Node *p_root) {
70 ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
71 ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
72 List<Node *> queue;
73 queue.push_back(p_root);
74 List<Node *> delete_queue;
75 while (!queue.is_empty()) {
76 List<Node *>::Element *E = queue.front();
77 Node *node = E->get();
78 ImporterMeshInstance3D *importer_mesh_3d = Object::cast_to<ImporterMeshInstance3D>(node);
79 if (importer_mesh_3d) {
80 delete_queue.push_back(importer_mesh_3d);
81 node = convert_importer_mesh_instance_3d(importer_mesh_3d);
82 }
83 int child_count = node->get_child_count();
84 for (int i = 0; i < child_count; i++) {
85 queue.push_back(node->get_child(i));
86 }
87 queue.pop_front();
88 }
89 while (!delete_queue.is_empty()) {
90 List<Node *>::Element *E = delete_queue.front();
91 Node *node = E->get();
92 memdelete(node);
93 delete_queue.pop_front();
94 }
95 return OK;
96 }