From a703e4492253de204a77537cb2f8179ae8f56073 Mon Sep 17 00:00:00 2001 From: Filippo Ghibellini Date: Sun, 24 May 2020 18:43:58 +0200 Subject: [PATCH] Make GLTF Scene Importer populate node metadata with GLTF extras This PR currently only generates metadata for Meshes, Cameras and Spatials. Other nodes can be added later. This feature can be enabled by a Scene Importer import option called "Meta". --- editor/import/editor_scene_importer_gltf.cpp | 18 ++++++++++++++++++ editor/import/editor_scene_importer_gltf.h | 4 ++++ editor/import/resource_importer_scene.cpp | 5 +++++ editor/import/resource_importer_scene.h | 2 +- 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/editor/import/editor_scene_importer_gltf.cpp b/editor/import/editor_scene_importer_gltf.cpp index 45bab4fa1dd..766a8e58b4a 100644 --- a/editor/import/editor_scene_importer_gltf.cpp +++ b/editor/import/editor_scene_importer_gltf.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "editor_scene_importer_gltf.h" +#include "core/core_string_names.h" #include "core/crypto/crypto_core.h" #include "core/io/json.h" #include "core/math/disjoint_set.h" @@ -273,6 +274,9 @@ Error EditorSceneImporterGLTF::_parse_nodes(GLTFState &state) { if (n.has("mesh")) { node->mesh = n["mesh"]; } + if (n.has("extras")) { + node->extras = (Dictionary)n["extras"]; + } if (n.has("skin")) { node->skin = n["skin"]; } @@ -2486,6 +2490,13 @@ BoneAttachment *EditorSceneImporterGLTF::_generate_bone_attachment(GLTFState &st return bone_attachment; } +void EditorSceneImporterGLTF::_set_meta_from_gltf_extras(GLTFState &state, Node *node, const GLTFNode *gltf_node) { + if (state.import_meta && !gltf_node->extras.empty()) { + const Dictionary &extras = gltf_node->extras; + node->set(CoreStringNames::get_singleton()->_meta, extras); + } +} + MeshInstance *EditorSceneImporterGLTF::_generate_mesh_instance(GLTFState &state, Node *scene_parent, const GLTFNodeIndex node_index) { const GLTFNode *gltf_node = state.nodes[node_index]; @@ -2497,6 +2508,8 @@ MeshInstance *EditorSceneImporterGLTF::_generate_mesh_instance(GLTFState &state, GLTFMesh &mesh = state.meshes.write[gltf_node->mesh]; mi->set_mesh(mesh.mesh); + _set_meta_from_gltf_extras(state, mi, gltf_node); + if (mesh.mesh->get_name() == "") { mesh.mesh->set_name(gltf_node->name); } @@ -2516,6 +2529,8 @@ Camera *EditorSceneImporterGLTF::_generate_camera(GLTFState &state, Node *scene_ Camera *camera = memnew(Camera); print_verbose("glTF: Creating camera for: " + gltf_node->name); + _set_meta_from_gltf_extras(state, camera, gltf_node); + const GLTFCamera &c = state.cameras[gltf_node->camera]; if (c.perspective) { camera->set_perspective(c.fov_size, c.znear, c.zfar); @@ -2532,6 +2547,8 @@ Spatial *EditorSceneImporterGLTF::_generate_spatial(GLTFState &state, Node *scen Spatial *spatial = memnew(Spatial); print_verbose("glTF: Creating spatial for: " + gltf_node->name); + _set_meta_from_gltf_extras(state, spatial, gltf_node); + return spatial; } @@ -2990,6 +3007,7 @@ Node *EditorSceneImporterGLTF::import_scene(const String &p_path, uint32_t p_fla state.major_version = version.get_slice(".", 0).to_int(); state.minor_version = version.get_slice(".", 1).to_int(); state.use_named_skin_binds = p_flags & IMPORT_USE_NAMED_SKIN_BINDS; + state.import_meta = p_flags & IMPORT_META; /* STEP 0 PARSE SCENE */ Error err = _parse_scenes(state); diff --git a/editor/import/editor_scene_importer_gltf.h b/editor/import/editor_scene_importer_gltf.h index 8e9c44db935..09fc0256ded 100644 --- a/editor/import/editor_scene_importer_gltf.h +++ b/editor/import/editor_scene_importer_gltf.h @@ -99,6 +99,7 @@ class EditorSceneImporterGLTF : public EditorSceneImporter { Transform xform; String name; + Dictionary extras; GLTFMeshIndex mesh; GLTFCameraIndex camera; @@ -300,6 +301,7 @@ class EditorSceneImporterGLTF : public EditorSceneImporter { Vector glb_data; bool use_named_skin_binds; + bool import_meta; Vector nodes; Vector > buffers; @@ -395,6 +397,8 @@ class EditorSceneImporterGLTF : public EditorSceneImporter { Error _parse_animations(GLTFState &state); + void _set_meta_from_gltf_extras(GLTFState &state, Node *node, const GLTFNode *gltf_node); + BoneAttachment *_generate_bone_attachment(GLTFState &state, Skeleton *skeleton, const GLTFNodeIndex node_index); MeshInstance *_generate_mesh_instance(GLTFState &state, Node *scene_parent, const GLTFNodeIndex node_index); Camera *_generate_camera(GLTFState &state, Node *scene_parent, const GLTFNodeIndex node_index); diff --git a/editor/import/resource_importer_scene.cpp b/editor/import/resource_importer_scene.cpp index 3ae69d8e444..4e4a7ead647 100644 --- a/editor/import/resource_importer_scene.cpp +++ b/editor/import/resource_importer_scene.cpp @@ -114,6 +114,7 @@ void EditorSceneImporter::_bind_methods() { BIND_CONSTANT(IMPORT_SCENE); BIND_CONSTANT(IMPORT_ANIMATION); + BIND_CONSTANT(IMPORT_META); BIND_CONSTANT(IMPORT_ANIMATION_DETECT_LOOP); BIND_CONSTANT(IMPORT_ANIMATION_OPTIMIZE); BIND_CONSTANT(IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS); @@ -1161,6 +1162,7 @@ void ResourceImporterScene::get_import_options(List *r_options, in bool animations_out = p_preset == PRESET_SEPARATE_ANIMATIONS || p_preset == PRESET_SEPARATE_MESHES_AND_ANIMATIONS || p_preset == PRESET_SEPARATE_MATERIALS_AND_ANIMATIONS || p_preset == PRESET_SEPARATE_MESHES_MATERIALS_AND_ANIMATIONS; r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "nodes/root_scale", PROPERTY_HINT_RANGE, "0.001,1000,0.001"), 1.0)); + r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "nodes/meta"), false)); r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "nodes/custom_script", PROPERTY_HINT_FILE, script_ext_hint), "")); r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "nodes/storage", PROPERTY_HINT_ENUM, "Single Scene,Instanced Sub-Scenes"), scenes_out ? 1 : 0)); r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "materials/location", PROPERTY_HINT_ENUM, "Node,Mesh"), (meshes_out || materials_out) ? 1 : 0)); @@ -1299,6 +1301,9 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p float fps = p_options["animation/fps"]; int import_flags = EditorSceneImporter::IMPORT_ANIMATION_DETECT_LOOP; + if (bool(p_options["nodes/meta"])) + import_flags |= EditorSceneImporter::IMPORT_META; + if (!bool(p_options["animation/optimizer/remove_unused_tracks"])) import_flags |= EditorSceneImporter::IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS; diff --git a/editor/import/resource_importer_scene.h b/editor/import/resource_importer_scene.h index 20e7af15b5d..d25130faceb 100644 --- a/editor/import/resource_importer_scene.h +++ b/editor/import/resource_importer_scene.h @@ -61,7 +61,7 @@ class EditorSceneImporter : public Reference { IMPORT_MATERIALS_IN_INSTANCES = 1024, IMPORT_USE_COMPRESSION = 2048, IMPORT_USE_NAMED_SKIN_BINDS = 4096, - + IMPORT_META = 8192, }; virtual uint32_t get_import_flags() const;