@@ -751,18 +751,22 @@ void Terrain3DInstancer::append_region(const Ref<Terrain3DRegion> &p_region, con
751
751
// Review all transforms in one area and adjust their transforms w/ the current height
752
752
void Terrain3DInstancer::update_transforms (const AABB &p_aabb) {
753
753
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
+ }
758
762
759
763
Terrain3DData *data = _terrain->get_data ();
760
764
int region_size = _terrain->get_region_size ();
761
765
real_t vertex_spacing = _terrain->get_vertex_spacing ();
762
766
763
767
// Build list of valid regions within AABB; add the locations as dictionary keys.
764
768
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.
766
770
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));
767
771
for (real_t x = global_position.x - half_size.x ; x <= global_position.x + half_size.x ; x += step.x ) {
768
772
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) {
831
835
PackedColorArray updated_colors;
832
836
for (int i = 0 ; i < xforms.size (); i++) {
833
837
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;
840
849
}
841
- t.origin .y = height;
842
- t.origin += height_offset;
843
850
updated_xforms.push_back (t);
844
851
updated_colors.push_back (colors[i]);
845
852
}
0 commit comments