Skip to content

Commit

Permalink
Fix #183 Limit regions to 16x16 and fix error spam
Browse files Browse the repository at this point in the history
  • Loading branch information
TokisanGames committed Aug 28, 2023
1 parent 56a5988 commit 4b7f8b8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
6 changes: 4 additions & 2 deletions project/addons/terrain_3d/editor/components/region_gizmo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ func _init() -> void:
selection_material = material.duplicate()
selection_material.set_render_priority(0)


func _redraw() -> void:
clear()

var rect_position = region_position * region_size

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)

Expand All @@ -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 = [
Expand Down
15 changes: 10 additions & 5 deletions src/terrain_3d_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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<Image> 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];
Expand Down Expand Up @@ -550,7 +554,8 @@ Error Terrain3DStorage::add_region(Vector3 p_global_position, const TypedArray<I
", array size: ", p_images.size(),
", update maps: ", p_update ? "yes" : "no");

if (ABS(uv_offset.x) > 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;
}
Expand Down

0 comments on commit 4b7f8b8

Please sign in to comment.