From 4b7f8b84bce30e74292ef5e7fd445d3831f9b744 Mon Sep 17 00:00:00 2001 From: Cory Petkovsek <632766+TokisanGames@users.noreply.github.com> Date: Mon, 28 Aug 2023 16:24:38 +0700 Subject: [PATCH] Fix #183 Limit regions to 16x16 and fix error spam --- .../terrain_3d/editor/components/region_gizmo.gd | 6 ++++-- src/terrain_3d_storage.cpp | 15 ++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/project/addons/terrain_3d/editor/components/region_gizmo.gd b/project/addons/terrain_3d/editor/components/region_gizmo.gd index 7fb27b51..594c77bf 100644 --- a/project/addons/terrain_3d/editor/components/region_gizmo.gd +++ b/project/addons/terrain_3d/editor/components/region_gizmo.gd @@ -23,6 +23,7 @@ func _init() -> void: selection_material = material.duplicate() selection_material.set_render_priority(0) + func _redraw() -> void: clear() @@ -30,7 +31,7 @@ func _redraw() -> void: if show_rect: var modulate: Color = main_color if !use_secondary_color else secondary_color - if abs(region_position.x) > 8 or abs(region_position.y) > 8: + if region_position.x > 7 or region_position.y > 7 or region_position.x < -8 or region_position.y < -8: modulate = Color.GRAY draw_rect(rect_position, region_size, selection_material, modulate) @@ -42,7 +43,8 @@ func _redraw() -> void: draw_rect(grid_tile_position, region_size, material, grid_color) - draw_rect(Vector2.ZERO, region_size * 17.0, material, border_color) + draw_rect(Vector2(-512,-512), region_size * 16.0, material, border_color) + func draw_rect(pos: Vector2, size: float, material: StandardMaterial3D, modulate: Color) -> void: var lines: PackedVector3Array = [ diff --git a/src/terrain_3d_storage.cpp b/src/terrain_3d_storage.cpp index 23c52c41..47a27669 100644 --- a/src/terrain_3d_storage.cpp +++ b/src/terrain_3d_storage.cpp @@ -275,9 +275,12 @@ void Terrain3DStorage::_update_regions() { for (int i = 0; i < _region_offsets.size(); i++) { Vector2i ofs = _region_offsets[i]; - + Vector2i img_pos = Vector2i(ofs + (REGION_MAP_VSIZE / 2)); + if (img_pos.x >= REGION_MAP_SIZE || img_pos.y >= REGION_MAP_SIZE || img_pos.x < 0 || img_pos.y < 0) { + continue; + } Color col = Color(float(i + 1) / 255.0, 1.0, 0, 1); - region_map_img->set_pixelv(ofs + (REGION_MAP_VSIZE / 2), col); + region_map_img->set_pixelv(img_pos, col); } _generated_region_map.create(region_map_img); RenderingServer::get_singleton()->material_set_param(_material, "region_map", _generated_region_map.get_rid()); @@ -515,14 +518,15 @@ int Terrain3DStorage::get_region_index(Vector3 p_global_position) { Vector2i uv_offset = get_region_offset(p_global_position); int index = -1; - if (ABS(uv_offset.x) > REGION_MAP_SIZE / 2 || ABS(uv_offset.y) > REGION_MAP_SIZE / 2) { + Vector2i img_pos = Vector2i(uv_offset + (REGION_MAP_VSIZE / 2)); + if (img_pos.x >= REGION_MAP_SIZE || img_pos.y >= REGION_MAP_SIZE || img_pos.x < 0 || img_pos.y < 0) { return index; } Ref img = _generated_region_map.get_image(); if (img.is_valid()) { - index = int(img->get_pixelv(uv_offset + (REGION_MAP_VSIZE / 2)).r * 255.0) - 1; + index = int(img->get_pixelv(img_pos).r * 255.0) - 1; } else { for (int i = 0; i < _region_offsets.size(); i++) { Vector2i ofs = _region_offsets[i]; @@ -550,7 +554,8 @@ Error Terrain3DStorage::add_region(Vector3 p_global_position, const TypedArray REGION_MAP_SIZE / 2 || ABS(uv_offset.y) > REGION_MAP_SIZE / 2) { + Vector2i region_pos = Vector2i(uv_offset + (REGION_MAP_VSIZE / 2)); + if (region_pos.x >= REGION_MAP_SIZE || region_pos.y >= REGION_MAP_SIZE || region_pos.x < 0 || region_pos.y < 0) { LOG(ERROR, "Specified position outside of maximum region map size: ", REGION_MAP_SIZE / 2 * _region_size); return FAILED; }