gltf_document_extension_convert_importer_mesh.cpp 4.9 KB

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