Added an importer for glft aswell.

This commit is contained in:
Relintai 2020-01-14 23:13:16 +01:00
parent 3fedb4c853
commit 7bef6db99e
10 changed files with 256 additions and 15 deletions

8
SCsub
View File

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

View File

@ -2,17 +2,17 @@
#ifndef EDITOR_IMPORT_COLLADA_MDR #ifndef EDITOR_IMPORT_COLLADA_MDR
#define 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/array.h"
#include "core/io/resource_saver.h"
#include "core/math/basis.h" #include "core/math/basis.h"
#include "core/math/transform.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" #include "editor/import/editor_import_collada.h"
class EditorImportColladaMdr : public EditorImportPlugin { class EditorImportColladaMdr : public EditorImportPlugin {

View File

@ -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<String> *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<ImportOption> *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<StringName, Variant> &p_options) const {
return true;
}
Error EditorImportGLTFMdr::import(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) {
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<MeshInstance>(c)) {
MeshInstance *mi = Object::cast_to<MeshInstance>(c);
Ref<ArrayMesh> mesh = mi->get_mesh();
if (mesh.is_valid()) {
Ref<MeshDataResource> 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<StringName, Variant> &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();
}

View File

@ -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<String> *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<ImportOption> *r_options, int p_preset = 0) const;
virtual bool get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const;
virtual Error import(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);
Array apply_transforms(Array &array, const Map<StringName, Variant> &p_options);
EditorImportGLTFMdr();
~EditorImportGLTFMdr();
private:
Ref<EditorSceneImporterGLTF> _importer;
};
#endif

View File

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

View File

@ -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<EditorImportGLTFMdr> _importer;
};
#endif

View File

@ -5,19 +5,20 @@
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
#include "editor/editor_plugin.h" #include "editor/editor_plugin.h"
#include "editor_plugin_collada_mdr.h" #include "plugin_collada/editor_plugin_collada_mdr.h"
#endif
#include "plugin_gltf/editor_plugin_gltf_mdr.h"
#endif
void register_mesh_data_resource_types() { void register_mesh_data_resource_types() {
ClassDB::register_class<MeshDataResource>(); ClassDB::register_class<MeshDataResource>();
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
EditorPlugins::add_by_type<EditorPluginColladaMdr>(); EditorPlugins::add_by_type<EditorPluginColladaMdr>();
#endif
EditorPlugins::add_by_type<EditorPluginGLTFMdr>();
#endif
} }
void unregister_mesh_data_resource_types() { void unregister_mesh_data_resource_types() {
} }