Skip to content

Commit

Permalink
Fix update_transforms running on non-height tool
Browse files Browse the repository at this point in the history
  • Loading branch information
TokisanGames committed Dec 30, 2024
1 parent e81483a commit a0941d4
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 22 deletions.
20 changes: 10 additions & 10 deletions project/addons/terrain_3d/utils/terrain_3d_objects.gd
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func get_terrain() -> Terrain3D:
terrain = terrains[0]
_terrain_id = terrain.get_instance_id() if terrain else 0

if terrain and terrain.data and not terrain.data.maps_edited.is_connected(_on_maps_edited):
terrain.data.maps_edited.connect(_on_maps_edited)
if terrain and terrain.data and not terrain.data.map_edited.is_connected(_on_map_edited):
terrain.data.map_edited.connect(_on_map_edited)

return terrain

Expand Down Expand Up @@ -180,12 +180,12 @@ func _update_child_position(p_node: Node3D) -> void:
p_node.position = new_position


func _on_maps_edited(p_edited_aabb: AABB) -> void:
var edited_area: AABB = p_edited_aabb.grow(1)
edited_area.position.y = -INF
edited_area.end.y = INF
func _on_map_edited(p_edited_aabb: AABB, p_map_type: int) -> void:
if p_map_type == Terrain3DRegion.MapType.TYPE_HEIGHT:
var edited_area: AABB = p_edited_aabb.grow(1)
edited_area.position.y = -INF
edited_area.end.y = INF

for child in get_children():
var node := child as Node3D
if node && edited_area.has_point(node.global_position):
_update_child_position(node)
for child in get_children():
if child is Node3D and edited_area.has_point(child.global_position):
_update_child_position(child)
6 changes: 3 additions & 3 deletions src/terrain_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ void Terrain3D::_initialize() {
_data->connect("height_maps_changed", callable_mp(this, &Terrain3D::update_aabbs));
}
// Connect height changes to update instances
if (!_data->is_connected("maps_edited", callable_mp(_instancer, &Terrain3DInstancer::update_transforms))) {
LOG(DEBUG, "Connecting maps_edited signal to update_transforms()");
_data->connect("maps_edited", callable_mp(_instancer, &Terrain3DInstancer::update_transforms));
if (!_data->is_connected("map_edited", callable_mp(_instancer, &Terrain3DInstancer::update_transforms))) {
LOG(DEBUG, "Connecting map_edited signal to update_transforms()");
_data->connect("map_edited", callable_mp(_instancer, &Terrain3DInstancer::update_transforms));
}
// Texture assets changed, update material
if (!_assets->is_connected("textures_changed", callable_mp(_material.ptr(), &Terrain3DMaterial::_update_texture_arrays))) {
Expand Down
6 changes: 3 additions & 3 deletions src/terrain_3d_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,13 +796,13 @@ Vector3 Terrain3DData::get_mesh_vertex(const int32_t p_lod, const HeightFilter p
return Vector3(p_global_position.x, height, p_global_position.z);
}

void Terrain3DData::add_edited_area(const AABB &p_area) {
void Terrain3DData::add_edited_area(const AABB &p_area, const MapType p_map_type) {
if (_edited_area.has_surface()) {
_edited_area = _edited_area.merge(p_area);
} else {
_edited_area = p_area;
}
emit_signal("maps_edited", p_area);
emit_signal("map_edited", p_area, p_map_type);
}

// Recalculates master height range from all active regions current height ranges
Expand Down Expand Up @@ -1207,5 +1207,5 @@ void Terrain3DData::_bind_methods() {
ADD_SIGNAL(MethodInfo("height_maps_changed"));
ADD_SIGNAL(MethodInfo("control_maps_changed"));
ADD_SIGNAL(MethodInfo("color_maps_changed"));
ADD_SIGNAL(MethodInfo("maps_edited", PropertyInfo(Variant::AABB, "edited_area")));
ADD_SIGNAL(MethodInfo("map_edited", PropertyInfo(Variant::AABB, "edited_area"), PropertyInfo(Variant::INT, "map_type")));
}
2 changes: 1 addition & 1 deletion src/terrain_3d_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class Terrain3DData : public Object {
Vector3 get_texture_id(const Vector3 &p_global_position) const;
Vector3 get_mesh_vertex(const int32_t p_lod, const HeightFilter p_filter, const Vector3 &p_global_position) const;

void add_edited_area(const AABB &p_area);
void add_edited_area(const AABB &p_area, const MapType p_map_type = TYPE_HEIGHT);
void clear_edited_area() { _edited_area = AABB(); }
AABB get_edited_area() const { return _edited_area; }

Expand Down
8 changes: 6 additions & 2 deletions src/terrain_3d_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void Terrain3DEditor::_operate_map(const Vector3 &p_global_position, const real_
if (map_type == TYPE_HEIGHT) {
real_t srcf = src.r;
// In case data in existing map has nan or inf saved, check, and reset to real number if required.
srcf = std::isnan(srcf) || std::isnan(srcf) ? 0.f : srcf;
srcf = std::isnan(srcf) || std::isnan(srcf) ? 0.f : srcf;
real_t destf = srcf;

switch (_operation) {
Expand Down Expand Up @@ -521,7 +521,11 @@ void Terrain3DEditor::_operate_map(const Vector3 &p_global_position, const real_
// If region qty was changed, must fully rebuild the maps
data->force_update_maps(map_type);
}
data->add_edited_area(edited_area);
data->add_edited_area(edited_area, map_type);
if (_tool == HOLES) {
// Holes will send out the signal for both control and height map
data->add_edited_area(edited_area, TYPE_HEIGHT);
}
}

void Terrain3DEditor::_store_undo() {
Expand Down
7 changes: 5 additions & 2 deletions src/terrain_3d_instancer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,10 @@ 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) {
void Terrain3DInstancer::update_transforms(const AABB &p_aabb, const MapType p_map_type) {
if (p_map_type != TYPE_HEIGHT) {
return;
}
IS_DATA_INIT_MESG("Instancer isn't initialized.", VOID);
Rect2 brush_rect = aabb2rect(p_aabb);
Vector2 global_position = brush_rect.get_center();
Expand Down Expand Up @@ -1044,7 +1047,7 @@ void Terrain3DInstancer::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_transforms", "mesh_id", "transforms", "colors", "update"), &Terrain3DInstancer::add_transforms, DEFVAL(PackedColorArray()), DEFVAL(true));
ClassDB::bind_method(D_METHOD("append_location", "region_location", "mesh_id", "transforms", "colors", "update"), &Terrain3DInstancer::append_location, DEFVAL(true));
ClassDB::bind_method(D_METHOD("append_region", "region", "mesh_id", "transforms", "colors", "update"), &Terrain3DInstancer::append_region, DEFVAL(true));
ClassDB::bind_method(D_METHOD("update_transforms", "aabb"), &Terrain3DInstancer::update_transforms);
ClassDB::bind_method(D_METHOD("update_transforms", "aabb", "map_type"), &Terrain3DInstancer::update_transforms, DEFVAL(TYPE_HEIGHT));
ClassDB::bind_method(D_METHOD("force_update_mmis"), &Terrain3DInstancer::force_update_mmis);
ClassDB::bind_method(D_METHOD("swap_ids", "src_id", "dest_id"), &Terrain3DInstancer::swap_ids);
ClassDB::bind_method(D_METHOD("dump_data"), &Terrain3DInstancer::dump_data);
Expand Down
2 changes: 1 addition & 1 deletion src/terrain_3d_instancer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Terrain3DInstancer : public Object {
const PackedColorArray &p_colors, const bool p_update = true);
void append_region(const Ref<Terrain3DRegion> &p_region, const int p_mesh_id, const TypedArray<Transform3D> &p_xforms,
const PackedColorArray &p_colors, const bool p_update = true);
void update_transforms(const AABB &p_aabb);
void update_transforms(const AABB &p_aabb, const MapType p_map_type = TYPE_HEIGHT);
void copy_paste_dfr(const Terrain3DRegion *p_src_region, const Rect2i &p_src_rect, const Terrain3DRegion *p_dst_region);

void swap_ids(const int p_src_id, const int p_dst_id);
Expand Down

0 comments on commit a0941d4

Please sign in to comment.