Skip to content

Commit 5ab6742

Browse files
committed
Update transforms only within aabb instead of cell
1 parent 7ea2191 commit 5ab6742

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/terrain_3d_instancer.cpp

+20-13
Original file line numberDiff line numberDiff line change
@@ -751,18 +751,22 @@ void Terrain3DInstancer::append_region(const Ref<Terrain3DRegion> &p_region, con
751751
// Review all transforms in one area and adjust their transforms w/ the current height
752752
void Terrain3DInstancer::update_transforms(const AABB &p_aabb) {
753753
IS_DATA_INIT_MESG("Instancer isn't initialized.", VOID);
754-
Rect2 brush_rect = aabb2rect(p_aabb);
755-
Vector2 global_position = brush_rect.get_center();
756-
Vector2 size = brush_rect.get_size();
757-
Vector2 half_size = size * 0.5 + Vector2(1.f, 1.f); // 1m margin
754+
Rect2 rect = aabb2rect(p_aabb);
755+
LOG(EXTREME, "Updating transforms within ", rect);
756+
Vector2 global_position = rect.get_center();
757+
Vector2 size = rect.get_size();
758+
Vector2 half_size = size * 0.5f + Vector2(1.f, 1.f); // 1m margin
759+
if (size == V2_ZERO) {
760+
return;
761+
}
758762

759763
Terrain3DData *data = _terrain->get_data();
760764
int region_size = _terrain->get_region_size();
761765
real_t vertex_spacing = _terrain->get_vertex_spacing();
762766

763767
// Build list of valid regions within AABB; add the locations as dictionary keys.
764768
Dictionary r_locs;
765-
// Calculate step distance to ensure every region is checked inside the bounds of brush size.
769+
// Calculate step distance to ensure every region is checked inside the bounds of AABB size.
766770
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));
767771
for (real_t x = global_position.x - half_size.x; x <= global_position.x + half_size.x; x += step.x) {
768772
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) {
831835
PackedColorArray updated_colors;
832836
for (int i = 0; i < xforms.size(); i++) {
833837
Transform3D t = xforms[i];
834-
Vector3 height_offset = t.basis.get_column(1) * mesh_height_offset;
835-
t.origin -= height_offset;
836-
real_t height = _terrain->get_data()->get_height(t.origin + global_local_offset);
837-
// If the new height is a nan due to creating a hole, remove the instance
838-
if (std::isnan(height)) {
839-
continue;
838+
Vector3 global_origin(t.origin + global_local_offset);
839+
if (rect.has_point(Vector2(global_origin.x, global_origin.z))) {
840+
Vector3 height_offset = t.basis.get_column(1) * mesh_height_offset;
841+
t.origin -= height_offset;
842+
real_t height = _terrain->get_data()->get_height(global_origin);
843+
// If the new height is a nan due to creating a hole, remove the instance
844+
if (std::isnan(height)) {
845+
continue;
846+
}
847+
t.origin.y = height;
848+
t.origin += height_offset;
840849
}
841-
t.origin.y = height;
842-
t.origin += height_offset;
843850
updated_xforms.push_back(t);
844851
updated_colors.push_back(colors[i]);
845852
}

0 commit comments

Comments
 (0)