Skip to content

Commit

Permalink
Update rendered mesh on data mesh changes
Browse files Browse the repository at this point in the history
wasn't connected before
  • Loading branch information
tefusion committed Mar 29, 2024
1 parent 22e508f commit 1b22b71
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
25 changes: 20 additions & 5 deletions src/nodes/subdiv_mesh_instance_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,17 @@ bool SubdivMeshInstance3D::_get(const StringName &p_name, Variant &return_value)

//start of non editor stuff
void SubdivMeshInstance3D::set_mesh(const Ref<TopologyDataMesh> &p_mesh) {
if (mesh.is_valid()) {
mesh->disconnect(StringName("changed"), Callable(this, "_mesh_changed"));
}
mesh = p_mesh;
_init_cached_data_array();
if (is_inside_tree()) {
_mesh_changed();
if (mesh.is_valid()) {
mesh->connect(StringName("changed"), Callable(this, "_mesh_changed"));

_init_cached_data_array();
if (is_inside_tree()) {
_mesh_changed();
}
}
}

Expand Down Expand Up @@ -323,10 +330,11 @@ void SubdivMeshInstance3D::_update_subdiv() {
return;
}

if (get_mesh().is_null() && subdiv_mesh) {
if ((get_mesh().is_null() || get_mesh()->get_surface_count() == 0) && subdiv_mesh) {
subdiv_mesh->clear();
return;
}

if (!subdiv_mesh) {
SubdivisionServer *subdivision_server = SubdivisionServer::get_singleton();
ERR_FAIL_COND(!subdivision_server);
Expand Down Expand Up @@ -359,7 +367,13 @@ void SubdivMeshInstance3D::_init_cached_data_array() {
surface_override_materials.resize(mesh->get_surface_count());
}
void SubdivMeshInstance3D::_mesh_changed() {
ERR_FAIL_COND(mesh.is_null());
if (mesh.is_null()) {
return;
}

if (cached_data_array.size() != mesh->get_surface_count()) {
_init_cached_data_array();
}

uint32_t initialize_bs_from = blend_shape_tracks.size();
blend_shape_tracks.resize(mesh->get_blend_shape_count());
Expand Down Expand Up @@ -430,6 +444,7 @@ void SubdivMeshInstance3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_subdiv_level"), &SubdivMeshInstance3D::get_subdiv_level);

ClassDB::bind_method(D_METHOD("_update_skinning"), &SubdivMeshInstance3D::_update_skinning);
ClassDB::bind_method(D_METHOD("_mesh_changed"), &SubdivMeshInstance3D::_mesh_changed);

ClassDB::bind_method(D_METHOD("get_blend_shape_value", "blend_shape_idx"), &SubdivMeshInstance3D::get_blend_shape_value);
ClassDB::bind_method(D_METHOD("set_blend_shape_value", "blend_shape_idx", "value"), &SubdivMeshInstance3D::set_blend_shape_value);
Expand Down
4 changes: 4 additions & 0 deletions src/resources/topology_data_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void TopologyDataMesh::add_surface(const Array &p_arrays, const Dictionary &p_lo
}

surfaces.push_back(s);
emit_changed();
}

Array TopologyDataMesh::surface_get_arrays(int p_surface) const {
Expand Down Expand Up @@ -111,6 +112,7 @@ Dictionary TopologyDataMesh::_get_data() const {
void TopologyDataMesh::clear() {
surfaces.clear();
blend_shapes.clear();
emit_changed();
}

int64_t TopologyDataMesh::get_surface_count() const {
Expand All @@ -125,6 +127,7 @@ BitField<Mesh::ArrayFormat> TopologyDataMesh::surface_get_format(int64_t index)
void TopologyDataMesh::surface_set_material(int64_t index, const Ref<Material> &material) {
ERR_FAIL_INDEX(index, surfaces.size());
surfaces.write[index].material = material;
emit_changed();
}
Ref<Material> TopologyDataMesh::surface_get_material(int64_t index) const {
ERR_FAIL_INDEX_V(index, surfaces.size(), Ref<Material>());
Expand All @@ -135,6 +138,7 @@ Ref<Material> TopologyDataMesh::surface_get_material(int64_t index) const {
void TopologyDataMesh::surface_set_topology_type(int64_t index, TopologyType p_topology_type) {
ERR_FAIL_INDEX(index, surfaces.size());
surfaces.write[index].topology_type = p_topology_type;
emit_changed();
}

TopologyDataMesh::TopologyType TopologyDataMesh::surface_get_topology_type(int64_t index) const {
Expand Down
10 changes: 7 additions & 3 deletions src/subdivision/subdivision_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
#include "triangle_subdivider.hpp"

Array SubdivisionMesh::_get_subdivided_arrays(const Array &p_arrays, int p_level, int32_t p_format, bool calculate_normals, TopologyDataMesh::TopologyType topology_type) {
const PackedVector3Array &vertex_array = p_arrays[TopologyDataMesh::ARRAY_VERTEX];
if (vertex_array.is_empty()) {
Array empty_surface;
empty_surface.resize(Mesh::ARRAY_MAX);
return empty_surface;
}
switch (topology_type) {
case TopologyDataMesh::QUAD: {
Ref<QuadSubdivider> subdivider;
Expand Down Expand Up @@ -56,14 +62,12 @@ void SubdivisionMesh::_update_subdivision(Ref<TopologyDataMesh> p_mesh, int32_t

Array v_arrays = cached_data_arrays.size() ? cached_data_arrays[surface_index]
: p_mesh->surface_get_arrays(surface_index);

Array subdiv_triangle_arrays = _get_subdivided_arrays(v_arrays, p_level, surface_format, true, p_mesh->surface_get_topology_type(surface_index));

Ref<Material> material = p_mesh->surface_get_material(surface_index);
subdiv_mesh.add_surface(subdiv_triangle_arrays, Dictionary(), material, "", surface_format);

current_level = p_level;
}
current_level = p_level;
}

void SubdivisionMesh::update_subdivision_vertices(int p_surface, const PackedVector3Array &new_vertex_array,
Expand Down

0 comments on commit 1b22b71

Please sign in to comment.