editor_scene_importer_gltf.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*************************************************************************/
  2. /* editor_scene_importer_gltf.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "core/crypto/crypto_core.h"
  31. #include "core/io/json.h"
  32. #include "core/math/disjoint_set.h"
  33. #include "core/math/math_defs.h"
  34. #include "core/os/file_access.h"
  35. #include "core/os/os.h"
  36. #include "editor/import/resource_importer_scene.h"
  37. #include "modules/gltf/gltf_state.h"
  38. #include "modules/regex/regex.h"
  39. #include "scene/3d/bone_attachment.h"
  40. #include "scene/3d/camera.h"
  41. #include "scene/3d/mesh_instance.h"
  42. #include "scene/animation/animation_player.h"
  43. #include "scene/resources/packed_scene.h"
  44. #include "scene/resources/surface_tool.h"
  45. #include "modules/gltf/editor_scene_importer_gltf.h"
  46. uint32_t EditorSceneImporterGLTF::get_import_flags() const {
  47. return ImportFlags::IMPORT_SCENE | ImportFlags::IMPORT_ANIMATION;
  48. }
  49. void EditorSceneImporterGLTF::get_extensions(List<String> *r_extensions) const {
  50. r_extensions->push_back("gltf");
  51. r_extensions->push_back("glb");
  52. }
  53. Node *EditorSceneImporterGLTF::import_scene(const String &p_path,
  54. uint32_t p_flags, int p_bake_fps, uint32_t p_compress_flags,
  55. List<String> *r_missing_deps,
  56. Error *r_err) {
  57. Ref<PackedSceneGLTF> importer;
  58. importer.instance();
  59. return importer->import_scene(p_path, p_flags, p_bake_fps, p_compress_flags, r_missing_deps, r_err, Ref<GLTFState>());
  60. }
  61. Ref<Animation> EditorSceneImporterGLTF::import_animation(const String &p_path,
  62. uint32_t p_flags,
  63. int p_bake_fps) {
  64. return Ref<Animation>();
  65. }
  66. void PackedSceneGLTF::_bind_methods() {
  67. ClassDB::bind_method(
  68. D_METHOD("export_gltf", "node", "path", "flags", "bake_fps"),
  69. &PackedSceneGLTF::export_gltf, DEFVAL(0), DEFVAL(1000.0f));
  70. ClassDB::bind_method(D_METHOD("pack_gltf", "path", "flags", "bake_fps", "compress_flags", "state"),
  71. &PackedSceneGLTF::pack_gltf, DEFVAL(0), DEFVAL(1000.0f), DEFVAL(Mesh::ARRAY_COMPRESS_DEFAULT), DEFVAL(Ref<GLTFState>()));
  72. ClassDB::bind_method(D_METHOD("import_gltf_scene", "path", "flags", "bake_fps", "compress_flags", "state"),
  73. &PackedSceneGLTF::import_gltf_scene, DEFVAL(0), DEFVAL(1000.0f), DEFVAL(Mesh::ARRAY_COMPRESS_DEFAULT), DEFVAL(Ref<GLTFState>()));
  74. }
  75. 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) {
  76. Error err = FAILED;
  77. List<String> deps;
  78. return import_scene(p_path, p_flags, p_bake_fps, p_compress_flags, &deps, &err, r_state);
  79. }
  80. Node *PackedSceneGLTF::import_scene(const String &p_path, uint32_t p_flags,
  81. int p_bake_fps, uint32_t p_compress_flags,
  82. List<String> *r_missing_deps,
  83. Error *r_err,
  84. Ref<GLTFState> r_state) {
  85. if (r_state == Ref<GLTFState>()) {
  86. r_state.instance();
  87. }
  88. r_state->use_named_skin_binds =
  89. p_flags & EditorSceneImporter::IMPORT_USE_NAMED_SKIN_BINDS;
  90. r_state->use_legacy_names =
  91. p_flags & EditorSceneImporter::IMPORT_USE_LEGACY_NAMES;
  92. r_state->compress_flags = p_compress_flags;
  93. Ref<GLTFDocument> gltf_document;
  94. gltf_document.instance();
  95. Error err = gltf_document->parse(r_state, p_path);
  96. *r_err = err;
  97. ERR_FAIL_COND_V(err != Error::OK, nullptr);
  98. Spatial *root = memnew(Spatial);
  99. if (r_state->use_legacy_names) {
  100. root->set_name(gltf_document->_legacy_validate_node_name(r_state->scene_name));
  101. } else {
  102. root->set_name(r_state->scene_name);
  103. }
  104. for (int32_t root_i = 0; root_i < r_state->root_nodes.size(); root_i++) {
  105. gltf_document->_generate_scene_node(r_state, root, root, r_state->root_nodes[root_i]);
  106. }
  107. gltf_document->_process_mesh_instances(r_state, root);
  108. if (r_state->animations.size()) {
  109. AnimationPlayer *ap = memnew(AnimationPlayer);
  110. root->add_child(ap);
  111. ap->set_owner(root);
  112. for (int i = 0; i < r_state->animations.size(); i++) {
  113. gltf_document->_import_animation(r_state, ap, i, p_bake_fps);
  114. }
  115. }
  116. return cast_to<Spatial>(root);
  117. }
  118. void PackedSceneGLTF::pack_gltf(String p_path, int32_t p_flags,
  119. real_t p_bake_fps, uint32_t p_compress_flags, Ref<GLTFState> r_state) {
  120. Error err = FAILED;
  121. List<String> deps;
  122. Node *root = import_scene(p_path, p_flags, p_bake_fps, p_compress_flags, &deps, &err, r_state);
  123. ERR_FAIL_COND(err != OK);
  124. pack(root);
  125. }
  126. void PackedSceneGLTF::save_scene(Node *p_node, const String &p_path,
  127. const String &p_src_path, uint32_t p_flags,
  128. int p_bake_fps, List<String> *r_missing_deps,
  129. Error *r_err) {
  130. Error err = FAILED;
  131. if (r_err) {
  132. *r_err = err;
  133. }
  134. Ref<GLTFDocument> gltf_document;
  135. gltf_document.instance();
  136. Ref<GLTFState> state;
  137. state.instance();
  138. err = gltf_document->serialize(state, p_node, p_path);
  139. if (r_err) {
  140. *r_err = err;
  141. }
  142. }
  143. void PackedSceneGLTF::_build_parent_hierachy(Ref<GLTFState> state) {
  144. // build the hierarchy
  145. for (GLTFNodeIndex node_i = 0; node_i < state->nodes.size(); node_i++) {
  146. for (int j = 0; j < state->nodes[node_i]->children.size(); j++) {
  147. GLTFNodeIndex child_i = state->nodes[node_i]->children[j];
  148. ERR_FAIL_INDEX(child_i, state->nodes.size());
  149. if (state->nodes.write[child_i]->parent != -1) {
  150. continue;
  151. }
  152. state->nodes.write[child_i]->parent = node_i;
  153. }
  154. }
  155. }
  156. Error PackedSceneGLTF::export_gltf(Node *p_root, String p_path,
  157. int32_t p_flags,
  158. real_t p_bake_fps) {
  159. ERR_FAIL_COND_V(!p_root, FAILED);
  160. List<String> deps;
  161. Error err;
  162. String path = p_path;
  163. int32_t flags = p_flags;
  164. real_t baked_fps = p_bake_fps;
  165. Ref<PackedSceneGLTF> exporter;
  166. exporter.instance();
  167. exporter->save_scene(p_root, path, "", flags, baked_fps, &deps, &err);
  168. int32_t error_code = err;
  169. if (error_code != 0) {
  170. return Error(error_code);
  171. }
  172. return OK;
  173. }