diff --git a/SCsub b/SCsub index 8b56071..23c5904 100644 --- a/SCsub +++ b/SCsub @@ -5,5 +5,9 @@ module_env = env.Clone() 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,"editor_import_collada_mdr.cpp") -module_env.add_source_files(env.modules_sources,"editor_plugin_collada_mdr.cpp") + +module_env.add_source_files(env.modules_sources,"plugin_collada/editor_import_collada_mdr.cpp") +module_env.add_source_files(env.modules_sources,"plugin_collada/editor_plugin_collada_mdr.cpp") + +module_env.add_source_files(env.modules_sources,"plugin_gltf/editor_import_gltf_mdr.cpp") +module_env.add_source_files(env.modules_sources,"plugin_gltf/editor_plugin_gltf_mdr.cpp") diff --git a/editor_import_collada_mdr.cpp b/plugin_collada/editor_import_collada_mdr.cpp similarity index 100% rename from editor_import_collada_mdr.cpp rename to plugin_collada/editor_import_collada_mdr.cpp diff --git a/editor_import_collada_mdr.h b/plugin_collada/editor_import_collada_mdr.h similarity index 97% rename from editor_import_collada_mdr.h rename to plugin_collada/editor_import_collada_mdr.h index 86ab48f..24304b2 100644 --- a/editor_import_collada_mdr.h +++ b/plugin_collada/editor_import_collada_mdr.h @@ -2,17 +2,17 @@ #ifndef EDITOR_IMPORT_COLLADA_MDR #define EDITOR_IMPORT_COLLADA_MDR -#include "editor/import/editor_import_plugin.h" -#include "core/ustring.h" -#include "scene/main/node.h" -#include "scene/resources/mesh.h" -#include "scene/3d/mesh_instance.h" -#include "core/io/resource_saver.h" #include "core/array.h" +#include "core/io/resource_saver.h" #include "core/math/basis.h" #include "core/math/transform.h" +#include "core/ustring.h" +#include "editor/import/editor_import_plugin.h" +#include "scene/3d/mesh_instance.h" +#include "scene/main/node.h" +#include "scene/resources/mesh.h" -#include "mesh_data_resource.h" +#include "../mesh_data_resource.h" #include "editor/import/editor_import_collada.h" class EditorImportColladaMdr : public EditorImportPlugin { diff --git a/editor_plugin_collada_mdr.cpp b/plugin_collada/editor_plugin_collada_mdr.cpp similarity index 100% rename from editor_plugin_collada_mdr.cpp rename to plugin_collada/editor_plugin_collada_mdr.cpp diff --git a/editor_plugin_collada_mdr.h b/plugin_collada/editor_plugin_collada_mdr.h similarity index 100% rename from editor_plugin_collada_mdr.h rename to plugin_collada/editor_plugin_collada_mdr.h diff --git a/plugin_gltf/editor_import_gltf_mdr.cpp b/plugin_gltf/editor_import_gltf_mdr.cpp new file mode 100644 index 0000000..e1dae12 --- /dev/null +++ b/plugin_gltf/editor_import_gltf_mdr.cpp @@ -0,0 +1,142 @@ +#include "editor_import_gltf_mdr.h" + +String EditorImportGLTFMdr::get_importer_name() const { + return "gltf_mdr"; +} + +String EditorImportGLTFMdr::get_visible_name() const { + return "GLTF MDR"; +} + +void EditorImportGLTFMdr::get_recognized_extensions(List *p_extensions) const { + p_extensions->push_back("gltf"); + p_extensions->push_back("glb"); +} + +String EditorImportGLTFMdr::get_save_extension() const { + return "res"; +} + +String EditorImportGLTFMdr::get_resource_type() const { + return "MeshDataResource"; +} + +float EditorImportGLTFMdr::get_priority() const { + return 1.0; +} + +int EditorImportGLTFMdr::get_preset_count() const { + return 0; +} + +String EditorImportGLTFMdr::get_preset_name(int p_idx) const { + return ""; +} + +void EditorImportGLTFMdr::get_import_options(List *r_options, int p_preset) const { + r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "offset"), Vector3(0, 0, 0))); + r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "rotation"), Vector3(0, 0, -(3.141592 / 2.0)))); + r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "scale"), Vector3(0.011, 0.011, 0.011))); +} + +bool EditorImportGLTFMdr::get_option_visibility(const String &p_option, const Map &p_options) const { + return true; +} + +Error EditorImportGLTFMdr::import(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) { + Node *n = _importer->import_scene(p_source_file, 0, 15); + + if (n == NULL) { + n->queue_delete(); + return Error::ERR_PARSE_ERROR; + } + + for (int i = 0; i < n->get_child_count(); ++i) { + Node *c = n->get_child(i); + print_error(String::num(i)); + + if (c == NULL) { + continue; + } + + if (Object::cast_to(c)) { + MeshInstance *mi = Object::cast_to(c); + + Ref mesh = mi->get_mesh(); + + if (mesh.is_valid()) { + Ref mdr; + mdr.instance(); + + Array arrays = mesh->surface_get_arrays(0); + + mdr->set_array(apply_transforms(arrays, p_options)); + + n->queue_delete(); + + return ResourceSaver::save(p_save_path + "." + get_save_extension(), mdr); + } + } + } + + n->queue_delete(); + return Error::ERR_PARSE_ERROR; +} + +Array EditorImportGLTFMdr::apply_transforms(Array &array, const Map &p_options) { + Vector3 offset = p_options["offset"]; + Vector3 rotation = p_options["rotation"]; + Vector3 scale = p_options["scale"]; + + Transform transform = Transform(Basis(rotation).scaled(scale), offset); + + Array verts = array.get(Mesh::ARRAY_VERTEX); + + for (int i = 0; i < verts.size(); ++i) { + Vector3 vert = verts[i]; + + vert = transform.xform(vert); + + verts.set(i, (vert)); + } + + Array normals = array.get(Mesh::ARRAY_NORMAL); + + for (int i = 0; i < normals.size(); ++i) { + Vector3 normal = normals[i]; + + normal = transform.basis.xform(normal); + + normals.set(i, normal); + } + + Array tangents = array.get(Mesh::ARRAY_TANGENT); + + if (tangents.size() == verts.size() * 4) { + + for (int i = 0; i < verts.size(); ++i) { + + Plane p(tangents[i * 4 + 0], tangents[i * 4 + 1], tangents[i * 4 + 2], tangents[i * 4 + 3]); + + Vector3 tangent = p.normal; + + tangent = transform.basis.xform(tangent); + + tangents.set(i, tangent); + } + } + + array.set(Mesh::ARRAY_VERTEX, verts); + array.set(Mesh::ARRAY_NORMAL, normals); + array.set(Mesh::ARRAY_TANGENT, tangents); + + return array; +} + +EditorImportGLTFMdr::EditorImportGLTFMdr() { + _importer.instance(); +} + +EditorImportGLTFMdr::~EditorImportGLTFMdr() { + _importer.unref(); +} diff --git a/plugin_gltf/editor_import_gltf_mdr.h b/plugin_gltf/editor_import_gltf_mdr.h new file mode 100644 index 0000000..4e78df2 --- /dev/null +++ b/plugin_gltf/editor_import_gltf_mdr.h @@ -0,0 +1,47 @@ + +#ifndef EDITOR_IMPORT_GLTF_MDR +#define EDITOR_IMPORT_GLTF_MDR + +#include "core/array.h" +#include "core/io/resource_saver.h" +#include "core/math/basis.h" +#include "core/math/transform.h" +#include "core/ustring.h" +#include "editor/import/editor_import_plugin.h" +#include "scene/3d/mesh_instance.h" +#include "scene/main/node.h" +#include "scene/resources/mesh.h" + +#include "../mesh_data_resource.h" +#include "editor/import/editor_scene_importer_gltf.h" + +class EditorImportGLTFMdr : public EditorImportPlugin { + + GDCLASS(EditorImportGLTFMdr, EditorImportPlugin); + +public: + virtual String get_importer_name() const; + virtual String get_visible_name() const; + virtual void get_recognized_extensions(List *p_extensions) const; + virtual String get_save_extension() const; + virtual String get_resource_type() const; + virtual float get_priority() const; + + virtual int get_preset_count() const; + virtual String get_preset_name(int p_idx) const; + + 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; + + virtual Error import(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); + + Array apply_transforms(Array &array, const Map &p_options); + + EditorImportGLTFMdr(); + ~EditorImportGLTFMdr(); + +private: + Ref _importer; +}; + +#endif diff --git a/plugin_gltf/editor_plugin_gltf_mdr.cpp b/plugin_gltf/editor_plugin_gltf_mdr.cpp new file mode 100644 index 0000000..a76424a --- /dev/null +++ b/plugin_gltf/editor_plugin_gltf_mdr.cpp @@ -0,0 +1,22 @@ +#include "editor_plugin_gltf_mdr.h" + +void EditorPluginGLTFMdr::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_ENTER_TREE: + _importer.instance(); + + add_import_plugin(_importer); + + break; + case NOTIFICATION_EXIT_TREE: + remove_import_plugin(_importer); + + _importer.unref(); + + break; + } +} + +EditorPluginGLTFMdr::EditorPluginGLTFMdr(EditorNode *node) { + _node = node; +} diff --git a/plugin_gltf/editor_plugin_gltf_mdr.h b/plugin_gltf/editor_plugin_gltf_mdr.h new file mode 100644 index 0000000..2c9878e --- /dev/null +++ b/plugin_gltf/editor_plugin_gltf_mdr.h @@ -0,0 +1,25 @@ + +#ifndef EDITOR_PLUGIN_GLTF_MDR +#define EDITOR_PLUGIN_GLTF_MDR + +#include "core/ustring.h" +#include "editor/editor_plugin.h" + +#include "editor_import_gltf_mdr.h" + +class EditorPluginGLTFMdr : public EditorPlugin { + + GDCLASS(EditorPluginGLTFMdr, EditorPlugin); + +public: + EditorPluginGLTFMdr(EditorNode *node); + +protected: + void _notification(int p_what); + +private: + EditorNode *_node; + Ref _importer; +}; + +#endif diff --git a/register_types.cpp b/register_types.cpp index 65a0d4d..916cf68 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -5,19 +5,20 @@ #ifdef TOOLS_ENABLED #include "editor/editor_plugin.h" -#include "editor_plugin_collada_mdr.h" -#endif +#include "plugin_collada/editor_plugin_collada_mdr.h" +#include "plugin_gltf/editor_plugin_gltf_mdr.h" +#endif void register_mesh_data_resource_types() { ClassDB::register_class(); - #ifdef TOOLS_ENABLED +#ifdef TOOLS_ENABLED EditorPlugins::add_by_type(); - #endif + + EditorPlugins::add_by_type(); +#endif } void unregister_mesh_data_resource_types() { - } -