Terrain3DUtil

Inherits: Object

Description

This class contains static utility functions. Reference them with the full class name. Eg. Terrain3DUtil.as_float().

Or you can instance the class for a shorter alias:

var util := Terrain3DUtil.new()
var my_float: float = util.as_float(my_int)

Note on uints: Various functions refer to unsigned integers as uint. Though GDScript doesn’t support unsigned integers as a type, the C++ that receives and returns these values will interpret them all as unsigned.

Note on bitwise-ORing: To write back to a control map, encode your values and bitwise OR the results, then reinterpret that uint as a float. The shader will interpret the float as uint and extract the bits.

var bits: int = util.enc_base(base_id) | util.enc_overlay(over_id) | util.enc_blend(blend) | \
    util.enc_auto(autoshader) | util.enc_nav(navigation) | util.enc_hole(hole)
var color: Color = Color(util.as_float(bits), 0., 0., 1.)
storage.set_control(global_pos, color)

Methods

float

as_float(value: int) static

int

as_uint(value: float) static

Image

black_to_alpha(image: Image) static

int

enc_auto(pixel: bool) static

int

enc_base(base: int) static

int

enc_blend(blend: int) static

int

enc_hole(pixel: bool) static

int

enc_nav(pixel: bool) static

int

enc_overlay(overlay: int) static

int

get_base(pixel: int) static

int

get_blend(pixel: int) static

Image

get_filled_image(size: Vector2i, color: Color, create_mipmaps: bool, format: Format) static

Vector2

get_min_max(image: Image) static

int

get_overlay(pixel: int) static

Image

get_thumbnail(image: Image, size: Vector2i = Vector2i(256, 256)) static

bool

is_auto(pixel: int) static

bool

is_hole(pixel: int) static

bool

is_nav(pixel: int) static

Image

load_image(file_name: String, cache_mode: int = 0, r16_height_range: Vector2 = Vector2(0, 255), r16_size: Vector2i = Vector2i(0, 0)) static

Image

pack_image(src_rgb: Image, src_r: Image, invert_green_channel: bool = false) static


Method Descriptions

float as_float(value: int) static

Returns a float typed variable with the contents of the memory stored in value, an integer typed variable.

This function does not convert integer values to float values (e.g. 4 -> 4.0). It reinterprets the memory block as if it were a float. If the data in value was a valid integer, it is now an invalid float.

my_float == util.as_float(util.as_uint(my_float))

See as_uint for the opposite.


int as_uint(value: float) static

Returns an integer typed variable with the contents of the memory stored in value, a float typed variable.

This function does not convert float values to integer values (e.g. 4.0 -> 4). It reinterprets the memory block as if it were an integer. If the data in value was a valid float, it is now a valid integer, but probably an unexepctedly large value.

my_int == util.as_uint(util.as_float(my_int))

See as_float for the opposite.


Image black_to_alpha(image: Image) static

Receives an image with a black background and returns one with a transparent background, aka an alpha mask.


int enc_auto(pixel: bool) static

Returns a control map uint with the auto shader bit set. See the top description for usage.


int enc_base(base: int) static

Returns a control map uint with the base texture ID encoded. See the top description for usage.


int enc_blend(blend: int) static

Returns a control map uint with the blend value encoded. See the top description for usage.


int enc_hole(pixel: bool) static

Returns a control map uint with the hole bit set. See the top description for usage.


int enc_nav(pixel: bool) static

Returns a control map uint with the nav bit set. See the top description for usage.


int enc_overlay(overlay: int) static

Returns a control map uint with the overlay texture ID encoded. See the top description for usage.


int get_base(pixel: int) static

Returns the base texture ID from a control map pixel.


int get_blend(pixel: int) static

Returns the blend value from a control map pixel.


Image get_filled_image(size: Vector2i, color: Color, create_mipmaps: bool, format: Format) static

Returns an Image filled with a specified color and format.

If color.a < 0, its filled with a checkered pattern multiplied by color.rgb.

The behavior changes if a compressed format is requested:

  • If the editor is running and the format is DXT1, DXT5, or BPTC_RGBA, it returns a filled image in the requested color and format.

  • All other compressed formats return a blank image in that format.

The reason for this is the Image compression library is available only in the editor. And it is unreliable, offering little control over the output format, choosing automatically and often wrong. We have selected a few compressed formats it gets right.


Vector2 get_min_max(image: Image) static

Returns the minimum and maximum r channel values of an Image. Used for heightmaps.


int get_overlay(pixel: int) static

Returns the overlay texture ID from a control map pixel.


Image get_thumbnail(image: Image, size: Vector2i = Vector2i(256, 256)) static

Returns an Image normalized and converted to RGB8. Used for creating a human viewable image of a heightmap, at any size.


bool is_auto(pixel: int) static

Returns true if the control map pixel has the autoshader bit set.


bool is_hole(pixel: int) static

Returns true if the control map pixel has the hole bit set.


bool is_nav(pixel: int) static

Returns true if the control map pixel has the nav bit set.


Image load_image(file_name: String, cache_mode: int = 0, r16_height_range: Vector2 = Vector2(0, 255), r16_size: Vector2i = Vector2i(0, 0)) static

Loads a file from disk and returns an Image.

filename - The file name on disk to load. Loads EXR, R16/RAW, PNG, or a ResourceLoader format (jpg, res, tres, etc).

cache_mode - Send this flag to the resource loader to force caching or not.

height_range - Heights for R16 format. x=Min & y=Max value ranges. Required for R16 import.

size - Image dimensions for R16 format. Default (0,0) auto detects size, assuming square images. Required for non-square R16.


Image pack_image(src_rgb: Image, src_r: Image, invert_green_channel: bool = false) static

Returns an Image with the following content:

  • RGB channels from src_rgb.

  • A channel from src_r.

  • G will be inverted if specified. Used for converting normal maps between DirectX and OpenGL.