Skip to content

Commit 74437e6

Browse files
committed
Fix legacy forward-compat
1 parent f59fea6 commit 74437e6

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

addons/zylann.hterrain/hterrain_data.gd

+28-16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const HT_Logger = preload("./util/logger.gd")
1212
const HT_ImageFileCache = preload("./util/image_file_cache.gd")
1313
const HT_XYZFormat = preload("./util/xyz_format.gd")
1414

15+
const ENABLE_LEGACY_HEIGHTMAP_RH_CONVERSION_IN_EDITOR = true
16+
1517
enum {
1618
BIT_DEPTH_UNDEFINED = 0,
1719
BIT_DEPTH_16 = 16,
@@ -1331,29 +1333,39 @@ func _load_map(dir: String, map_type: int, index: int, resource_loader_cache_mod
13311333

13321334
var must_load_image_in_editor := true
13331335

1334-
# Forward-compatibility with old heightmap encodings
1335-
if Engine.is_editor_hint() and tex == null and map_type == CHANNEL_HEIGHT:
1336-
var legacy_fpath := fpath.get_basename() + ".png"
1337-
var temp = ResourceLoader.load(legacy_fpath, "", resource_loader_cache_mode)
1338-
if temp != null:
1339-
if temp is Texture2D:
1340-
temp = temp.get_image()
1341-
if temp is Image:
1342-
if temp.get_format() == Image.FORMAT_RGB8:
1336+
if Engine.is_editor_hint():
1337+
# Short-term compatibility with RGB8 encoding from the godot4 branch
1338+
if tex == null and map_type == CHANNEL_HEIGHT:
1339+
var legacy_fpath := fpath.get_basename() + ".png"
1340+
var temp = ResourceLoader.load(legacy_fpath, "", resource_loader_cache_mode)
1341+
if temp != null:
1342+
if temp is Texture2D:
1343+
temp = temp.get_image()
1344+
if temp is Image and temp.get_format() == Image.FORMAT_RGB8:
13431345
_logger.warn(str(
13441346
"Found a heightmap using legacy RGB8 format. It will be converted to RF. ",
13451347
"You may want to remove the old file: {0}").format([fpath]))
13461348
tex = convert_heightmap_to_float(temp, _logger)
13471349
# This is a different file so we can save without overwriting the old path
13481350
_save_map_image(fpath.get_basename(), map_type, tex)
13491351

1350-
elif temp.get_format() == Image.FORMAT_RH:
1351-
_logger.warn(str(
1352-
"Found a heightmap using legacy RH format. It will be converted to RF. ",
1353-
"You may edit and re-save to make the upgrade persist."))
1354-
tex = convert_heightmap_to_float(temp, _logger)
1355-
# Not saving yet to prevent unintentional data loss if anything goes wrong?
1356-
#_save_map_image(fpath.get_basename(), map_type, tex)
1352+
# Forward-compatibility with legacy format used in godot3
1353+
if ENABLE_LEGACY_HEIGHTMAP_RH_CONVERSION_IN_EDITOR \
1354+
and tex != null and map_type == CHANNEL_HEIGHT:
1355+
var temp := tex as Image
1356+
if temp == null and tex is Texture2D:
1357+
var tex_format := RenderingServer.texture_get_format(tex.get_rid())
1358+
if tex_format == Image.FORMAT_RH:
1359+
# Downloads image from RenderingServer, might be expensive
1360+
temp = tex.get_image()
1361+
if temp != null and temp.get_format() == Image.FORMAT_RH:
1362+
_logger.warn(str(
1363+
"Found a heightmap using legacy RH format. It will be converted to RF. ",
1364+
"You may edit and re-save to make the upgrade persist."))
1365+
temp.convert(Image.FORMAT_RF)
1366+
tex = temp
1367+
# Not saving yet to prevent unintentional data loss if anything goes wrong?
1368+
#_save_map_image(fpath.get_basename(), map_type, tex)
13571369

13581370
if tex != null and tex is Image:
13591371
# The texture is imported as Image,

0 commit comments

Comments
 (0)