editor_scene_importer_gltf.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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-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 "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_3d.h"
  40. #include "scene/3d/camera_3d.h"
  41. #include "scene/3d/mesh_instance_3d.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,
  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, 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", "state"),
  71. &PackedSceneGLTF::pack_gltf, DEFVAL(0), DEFVAL(1000.0f), DEFVAL(Ref<GLTFState>()));
  72. ClassDB::bind_method(D_METHOD("import_gltf_scene", "path", "flags", "bake_fps", "state"),
  73. &PackedSceneGLTF::import_gltf_scene, DEFVAL(0), DEFVAL(1000.0f), DEFVAL(Ref<GLTFState>()));
  74. }
  75. Node *PackedSceneGLTF::import_gltf_scene(const String &p_path, uint32_t p_flags, float p_bake_fps, Ref<GLTFState> r_state) {
  76. Error err = FAILED;
  77. List<String> deps;
  78. return import_scene(p_path, p_flags, p_bake_fps, &deps, &err, r_state);
  79. }
  80. Node *PackedSceneGLTF::import_scene(const String &p_path, uint32_t p_flags,
  81. int p_bake_fps,
  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. Ref<GLTFDocument> gltf_document;
  91. gltf_document.instance();
  92. Error err = gltf_document->parse(r_state, p_path);
  93. *r_err = err;
  94. ERR_FAIL_COND_V(err != Error::OK, nullptr);
  95. Node3D *root = memnew(Node3D);
  96. for (int32_t root_i = 0; root_i < r_state->root_nodes.size(); root_i++) {
  97. gltf_document->_generate_scene_node(r_state, root, root, r_state->root_nodes[root_i]);
  98. }
  99. gltf_document->_process_mesh_instances(r_state, root);
  100. if (r_state->animations.size()) {
  101. AnimationPlayer *ap = memnew(AnimationPlayer);
  102. root->add_child(ap);
  103. ap->set_owner(root);
  104. for (int i = 0; i < r_state->animations.size(); i++) {
  105. gltf_document->_import_animation(r_state, ap, i, p_bake_fps);
  106. }
  107. }
  108. return cast_to<Node3D>(root);
  109. }
  110. void PackedSceneGLTF::pack_gltf(String p_path, int32_t p_flags,
  111. real_t p_bake_fps, Ref<GLTFState> r_state) {
  112. Error err = FAILED;
  113. List<String> deps;
  114. Node *root = import_scene(p_path, p_flags, p_bake_fps, &deps, &err, r_state);
  115. ERR_FAIL_COND(err != OK);
  116. pack(root);
  117. }
  118. void PackedSceneGLTF::save_scene(Node *p_node, const String &p_path,
  119. const String &p_src_path, uint32_t p_flags,
  120. int p_bake_fps, List<String> *r_missing_deps,
  121. Error *r_err) {
  122. Error err = FAILED;
  123. if (r_err) {
  124. *r_err = err;
  125. }
  126. Ref<GLTFDocument> gltf_document;
  127. gltf_document.instance();
  128. Ref<GLTFState> state;
  129. state.instance();
  130. err = gltf_document->serialize(state, p_node, p_path);
  131. if (r_err) {
  132. *r_err = err;
  133. }
  134. }
  135. void PackedSceneGLTF::_build_parent_hierachy(Ref<GLTFState> state) {
  136. // build the hierarchy
  137. for (GLTFNodeIndex node_i = 0; node_i < state->nodes.size(); node_i++) {
  138. for (int j = 0; j < state->nodes[node_i]->children.size(); j++) {
  139. GLTFNodeIndex child_i = state->nodes[node_i]->children[j];
  140. ERR_FAIL_INDEX(child_i, state->nodes.size());
  141. if (state->nodes.write[child_i]->parent != -1) {
  142. continue;
  143. }
  144. state->nodes.write[child_i]->parent = node_i;
  145. }
  146. }
  147. }
  148. Error PackedSceneGLTF::export_gltf(Node *p_root, String p_path,
  149. int32_t p_flags,
  150. real_t p_bake_fps) {
  151. ERR_FAIL_COND_V(!p_root, FAILED);
  152. List<String> deps;
  153. Error err;
  154. String path = p_path;
  155. int32_t flags = p_flags;
  156. real_t baked_fps = p_bake_fps;
  157. Ref<PackedSceneGLTF> exporter;
  158. exporter.instance();
  159. exporter->save_scene(p_root, path, "", flags, baked_fps, &deps, &err);
  160. int32_t error_code = err;
  161. if (error_code != 0) {
  162. return Error(error_code);
  163. }
  164. return OK;
  165. }