Skip to content

Commit

Permalink
Convert control map to new format
Browse files Browse the repository at this point in the history
  • Loading branch information
TokisanGames committed Oct 26, 2023
1 parent 64dc3e4 commit ab69a75
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
else if (level == WARN) \
UtilityFunctions::push_warning(__class__, "::", __func__, ": ", __VA_ARGS__); \
else if (Terrain3D::_debug_level >= level) \
UtilityFunctions::print(__class__, "::", __func__, ": ", __VA_ARGS__)
UtilityFunctions::print(__class__, "::", __func__, ": ", __VA_ARGS__);

#endif // LOGGER_CLASS_H
32 changes: 31 additions & 1 deletion src/terrain_3d_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,37 @@ void Terrain3DStorage::set_height_maps(const TypedArray<Image> &p_maps) {

void Terrain3DStorage::set_control_maps(const TypedArray<Image> &p_maps) {
LOG(INFO, "Setting control maps: ", p_maps.size());
_control_maps = sanitize_maps(TYPE_CONTROL, p_maps);
TypedArray<Image> maps = p_maps;
// DEPRECATED 0.8.4 Remove 0.9
// Convert old RGB8 control format <0.8.4 to bit based format 0.8.41
if (_version < CURRENT_VERSION && maps.size() > 0 && (Ref<Image>(maps[0])->get_format() != FORMAT[TYPE_CONTROL])) {
LOG(WARN, "Control maps are being upgraded to int format: ", vformat("%.2f", _version), "->", vformat("%.2f", CURRENT_VERSION));
for (int i = 0; i < maps.size(); i++) {
Ref<Image> old_img = maps[i];
PackedByteArray pba;
pba.resize(_region_size * _region_size * sizeof(uint32_t));
for (int x = 0; x < old_img->get_width(); x++) {
for (int y = 0; y < old_img->get_height(); y++) {
Color pixel = old_img->get_pixel(x, y);
int base = (int(pixel.r * 255) & 0x1F) << 27; // 5 bits 31-27
int over = (int(pixel.g * 255) & 0x1F) << 22; // 5 bits 26-22
int blend_index = 7;
for (int k = 0; k < (sizeof(RANGE) / sizeof(*RANGE)); k++) {
if (pixel.b < (RANGE[k] + RANGE[k + 1]) / 2.0f) {
blend_index = k;
break;
}
}
blend_index <<= 19; // 3 bits 21-19
uint32_t value = base | over | blend_index;
pba.encode_u32((y * _region_size + x) * sizeof(uint32_t), value);
}
}
Ref<Image> new_img = Image::create_from_data(_region_size, _region_size, false, Image::FORMAT_RF, pba);
maps[i] = new_img;
}
}
_control_maps = sanitize_maps(TYPE_CONTROL, maps);
force_update_maps(TYPE_CONTROL);
}

Expand Down
4 changes: 3 additions & 1 deletion src/terrain_3d_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Terrain3DStorage : public Resource {

static inline const Image::Format FORMAT[] = {
Image::FORMAT_RF, // TYPE_HEIGHT
Image::FORMAT_RGB8, // TYPE_CONTROL
Image::FORMAT_RF, // TYPE_CONTROL
Image::FORMAT_RGBA8, // TYPE_COLOR
Image::Format(TYPE_MAX), // Proper size of array instead of FORMAT_MAX
};
Expand All @@ -52,6 +52,8 @@ class Terrain3DStorage : public Resource {
COLOR_ZERO, // TYPE_MAX, unused just in case someone indexes the array
};

static inline const float RANGE[] = { 0.0f, .125f, .25f, .334f, .5f, .667f, .8f, 1.0f };

enum RegionSize {
//SIZE_64 = 64,
//SIZE_128 = 128,
Expand Down

0 comments on commit ab69a75

Please sign in to comment.