packed_scene_gltf.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**************************************************************************/
  2. /* packed_scene_gltf.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. #ifdef TOOLS_ENABLED
  31. #include "packed_scene_gltf.h"
  32. #include "editor/import/resource_importer_scene.h"
  33. #include "scene/3d/spatial.h"
  34. #include "scene/animation/animation_player.h"
  35. #include "scene/resources/mesh.h"
  36. #include "gltf_document.h"
  37. void PackedSceneGLTF::_bind_methods() {
  38. ClassDB::bind_method(D_METHOD("export_gltf", "node", "path", "flags", "bake_fps"),
  39. &PackedSceneGLTF::export_gltf, DEFVAL(0), DEFVAL(1000.0f));
  40. ClassDB::bind_method(D_METHOD("pack_gltf", "path", "flags", "bake_fps", "compress_flags", "state"),
  41. &PackedSceneGLTF::pack_gltf, DEFVAL(0), DEFVAL(1000.0f), DEFVAL(Mesh::ARRAY_COMPRESS_DEFAULT), DEFVAL(Ref<GLTFState>()));
  42. ClassDB::bind_method(D_METHOD("import_gltf_scene", "path", "flags", "bake_fps", "compress_flags", "state"),
  43. &PackedSceneGLTF::import_gltf_scene, DEFVAL(0), DEFVAL(1000.0f), DEFVAL(Mesh::ARRAY_COMPRESS_DEFAULT), DEFVAL(Ref<GLTFState>()));
  44. }
  45. Node *PackedSceneGLTF::import_gltf_scene(const String &p_path, uint32_t p_flags, float p_bake_fps, uint32_t p_compress_flags, Ref<GLTFState> r_state) {
  46. Error err = FAILED;
  47. List<String> deps;
  48. return import_scene(p_path, p_flags, p_bake_fps, p_compress_flags, &deps, &err, r_state);
  49. }
  50. Node *PackedSceneGLTF::import_scene(const String &p_path, uint32_t p_flags,
  51. int p_bake_fps, uint32_t p_compress_flags,
  52. List<String> *r_missing_deps,
  53. Error *r_err,
  54. Ref<GLTFState> r_state) {
  55. if (r_state == Ref<GLTFState>()) {
  56. r_state.instance();
  57. }
  58. r_state->use_named_skin_binds =
  59. p_flags & EditorSceneImporter::IMPORT_USE_NAMED_SKIN_BINDS;
  60. r_state->use_legacy_names =
  61. p_flags & EditorSceneImporter::IMPORT_USE_LEGACY_NAMES;
  62. r_state->compress_flags = p_compress_flags;
  63. r_state->set_create_animations(p_flags & EditorSceneImporter::IMPORT_ANIMATION);
  64. Ref<GLTFDocument> gltf_document;
  65. gltf_document.instance();
  66. Error err = gltf_document->parse(r_state, p_path);
  67. *r_err = err;
  68. ERR_FAIL_COND_V(err != Error::OK, nullptr);
  69. Spatial *root = memnew(Spatial);
  70. if (r_state->use_legacy_names) {
  71. root->set_name(gltf_document->_legacy_validate_node_name(r_state->scene_name));
  72. } else {
  73. root->set_name(r_state->scene_name);
  74. }
  75. for (int32_t root_i = 0; root_i < r_state->root_nodes.size(); root_i++) {
  76. gltf_document->_generate_scene_node(r_state, root, root, r_state->root_nodes[root_i]);
  77. }
  78. gltf_document->_process_mesh_instances(r_state, root);
  79. if (r_state->get_create_animations() && r_state->animations.size()) {
  80. AnimationPlayer *ap = memnew(AnimationPlayer);
  81. root->add_child(ap);
  82. ap->set_owner(root);
  83. for (int i = 0; i < r_state->animations.size(); i++) {
  84. gltf_document->_import_animation(r_state, ap, i, p_bake_fps);
  85. }
  86. }
  87. gltf_document->extension_generate_scene(r_state);
  88. return cast_to<Spatial>(root);
  89. }
  90. void PackedSceneGLTF::pack_gltf(String p_path, int32_t p_flags,
  91. real_t p_bake_fps, uint32_t p_compress_flags, Ref<GLTFState> r_state) {
  92. Error err = FAILED;
  93. List<String> deps;
  94. Node *root = import_scene(p_path, p_flags, p_bake_fps, p_compress_flags, &deps, &err, r_state);
  95. ERR_FAIL_COND(err != OK);
  96. pack(root);
  97. }
  98. void PackedSceneGLTF::save_scene(Node *p_node, const String &p_path,
  99. const String &p_src_path, uint32_t p_flags,
  100. int p_bake_fps, List<String> *r_missing_deps,
  101. Error *r_err) {
  102. Error err = FAILED;
  103. if (r_err) {
  104. *r_err = err;
  105. }
  106. Ref<GLTFDocument> gltf_document;
  107. gltf_document.instance();
  108. Ref<GLTFState> state;
  109. state.instance();
  110. err = gltf_document->serialize(state, p_node, p_path);
  111. if (r_err) {
  112. *r_err = err;
  113. }
  114. }
  115. void PackedSceneGLTF::_build_parent_hierachy(Ref<GLTFState> state) {
  116. // build the hierarchy
  117. for (GLTFNodeIndex node_i = 0; node_i < state->nodes.size(); node_i++) {
  118. for (int j = 0; j < state->nodes[node_i]->children.size(); j++) {
  119. GLTFNodeIndex child_i = state->nodes[node_i]->children[j];
  120. ERR_FAIL_INDEX(child_i, state->nodes.size());
  121. if (state->nodes.write[child_i]->parent != -1) {
  122. continue;
  123. }
  124. state->nodes.write[child_i]->parent = node_i;
  125. }
  126. }
  127. }
  128. Error PackedSceneGLTF::export_gltf(Node *p_root, String p_path,
  129. int32_t p_flags,
  130. real_t p_bake_fps) {
  131. ERR_FAIL_COND_V(!p_root, FAILED);
  132. List<String> deps;
  133. Error err;
  134. String path = p_path;
  135. int32_t flags = p_flags;
  136. real_t baked_fps = p_bake_fps;
  137. Ref<PackedSceneGLTF> exporter;
  138. exporter.instance();
  139. exporter->save_scene(p_root, path, "", flags, baked_fps, &deps, &err);
  140. int32_t error_code = err;
  141. if (error_code != 0) {
  142. return Error(error_code);
  143. }
  144. return OK;
  145. }
  146. #endif // TOOLS_ENABLED