From ab69a75264acd2a6582dab27f609a93c94c0b092 Mon Sep 17 00:00:00 2001 From: Cory Petkovsek <632766+TokisanGames@users.noreply.github.com> Date: Sat, 21 Oct 2023 02:31:14 +0700 Subject: [PATCH] =?UTF-8?q?=EF=BB=BFConvert=20control=20map=20to=20new=20f?= =?UTF-8?q?ormat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/logger.h | 2 +- src/terrain_3d_storage.cpp | 32 +++++++++++++++++++++++++++++++- src/terrain_3d_storage.h | 4 +++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/logger.h b/src/logger.h index 41f4d9ced..c48dac0f2 100644 --- a/src/logger.h +++ b/src/logger.h @@ -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 \ No newline at end of file diff --git a/src/terrain_3d_storage.cpp b/src/terrain_3d_storage.cpp index 19c0cb150..ac6272f15 100644 --- a/src/terrain_3d_storage.cpp +++ b/src/terrain_3d_storage.cpp @@ -371,7 +371,37 @@ void Terrain3DStorage::set_height_maps(const TypedArray &p_maps) { void Terrain3DStorage::set_control_maps(const TypedArray &p_maps) { LOG(INFO, "Setting control maps: ", p_maps.size()); - _control_maps = sanitize_maps(TYPE_CONTROL, p_maps); + TypedArray 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(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 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 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); } diff --git a/src/terrain_3d_storage.h b/src/terrain_3d_storage.h index f600dcc02..f26ff0faa 100644 --- a/src/terrain_3d_storage.h +++ b/src/terrain_3d_storage.h @@ -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 }; @@ -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,