Skip to content

Commit

Permalink
Support exporting 24-bit raw files
Browse files Browse the repository at this point in the history
  • Loading branch information
MJacred committed Jun 3, 2023
1 parent 691e8c5 commit 98bf3c4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ For a more detailed list of past and incoming changes, see the commit history.
- Added `cast_shadow` setting to `HTerrain`
- Added `cast_shadow` setting to `HTerrainDetailLayer`
- Added slope limit slider to detail density painting
- Added 24-bit support for importing raw files
- Added 24-bit support for exporting raw files
- Exposed `roughness` in detail layer shader (but reflections may be off due to the normals hack)
- Allow decimal values in `min_height` and `max_height` when importing a heightmap
- Fixed terrain not functional when using a 32-bit version of Godot (The GDNative library is only maintained for 64-bit)
Expand Down
30 changes: 23 additions & 7 deletions addons/zylann.hterrain/tools/exporter/export_image_dialog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ const HT_Logger = preload("../../util/logger.gd")

const FORMAT_RH = 0
const FORMAT_R16 = 1
const FORMAT_PNG8 = 2
const FORMAT_EXRH = 3
const FORMAT_COUNT = 4
const FORMAT_R24 = 2
const FORMAT_PNG8 = 3
const FORMAT_EXRH = 4
const FORMAT_COUNT = 5

@onready var _output_path_line_edit := $VB/Grid/OutputPath/HeightmapPathLineEdit as LineEdit
@onready var _format_selector := $VB/Grid/FormatSelector as OptionButton
Expand All @@ -38,12 +39,14 @@ func _ready():
_format_extensions.resize(FORMAT_COUNT)

_format_names[FORMAT_RH] = "16-bit RAW float"
_format_names[FORMAT_R16] = "16-bit RAW unsigned"
_format_names[FORMAT_R16] = "16-bit RAW int unsigned (little endian)"
_format_names[FORMAT_R24] = "24-bit RAW int unsigned (little endian)"
_format_names[FORMAT_PNG8] = "8-bit PNG"
_format_names[FORMAT_EXRH] = "16-bit float greyscale EXR"

_format_extensions[FORMAT_RH] = "raw"
_format_extensions[FORMAT_R16] = "raw"
_format_extensions[FORMAT_R24] = "raw"
_format_extensions[FORMAT_PNG8] = "png"
_format_extensions[FORMAT_EXRH] = "exr"

Expand Down Expand Up @@ -133,11 +136,11 @@ func _export() -> bool:
var err := FileAccess.get_open_error()
_print_file_error(fpath, err)
return false

if format == FORMAT_RH:
float_heightmap.convert(Image.FORMAT_RH)
f.store_buffer(float_heightmap.get_data())

elif format == FORMAT_R16:
var hscale := 65535.0 / (height_max - height_min)
for y in float_heightmap.get_height():
Expand All @@ -150,7 +153,20 @@ func _export() -> bool:
if x % 50 == 0:
_logger.debug(str(h))
f.store_16(h)


elif format == FORMAT_R24:
var hscale := 16777215.0 / (height_max - height_min)
for y in float_heightmap.get_height():
for x in float_heightmap.get_width():
var h := int((float_heightmap.get_pixel(x, y).r - height_min) * hscale)
if h < 0:
h = 0
elif h > 16777215:
h = 16777215
if x % 50 == 0:
_logger.debug(str(h))
HT_Util.file_store_24(f, h)

if save_error == OK:
_logger.debug("Exported heightmap as \"{0}\"".format([fpath]))
return true
Expand Down
15 changes: 15 additions & 0 deletions addons/zylann.hterrain/util/util.gd
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ static func file_get_24(f: FileAccess) -> int:
return res


static func file_store_24(f: FileAccess, h: int) -> void:
var a : int = h & 0xff
var b : int = (h >> 8) & 0xff
var c : int = (h >> 16) & 0xff

if f.big_endian:
var tmp : int = a
a = c
c = tmp

f.store_8(a)
f.store_8(b)
f.store_8(c)


static func integer_square_root(x: int) -> int:
assert(typeof(x) == TYPE_INT)
var r := int(roundf(sqrt(x)))
Expand Down

0 comments on commit 98bf3c4

Please sign in to comment.