mirror of
https://github.com/Relintai/mesh_data_resource.git
synced 2024-11-12 10:15:21 +01:00
Moved most of the code from the gltf importer to a new base class, and made it more modular.
This commit is contained in:
parent
85417ebee9
commit
515a3b250f
2
SCsub
2
SCsub
@ -14,6 +14,8 @@ 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,"plugin/mdr_import_plugin_base.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")
|
||||
|
||||
|
379
plugin/mdr_import_plugin_base.cpp
Normal file
379
plugin/mdr_import_plugin_base.cpp
Normal file
@ -0,0 +1,379 @@
|
||||
/*
|
||||
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 "mdr_import_plugin_base.h"
|
||||
|
||||
#include "scene/resources/box_shape.h"
|
||||
#include "scene/resources/capsule_shape.h"
|
||||
#include "scene/resources/concave_polygon_shape.h"
|
||||
#include "scene/resources/convex_polygon_shape.h"
|
||||
#include "scene/resources/cylinder_shape.h"
|
||||
#include "scene/resources/shape.h"
|
||||
#include "scene/resources/sphere_shape.h"
|
||||
|
||||
void MDRImportPluginBase::get_import_options(List<ImportOption> *r_options, int p_preset) const {
|
||||
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)));
|
||||
//Todo this should be changed to 0, 0, 0
|
||||
r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "rotation"), Vector3(0, 0, -(3.141592 / 2.0))));
|
||||
//Todo this should be changed to 1, 1, 1
|
||||
r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "scale"), Vector3(0.011, 0.011, 0.011)));
|
||||
}
|
||||
|
||||
bool MDRImportPluginBase::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
int MDRImportPluginBase::get_mesh_count(Node *n) {
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; i < n->get_child_count(); ++i) {
|
||||
Node *c = n->get_child(i);
|
||||
|
||||
if (Object::cast_to<MeshInstance>(c)) {
|
||||
++count;
|
||||
}
|
||||
|
||||
count += get_mesh_count(c);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
Error MDRImportPluginBase::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) {
|
||||
MeshDataResource::ColliderType collider_type = static_cast<MeshDataResource::ColliderType>(static_cast<int>(p_options["collider_type"]));
|
||||
|
||||
Vector3 scale = p_options["scale"];
|
||||
|
||||
ERR_FAIL_COND_V(n == NULL, Error::ERR_PARSE_ERROR);
|
||||
|
||||
for (int i = 0; i < n->get_child_count(); ++i) {
|
||||
Node *c = n->get_child(i);
|
||||
|
||||
if (Object::cast_to<MeshInstance>(c)) {
|
||||
MeshInstance *mi = Object::cast_to<MeshInstance>(c);
|
||||
|
||||
Ref<MeshDataResource> mdr = get_mesh(mi, p_options, collider_type, scale);
|
||||
|
||||
ERR_FAIL_COND_V(!mdr.is_valid(), Error::ERR_PARSE_ERROR);
|
||||
|
||||
return ResourceSaver::save(p_save_path + "." + get_save_extension(), mdr);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
MeshDataResource::ColliderType collider_type = static_cast<MeshDataResource::ColliderType>(static_cast<int>(p_options["collider_type"]));
|
||||
|
||||
Vector3 scale = p_options["scale"];
|
||||
|
||||
ERR_FAIL_COND_V(n == NULL, Error::ERR_PARSE_ERROR);
|
||||
|
||||
for (int i = 0; i < n->get_child_count(); ++i) {
|
||||
Node *c = n->get_child(i);
|
||||
|
||||
if (Object::cast_to<MeshInstance>(c)) {
|
||||
MeshInstance *mi = Object::cast_to<MeshInstance>(c);
|
||||
|
||||
Ref<MeshDataResource> mdr = get_mesh(mi, p_options, collider_type, scale);
|
||||
|
||||
String node_name = n->get_name();
|
||||
|
||||
node_name = node_name.to_lower();
|
||||
|
||||
Error err = ResourceSaver::save(p_save_path + "_" + node_name + "." + get_save_extension(), mdr);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return Error::OK;
|
||||
}
|
||||
|
||||
Ref<MeshDataResource> MDRImportPluginBase::get_mesh(MeshInstance *mi, const Map<StringName, Variant> &p_options, MeshDataResource::ColliderType collider_type, Vector3 scale) {
|
||||
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));
|
||||
|
||||
if (collider_type == MeshDataResource::COLLIDER_TYPE_TRIMESH_COLLISION_SHAPE) {
|
||||
Ref<ArrayMesh> m;
|
||||
m.instance();
|
||||
m->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mdr->get_array());
|
||||
|
||||
Ref<Shape> shape = m->create_trimesh_shape();
|
||||
|
||||
if (!shape.is_null()) {
|
||||
mdr->add_collision_shape(Transform(), scale_shape(shape, scale));
|
||||
}
|
||||
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_SINGLE_CONVEX_COLLISION_SHAPE) {
|
||||
Ref<ArrayMesh> m;
|
||||
m.instance();
|
||||
m->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mdr->get_array());
|
||||
|
||||
Ref<Shape> shape = mesh->create_convex_shape();
|
||||
|
||||
if (!shape.is_null()) {
|
||||
mdr->add_collision_shape(Transform(), scale_shape(shape, scale));
|
||||
}
|
||||
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_MULTIPLE_CONVEX_COLLISION_SHAPES) {
|
||||
Ref<ArrayMesh> m;
|
||||
m.instance();
|
||||
m->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mdr->get_array());
|
||||
|
||||
Vector<Ref<Shape> > shapes = mesh->convex_decompose();
|
||||
|
||||
for (int j = 0; j < shapes.size(); ++j) {
|
||||
scale_shape(shapes[j], scale);
|
||||
}
|
||||
|
||||
for (int j = 0; j < shapes.size(); ++j) {
|
||||
mdr->add_collision_shape(Transform(), shapes[j]);
|
||||
}
|
||||
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_BOX) {
|
||||
Ref<ArrayMesh> m;
|
||||
m.instance();
|
||||
m->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mdr->get_array());
|
||||
|
||||
Ref<BoxShape> shape;
|
||||
shape.instance();
|
||||
|
||||
AABB aabb = m->get_aabb();
|
||||
Vector3 size = aabb.get_size();
|
||||
|
||||
shape->set_extents(size * 0.5);
|
||||
|
||||
Vector3 pos = aabb.position;
|
||||
pos += size / 2.0;
|
||||
|
||||
Transform t;
|
||||
t.origin = pos;
|
||||
|
||||
mdr->add_collision_shape(t, shape);
|
||||
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_CAPSULE) {
|
||||
Ref<ArrayMesh> m;
|
||||
m.instance();
|
||||
m->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mdr->get_array());
|
||||
|
||||
Ref<CapsuleShape> shape;
|
||||
shape.instance();
|
||||
|
||||
AABB aabb = m->get_aabb();
|
||||
Vector3 size = aabb.get_size();
|
||||
|
||||
shape->set_height(size.y * 0.5);
|
||||
shape->set_radius(MIN(size.x, size.z) * 0.5);
|
||||
|
||||
Vector3 pos = aabb.position;
|
||||
pos += size / 2.0;
|
||||
|
||||
Transform t = Transform(Basis().rotated(Vector3(1, 0, 0), M_PI_2));
|
||||
t.origin = pos;
|
||||
|
||||
mdr->add_collision_shape(t, shape);
|
||||
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_CYLINDER) {
|
||||
Ref<ArrayMesh> m;
|
||||
m.instance();
|
||||
m->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mdr->get_array());
|
||||
|
||||
Ref<CylinderShape> shape;
|
||||
shape.instance();
|
||||
|
||||
AABB aabb = m->get_aabb();
|
||||
Vector3 size = aabb.get_size();
|
||||
|
||||
shape->set_height(size.y * 0.5);
|
||||
shape->set_radius(MIN(size.x, size.z) * 0.5);
|
||||
|
||||
Vector3 pos = aabb.position;
|
||||
pos += size / 2.0;
|
||||
|
||||
Transform t;
|
||||
t.origin = pos;
|
||||
|
||||
mdr->add_collision_shape(t, shape);
|
||||
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_SPHERE) {
|
||||
Ref<ArrayMesh> m;
|
||||
m.instance();
|
||||
m->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mdr->get_array());
|
||||
|
||||
Ref<SphereShape> shape;
|
||||
shape.instance();
|
||||
|
||||
AABB aabb = m->get_aabb();
|
||||
Vector3 size = aabb.get_size();
|
||||
|
||||
shape->set_radius(MIN(size.x, MIN(size.y, size.z)) * 0.5);
|
||||
|
||||
Vector3 mid = aabb.get_size() / 2.0;
|
||||
|
||||
Transform t;
|
||||
t.origin = aabb.position + mid;
|
||||
|
||||
mdr->add_collision_shape(t, shape);
|
||||
}
|
||||
|
||||
return mdr;
|
||||
}
|
||||
|
||||
return Ref<MeshDataResource>();
|
||||
}
|
||||
|
||||
Array MDRImportPluginBase::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);
|
||||
|
||||
PoolVector3Array 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));
|
||||
}
|
||||
|
||||
PoolVector3Array 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;
|
||||
}
|
||||
|
||||
Ref<Shape> MDRImportPluginBase::scale_shape(Ref<Shape> shape, const Vector3 &scale) {
|
||||
|
||||
if (shape.is_null())
|
||||
return shape;
|
||||
|
||||
if (Object::cast_to<SphereShape>(*shape)) {
|
||||
|
||||
Ref<SphereShape> ss = shape;
|
||||
|
||||
ss->set_radius(ss->get_radius() * MAX(scale.x, MAX(scale.y, scale.z)));
|
||||
}
|
||||
|
||||
if (Object::cast_to<BoxShape>(*shape)) {
|
||||
|
||||
Ref<BoxShape> bs = shape;
|
||||
|
||||
bs->set_extents(bs->get_extents() * scale);
|
||||
}
|
||||
|
||||
if (Object::cast_to<CapsuleShape>(*shape)) {
|
||||
|
||||
Ref<CapsuleShape> cs = shape;
|
||||
|
||||
float sc = MAX(scale.x, MAX(scale.y, scale.z));
|
||||
|
||||
cs->set_radius(cs->get_radius() * sc);
|
||||
cs->set_height(cs->get_height() * sc);
|
||||
}
|
||||
|
||||
if (Object::cast_to<CylinderShape>(*shape)) {
|
||||
|
||||
Ref<CylinderShape> cs = shape;
|
||||
|
||||
float sc = MAX(scale.x, MAX(scale.y, scale.z));
|
||||
|
||||
cs->set_radius(cs->get_radius() * sc);
|
||||
cs->set_height(cs->get_height() * sc);
|
||||
}
|
||||
|
||||
if (Object::cast_to<ConcavePolygonShape>(*shape)) {
|
||||
|
||||
Ref<ConcavePolygonShape> cps = shape;
|
||||
|
||||
PoolVector3Array arr = cps->get_faces();
|
||||
|
||||
Basis b = Basis().scaled(scale);
|
||||
|
||||
for (int i = 0; i < arr.size(); ++i) {
|
||||
arr.set(i, b.xform(arr[i]));
|
||||
}
|
||||
|
||||
cps->set_faces(arr);
|
||||
}
|
||||
|
||||
if (Object::cast_to<ConvexPolygonShape>(*shape)) {
|
||||
|
||||
Ref<ConvexPolygonShape> cps = shape;
|
||||
|
||||
PoolVector3Array arr = cps->get_points();
|
||||
|
||||
Basis b = Basis().scaled(scale);
|
||||
|
||||
for (int i = 0; i < arr.size(); ++i) {
|
||||
arr.set(i, b.xform(arr[i]));
|
||||
}
|
||||
|
||||
cps->set_points(arr);
|
||||
}
|
||||
|
||||
return shape;
|
||||
}
|
||||
|
||||
MDRImportPluginBase::MDRImportPluginBase() {
|
||||
}
|
||||
|
||||
MDRImportPluginBase::~MDRImportPluginBase() {
|
||||
}
|
70
plugin/mdr_import_plugin_base.h
Normal file
70
plugin/mdr_import_plugin_base.h
Normal file
@ -0,0 +1,70 @@
|
||||
/*
|
||||
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 MDR_IMPORT_PLUGIN_BASE
|
||||
#define MDR_IMPORT_PLUGIN_BASE
|
||||
|
||||
#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/main/node.h"
|
||||
#include "scene/resources/mesh.h"
|
||||
|
||||
#include "../mesh_data_resource.h"
|
||||
#include "editor/import/editor_scene_importer_gltf.h"
|
||||
|
||||
#include "core/math/transform.h"
|
||||
|
||||
#include "core/version.h"
|
||||
|
||||
#if VERSION_MAJOR < 4
|
||||
#include "scene/3d/mesh_instance.h"
|
||||
#else
|
||||
#include "scene/3d/mesh_instance_3d.h"
|
||||
|
||||
#define MeshInstance MeshInstance3D
|
||||
#endif
|
||||
|
||||
class MDRImportPluginBase : public EditorImportPlugin {
|
||||
|
||||
GDCLASS(MDRImportPluginBase, EditorImportPlugin);
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
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);
|
||||
Ref<Shape> scale_shape(Ref<Shape> shape, const Vector3 &scale);
|
||||
|
||||
MDRImportPluginBase();
|
||||
~MDRImportPluginBase();
|
||||
};
|
||||
|
||||
#endif
|
@ -63,298 +63,17 @@ 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::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)));
|
||||
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) {
|
||||
MeshDataResource::ColliderType collider_type = static_cast<MeshDataResource::ColliderType>(static_cast<int>(p_options["collider_type"]));
|
||||
//MeshDataResource::ColliderType collider_type = static_cast<MeshDataResource::ColliderType>(static_cast<int>(p_options["collider_type"]));
|
||||
|
||||
Node *n = _importer->import_scene(p_source_file, 0, 15);
|
||||
|
||||
Vector3 scale = p_options["scale"];
|
||||
ERR_FAIL_COND_V(!n, Error::ERR_PARSE_ERROR);
|
||||
|
||||
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);
|
||||
|
||||
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));
|
||||
|
||||
if (collider_type == MeshDataResource::COLLIDER_TYPE_TRIMESH_COLLISION_SHAPE) {
|
||||
Ref<ArrayMesh> m;
|
||||
m.instance();
|
||||
m->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mdr->get_array());
|
||||
|
||||
Ref<Shape> shape = m->create_trimesh_shape();
|
||||
|
||||
if (!shape.is_null()) {
|
||||
mdr->add_collision_shape(Transform(), scale_shape(shape, scale));
|
||||
}
|
||||
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_SINGLE_CONVEX_COLLISION_SHAPE) {
|
||||
Ref<ArrayMesh> m;
|
||||
m.instance();
|
||||
m->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mdr->get_array());
|
||||
|
||||
Ref<Shape> shape = mesh->create_convex_shape();
|
||||
|
||||
if (!shape.is_null()) {
|
||||
mdr->add_collision_shape(Transform(), scale_shape(shape, scale));
|
||||
}
|
||||
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_MULTIPLE_CONVEX_COLLISION_SHAPES) {
|
||||
Ref<ArrayMesh> m;
|
||||
m.instance();
|
||||
m->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mdr->get_array());
|
||||
|
||||
Vector<Ref<Shape> > shapes = mesh->convex_decompose();
|
||||
|
||||
for (int j = 0; j < shapes.size(); ++j) {
|
||||
scale_shape(shapes[j], scale);
|
||||
}
|
||||
|
||||
for (int j = 0; j < shapes.size(); ++j) {
|
||||
mdr->add_collision_shape(Transform(), shapes[j]);
|
||||
}
|
||||
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_BOX) {
|
||||
Ref<ArrayMesh> m;
|
||||
m.instance();
|
||||
m->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mdr->get_array());
|
||||
|
||||
Ref<BoxShape> shape;
|
||||
shape.instance();
|
||||
|
||||
AABB aabb = m->get_aabb();
|
||||
Vector3 size = aabb.get_size();
|
||||
|
||||
shape->set_extents(size * 0.5);
|
||||
|
||||
Vector3 pos = aabb.position;
|
||||
pos += size / 2.0;
|
||||
|
||||
Transform t;
|
||||
t.origin = pos;
|
||||
|
||||
mdr->add_collision_shape(t, shape);
|
||||
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_CAPSULE) {
|
||||
Ref<ArrayMesh> m;
|
||||
m.instance();
|
||||
m->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mdr->get_array());
|
||||
|
||||
Ref<CapsuleShape> shape;
|
||||
shape.instance();
|
||||
|
||||
AABB aabb = m->get_aabb();
|
||||
Vector3 size = aabb.get_size();
|
||||
|
||||
shape->set_height(size.y * 0.5);
|
||||
shape->set_radius(MIN(size.x, size.z) * 0.5);
|
||||
|
||||
Vector3 pos = aabb.position;
|
||||
pos += size / 2.0;
|
||||
|
||||
Transform t = Transform(Basis().rotated(Vector3(1, 0, 0), M_PI_2));
|
||||
t.origin = pos;
|
||||
|
||||
mdr->add_collision_shape(t, shape);
|
||||
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_CYLINDER) {
|
||||
Ref<ArrayMesh> m;
|
||||
m.instance();
|
||||
m->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mdr->get_array());
|
||||
|
||||
Ref<CylinderShape> shape;
|
||||
shape.instance();
|
||||
|
||||
AABB aabb = m->get_aabb();
|
||||
Vector3 size = aabb.get_size();
|
||||
|
||||
shape->set_height(size.y * 0.5);
|
||||
shape->set_radius(MIN(size.x, size.z) * 0.5);
|
||||
|
||||
Vector3 pos = aabb.position;
|
||||
pos += size / 2.0;
|
||||
|
||||
Transform t;
|
||||
t.origin = pos;
|
||||
|
||||
mdr->add_collision_shape(t, shape);
|
||||
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_SPHERE) {
|
||||
Ref<ArrayMesh> m;
|
||||
m.instance();
|
||||
m->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mdr->get_array());
|
||||
|
||||
Ref<SphereShape> shape;
|
||||
shape.instance();
|
||||
|
||||
AABB aabb = m->get_aabb();
|
||||
Vector3 size = aabb.get_size();
|
||||
|
||||
shape->set_radius(MIN(size.x, MIN(size.y, size.z)) * 0.5);
|
||||
|
||||
Vector3 mid = aabb.get_size() / 2.0;
|
||||
|
||||
Transform t;
|
||||
t.origin = aabb.position + mid;
|
||||
|
||||
mdr->add_collision_shape(t, shape);
|
||||
}
|
||||
|
||||
n->queue_delete();
|
||||
|
||||
return ResourceSaver::save(p_save_path + "." + get_save_extension(), mdr);
|
||||
}
|
||||
}
|
||||
}
|
||||
Error err = process_node_single(n, p_source_file, p_save_path, p_options, r_platform_variants, r_gen_files, r_metadata);
|
||||
|
||||
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);
|
||||
|
||||
PoolVector3Array 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));
|
||||
}
|
||||
|
||||
PoolVector3Array 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;
|
||||
}
|
||||
|
||||
Ref<Shape> EditorImportGLTFMdr::scale_shape(Ref<Shape> shape, const Vector3 &scale) {
|
||||
|
||||
if (shape.is_null())
|
||||
return shape;
|
||||
|
||||
if (Object::cast_to<SphereShape>(*shape)) {
|
||||
|
||||
Ref<SphereShape> ss = shape;
|
||||
|
||||
ss->set_radius(ss->get_radius() * MAX(scale.x, MAX(scale.y, scale.z)));
|
||||
}
|
||||
|
||||
if (Object::cast_to<BoxShape>(*shape)) {
|
||||
|
||||
Ref<BoxShape> bs = shape;
|
||||
|
||||
bs->set_extents(bs->get_extents() * scale);
|
||||
}
|
||||
|
||||
if (Object::cast_to<CapsuleShape>(*shape)) {
|
||||
|
||||
Ref<CapsuleShape> cs = shape;
|
||||
|
||||
float sc = MAX(scale.x, MAX(scale.y, scale.z));
|
||||
|
||||
cs->set_radius(cs->get_radius() * sc);
|
||||
cs->set_height(cs->get_height() * sc);
|
||||
}
|
||||
|
||||
if (Object::cast_to<CylinderShape>(*shape)) {
|
||||
|
||||
Ref<CylinderShape> cs = shape;
|
||||
|
||||
float sc = MAX(scale.x, MAX(scale.y, scale.z));
|
||||
|
||||
cs->set_radius(cs->get_radius() * sc);
|
||||
cs->set_height(cs->get_height() * sc);
|
||||
}
|
||||
|
||||
if (Object::cast_to<ConcavePolygonShape>(*shape)) {
|
||||
|
||||
Ref<ConcavePolygonShape> cps = shape;
|
||||
|
||||
PoolVector3Array arr = cps->get_faces();
|
||||
|
||||
Basis b = Basis().scaled(scale);
|
||||
|
||||
for (int i = 0; i < arr.size(); ++i) {
|
||||
arr.set(i, b.xform(arr[i]));
|
||||
}
|
||||
|
||||
cps->set_faces(arr);
|
||||
}
|
||||
|
||||
if (Object::cast_to<ConvexPolygonShape>(*shape)) {
|
||||
|
||||
Ref<ConvexPolygonShape> cps = shape;
|
||||
|
||||
PoolVector3Array arr = cps->get_points();
|
||||
|
||||
Basis b = Basis().scaled(scale);
|
||||
|
||||
for (int i = 0; i < arr.size(); ++i) {
|
||||
arr.set(i, b.xform(arr[i]));
|
||||
}
|
||||
|
||||
cps->set_points(arr);
|
||||
}
|
||||
|
||||
return shape;
|
||||
return err;
|
||||
}
|
||||
|
||||
EditorImportGLTFMdr::EditorImportGLTFMdr() {
|
||||
|
@ -23,12 +23,13 @@ SOFTWARE.
|
||||
#ifndef EDITOR_IMPORT_GLTF_MDR
|
||||
#define EDITOR_IMPORT_GLTF_MDR
|
||||
|
||||
#include "../plugin/mdr_import_plugin_base.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/main/node.h"
|
||||
#include "scene/resources/mesh.h"
|
||||
|
||||
@ -47,9 +48,9 @@ SOFTWARE.
|
||||
#define MeshInstance MeshInstance3D
|
||||
#endif
|
||||
|
||||
class EditorImportGLTFMdr : public EditorImportPlugin {
|
||||
class EditorImportGLTFMdr : public MDRImportPluginBase {
|
||||
|
||||
GDCLASS(EditorImportGLTFMdr, EditorImportPlugin);
|
||||
GDCLASS(EditorImportGLTFMdr, MDRImportPluginBase);
|
||||
|
||||
public:
|
||||
virtual String get_importer_name() const;
|
||||
@ -62,14 +63,8 @@ public:
|
||||
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);
|
||||
Ref<Shape> scale_shape(Ref<Shape> shape, const Vector3 &scale);
|
||||
|
||||
EditorImportGLTFMdr();
|
||||
~EditorImportGLTFMdr();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user