Skip to content

Commit

Permalink
Add shortcuts for MapType
Browse files Browse the repository at this point in the history
  • Loading branch information
TokisanGames committed Aug 2, 2024
1 parent 8d93422 commit 9156142
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
16 changes: 8 additions & 8 deletions src/terrain_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,22 +414,22 @@ void Terrain3D::_update_collision() {

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);
map = _storage->get_map_region(TYPE_HEIGHT, i);
cmap = _storage->get_map_region(TYPE_CONTROL, i);
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);
map_x = _storage->get_map_region(TYPE_HEIGHT, region_id);
cmap_x = _storage->get_map_region(TYPE_CONTROL, region_id);
}
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);
map_z = _storage->get_map_region(TYPE_HEIGHT, region_id);
cmap_z = _storage->get_map_region(TYPE_CONTROL, region_id);
}
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);
map_xz = _storage->get_map_region(TYPE_HEIGHT, region_id);
cmap_xz = _storage->get_map_region(TYPE_CONTROL, region_id);
}

for (int z = 0; z < shape_size; z++) {
Expand Down
34 changes: 17 additions & 17 deletions src/terrain_3d_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void Terrain3DEditor::_operate_region(const Vector3 &p_global_position) {
} else {
if (has_region) {
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);
Ref<Image> height_map = _terrain->get_storage()->get_map_region(TYPE_HEIGHT, region_id);
height_range = Util::get_min_max(height_map);

_terrain->get_storage()->remove_region(p_global_position);
Expand Down Expand Up @@ -76,23 +76,23 @@ void Terrain3DEditor::_operate_map(const Vector3 &p_global_position, const real_
}
}

Terrain3DStorage::MapType map_type;
MapType map_type;
switch (_tool) {
case HEIGHT:
case INSTANCER:
map_type = Terrain3DStorage::TYPE_HEIGHT;
map_type = TYPE_HEIGHT;
break;
case TEXTURE:
case AUTOSHADER:
case HOLES:
case NAVIGATION:
case ANGLE:
case SCALE:
map_type = Terrain3DStorage::TYPE_CONTROL;
map_type = TYPE_CONTROL;
break;
case COLOR:
case ROUGHNESS:
map_type = Terrain3DStorage::TYPE_COLOR;
map_type = TYPE_COLOR;
break;
default:
LOG(ERROR, "Invalid tool selected");
Expand Down Expand Up @@ -193,7 +193,7 @@ void Terrain3DEditor::_operate_map(const Vector3 &p_global_position, const real_
Color src = map->get_pixelv(map_pixel_position);
Color dest = src;

if (map_type == Terrain3DStorage::TYPE_HEIGHT) {
if (map_type == TYPE_HEIGHT) {
real_t srcf = src.r;
real_t destf = dest.r;

Expand Down Expand Up @@ -295,7 +295,7 @@ void Terrain3DEditor::_operate_map(const Vector3 &p_global_position, const real_
edited_position.y = destf;
edited_area = edited_area.expand(edited_position);

} else if (map_type == Terrain3DStorage::TYPE_CONTROL) {
} else if (map_type == TYPE_CONTROL) {
// Get bit field from pixel
uint32_t base_id = get_base(src.r);
uint32_t overlay_id = get_overlay(src.r);
Expand Down Expand Up @@ -412,7 +412,7 @@ void Terrain3DEditor::_operate_map(const Vector3 &p_global_position, const real_
// Write back to pixel in FORMAT_RF. Must be a 32-bit float
dest = Color(as_float(bits), 0.f, 0.f, 1.f);

} else if (map_type == Terrain3DStorage::TYPE_COLOR) {
} else if (map_type == TYPE_COLOR) {
switch (_tool) {
case COLOR:
dest = src.lerp((_operation == ADD) ? color : COLOR_WHITE, brush_alpha * strength);
Expand Down Expand Up @@ -482,9 +482,9 @@ Dictionary Terrain3DEditor::_get_undo_data() const {
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);
data["color_map"] = _terrain->get_storage()->get_maps_copy(Terrain3DStorage::TYPE_COLOR);
data["height_map"] = _terrain->get_storage()->get_maps_copy(TYPE_HEIGHT);
data["control_map"] = _terrain->get_storage()->get_maps_copy(TYPE_CONTROL);
data["color_map"] = _terrain->get_storage()->get_maps_copy(TYPE_COLOR);
data["height_range"] = _terrain->get_storage()->get_height_range();
data["edited_area"] = _terrain->get_storage()->get_edited_area();
}
Expand All @@ -493,7 +493,7 @@ Dictionary Terrain3DEditor::_get_undo_data() const {
case HEIGHT:
LOG(DEBUG, "Storing height maps and range");
data["region_locations"] = _terrain->get_storage()->get_region_locations().duplicate();
data["height_map"] = _terrain->get_storage()->get_maps_copy(Terrain3DStorage::TYPE_HEIGHT);
data["height_map"] = _terrain->get_storage()->get_maps_copy(TYPE_HEIGHT);
data["height_range"] = _terrain->get_storage()->get_height_range();
data["edited_area"] = _terrain->get_storage()->get_edited_area();
break;
Expand All @@ -506,13 +506,13 @@ Dictionary Terrain3DEditor::_get_undo_data() const {
case AUTOSHADER:
case NAVIGATION:
LOG(DEBUG, "Storing control maps");
data["control_map"] = _terrain->get_storage()->get_maps_copy(Terrain3DStorage::TYPE_CONTROL);
data["control_map"] = _terrain->get_storage()->get_maps_copy(TYPE_CONTROL);
break;

case COLOR:
case ROUGHNESS:
LOG(DEBUG, "Storing color maps");
data["color_map"] = _terrain->get_storage()->get_maps_copy(Terrain3DStorage::TYPE_COLOR);
data["color_map"] = _terrain->get_storage()->get_maps_copy(TYPE_COLOR);
break;

case INSTANCER:
Expand Down Expand Up @@ -567,11 +567,11 @@ void Terrain3DEditor::_apply_undo(const Dictionary &p_set) {
if (key == "region_offsets") {
_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]);
_terrain->get_storage()->set_maps(TYPE_HEIGHT, p_set[key]);
} else if (key == "control_map") {
_terrain->get_storage()->set_maps(Terrain3DStorage::TYPE_CONTROL, p_set[key]);
_terrain->get_storage()->set_maps(TYPE_CONTROL, p_set[key]);
} else if (key == "color_map") {
_terrain->get_storage()->set_maps(Terrain3DStorage::TYPE_COLOR, p_set[key]);
_terrain->get_storage()->set_maps(TYPE_COLOR, p_set[key]);
} else if (key == "height_range") {
_terrain->get_storage()->set_height_range(p_set[key]);
} else if (key == "edited_area") {
Expand Down
2 changes: 1 addition & 1 deletion src/terrain_3d_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ void Terrain3DStorage::save() {
if (_save_16_bit) {
LOG(DEBUG, "16-bit save requested, converting heightmaps");
TypedArray<Image> original_maps;
original_maps = get_maps_copy(Terrain3DStorage::MapType::TYPE_HEIGHT);
original_maps = get_maps_copy(TYPE_HEIGHT);
for (int i = 0; i < _height_maps.size(); i++) {
Ref<Image> img = _height_maps[i];
img->convert(Image::FORMAT_RH);
Expand Down
5 changes: 5 additions & 0 deletions src/terrain_3d_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,12 @@ class Terrain3DStorage : public Resource {
static void _bind_methods();
};

typedef Terrain3DStorage::MapType MapType;
VARIANT_ENUM_CAST(Terrain3DStorage::MapType);
constexpr Terrain3DStorage::MapType TYPE_HEIGHT = Terrain3DStorage::MapType::TYPE_HEIGHT;
constexpr Terrain3DStorage::MapType TYPE_CONTROL = Terrain3DStorage::MapType::TYPE_CONTROL;
constexpr Terrain3DStorage::MapType TYPE_COLOR = Terrain3DStorage::MapType::TYPE_COLOR;
constexpr Terrain3DStorage::MapType TYPE_MAX = Terrain3DStorage::MapType::TYPE_MAX;
VARIANT_ENUM_CAST(Terrain3DStorage::RegionSize);
VARIANT_ENUM_CAST(Terrain3DStorage::HeightFilter);

Expand Down
2 changes: 1 addition & 1 deletion src/terrain_3d_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ Ref<Image> Terrain3DUtil::load_image(const String &p_file_name, const int p_cach
LOG(DEBUG, "Total file size is: ", fsize, " calculated width: ", fwidth, " dimensions: ", r16_size);
file->seek(0);
}
img = Image::create(r16_size.x, r16_size.y, false, Terrain3DStorage::FORMAT[Terrain3DStorage::TYPE_HEIGHT]);
img = Image::create(r16_size.x, r16_size.y, false, Terrain3DStorage::FORMAT[TYPE_HEIGHT]);
for (int y = 0; y < r16_size.y; y++) {
for (int x = 0; x < r16_size.x; x++) {
real_t h = real_t(file->get_16()) / 65535.0f;
Expand Down

0 comments on commit 9156142

Please sign in to comment.