Skip to content

Commit

Permalink
API change first pass - Breaks loading region_offsets!
Browse files Browse the repository at this point in the history
  • Loading branch information
TokisanGames committed Jul 27, 2024
1 parent ce9b375 commit 4fcb2a4
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 262 deletions.
2 changes: 1 addition & 1 deletion project/addons/terrain_3d/editor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func update_region_grid() -> void:
region_gizmo.use_secondary_color = editor.get_operation() == Terrain3DEditor.SUBTRACT
region_gizmo.region_position = current_region_position
region_gizmo.region_size = terrain.get_storage().get_region_size() * terrain.get_mesh_vertex_spacing()
region_gizmo.grid = terrain.get_storage().get_region_offsets()
region_gizmo.grid = terrain.get_storage().get_region_locations()

terrain.update_gizmos()
return
Expand Down
6 changes: 3 additions & 3 deletions project/addons/terrain_3d/extras/minimum.gdshader
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ uniform float _mesh_vertex_spacing = 1.0;
uniform float _mesh_vertex_density = 1.0; // = 1/_mesh_vertex_spacing
uniform int _region_map_size = 16;
uniform int _region_map[256];
uniform vec2 _region_offsets[256];
uniform vec2 _region_locations[256];
uniform sampler2DArray _height_maps : repeat_disable;
uniform usampler2DArray _control_maps : repeat_disable;
uniform sampler2DArray _color_maps : source_color, filter_linear_mipmap_anisotropic, repeat_disable;
Expand Down Expand Up @@ -38,7 +38,7 @@ ivec3 get_region_uv(vec2 uv) {
ivec2 pos = ivec2(floor(uv)) + (_region_map_size / 2);
int bounds = int(pos.x>=0 && pos.x<_region_map_size && pos.y>=0 && pos.y<_region_map_size);
int layer_index = _region_map[ pos.y * _region_map_size + pos.x ] * bounds - 1;
return ivec3(ivec2((uv - _region_offsets[layer_index]) * _region_size), layer_index);
return ivec3(ivec2((uv - _region_locations[layer_index]) * _region_size), layer_index);
}

// Takes in UV2 region space coordinates, returns vec3 with:
Expand All @@ -52,7 +52,7 @@ vec3 get_region_uv2(vec2 uv) {
int bounds = int(pos.x>=0 && pos.x<_region_map_size && pos.y>=0 && pos.y<_region_map_size);
int layer_index = _region_map[ pos.y * _region_map_size + pos.x ] * bounds - 1;
// The return value is still texel-centered.
return vec3(uv - _region_offsets[layer_index], float(layer_index));
return vec3(uv - _region_locations[layer_index], float(layer_index));
}

// 1 lookup
Expand Down
8 changes: 4 additions & 4 deletions src/shaders/main.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ uniform float _mesh_vertex_spacing = 1.0;
uniform float _mesh_vertex_density = 1.0; // = 1/_mesh_vertex_spacing
uniform int _region_map_size = 16;
uniform int _region_map[256];
uniform vec2 _region_offsets[256];
uniform vec2 _region_locations[256];
uniform sampler2DArray _height_maps : repeat_disable;
uniform usampler2DArray _control_maps : repeat_disable;
//INSERT: TEXTURE_SAMPLERS_NEAREST
Expand Down Expand Up @@ -91,7 +91,7 @@ ivec3 get_region_uv(vec2 uv) {
ivec2 pos = ivec2(floor(uv)) + (_region_map_size / 2);
int bounds = int(pos.x >= 0 && pos.x < _region_map_size && pos.y >= 0 && pos.y < _region_map_size);
int layer_index = _region_map[ pos.y * _region_map_size + pos.x ] * bounds - 1;
return ivec3(ivec2((uv - _region_offsets[layer_index]) * _region_size), layer_index);
return ivec3(ivec2((uv - _region_locations[layer_index]) * _region_size), layer_index);
}

// Takes in UV2 region space coordinates, returns vec3 with:
Expand All @@ -105,7 +105,7 @@ vec3 get_region_uv2(vec2 uv) {
int bounds = int(pos.x >= 0 && pos.x < _region_map_size && pos.y >= 0 && pos.y < _region_map_size);
int layer_index = _region_map[ pos.y * _region_map_size + pos.x ] * bounds - 1;
// The return value is still texel-centered.
return vec3(uv - _region_offsets[layer_index], float(layer_index));
return vec3(uv - _region_locations[layer_index], float(layer_index));
}

//INSERT: WORLD_NOISE1
Expand Down Expand Up @@ -265,7 +265,7 @@ void get_material(vec2 base_uv, uint control, ivec3 iuv_center, vec3 normal, out
//INSERT: TEXTURE_ID
// Control map scale & rotation, apply to both base and
// uv_center. Translate uv center to the current region.
uv_center += _region_offsets[region] * _region_size;
uv_center += _region_locations[region] * _region_size;
// Define base scale from control map value as array index. 0.5 as baseline.
float[8] scale_array = { 0.5, 0.4, 0.3, 0.2, 0.1, 0.8, 0.7, 0.6};
float control_scale = scale_array[(control >>7u & 0x7u)];
Expand Down
40 changes: 20 additions & 20 deletions src/terrain_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void Terrain3D::_initialize() {
LOG(DEBUG, "Connecting region_size_changed signal to _material->_update_regions()");
_storage->connect("region_size_changed", callable_mp(_material.ptr(), &Terrain3DMaterial::_update_regions));
}
// Any map was regenerated or region offsets changed, update material
// Any map was regenerated or region locations changed, update material
if (!_storage->is_connected("regions_changed", callable_mp(_material.ptr(), &Terrain3DMaterial::_update_regions))) {
LOG(DEBUG, "Connecting _storage::regions_changed signal to _material->_update_regions()");
_storage->connect("regions_changed", callable_mp(_material.ptr(), &Terrain3DMaterial::_update_regions));
Expand Down Expand Up @@ -409,27 +409,27 @@ void Terrain3D::_update_collision() {
PackedRealArray map_data = PackedRealArray();
map_data.resize(shape_size * shape_size);

Vector2i global_offset = Vector2i(_storage->get_region_offsets()[i]) * region_size;
Vector3 global_pos = Vector3(global_offset.x, 0.f, global_offset.y);
Vector2i global_loc = Vector2i(_storage->get_region_locations()[i]) * region_size;
Vector3 global_pos = Vector3(global_loc.x, 0.f, global_loc.y);

Ref<Image> map, map_x, map_z, map_xz;
Ref<Image> cmap, cmap_x, cmap_z, cmap_xz;
map = _storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, i);
cmap = _storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, i);
int region = _storage->get_region_index(Vector3(global_pos.x + region_size, 0.f, global_pos.z) * _mesh_vertex_spacing);
if (region >= 0) {
map_x = _storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region);
cmap_x = _storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, region);
int region_id = _storage->get_region_id(Vector3(global_pos.x + region_size, 0.f, global_pos.z) * _mesh_vertex_spacing);
if (region_id >= 0) {
map_x = _storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region_id);
cmap_x = _storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, region_id);
}
region = _storage->get_region_index(Vector3(global_pos.x, 0.f, global_pos.z + region_size) * _mesh_vertex_spacing);
if (region >= 0) {
map_z = _storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region);
cmap_z = _storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, region);
region_id = _storage->get_region_id(Vector3(global_pos.x, 0.f, global_pos.z + region_size) * _mesh_vertex_spacing);
if (region_id >= 0) {
map_z = _storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region_id);
cmap_z = _storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, region_id);
}
region = _storage->get_region_index(Vector3(global_pos.x + region_size, 0.f, global_pos.z + region_size) * _mesh_vertex_spacing);
if (region >= 0) {
map_xz = _storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region);
cmap_xz = _storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, region);
region_id = _storage->get_region_id(Vector3(global_pos.x + region_size, 0.f, global_pos.z + region_size) * _mesh_vertex_spacing);
if (region_id >= 0) {
map_xz = _storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region_id);
cmap_xz = _storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, region_id);
}

for (int z = 0; z < shape_size; z++) {
Expand Down Expand Up @@ -547,12 +547,12 @@ void Terrain3D::_generate_triangles(PackedVector3Array &p_vertices, PackedVector
if (!p_global_aabb.has_volume()) {
int32_t region_size = (int)_storage->get_region_size();

TypedArray<Vector2i> region_offsets = _storage->get_region_offsets();
for (int r = 0; r < region_offsets.size(); ++r) {
Vector2i region_offset = (Vector2i)region_offsets[r] * region_size;
TypedArray<Vector2i> region_locations = _storage->get_region_locations();
for (int r = 0; r < region_locations.size(); ++r) {
Vector2i region_loc = (Vector2i)region_locations[r] * region_size;

for (int32_t z = region_offset.y; z < region_offset.y + region_size; z += step) {
for (int32_t x = region_offset.x; x < region_offset.x + region_size; x += step) {
for (int32_t z = region_loc.y; z < region_loc.y + region_size; z += step) {
for (int32_t x = region_loc.x; x < region_loc.x + region_size; x += step) {
_generate_triangle_pair(p_vertices, p_uvs, p_lod, p_filter, p_require_nav, x, z);
}
}
Expand Down
38 changes: 19 additions & 19 deletions src/terrain_3d_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
///////////////////////////

void Terrain3DEditor::_region_modified(const Vector3 &p_global_position, const Vector2 &p_height_range) {
Vector2i region_offset = _terrain->get_storage()->get_region_offset(p_global_position);
Vector2i region_loc = _terrain->get_storage()->get_region_location(p_global_position);
Terrain3DStorage::RegionSize region_size = _terrain->get_storage()->get_region_size();

AABB edited_area;
edited_area.position = Vector3(region_offset.x * region_size, p_height_range.x, region_offset.y * region_size);
edited_area.position = Vector3(region_loc.x * region_size, p_height_range.x, region_loc.y * region_size);
edited_area.size = Vector3(region_size, p_height_range.y - p_height_range.x, region_size);
edited_area.position *= _terrain->get_mesh_vertex_spacing();
edited_area.size *= _terrain->get_mesh_vertex_spacing();
Expand All @@ -37,8 +37,8 @@ void Terrain3DEditor::_operate_region(const Vector3 &p_global_position) {
}
} else {
if (has_region) {
int region_index = _terrain->get_storage()->get_region_index(p_global_position);
Ref<Image> height_map = _terrain->get_storage()->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region_index);
int region_id = _terrain->get_storage()->get_region_id(p_global_position);
Ref<Image> height_map = _terrain->get_storage()->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region_id);
height_range = Util::get_min_max(height_map);

_terrain->get_storage()->remove_region(p_global_position);
Expand All @@ -59,16 +59,16 @@ void Terrain3DEditor::_operate_map(const Vector3 &p_global_position, const real_
Ref<Terrain3DStorage> storage = _terrain->get_storage();
int region_size = storage->get_region_size();
Vector2i region_vsize = Vector2i(region_size, region_size);
int region_index = storage->get_region_index(p_global_position);
if (region_index == -1) {
int region_id = storage->get_region_id(p_global_position);
if (region_id == -1) {
if (!_brush_data["auto_regions"] || _tool != HEIGHT) {
return;
} else {
LOG(DEBUG, "No region to operate on, attempting to add");
storage->add_region(p_global_position);
region_size = storage->get_region_size();
region_index = storage->get_region_index(p_global_position);
if (region_index == -1) {
region_id = storage->get_region_id(p_global_position);
if (region_id == -1) {
LOG(ERROR, "Failed to add region, no region to operate on");
return;
}
Expand Down Expand Up @@ -102,7 +102,7 @@ void Terrain3DEditor::_operate_map(const Vector3 &p_global_position, const real_
return;
}

Ref<Image> map = storage->get_map_region(map_type, region_index);
Ref<Image> map = storage->get_map_region(map_type, region_id);
real_t brush_size = _brush_data["size"];
int asset_id = _brush_data["asset_id"];
Vector2i img_size = _brush_data["brush_image_size"];
Expand Down Expand Up @@ -152,22 +152,22 @@ void Terrain3DEditor::_operate_map(const Vector3 &p_global_position, const real_
p_global_position.z + brush_offset.y + .5f);

// If we're brushing across a region boundary, possibly add a region, and get the other map
int new_region_index = storage->get_region_index(brush_global_position);
if (new_region_index == -1) {
int new_region_id = storage->get_region_id(brush_global_position);
if (new_region_id == -1) {
if (!_brush_data["auto_regions"] || _tool != HEIGHT) {
continue;
}
Error err = storage->add_region(brush_global_position);
if (err) {
continue;
}
new_region_index = storage->get_region_index(brush_global_position);
new_region_id = storage->get_region_id(brush_global_position);
_region_modified(brush_global_position);
}

if (new_region_index != region_index) {
region_index = new_region_index;
map = storage->get_map_region(map_type, region_index);
if (new_region_id != region_id) {
region_id = new_region_id;
map = storage->get_map_region(map_type, region_id);
}

// Identify position on map image
Expand Down Expand Up @@ -481,8 +481,8 @@ Dictionary Terrain3DEditor::_get_undo_data() const {
}
switch (_tool) {
case REGION:
LOG(DEBUG, "Storing region offsets");
data["region_offsets"] = _terrain->get_storage()->get_region_offsets().duplicate();
LOG(DEBUG, "Storing region locations");
data["region_locations"] = _terrain->get_storage()->get_region_locations().duplicate();
if (_operation == SUBTRACT) {
data["height_map"] = _terrain->get_storage()->get_maps_copy(Terrain3DStorage::TYPE_HEIGHT);
data["control_map"] = _terrain->get_storage()->get_maps_copy(Terrain3DStorage::TYPE_CONTROL);
Expand All @@ -494,7 +494,7 @@ Dictionary Terrain3DEditor::_get_undo_data() const {

case HEIGHT:
LOG(DEBUG, "Storing height maps and range");
data["region_offsets"] = _terrain->get_storage()->get_region_offsets().duplicate();
data["region_locations"] = _terrain->get_storage()->get_region_locations().duplicate();
data["height_map"] = _terrain->get_storage()->get_maps_copy(Terrain3DStorage::TYPE_HEIGHT);
data["height_range"] = _terrain->get_storage()->get_height_range();
data["edited_area"] = _terrain->get_storage()->get_edited_area();
Expand Down Expand Up @@ -564,7 +564,7 @@ void Terrain3DEditor::_apply_undo(const Dictionary &p_set) {
for (int i = 0; i < keys.size(); i++) {
String key = keys[i];
if (key == "region_offsets") {
_terrain->get_storage()->set_region_offsets(p_set[key]);
_terrain->get_storage()->set_region_locations(p_set[key]);
} else if (key == "height_map") {
_terrain->get_storage()->set_maps(Terrain3DStorage::TYPE_HEIGHT, p_set[key]);
} else if (key == "control_map") {
Expand Down
Loading

0 comments on commit 4fcb2a4

Please sign in to comment.