From 2c1508e616c6ca8fd78bba2f9f733053ecbaec4a Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 26 Jul 2020 14:51:53 +0200 Subject: [PATCH] Now the importer can handle files with multiple meshes in them. --- SCsub | 1 + mesh_data_resource.h | 1 - mesh_data_resource_collection.cpp | 65 ++++++++++++++++++++++++++ mesh_data_resource_collection.h | 51 ++++++++++++++++++++ plugin/mdr_import_plugin_base.cpp | 42 +++++++++++++++-- plugin/mdr_import_plugin_base.h | 17 +++++-- plugin_gltf/editor_import_gltf_mdr.cpp | 2 +- register_types.cpp | 2 + 8 files changed, 173 insertions(+), 8 deletions(-) create mode 100644 mesh_data_resource_collection.cpp create mode 100644 mesh_data_resource_collection.h diff --git a/SCsub b/SCsub index a8b52ac..9da9832 100644 --- a/SCsub +++ b/SCsub @@ -13,6 +13,7 @@ if os.path.isdir('../props'): module_env.add_source_files(env.modules_sources,"register_types.cpp") module_env.add_source_files(env.modules_sources,"mesh_data_resource.cpp") +module_env.add_source_files(env.modules_sources,"mesh_data_resource_collection.cpp") module_env.add_source_files(env.modules_sources,"plugin/mdr_import_plugin_base.cpp") diff --git a/mesh_data_resource.h b/mesh_data_resource.h index 11a924c..3cf8ae2 100644 --- a/mesh_data_resource.h +++ b/mesh_data_resource.h @@ -29,7 +29,6 @@ SOFTWARE. #include "scene/resources/shape.h" class MeshDataResource : public Resource { - GDCLASS(MeshDataResource, Resource); RES_BASE_EXTENSION("mdres"); diff --git a/mesh_data_resource_collection.cpp b/mesh_data_resource_collection.cpp new file mode 100644 index 0000000..1b1889d --- /dev/null +++ b/mesh_data_resource_collection.cpp @@ -0,0 +1,65 @@ +/* +Copyright (c) 2019-2020 Péter Magyar + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "mesh_data_resource_collection.h" + +#include "core/version.h" + +void MeshDataResourceCollection::add_mdr(Ref mdr) { + _mdrs.push_back(mdr); +} + +Vector MeshDataResourceCollection::get_mdrs() { + Vector r; + for (int i = 0; i < _mdrs.size(); i++) { +#if VERSION_MAJOR < 4 + r.push_back(_mdrs[i].get_ref_ptr()); +#else + r.push_back(_mdrs[i]); +#endif + } + return r; +} +void MeshDataResourceCollection::set_mdrs(const Vector &p_arrays) { + _mdrs.clear(); + + for (int i = 0; i < p_arrays.size(); ++i) { + Ref e = Ref(p_arrays[i]); + + _mdrs.push_back(e); + } +} + +MeshDataResourceCollection::MeshDataResourceCollection() { +} + +MeshDataResourceCollection::~MeshDataResourceCollection() { + _mdrs.clear(); +} + +void MeshDataResourceCollection::_bind_methods() { + ClassDB::bind_method(D_METHOD("add_mdr", "mdr"), &MeshDataResourceCollection::add_mdr); + + ClassDB::bind_method(D_METHOD("get_mdrs"), &MeshDataResourceCollection::get_mdrs); + ClassDB::bind_method(D_METHOD("set_mdrs", "array"), &MeshDataResourceCollection::set_mdrs); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "mdrs"), "set_mdrs", "get_mdrs"); +} diff --git a/mesh_data_resource_collection.h b/mesh_data_resource_collection.h new file mode 100644 index 0000000..681aabd --- /dev/null +++ b/mesh_data_resource_collection.h @@ -0,0 +1,51 @@ +/* +Copyright (c) 2019-2020 Péter Magyar + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef MESH_DATA_REOURCE_COLLECTION_H +#define MESH_DATA_REOURCE_COLLECTION_H + +#include "core/resource.h" + +#include "core/vector.h" + +#include "mesh_data_resource.h" + +class MeshDataResourceCollection : public Resource { + GDCLASS(MeshDataResourceCollection, Resource); + +public: + void add_mdr(Ref mdr); + + Vector get_mdrs(); + void set_mdrs(const Vector &p_arrays); + + MeshDataResourceCollection(); + ~MeshDataResourceCollection(); + +protected: + static void _bind_methods(); + +private: + Vector > _mdrs; +}; + +#endif diff --git a/plugin/mdr_import_plugin_base.cpp b/plugin/mdr_import_plugin_base.cpp index 85389d8..f1b50a2 100644 --- a/plugin/mdr_import_plugin_base.cpp +++ b/plugin/mdr_import_plugin_base.cpp @@ -30,7 +30,10 @@ SOFTWARE. #include "scene/resources/shape.h" #include "scene/resources/sphere_shape.h" +const String MDRImportPluginBase::BINDING_MDR_IMPORT_TYPE = "Single,Single Merged,Multiple"; + void MDRImportPluginBase::get_import_options(List *r_options, int p_preset) const { + r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "import_type", PROPERTY_HINT_ENUM, BINDING_MDR_IMPORT_TYPE), MDRImportPluginBase::MDR_IMPORT_TIME_SINGLE)); r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "collider_type", PROPERTY_HINT_ENUM, MeshDataResource::BINDING_STRING_COLLIDER_TYPE), MeshDataResource::COLLIDER_TYPE_NONE)); r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "offset"), Vector3(0, 0, 0))); @@ -44,6 +47,29 @@ bool MDRImportPluginBase::get_option_visibility(const String &p_option, const Ma return true; } +Error MDRImportPluginBase::process_node(Node *n, const String &p_source_file, const String &p_save_path, const Map &p_options, List *r_platform_variants, List *r_gen_files, Variant *r_metadata) { + MDRImportPluginBase::MDRImportType type = static_cast(static_cast(p_options["import_type"])); + + switch (type) { + case MDR_IMPORT_TIME_SINGLE: { + return process_node_single(n, p_source_file, p_save_path, p_options, r_platform_variants, r_gen_files, r_metadata); + } + case MDR_IMPORT_TIME_SINGLE_MERGED: { + ERR_FAIL_V_MSG(Error::ERR_UNAVAILABLE, "import type Single Merged is not yet implemented! " + p_source_file); + } + case MDR_IMPORT_TIME_MULTIPLE: { + Ref coll; + coll.instance(); + + process_node_multi(n, p_source_file, p_save_path, p_options, r_platform_variants, r_gen_files, r_metadata, coll); + + return ResourceSaver::save(p_save_path + "." + get_save_extension(), coll); + } + } + + return Error::ERR_PARSE_ERROR; +} + int MDRImportPluginBase::get_mesh_count(Node *n) { int count = 0; @@ -79,12 +105,16 @@ Error MDRImportPluginBase::process_node_single(Node *n, const String &p_source_f return ResourceSaver::save(p_save_path + "." + get_save_extension(), mdr); } + + if (process_node_single(c, p_source_file, p_save_path, p_options, r_platform_variants, r_gen_files, r_metadata) == Error::OK) { + return Error::OK; + } } return Error::ERR_PARSE_ERROR; } -Error MDRImportPluginBase::process_node_multi(Node *n, const String &p_source_file, const String &p_save_path, const Map &p_options, List *r_platform_variants, List *r_gen_files, Variant *r_metadata) { +Error MDRImportPluginBase::process_node_multi(Node *n, const String &p_source_file, const String &p_save_path, const Map &p_options, List *r_platform_variants, List *r_gen_files, Variant *r_metadata, Ref coll) { MeshDataResource::ColliderType collider_type = static_cast(static_cast(p_options["collider_type"])); Vector3 scale = p_options["scale"]; @@ -103,14 +133,20 @@ Error MDRImportPluginBase::process_node_multi(Node *n, const String &p_source_fi node_name = node_name.to_lower(); - Error err = ResourceSaver::save(p_save_path + "_" + node_name + "." + get_save_extension(), mdr); + String filename = p_source_file.get_basename() + "_" + node_name + "." + get_save_extension(); + + Error err = ResourceSaver::save(filename, mdr); + + Ref mdrl = ResourceLoader::load(filename); + + coll->add_mdr(mdrl); if (err != Error::OK) { return err; } } - process_node_multi(c, p_source_file, p_save_path, p_options, r_platform_variants, r_gen_files, r_metadata); + process_node_multi(c, p_source_file, p_save_path, p_options, r_platform_variants, r_gen_files, r_metadata, coll); } return Error::OK; diff --git a/plugin/mdr_import_plugin_base.h b/plugin/mdr_import_plugin_base.h index c484b4d..739aa87 100644 --- a/plugin/mdr_import_plugin_base.h +++ b/plugin/mdr_import_plugin_base.h @@ -23,6 +23,7 @@ SOFTWARE. #ifndef MDR_IMPORT_PLUGIN_BASE #define MDR_IMPORT_PLUGIN_BASE +#include "../mesh_data_resource_collection.h" #include "core/array.h" #include "core/io/resource_saver.h" #include "core/math/basis.h" @@ -48,16 +49,26 @@ SOFTWARE. #endif class MDRImportPluginBase : public EditorImportPlugin { - GDCLASS(MDRImportPluginBase, EditorImportPlugin); +public: + static const String BINDING_MDR_IMPORT_TYPE; + + enum MDRImportType { + MDR_IMPORT_TIME_SINGLE = 0, + MDR_IMPORT_TIME_SINGLE_MERGED, + MDR_IMPORT_TIME_MULTIPLE, + }; + public: virtual void get_import_options(List *r_options, int p_preset = 0) const; virtual bool get_option_visibility(const String &p_option, const Map &p_options) const; + Error process_node(Node *n, const String &p_source_file, const String &p_save_path, const Map &p_options, List *r_platform_variants, List *r_gen_files = NULL, Variant *r_metadata = NULL); + int get_mesh_count(Node *n); - Error process_node_single(Node *n, const String &p_source_file, const String &p_save_path, const Map &p_options, List *r_platform_variants, List *r_gen_files = NULL, Variant *r_metadata = NULL); - Error process_node_multi(Node *n, const String &p_source_file, const String &p_save_path, const Map &p_options, List *r_platform_variants, List *r_gen_files = NULL, Variant *r_metadata = NULL); + Error process_node_single(Node *n, const String &p_source_file, const String &p_save_path, const Map &p_options, List *r_platform_variants, List *r_gen_files, Variant *r_metadata); + Error process_node_multi(Node *n, const String &p_source_file, const String &p_save_path, const Map &p_options, List *r_platform_variants, List *r_gen_files, Variant *r_metadata, Ref coll); Ref get_mesh(MeshInstance *mi, const Map &p_options, MeshDataResource::ColliderType collider_type, Vector3 scale); Array apply_transforms(Array &array, const Map &p_options); diff --git a/plugin_gltf/editor_import_gltf_mdr.cpp b/plugin_gltf/editor_import_gltf_mdr.cpp index 611951c..8f4bf97 100644 --- a/plugin_gltf/editor_import_gltf_mdr.cpp +++ b/plugin_gltf/editor_import_gltf_mdr.cpp @@ -70,7 +70,7 @@ Error EditorImportGLTFMdr::import(const String &p_source_file, const String &p_s ERR_FAIL_COND_V(!n, Error::ERR_PARSE_ERROR); - Error err = process_node_single(n, p_source_file, p_save_path, p_options, r_platform_variants, r_gen_files, r_metadata); + Error err = process_node(n, p_source_file, p_save_path, p_options, r_platform_variants, r_gen_files, r_metadata); n->queue_delete(); return err; diff --git a/register_types.cpp b/register_types.cpp index 716e704..3d9e190 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -23,6 +23,7 @@ SOFTWARE. #include "register_types.h" #include "mesh_data_resource.h" +#include "mesh_data_resource_collection.h" #include "nodes/mesh_data_instance.h" #ifdef TOOLS_ENABLED @@ -40,6 +41,7 @@ SOFTWARE. void register_mesh_data_resource_types() { ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class();