From 5ab674214bf2e64da6c642df156979ab26cf489f Mon Sep 17 00:00:00 2001 From: Cory Petkovsek <632766+TokisanGames@users.noreply.github.com> Date: Tue, 31 Dec 2024 00:06:08 +0700 Subject: [PATCH] Update transforms only within aabb instead of cell --- src/terrain_3d_instancer.cpp | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/terrain_3d_instancer.cpp b/src/terrain_3d_instancer.cpp index a3a36475..9c7bf1cd 100644 --- a/src/terrain_3d_instancer.cpp +++ b/src/terrain_3d_instancer.cpp @@ -751,10 +751,14 @@ void Terrain3DInstancer::append_region(const Ref &p_region, con // Review all transforms in one area and adjust their transforms w/ the current height void Terrain3DInstancer::update_transforms(const AABB &p_aabb) { IS_DATA_INIT_MESG("Instancer isn't initialized.", VOID); - Rect2 brush_rect = aabb2rect(p_aabb); - Vector2 global_position = brush_rect.get_center(); - Vector2 size = brush_rect.get_size(); - Vector2 half_size = size * 0.5 + Vector2(1.f, 1.f); // 1m margin + Rect2 rect = aabb2rect(p_aabb); + LOG(EXTREME, "Updating transforms within ", rect); + Vector2 global_position = rect.get_center(); + Vector2 size = rect.get_size(); + Vector2 half_size = size * 0.5f + Vector2(1.f, 1.f); // 1m margin + if (size == V2_ZERO) { + return; + } Terrain3DData *data = _terrain->get_data(); int region_size = _terrain->get_region_size(); @@ -762,7 +766,7 @@ void Terrain3DInstancer::update_transforms(const AABB &p_aabb) { // Build list of valid regions within AABB; add the locations as dictionary keys. Dictionary r_locs; - // Calculate step distance to ensure every region is checked inside the bounds of brush size. + // Calculate step distance to ensure every region is checked inside the bounds of AABB size. Vector2 step = Vector2(size.x / ceil(size.x / real_t(region_size) / vertex_spacing), size.y / ceil(size.y / real_t(region_size) / vertex_spacing)); for (real_t x = global_position.x - half_size.x; x <= global_position.x + half_size.x; x += step.x) { for (real_t z = global_position.y - half_size.y; z <= global_position.y + half_size.y; z += step.y) { @@ -831,15 +835,18 @@ void Terrain3DInstancer::update_transforms(const AABB &p_aabb) { PackedColorArray updated_colors; for (int i = 0; i < xforms.size(); i++) { Transform3D t = xforms[i]; - Vector3 height_offset = t.basis.get_column(1) * mesh_height_offset; - t.origin -= height_offset; - real_t height = _terrain->get_data()->get_height(t.origin + global_local_offset); - // If the new height is a nan due to creating a hole, remove the instance - if (std::isnan(height)) { - continue; + Vector3 global_origin(t.origin + global_local_offset); + if (rect.has_point(Vector2(global_origin.x, global_origin.z))) { + Vector3 height_offset = t.basis.get_column(1) * mesh_height_offset; + t.origin -= height_offset; + real_t height = _terrain->get_data()->get_height(global_origin); + // If the new height is a nan due to creating a hole, remove the instance + if (std::isnan(height)) { + continue; + } + t.origin.y = height; + t.origin += height_offset; } - t.origin.y = height; - t.origin += height_offset; updated_xforms.push_back(t); updated_colors.push_back(colors[i]); }