gltf_document_extension_convert_importer_mesh.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #include "gltf_document_extension_convert_importer_mesh.h"
  31. #include "core/error/error_macros.h"
  32. #include "scene/3d/mesh_instance_3d.h"
  33. #include "scene/resources/importer_mesh.h"
  34. #include <cstddef>
  35. void GLTFDocumentExtensionConvertImporterMesh::_bind_methods() {
  36. }
  37. Error GLTFDocumentExtensionConvertImporterMesh::import_post(Ref<GLTFDocument> p_document, Node *p_node) {
  38. ERR_FAIL_NULL_V(p_document, ERR_INVALID_PARAMETER);
  39. ERR_FAIL_NULL_V(p_node, ERR_INVALID_PARAMETER);
  40. List<Node *> queue;
  41. queue.push_back(p_node);
  42. List<Node *> delete_queue;
  43. while (!queue.is_empty()) {
  44. List<Node *>::Element *E = queue.front();
  45. Node *node = E->get();
  46. {
  47. ImporterMeshInstance3D *mesh_3d = cast_to<ImporterMeshInstance3D>(node);
  48. if (mesh_3d) {
  49. MeshInstance3D *mesh_instance_node_3d = memnew(MeshInstance3D);
  50. Ref<ImporterMesh> mesh = mesh_3d->get_mesh();
  51. if (mesh.is_valid()) {
  52. Ref<ArrayMesh> array_mesh = mesh->get_mesh();
  53. mesh_instance_node_3d->set_name(node->get_name());
  54. mesh_instance_node_3d->set_transform(mesh_3d->get_transform());
  55. mesh_instance_node_3d->set_mesh(array_mesh);
  56. mesh_instance_node_3d->set_skin(mesh_3d->get_skin());
  57. mesh_instance_node_3d->set_skeleton_path(mesh_3d->get_skeleton_path());
  58. node->replace_by(mesh_instance_node_3d);
  59. delete_queue.push_back(node);
  60. } else {
  61. memdelete(mesh_instance_node_3d);
  62. }
  63. }
  64. }
  65. int child_count = node->get_child_count();
  66. for (int i = 0; i < child_count; i++) {
  67. queue.push_back(node->get_child(i));
  68. }
  69. queue.pop_front();
  70. }
  71. while (!queue.is_empty()) {
  72. List<Node *>::Element *E = delete_queue.front();
  73. Node *node = E->get();
  74. memdelete(node);
  75. delete_queue.pop_front();
  76. }
  77. return OK;
  78. }