Now the importer can handle files with multiple meshes in them.

This commit is contained in:
Relintai 2020-07-26 14:51:53 +02:00
parent 515a3b250f
commit 2c1508e616
8 changed files with 173 additions and 8 deletions

1
SCsub
View File

@ -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")

View File

@ -29,7 +29,6 @@ SOFTWARE.
#include "scene/resources/shape.h"
class MeshDataResource : public Resource {
GDCLASS(MeshDataResource, Resource);
RES_BASE_EXTENSION("mdres");

View File

@ -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<MeshDataResource> mdr) {
_mdrs.push_back(mdr);
}
Vector<Variant> MeshDataResourceCollection::get_mdrs() {
Vector<Variant> 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<Variant> &p_arrays) {
_mdrs.clear();
for (int i = 0; i < p_arrays.size(); ++i) {
Ref<MeshDataResource> e = Ref<MeshDataResource>(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");
}

View File

@ -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<MeshDataResource> mdr);
Vector<Variant> get_mdrs();
void set_mdrs(const Vector<Variant> &p_arrays);
MeshDataResourceCollection();
~MeshDataResourceCollection();
protected:
static void _bind_methods();
private:
Vector<Ref<MeshDataResource> > _mdrs;
};
#endif

View File

@ -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<ImportOption> *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<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
MDRImportPluginBase::MDRImportType type = static_cast<MDRImportPluginBase::MDRImportType>(static_cast<int>(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<MeshDataResourceCollection> 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<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *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<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata, Ref<MeshDataResourceCollection> coll) {
MeshDataResource::ColliderType collider_type = static_cast<MeshDataResource::ColliderType>(static_cast<int>(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<MeshDataResource> 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;

View File

@ -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<ImportOption> *r_options, int p_preset = 0) const;
virtual bool get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const;
Error process_node(Node *n, const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *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<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *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<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *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<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata);
Error process_node_multi(Node *n, const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata, Ref<MeshDataResourceCollection> coll);
Ref<MeshDataResource> get_mesh(MeshInstance *mi, const Map<StringName, Variant> &p_options, MeshDataResource::ColliderType collider_type, Vector3 scale);
Array apply_transforms(Array &array, const Map<StringName, Variant> &p_options);

View File

@ -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;

View File

@ -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<MeshDataResource>();
ClassDB::register_class<MeshDataResourceCollection>();
ClassDB::register_class<MeshDataInstance>();