BGE/VoxelMaps
Building and loading BGE.VoxelMaps (kept out of VoxelMap.bs: a namespace function returning a class from its own file can corrupt that file's types).
Alias: BGE.VoxelMaps
Static Methods
sizeShiftOf(size: integer): integer
log2 of a map size, or -1 if it isn't a power of 2 from 64 to 1024.
Parameters
size(integer)
Returns
integer
maxLevelCount(sizeShift: integer): integer
How many mip levels a map of this size has: up to 6, none smaller than 8x8.
Parameters
sizeShift(integer)
Returns
integer
toBytes(values: Array.<integer>): roByteArray
An integer array (values 0-255) as an roByteArray.
Parameters
values(Array.<integer>)
Returns
roByteArray
averageHeights(
heights: roByteArray,
levelShift: integer,
): roByteArray
The next mip level of a heightmap: each 2x2 averaged, rounded down.
Parameters
heights(roByteArray) — a level (1 << levelShift) cells squarelevelShift(integer) — log2 of that level's size
Returns
roByteArray
mostCommonColors(
colors: roByteArray,
levelShift: integer,
): roByteArray
The next mip level of a colour map: the most common palette index of each 2x2, a tie going to the first of the tied ones in the order (x, z), (x + 1, z), (x, z + 1), (x + 1, z + 1). tools/voxel-map.py uses the same rule.
Parameters
colors(roByteArray) — a level (1 << levelShift) cells squarelevelShift(integer) — log2 of that level's size
Returns
roByteArray
fromArrays(
size: integer,
heights: Array.<integer>,
colors: Array.<integer>,
palette: Array.<integer>,
options?: BGE.VoxelMapOptions,
): dynamic
A map from flat arrays, with its mip levels built.
Parameters
size(integer) — cells along each side: a power of 2 from 64 to 1024heights(Array.<integer>) — size * size heights, 0-255, row by row (index z * size + x)colors(Array.<integer>) — size * size palette indexespalette(Array.<integer>) — RGB colours (0xRRGGBB), 1-256 of themoptions(BGE.VoxelMapOptions, optional, default: "{}")
Returns
dynamic— aBGE.VoxelMap, or invalid (and a printed reason) if the input is bad
fromLevels(
sizeShift: integer,
palette: Array.<integer>,
heightLevels: Array.<roByteArray>,
colorLevels: Array.<roByteArray>,
options?: BGE.VoxelMapOptions,
): dynamic
A map from mip levels already built (e.g. a generator's, shaded per level). Checks the sizes, not the values.
Parameters
sizeShift(integer) — log2 of the size, 6-10palette(Array.<integer>) — RGB colours (0xRRGGBB), 1-256 of themheightLevels(Array.<roByteArray>) — level 0 (size x size) first, each a quarter of the lastcolorLevels(Array.<roByteArray>) — the same for palette indexesoptions(BGE.VoxelMapOptions, optional, default: "{}")
Returns
dynamic— aBGE.VoxelMap, or invalid (and a printed reason) if the input is bad
fail(message: string): dynamic
Prints why a map couldn't be made, and returns invalid.
Parameters
message(string)
Returns
dynamic
fromFile(path: string): dynamic
Loads a .bvox file (from tools/voxel-map.py or VoxelMap.saveToFile()). Each level is read straight into its own byte array, so it's quick: no loop over the cells.
Parameters
path(string) — e.g. "pkg:/maps/island.bvox"
Returns
dynamic— aBGE.VoxelMap, or invalid (and a printed reason)
fromByteArray(bytes: roByteArray): dynamic
A map from a .bvox file's bytes. Version 1 layout, one byte per value: "BGEV", version (1), log2 size, level count, edge mode (0 wrap, 1 flat), flat height, flat colour, palette count (0 = 256), RGB per palette entry, then per level (level 0 first) its heights then its colours, row by row.
Parameters
bytes(roByteArray)
Returns
dynamic— aBGE.VoxelMap, or invalid (and a printed reason)
parseHeader(bytes: roByteArray, total: integer): dynamic
Checks a .bvox header (its first HEADER_SIZE bytes) against the file's length.
Parameters
bytes(roByteArray)total(integer)
Returns
dynamic— shift, levelCount, edge, flatHeight, flatColor and paletteCount, or invalid (and a printed reason)
fromParts(
info: roAssociativeArray,
paletteBytes: roByteArray,
paletteOffset: integer,
heightLevels: Array.<roByteArray>,
colorLevels: Array.<roByteArray>,
): dynamic
The map from a parsed header, its palette bytes (RGB from paletteOffset) and levels
Parameters
info(roAssociativeArray)paletteBytes(roByteArray)paletteOffset(integer)heightLevels(Array.<roByteArray>)colorLevels(Array.<roByteArray>)
Returns
dynamic
Other
MIN_SIZE_SHIFT
Smallest and largest map sizes, as log2
Default: 6
MAX_SIZE_SHIFT
Default: 10
MAX_LEVELS
Most mip levels a map has, level 0 included
Default: 6
HEADER_SIZE
Bytes before a .bvox file's palette
Default: 11