gltf_document_extension_convert_importer_mesh.cpp 4.4 KB

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