From b66b9c5ba9b73fd21cc9cc27735ca447534d24b9 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 14 Aug 2021 13:44:35 +0200 Subject: [PATCH] Now MeshDataInstance inherits from VisualInstance. --- mesh_data_resource.cpp | 3 ++ mesh_data_resource.h | 1 + nodes/mesh_data_instance.cpp | 66 +++++++++++++++++++++++++----------- nodes/mesh_data_instance.h | 10 +++--- 4 files changed, 57 insertions(+), 23 deletions(-) diff --git a/mesh_data_resource.cpp b/mesh_data_resource.cpp index 63e8bce..86dc4b8 100644 --- a/mesh_data_resource.cpp +++ b/mesh_data_resource.cpp @@ -38,6 +38,9 @@ void MeshDataResource::set_array(const Array &p_arrays) { recompute_aabb(); } +Array MeshDataResource::get_array_const() const { + return _arrays; +} AABB MeshDataResource::get_aabb() const { return _aabb; diff --git a/mesh_data_resource.h b/mesh_data_resource.h index 04df2e7..59911c0 100644 --- a/mesh_data_resource.h +++ b/mesh_data_resource.h @@ -65,6 +65,7 @@ public: public: Array get_array(); void set_array(const Array &p_arrays); + Array get_array_const() const; AABB get_aabb() const; void set_aabb(const AABB &aabb); diff --git a/nodes/mesh_data_instance.cpp b/nodes/mesh_data_instance.cpp index 2150e65..82b66ad 100644 --- a/nodes/mesh_data_instance.cpp +++ b/nodes/mesh_data_instance.cpp @@ -64,23 +64,57 @@ void MeshDataInstance::set_material(const Ref &mat) { refresh(); } +AABB MeshDataInstance::get_aabb() const { + if (!_mesh.is_valid()) { + return AABB(); + } + + return _mesh->get_aabb(); +} + +PoolVector MeshDataInstance::get_faces(uint32_t p_usage_flags) const { + PoolVector faces; + + if (_mesh.is_valid()) { + Array arrs = _mesh->get_array_const(); + + if (arrs.size() != Mesh::ARRAY_MAX) { + return faces; + } + + PoolVector vertices = arrs[Mesh::ARRAY_VERTEX]; + PoolVector indices = arrs[Mesh::ARRAY_INDEX]; + + int ts = indices.size() / 3; + faces.resize(ts); + + PoolVector::Write w = faces.write(); + PoolVector::Read rv = vertices.read(); + PoolVector::Read ri = indices.read(); + + for (int i = 0; i < ts; i++) { + int im3 = (i * 3); + + for (int j = 0; j < 3; j++) { + w[i].vertex[j] = rv[indices[im3 + j]]; + } + } + + w.release(); + } + + return faces; +} + void MeshDataInstance::refresh() { if (!is_inside_tree()) { return; } if (_mesh_rid == RID()) { - _mesh_instance = VisualServer::get_singleton()->instance_create(); - - if (GET_WORLD().is_valid()) - VS::get_singleton()->instance_set_scenario(_mesh_instance, GET_WORLD()->get_scenario()); - _mesh_rid = VisualServer::get_singleton()->mesh_create(); - VS::get_singleton()->instance_set_base(_mesh_instance, _mesh_rid); - VS::get_singleton()->instance_set_transform(_mesh_instance, get_transform()); - - VS::get_singleton()->instance_set_visible(_mesh_instance, true); + VS::get_singleton()->instance_set_base(get_instance(), _mesh_rid); } VisualServer::get_singleton()->mesh_clear(_mesh_rid); @@ -159,11 +193,6 @@ void MeshDataInstance::setup_material_texture() { } void MeshDataInstance::free_meshes() { - if (_mesh_instance != RID()) { - VS::get_singleton()->free(_mesh_instance); - _mesh_instance = RID(); - } - if (_mesh_rid != RID()) { VS::get_singleton()->free(_mesh_rid); _mesh_rid = RID(); @@ -175,7 +204,7 @@ MeshDataInstance::MeshDataInstance() { _snap_to_mesh = false; _snap_axis = Vector3(0, -1, 0); - set_notify_transform(true); + //set_notify_transform(true); } MeshDataInstance::~MeshDataInstance() { _mesh.unref(); @@ -194,15 +223,14 @@ void MeshDataInstance::_notification(int p_what) { free_meshes(); break; } + /* case NOTIFICATION_TRANSFORM_CHANGED: { VisualServer *vs = VisualServer::get_singleton(); - if (_mesh_instance != RID()) { - vs->instance_set_transform(_mesh_instance, get_global_transform()); - } + vs->instance_set_transform(get_instance(), get_global_transform()); break; - } + }*/ } } diff --git a/nodes/mesh_data_instance.h b/nodes/mesh_data_instance.h index 1de4db9..824ae15 100644 --- a/nodes/mesh_data_instance.h +++ b/nodes/mesh_data_instance.h @@ -27,7 +27,7 @@ SOFTWARE. #include "scene/resources/texture.h" #if VERSION_MAJOR < 4 -#include "scene/3d/spatial.h" +#include "scene/3d/visual_instance.h" #else #include "scene/3d/node_3d.h" @@ -42,8 +42,8 @@ SOFTWARE. class PropInstance; -class MeshDataInstance : public Spatial { - GDCLASS(MeshDataInstance, Spatial); +class MeshDataInstance : public VisualInstance { + GDCLASS(MeshDataInstance, VisualInstance); public: bool get_snap_to_mesh() const; @@ -61,6 +61,9 @@ public: Ref get_material(); void set_material(const Ref &mat); + AABB get_aabb() const; + PoolVector get_faces(uint32_t p_usage_flags) const; + void refresh(); void setup_material_texture(); void free_meshes(); @@ -81,7 +84,6 @@ private: Ref _material; RID _mesh_rid; - RID _mesh_instance; }; #endif