diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-04-27 11:07:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-27 11:07:22 +0200 |
commit | 523a7ecef31d93f0f794e06aa5aa84cf3f53901c (patch) | |
tree | 3164399660394c2dfaf7d153d0479bee7884e184 | |
parent | 2890ac71002d173262eb863b8c4e2d04dec60c04 (diff) | |
parent | 44b6ee001e1af8a63671c9f381e04f7964f7f174 (diff) |
Merge pull request #60481 from V-Sekai/animation-library-gltf
-rw-r--r-- | modules/gltf/gltf_document.cpp | 68 | ||||
-rw-r--r-- | modules/gltf/gltf_state.cpp | 8 | ||||
-rw-r--r-- | modules/gltf/gltf_state.h | 4 |
3 files changed, 50 insertions, 30 deletions
diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp index 784733aba2..281f62c4ad 100644 --- a/modules/gltf/gltf_document.cpp +++ b/modules/gltf/gltf_document.cpp @@ -65,6 +65,7 @@ #include "scene/3d/node_3d.h" #include "scene/animation/animation_player.h" #include "scene/resources/importer_mesh.h" +#include "scene/resources/material.h" #include "scene/resources/mesh.h" #include "scene/resources/multimesh.h" #include "scene/resources/surface_tool.h" @@ -80,6 +81,7 @@ // FIXME: Hardcoded to avoid editor dependency. #define GLTF_IMPORT_USE_NAMED_SKIN_BINDS 16 +#define GLTF_IMPORT_DISCARD_MESHES_AND_MATERIALS 32 #include <stdio.h> #include <stdlib.h> @@ -2918,30 +2920,32 @@ Error GLTFDocument::_parse_meshes(Ref<GLTFState> state) { } } - //just add it - Ref<BaseMaterial3D> mat; - if (p.has("material")) { - const int material = p["material"]; - ERR_FAIL_INDEX_V(material, state->materials.size(), ERR_FILE_CORRUPT); - Ref<BaseMaterial3D> mat3d = state->materials[material]; - ERR_FAIL_NULL_V(mat3d, ERR_FILE_CORRUPT); - if (has_vertex_color) { - mat3d->set_flag(BaseMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); - } - mat = mat3d; + String mat_name; + if (!state->discard_meshes_and_materials) { + if (p.has("material")) { + const int material = p["material"]; + ERR_FAIL_INDEX_V(material, state->materials.size(), ERR_FILE_CORRUPT); + Ref<BaseMaterial3D> mat3d = state->materials[material]; + ERR_FAIL_NULL_V(mat3d, ERR_FILE_CORRUPT); + if (has_vertex_color) { + mat3d->set_flag(BaseMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); + } + mat = mat3d; - } else { - Ref<StandardMaterial3D> mat3d; - mat3d.instantiate(); - if (has_vertex_color) { - mat3d->set_flag(BaseMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); + } else { + Ref<StandardMaterial3D> mat3d; + mat3d.instantiate(); + if (has_vertex_color) { + mat3d->set_flag(BaseMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); + } + mat = mat3d; } - mat = mat3d; + ERR_FAIL_NULL_V(mat, ERR_FILE_CORRUPT); + mat_name = mat->get_name(); } - ERR_FAIL_NULL_V(mat, ERR_FILE_CORRUPT); import_mesh->add_surface(primitive, array, morphs, - Dictionary(), mat, mat->get_name(), flags); + Dictionary(), mat, mat_name, flags); } Vector<float> blend_weights; @@ -6908,8 +6912,8 @@ Node *GLTFDocument::generate_scene(Ref<GLTFState> state, int32_t p_bake_fps) { Error GLTFDocument::append_from_scene(Node *p_node, Ref<GLTFState> state, uint32_t p_flags, int32_t p_bake_fps) { ERR_FAIL_COND_V(state.is_null(), FAILED); - state->use_named_skin_binds = - p_flags & GLTF_IMPORT_USE_NAMED_SKIN_BINDS; + state->use_named_skin_binds = p_flags & GLTF_IMPORT_USE_NAMED_SKIN_BINDS; + state->discard_meshes_and_materials = p_flags & GLTF_IMPORT_DISCARD_MESHES_AND_MATERIALS; _convert_scene_node(state, p_node, -1, -1); if (!state->buffers.size()) { @@ -6929,6 +6933,7 @@ Error GLTFDocument::append_from_buffer(PackedByteArray p_bytes, String p_base_pa // TODO Add missing texture and missing .bin file paths to r_missing_deps 2021-09-10 fire Error err = FAILED; state->use_named_skin_binds = p_flags & GLTF_IMPORT_USE_NAMED_SKIN_BINDS; + state->discard_meshes_and_materials = p_flags & GLTF_IMPORT_DISCARD_MESHES_AND_MATERIALS; Ref<FileAccessMemory> file_access; file_access.instantiate(); @@ -6968,20 +6973,22 @@ Error GLTFDocument::_parse_gltf_state(Ref<GLTFState> state, const String &p_sear ERR_FAIL_COND_V(err != OK, ERR_PARSE_ERROR); - /* PARSE IMAGES */ - err = _parse_images(state, p_search_path); + if (!state->discard_meshes_and_materials) { + /* PARSE IMAGES */ + err = _parse_images(state, p_search_path); - ERR_FAIL_COND_V(err != OK, ERR_PARSE_ERROR); + ERR_FAIL_COND_V(err != OK, ERR_PARSE_ERROR); - /* PARSE TEXTURES */ - err = _parse_textures(state); + /* PARSE TEXTURES */ + err = _parse_textures(state); - ERR_FAIL_COND_V(err != OK, ERR_PARSE_ERROR); + ERR_FAIL_COND_V(err != OK, ERR_PARSE_ERROR); - /* PARSE TEXTURES */ - err = _parse_materials(state); + /* PARSE TEXTURES */ + err = _parse_materials(state); - ERR_FAIL_COND_V(err != OK, ERR_PARSE_ERROR); + ERR_FAIL_COND_V(err != OK, ERR_PARSE_ERROR); + } /* PARSE SKINS */ err = _parse_skins(state); @@ -7033,6 +7040,7 @@ Error GLTFDocument::append_from_file(String p_path, Ref<GLTFState> r_state, uint } r_state->filename = p_path.get_file().get_basename(); r_state->use_named_skin_binds = p_flags & GLTF_IMPORT_USE_NAMED_SKIN_BINDS; + r_state->discard_meshes_and_materials = p_flags & GLTF_IMPORT_DISCARD_MESHES_AND_MATERIALS; Error err; Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err); ERR_FAIL_COND_V(err != OK, ERR_FILE_CANT_OPEN); diff --git a/modules/gltf/gltf_state.cpp b/modules/gltf/gltf_state.cpp index 3f638bbca5..6ead2f69c3 100644 --- a/modules/gltf/gltf_state.cpp +++ b/modules/gltf/gltf_state.cpp @@ -305,3 +305,11 @@ AnimationPlayer *GLTFState::get_animation_player(int idx) { ERR_FAIL_INDEX_V(idx, animation_players.size(), nullptr); return animation_players[idx]; } + +void GLTFState::set_discard_meshes_and_materials(bool p_discard_meshes_and_materials) { + discard_meshes_and_materials = p_discard_meshes_and_materials; +} + +bool GLTFState::get_discard_meshes_and_materials() { + return discard_meshes_and_materials; +} diff --git a/modules/gltf/gltf_state.h b/modules/gltf/gltf_state.h index d03434d2f1..42ca079f1c 100644 --- a/modules/gltf/gltf_state.h +++ b/modules/gltf/gltf_state.h @@ -62,6 +62,7 @@ class GLTFState : public Resource { Vector<uint8_t> glb_data; bool use_named_skin_binds = false; + bool discard_meshes_and_materials = false; Vector<Ref<GLTFNode>> nodes; Vector<Vector<uint8_t>> buffers; @@ -112,6 +113,9 @@ public: bool get_use_named_skin_binds(); void set_use_named_skin_binds(bool p_use_named_skin_binds); + bool get_discard_meshes_and_materials(); + void set_discard_meshes_and_materials(bool p_discard_meshes_and_materials); + Array get_nodes(); void set_nodes(Array p_nodes); |