11// Copyright © 2025 Cory Petkovsek, Roope Palmroos, and Contributors.
22
3- #include < godot_cpp/classes/engine.hpp>
43#include < godot_cpp/classes/resource_saver.hpp>
54
65#include " logger.h"
@@ -66,9 +65,6 @@ Ref<Image> Terrain3DRegion::get_map(const MapType p_map_type) const {
6665 case TYPE_CONTROL:
6766 return _control_map;
6867 case TYPE_COLOR:
69- if (!IS_EDITOR && _compressed_color_map.is_valid ()) {
70- return _compressed_color_map;
71- }
7268 return _color_map;
7369 default :
7470 LOG (ERROR, " Requested map type " , p_map_type, " , is invalid" );
@@ -83,9 +79,6 @@ Image *Terrain3DRegion::get_map_ptr(const MapType p_map_type) const {
8379 case TYPE_CONTROL:
8480 return *_control_map;
8581 case TYPE_COLOR:
86- if (!IS_EDITOR && _compressed_color_map.is_valid ()) {
87- return *_compressed_color_map;
88- }
8982 return *_color_map;
9083 default :
9184 LOG (ERROR, " Requested map type " , p_map_type, " , is invalid" );
@@ -156,6 +149,11 @@ void Terrain3DRegion::set_color_map(const Ref<Image> &p_map) {
156149 _color_map = map;
157150}
158151
152+ void Terrain3DRegion::clear_color_map () {
153+ LOG (WARN, " Freeing color map for region: " , (_location.x != INT32_MAX) ? String (_location) : " (new)" );
154+ _color_map.unref ();
155+ }
156+
159157void Terrain3DRegion::set_compressed_color_map (const Ref<Image> &p_map) {
160158 SET_IF_DIFF (_compressed_color_map, p_map);
161159 LOG (INFO, " Setting compressed color map for region: " , (_location.x != INT32_MAX) ? String (_location) : " (new)" );
@@ -169,12 +167,30 @@ void Terrain3DRegion::set_compressed_color_map(const Ref<Image> &p_map) {
169167 _compressed_color_map = p_map;
170168}
171169
172- void Terrain3DRegion::free_uncompressed_color_map () {
173- LOG (INFO, " Freeing uncompressed color map" );
174- _color_map.unref ();
170+ void Terrain3DRegion::compress_color_map (const CompressMode p_compress_mode) {
171+ if (_color_map.is_null () || _color_map->is_empty ()) {
172+ LOG (ERROR, " Color map is null or empty" );
173+ }
174+ if (!IS_EDITOR) {
175+ LOG (ERROR, " Cannot compress maps in export builds" );
176+ return ;
177+ }
178+ if (p_compress_mode > COMPRESS_NONE && p_compress_mode <= COMPRESS_ASTC) {
179+ LOG (MESG, " Compressing color map with " , COMPRESS_STR[p_compress_mode]);
180+ _compressed_color_map = Image::create_from_data (_color_map->get_width (), _color_map->get_height (),
181+ _color_map->has_mipmaps (), _color_map->get_format (), _color_map->get_data ());
182+ _compressed_color_map->copy_from (_color_map);
183+ _compressed_color_map->compress_from_channels (get_image_compress_mode (p_compress_mode), Image::USED_CHANNELS_RGBA);
184+ _modified = true ;
185+ }
186+ }
187+
188+ void Terrain3DRegion::clear_compressed_color_map () {
189+ LOG (WARN, " Freeing compressed color map for region: " , (_location.x != INT32_MAX) ? String (_location) : " (new)" );
190+ _compressed_color_map.unref ();
175191}
176192
177- void Terrain3DRegion::sanitize_maps (bool p_free_uncompressed_color_maps ) {
193+ void Terrain3DRegion::sanitize_maps () {
178194 if (_region_size == 0 ) { // blank region, no set_*_map has been called
179195 LOG (ERROR, " Set region_size first" );
180196 return ;
@@ -189,10 +205,6 @@ void Terrain3DRegion::sanitize_maps(bool p_free_uncompressed_color_maps) {
189205 _modified = true ;
190206 }
191207 _control_map = map;
192- if (p_free_uncompressed_color_maps) {
193- free_uncompressed_color_map ();
194- return ;
195- }
196208 map = sanitize_map (TYPE_COLOR, _color_map);
197209 if (_color_map != map) {
198210 _modified = true ;
@@ -332,11 +344,7 @@ Error Terrain3DRegion::save(const String &p_path, const bool p_16_bit, const Com
332344 Error err = OK;
333345 _compressed_color_map.unref ();
334346 if (IS_EDITOR && p_color_compress_mode != COMPRESS_NONE) {
335- LOG (DEBUG, " Compressing color map to format: " , COMPRESS_STR[p_color_compress_mode]);
336- _compressed_color_map = Image::create_from_data (_color_map->get_width (), _color_map->get_height (),
337- _color_map->has_mipmaps (), _color_map->get_format (), _color_map->get_data ());
338- _compressed_color_map->copy_from (_color_map);
339- _compressed_color_map->compress_from_channels (get_image_compress_mode (p_color_compress_mode), Image::USED_CHANNELS_RGBA);
347+ compress_color_map (p_color_compress_mode);
340348 }
341349 if (p_16_bit) {
342350 Ref<Image> original_map;
@@ -390,6 +398,7 @@ Dictionary Terrain3DRegion::get_data() const {
390398 dict[" height_map" ] = _height_map;
391399 dict[" control_map" ] = _control_map;
392400 dict[" color_map" ] = _color_map;
401+ dict[" compressed_color_map" ] = _compressed_color_map;
393402 dict[" instances" ] = _instances;
394403 return dict;
395404}
@@ -503,10 +512,13 @@ void Terrain3DRegion::_bind_methods() {
503512 ClassDB::bind_method (D_METHOD (" get_control_map" ), &Terrain3DRegion::get_control_map);
504513 ClassDB::bind_method (D_METHOD (" set_color_map" , " map" ), &Terrain3DRegion::set_color_map);
505514 ClassDB::bind_method (D_METHOD (" get_color_map" ), &Terrain3DRegion::get_color_map);
515+ ClassDB::bind_method (D_METHOD (" clear_color_map" ), &Terrain3DRegion::clear_color_map);
516+ ClassDB::bind_method (D_METHOD (" get_active_color_map" ), &Terrain3DRegion::get_active_color_map);
506517 ClassDB::bind_method (D_METHOD (" set_compressed_color_map" , " map" ), &Terrain3DRegion::set_compressed_color_map);
507518 ClassDB::bind_method (D_METHOD (" get_compressed_color_map" ), &Terrain3DRegion::get_compressed_color_map);
508- ClassDB::bind_method (D_METHOD (" free_uncompressed_color_map" ), &Terrain3DRegion::free_uncompressed_color_map);
509- ClassDB::bind_method (D_METHOD (" sanitize_maps" , " free_uncompressed_color_maps" ), &Terrain3DRegion::sanitize_maps, DEFVAL (false ));
519+ ClassDB::bind_method (D_METHOD (" compress_color_map" ), &Terrain3DRegion::compress_color_map);
520+ ClassDB::bind_method (D_METHOD (" clear_compressed_color_map" ), &Terrain3DRegion::clear_compressed_color_map);
521+ ClassDB::bind_method (D_METHOD (" sanitize_maps" ), &Terrain3DRegion::sanitize_maps);
510522 ClassDB::bind_method (D_METHOD (" sanitize_map" , " map_type" , " map" ), &Terrain3DRegion::sanitize_map);
511523 ClassDB::bind_method (D_METHOD (" validate_map_size" , " map" ), &Terrain3DRegion::validate_map_size);
512524
0 commit comments