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_uv_rotation(uvrotation) | \
util.enc_uv_scale(uvscale) | util.enc_auto(autoshader) | \
util.enc_nav(navigation) | util.enc_hole(hole)
var color: Color = Color(util.as_float(bits), 0., 0., 1.)
data.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

enc_uv_rotation(rotation: int) static

int

enc_uv_scale(scale: int) static

Vector2i

filename_to_location(filename: String) 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: Image.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

int

get_uv_rotation(pixel: int) static

int

get_uv_scale(pixel: int) 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

String

location_to_filename(region_location: Vector2i) static

Image

luminance_to_height(src_rgb: Image) static

Image

pack_image(src_rgb: Image, src_a: Image, invert_green: bool = false, invert_alpha: bool = false, alpha_channel: int = 0) 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 enc_uv_rotation(rotation: int) static 🔗

Returns a control map uint with the texture rotation encoded. See the top description for usage. See get_uv_rotation() for values.


int enc_uv_scale(scale: int) static 🔗

Returns a control map uint with the texture scale encoded. See the top description for usage. See get_uv_scale() for values.


Vector2i filename_to_location(filename: String) static 🔗

Converts a file name string like terrain3d-01_02.res to a region location like (-1, 2). - is negative, _ is positive.


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: Image.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.


int get_uv_rotation(pixel: int) static 🔗

Returns the texture rotation from a control map pixel. Values are 0 - 15, which provides degrees when multiplied by 22.5. (360/16).


int get_uv_scale(pixel: int) static 🔗

Returns the texture scale modification from a control map pixel. Values are an index into the array { 0, 20, 40, 60, 80, -60, -40, -20 }. 0 indicates no scale modification. Index 2 indicates a 40% increase in texture scale at that pixel. Index -1 or 7 indicates a -20% texture scale change.


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.


String location_to_filename(region_location: Vector2i) static 🔗

Converts a region location like (-1, 2) to a file name string like terrain3d-01_02.res. - is negative, _ is positive.


Image luminance_to_height(src_rgb: Image) static 🔗

Generates a greyscale RGB8 height texture from the luminance values of the source image.


Image pack_image(src_rgb: Image, src_a: Image, invert_green: bool = false, invert_alpha: bool = false, alpha_channel: int = 0) static 🔗

Returns an RGBA Image packed for terrain usage.

  • src_rgb - The source Image for the RGB channels.

  • src_a - The source image for the A channel.

  • invert_green - Inverts the green channel to convert between OpenGL and DirectX normal maps.

  • invert_alpha - Inverts the alpha channel to convert between Roughness and Smoothness maps.

  • alpha_channel - The channel index (0-3: R,G,B,A) to use from src_a for the alpha channel.