Skip to content

Commit

Permalink
Update transforms only within aabb instead of cell
Browse files Browse the repository at this point in the history
  • Loading branch information
TokisanGames committed Dec 31, 2024
1 parent fae86c0 commit c75f6a3
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/terrain_3d_instancer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,18 +775,22 @@ void Terrain3DInstancer::append_region(const Ref<Terrain3DRegion> &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();
real_t vertex_spacing = _terrain->get_vertex_spacing();

// 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) {
Expand Down Expand Up @@ -855,15 +859,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]);
}
Expand Down

0 comments on commit c75f6a3

Please sign in to comment.