Moved the classes from ESS to this module.

This commit is contained in:
Relintai 2019-12-20 16:14:37 +01:00
parent b35afb14c6
commit 4cf72ef1e6
11 changed files with 335 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
.import
*.d
*.o
*.meta
*.obj
*.pyc
*.bc

9
SCsub Normal file
View File

@ -0,0 +1,9 @@
Import('env')
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")

11
config.py Normal file
View File

@ -0,0 +1,11 @@
def can_build(env, platform):
return True
def configure(env):
pass

View File

@ -0,0 +1,142 @@
#include "editor_import_collada_mdr.h"
String EditorImportColladaMdr::get_importer_name() const {
return "collada_mdr";
}
String EditorImportColladaMdr::get_visible_name() const {
return "Collada MDR";
}
void EditorImportColladaMdr::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->push_back("dae");
}
String EditorImportColladaMdr::get_save_extension() const {
return "res";
}
String EditorImportColladaMdr::get_resource_type() const {
return "MeshDataResource";
}
float EditorImportColladaMdr::get_priority() const {
return 1.0;
}
int EditorImportColladaMdr::get_preset_count() const {
return 0;
}
String EditorImportColladaMdr::get_preset_name(int p_idx) const {
return "";
}
void EditorImportColladaMdr::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 EditorImportColladaMdr::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
return true;
}
Error EditorImportColladaMdr::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 EditorImportColladaMdr::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;
}
EditorImportColladaMdr::EditorImportColladaMdr() {
_importer.instance();
}
EditorImportColladaMdr::~EditorImportColladaMdr() {
_importer.unref();
}

View File

@ -0,0 +1,47 @@
#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/math/basis.h"
#include "core/math/transform.h"
#include "mesh_data_resource.h"
#include "editor/import/editor_import_collada.h"
class EditorImportColladaMdr : public EditorImportPlugin {
GDCLASS(EditorImportColladaMdr, 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);
EditorImportColladaMdr();
~EditorImportColladaMdr();
private:
Ref<EditorSceneImporterCollada> _importer;
};
#endif

View File

@ -0,0 +1,22 @@
#include "editor_plugin_collada_mdr.h"
void EditorPluginColladaMdr::_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;
}
}
EditorPluginColladaMdr::EditorPluginColladaMdr(EditorNode *node) {
_node = node;
}

View File

@ -0,0 +1,25 @@
#ifndef EDITOR_PLUGIN_COLLADA_MDR
#define EDITOR_PLUGIN_COLLADA_MDR
#include "editor/editor_plugin.h"
#include "core/ustring.h"
#include "editor_import_collada_mdr.h"
class EditorPluginColladaMdr : public EditorPlugin {
GDCLASS(EditorPluginColladaMdr, EditorPlugin);
public:
EditorPluginColladaMdr(EditorNode *node);
protected:
void _notification(int p_what);
private:
EditorNode *_node;
Ref<EditorImportColladaMdr> _importer;
};
#endif

20
mesh_data_resource.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "mesh_data_resource.h"
Array MeshDataResource::get_array() {
return _arrays;
}
void MeshDataResource::set_array(const Array &p_arrays) {
_arrays.clear();
_arrays = p_arrays.duplicate(true);
}
MeshDataResource::MeshDataResource() {
}
void MeshDataResource::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_array"), &MeshDataResource::get_array);
ClassDB::bind_method(D_METHOD("set_array", "array"), &MeshDataResource::set_array);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "array"), "set_array", "get_array");
}

26
mesh_data_resource.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef MESH_DATA_REOURCE_H
#define MESH_DATA_REOURCE_H
#include "core/resource.h"
#include "core/array.h"
#include "scene/resources/mesh.h"
class MeshDataResource : public Resource {
GDCLASS(MeshDataResource, Resource);
RES_BASE_EXTENSION("mdres");
public:
Array get_array();
void set_array(const Array &p_arrays);
MeshDataResource();
protected:
static void _bind_methods();
private:
Array _arrays;
};
#endif

23
register_types.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "register_types.h"
#include "mesh_data_resource.h"
#ifdef TOOLS_ENABLED
#include "editor/editor_plugin.h"
#include "editor_plugin_collada_mdr.h"
#endif
void register_mesh_data_resource_types() {
ClassDB::register_class<MeshDataResource>();
#ifdef TOOLS_ENABLED
EditorPlugins::add_by_type<EditorPluginColladaMdr>();
#endif
}
void unregister_mesh_data_resource_types() {
}

3
register_types.h Normal file
View File

@ -0,0 +1,3 @@
void register_mesh_data_resource_types();
void unregister_mesh_data_resource_types();