Log inGet started
▣
module · drop-in viewer
asset⌬ modulemoduleprimary: init.luau·part ofpackage terrainSystem.package·originates fromworld 07158574-5…

terrainHeightfield

A `.texture` standing over a rect of world, read as ground.

by◐lumi·posted 3mo ago
What it does

terrainHeightfield

A .texture standing over a rect of world, read as ground.

The durable form of a landscape is an ordinary texture asset — one with a guid, that a component references, that travels with a world, that the renderer already knows how to sample. terrain.to_texture is what puts a graph's raster into one and texture.write persists it. This module is the other end: what a runtime holds while that ground is on screen.

local terrainHeightfield = require("~terrainSystem.modules.terrainHeightfield")

-- A `.terrainData` states the rect it covers, so opening one needs nothing else.
local hf = terrainHeightfield.open("/zero/source/terrain/dunes.terrainData")

-- The heightfield alone can be read too; then the caller says where it stands.
local bare = terrainHeightfield.open(tex, { size = { 4000, 4000 }, origin = { 0, 0 } })

local y = hf:heightAt(120, -60)   -- metres
local nx, ny, nz = hf:normalAt(120, -60)
local slope = hf:slopeAt(120, -60) -- radians
hf:close()

The texels

rgba32f, in the layout terrain.to_texture writes:

ChannelHolds
Rthe height, in METRES — it goes negative
Gthe gradient along world X
Bthe gradient along world Z
Acoverage

A height's interesting relief is often a few metres inside a range of hundreds, so eight bits band it into terraces; that is what the precision is for. The gradient is what a surface normal comes out of without differencing the height four more times, and it is why terrain_mesh costs four texel reads a vertex rather than twenty.

A heightmap authored anywhere else — an 8-bit image, a downloaded elevation tile, a raster an earlier graph baked — becomes one of these by going through a graph: terrain.from_texture reads it as ground, and terrain.to_texture writes the result back out at this precision.

The lattice pyramid

A heightfield also holds a pyramid of coarser levels, built when it opens on the GPU from the texels (terrain_pyramid). Level 0 is the texels, read along the lattice the terrain's collider stands its samples on; level k holds one height at every 2^k-th stop of that lattice, each the tent [1/4, 1/2, 1/4] of the level below at the three stops it spans. A mesh node whose vertices stand 2^k stops apart draws level k, so far ground is the filtered ground around each vertex. A stop on the field's own edge filters along the edge only, so two fields that meet along an edge and hold the same heights there hold one value at every coarse stop of it.

The stops stand on one of two anchors. "lattice", the default, is the field's own lattice: stop 0 the near edge of its rect, stop i the centre of texel i - 1, the far edge stop texels + 1. "texel" is a field whose first and last texel stand on the edges of the rect that is drawn, as a streamed tile's do: stop i is texel i. open and fromHeights take anchor; a field that meets the other kind of mesh builds that pyramid when pyramidFor asks.

hf.pyramid is the pyramid the field opened with, hf.pyramidLevels its levels from level 1 as { offset, cols, rows }, and hf.pyramids every one held by anchor. A field of 832 x 832 texels holds ten levels in 0.9 MiB, and one of 4096 x 4096 about 22 MiB. setHeights filters again only the points an edit reaches, a point more on every side at each level; refresh filters all of them; close frees them.

Two carriers, one set of texels

An open heightfield holds the texture in both forms the runtime reads it in:

  • hf.texels — a GPU buffer of the texel bytes, which the terrain_mesh compute kernel builds vertices out of.
  • hf.cpu — the pixels, which heightAt / normalAt / slopeAt / sampleGrid answer from.

Both are the same texels, uploaded from one read, so what the ground LOOKS like and what a query SAYS about it cannot drift apart.

The pixels are the heightfield's own copy, decoded from the asset when it opens and held in a buffer for as long as it stands, and so are its holes mask, its water surface and its layer weights. A heightfield answers in edit, in play and after play, whatever else loads or unloads the same texture. The cost is one copy a heightfield: 16 bytes a texel for the heights and the water, 4 for the holes mask and each layer-weight texture. Two heightfields open on one landscape hold two copies, which is also what keeps an edit to one (setHeights) to that one.

The surface

Verb
open(ground, opts?)Read a .terrainData — or the heightfield .texture alone — as ground. A container carries its own { size, origin, heightRange }; a bare texture takes them from opts, and an explicit opt outranks the container either way.
hf:heightAt(x, z)Metres: the texel cell around the point read through cellHeight, the surface the terrain's collider holds.
hf:normalAt(x, z)Unit normal, three numbers, from the carried gradient.
hf:slopeAt(x, z)Radians; 0 is flat.
hf:sampleGrid(cols, rows)Row-major heights over the whole rect at any sample count, read through heightAt.
hf:colliderTiles(perTile?)The heightfield collider tiles that hold this ground exactly: samples on the texel centres, a half-texel strip out to each edge. Each tile is { minX, minZ, sizeX, sizeZ, xs, zs }.
hf:texelLattice(xs, zs)Row-major heights of the texels at the named columns and rows, as they are: a tile's grid from colliderTiles.
terrainHeightfield.cellHeight(h00, h10, h01, h11, fx, fz)The ground inside one cell from its four corner heights.
hf:hasGroundAt(x, z)Whether there is ground at all — false over a hole.
hf:refresh()Read the texture's pixels again and put them back on the GPU. Bumps gen.
hf:close()Let go of the pixels and the GPU buffer. The asset is untouched.

heightRange is the vertical reach the geometry declares, which becomes the mesh's bounding box — a range smaller than the ground culls the terrain away at grazing angles. Left out, it is read off the pixels, which costs one pass over them.

One surface

The ground between texel centres is two flat triangles a cell, split along the diagonal from the cell's corner one texel along +Z to its corner one texel along +X: the split a Rapier heightfield collider makes, so a collider built from colliderTiles meets a ray at the height heightAt answers there, to float precision. The terrain_mesh kernel stands every vertex on a stop of the collider's lattice (the rect's edges and the texel centres), and the mesh cuts that lattice, so its finest node, a vertex at every texel centre, draws the collider's own triangles (save the strip along an edge stitched to a coarser neighbour, which follows that neighbour's edge), and a coarser one differs from it only by the level of detail it is cut to. Past the outermost texel centres the ground runs level out to the rect's edge, for the query, the collider and the mesh alike.

Holes

A landscape can be CUT OUT — a cave mouth, a quarry, a shaft — and its holes mask is carried the same two ways the heightfield is: on the GPU for the index kernel, which writes the quads it cuts out as degenerate triangles, and on the CPU for hasGroundAt. Same pixels, so a place with no surface to draw is a place nothing can be stood on either, and scatter that asks before it places never drops a tree into a shaft.

A landscape with no mask is solid everywhere and answers hasGroundAt true without reading anything. The edge of a hole follows the mask at the density the node is meshed at, so a hole is quad-shaped at the LOD it is seen from.

Generations

hf.gen counts the times the texels have been read. terrainMesh records the generation each node was meshed at, so re-authoring the ground under a standing terrain and calling hf:refresh() brings the geometry with it — nobody has to say which nodes changed.

Interface

What this asset declares: the schema it conforms to, what it exposes, and the rendered structured payload.

conforms to

zero/source-extract/v2

terrainHeightfield — a texture asset standing over a rect of world, read as ground. The durable form of a landscape is a `.texture`: an asset with a guid, that a component references, that travels with a world and that every other part of the engine already knows how to carry. `terrain.to_texture` is what puts a graph's raster into one. This module is the other end — what a runtime holds while the ground is on screen. A heightfield opened here carries the texture in the two forms the runtime reads it in, from the one set of pixels: * a GPU BUFFER of the texel bytes, which `terrain_mesh` builds vertices out of, and * the CPU pixels, which `heightAt` / `normalAt` / `slopeAt` answer from. Both are the same texels, so what the ground looks like and what a query says about it cannot drift apart. A landscape's HOLES mask and its WATER SURFACE are carried the same two ways: the index kernel drops the quads a hole cuts out and `terrain_water_mesh` builds the surface, while `hasGroundAt` and `waterAt` answer from the same pixels — so a place with no ground to draw is a place nothing can be stood on, and the sea a scatter avoids is the sea that is drawn. The texels are `rgba32f`, in the layout `terrain.to_texture` writes: R is the height in METRES, G and B are its gradient along world X and Z, and A is coverage. A height goes negative and a landscape's interesting relief is often a few metres inside a range of hundreds, which is what the precision is for; the gradient is what a surface normal comes out of without differencing the height four more times. A heightmap authored anywhere else — an 8-bit image, a downloaded elevation tile — becomes one of these by going through a graph: `terrain.from_texture` reads it as a raster and `terrain.to_texture` writes the result back out. local hf = terrainHeightfield.open(tex, { size = { 4000, 4000 } }) local y = hf:heightAt(120, -60)

pixelsOver(buf: buffer, width: number, height: number, format: string) → any

argtypedescription
bufbuffer
widthnumber
heightnumber
formatstring

readChannel(buf: buffer, o: number, channelBytes: number) → number

argtypedescription
bufbuffer
onumber
channelBytesnumber

writeChannel(buf: buffer, o: number, channelBytes: number, v: number) → void

argtypedescription
bufbuffer
onumber
channelBytesnumber
vnumber

readPixel(self: any, ix: number, iz: number) → void

argtypedescription
selfany
ixnumber
iznumber

writePixel(self: any, ix: number, iz: number, r: number, g: number, b: number, a: number) → void

argtypedescription
selfany
ixnumber
iznumber
rnumber
gnumber
bnumber
anumber

rgba(self: any) → string

argtypedescription
selfany

payloadOf(texture: any) → string

The bytes of a texture asset's payload, read where they lie. Content still arriving (a browser engine reads a world's files over the network) is waited for when the caller can wait.

argtypedescription
textureany

readPixels(texture: any) → any

A texture's pixels, decoded from its asset into a buffer this reading owns. `textureRef:load()` answers a handle onto the one CPU store every reading of that texture shares, which an unload, a decode landing or the asset's release lets go of whenever it happens, so ground that answers for as long as it stands holds pixels of its own instead: the decode goes under a guid of its own, is copied out, and that guid is unloaded at once. The cost is one copy a reading, `width * height` texels at the format's texel width (16 bytes an rgba32f texel, 4 an rgba8 one), in Luau memory.

argtypedescription
textureany

placement(opts: { [string]: any }, width: number, height: number) → void

The world rect an `open` was given, or the one a raster grid would have had.

argtypedescription
opts{ [string]: any }
widthnumber
heightnumber

verticalReach(opts: { [string]: any }, cpu: ?, width: number, height: number) → void

The vertical reach declared for this ground. A range that says nothing is a range read off the pixels, which costs a pass over them but is the only honest answer when the caller has none — a too-small box culls the terrain away at grazing angles.

argtypedescription
opts{ [string]: any }
cpu?
widthnumber
heightnumber

referenceName(value: any) → string

The name one asset answers to, whatever shape the reference arrived in. A graph's sink yields a ref shaped as DATA — a guid, a path and an identity, no methods — because it travels through the node's outputs; a component field and a hand-written call yield the interned ref that carries the assetType's own verbs. Both name one asset.

argtypedescription
valueany

resolveGround(value: any) → void

Every spelling of "which ground" resolved to the live texture ref the pixels are read through, plus the placement that came with it. A `.terrainData` is a landscape — a heightfield, its control textures, its holes mask and the layer table — and it knows the rect it covers, so opening one takes its placement from the container rather than from the caller. A bare `.texture` is the heightfield alone, and the caller says where it stands.

argtypedescription
valueany

openLayers(data: any) → void

The layers a landscape paints its ground with: each layer's name, its weight textures and their pixels, which a query reads which layer covers a point by. A landscape with no layers, or a bare heightfield, has none; one whose weight textures cannot be read has none either.

argtypedescription
dataany

anchorOf(name: any) → number

The anchor a name states, or nil.

argtypedescription
nameany

pyramidShader( ) → any

pyramidLayout(texelsX: number, texelsZ: number, anchor: number) → void

The levels a field of `texelsX` x `texelsZ` texels holds at an anchor, and the heights they hold in all.

argtypedescription
texelsXnumber
texelsZnumber
anchornumber

destroyPyramid(pyramid: Pyramid) → void

argtypedescription
pyramidPyramid

allocatePyramid(texelsX: number, texelsZ: number, anchor: number) → void

Allocate a pyramid for a field and write its level table. The levels hold nothing until `fillPyramid` runs.

argtypedescription
texelsXnumber
texelsZnumber
anchornumber

reached(lo: number, hi: number, srcLast: number, dstLast: number) → void

The destination points of a level an edit of the source level's points `lo .. hi` reaches along one axis: every point whose three taps include one of them, and every point reading the source's last point through the clamp when the edit reaches it (or its first, at the start).

argtypedescription
lonumber
hinumber
srcLastnumber
dstLastnumber

fillPyramid(pyramid: Pyramid, texels: any, texelsX: number, texelsZ: number, stops: { number }?) → void

Filter the pyramid of a field, level by level, over the stops of level 0 an edit reached (`{ minX, minZ, maxX, maxZ }`, inclusive), or all of them.

argtypedescription
pyramidPyramid
texelsany
texelsXnumber
texelsZnumber
stops{ number }?

pyramidFor(self: Heightfield, anchor: string?) →

The pyramid of lattice levels this field holds at an anchor, built on first ask when the field was not opened at it. Level `k` holds the heights at every `2^k`-th stop of the field's lattice, each filtered from the level below it by a tent, and is what the mesh draws a node whose vertices stand `2^k` stops apart from. stop `i` the centre of texel `i - 1`, the far edge stop `texels + 1`) or `"texel"` (stop `i` is texel `i`, the edge texels standing on the edges of the rect that is drawn). buffer, table }`, or `(nil, message)`.

argtypedescription
selfHeightfieldThe heightfield.
anchorstring?`"lattice"` (the default: stop 0 is the near edge of the rect,

examples

local pyramid = terrainHeightfield.pyramidFor(hf)

open(ground: any, opts: { [string]: any }?) →

Open ground for reading: the texels on the GPU for the mesh kernel and on the CPU for queries, standing over a rect of world. placement — or the heightfield `.texture` alone. Either as an `AssetRef`, a guid, a path or an identity. The texels are `rgba32f` in the layout `terrain.write` and `terrain.to_texture` produce. anchor? }` — where the ground stands, for a bare texture that does not say. A `.terrainData` states its own, and anything passed here outranks it. `anchor` is `"lattice"` (the default) or `"texel"`, the stops the field's pyramid of coarse levels is stood on (`pyramidFor`).

argtypedescription
groundanyA `.terrainData` — the usual case, and it carries its own
opts{ [string]: any }?`{ size = { x, z }, origin? = { x, z }, heightRange? = { lo, hi },

examples

local hf = terrainHeightfield.open(dunes)
local hf = terrainHeightfield.open(tex, { size = { 4000, 4000 } })

packHeights(opts: { [string]: any }, cols: number, rows: number, border: number, place: any) → void

A grid of heights laid out as texels the way `terrain.to_texture` lays them out: R the height, G and B its gradient along X and Z, A full coverage. A gradient is the central difference across the texel, one-sided on an edge with no border past it. Returns the texels and the lowest and highest height.

argtypedescription
opts{ [string]: any }
colsnumber
rowsnumber
bordernumber
placeany

write(self: any, texels: buffer, firstRow: number, rowCount: number) → boolean

Put a run of rows of laid-out texels on the GPU. `fromHeights` takes them; rows `firstRow` to `firstRow + rowCount - 1` of it are written.

argtypedescription
selfanyThe staging.
texelsbufferA `buffer` of `cols * rows` rgba32f texels, laid out as
firstRownumberThe first row written, from 0.
rowCountnumberHow many rows.

examples

staging:write(texels, 0, 16)

destroy(self: any) →

Let go of a staging no heightfield took, and its GPU buffer.

argtypedescription
selfanyThe staging.

examples

staging:destroy()

stageTexels(cols: number, rows: number) →

Put a heightfield's texels on the GPU a run of rows at a time, ahead of the heightfield they will stand behind, so a large field's upload can be spread over several frames. Write every row with `:write(texels, firstRow, rowCount)`, then hand the staging to `fromHeights` as `staged` with the same `texels`: the field takes the GPU buffer as it stands. `:destroy()` lets go of a staging no field took.

argtypedescription
colsnumberTexels along world X, at least 2.
rowsnumberTexels along world Z, at least 2.

examples

local st = terrainHeightfield.stageTexels(513, 513); st:write(texels, 0, 513)

fromHeights(opts: { [string]: any }) →

Ground built from a grid of heights rather than read from an asset: a heightfield answering every query `open` answers, with its texels on the GPU for the mesh kernel, from heights a function, a generator or another field produced. The texels spread over `size` around `origin` the way a texture's do, each at the centre of its cell. Each texel's gradient is differenced from its neighbours; a `border` of one sample around the grid is read for the gradient alone, so a field cut out of larger ground carries the gradient that ground has at its edge. border? = 0, heightRange? = { lo, hi }, layers? = { names }, layerMap?, anchor? }`. `anchor` is `"lattice"` (the default) or `"texel"`, the stops the field's pyramid of coarse levels is stood on (`pyramidFor`). `heights` is row-major, `(cols + 2 * border) * (rows + 2 * border)` metres, as an array of numbers or a `buffer` of `f32`. `layerMap` names, per texel and row-major over `cols * rows`, the one-based index into `layers` of the layer covering it (0 for none), as an array or a `buffer` of `u8`. `texels`, in place of `heights`, is the texels already laid out: a `buffer` of `cols * rows` rgba32f texels, R the height and G, B its gradient along X and Z, uploaded as they are. `staged`, beside `texels`, is a `stageTexels` staging every row of those texels was written through: the field takes its GPU buffer and uploads nothing.

argtypedescription
opts{ [string]: any }`{ heights, cols, rows, size = { x, z }, origin? = { x, z },

examples

local hf = terrainHeightfield.fromHeights({ heights = h, cols = 257, rows = 257, size = { 514, 514 } })

locate(self: Heightfield, x: number, z: number) → void

The texel index a world position falls in, clamped to the field, plus the fractions into the cell between it and the next texel over. A position on the last texel's centre or past it reads that texel exactly (the cell before it at a fraction of one), so a field whose edge texels stand on its rect's edge answers the edge sample itself there.

argtypedescription
selfHeightfield
xnumber
znumber

heightAt(self: Heightfield, x: number, z: number) → number

Height of the ground at a world position, in metres: the texel cell around it read through `cellHeight`, which is the surface the terrain's collider holds. Past the outermost texel centres the ground runs level out to the rect's edge.

argtypedescription
selfHeightfieldThe heightfield.
xnumberWorld X in metres.
znumberWorld Z in metres.

examples

local y = hf:heightAt(120, -60)

normalAt(self: Heightfield, x: number, z: number) →

Surface normal at a world position — three numbers, unit length. Read from the gradient the texture carries rather than differenced out of the height, so it is the same normal the geometry is built with.

argtypedescription
selfHeightfieldThe heightfield.
xnumberWorld X in metres.
znumberWorld Z in metres.

examples

local nx, ny, nz = hf:normalAt(120, -60)

slopeAt(self: Heightfield, x: number, z: number) → number

Slope at a world position, in DEGREES from level. 0 is flat, 35 is steep, 90 is a wall — the same numbers `terrain.mask`'s slope bands and `terrain.scatter`'s `maxSlope` are written in, so a slope read here can be handed straight to the rule that acts on it.

argtypedescription
selfHeightfieldThe heightfield.
xnumberWorld X in metres.
znumberWorld Z in metres.

examples

local s = hf:slopeAt(120, -60)

sampleGrid(self: Heightfield, cols: number, rows: number) →

Row-major heights over a grid of samples across the whole field — the whole-field case of `sampleRect`. or nil when the pixels are gone.

argtypedescription
selfHeightfieldThe heightfield.
colsnumberSamples along world X, at least 2.
rowsnumberSamples along world Z, at least 2.

examples

local grid = hf:sampleGrid(129, 129)

texelGrid(self: Heightfield, ix: number, iz: number, cols: number, rows: number) →

Row-major heights of a block of the field's own texels, read as they are without interpolation: the grid a heightfield collider takes when its samples stand on the texels. Two blocks sharing a row or column of texels carry identical samples along it. A cut-out texel drops the way `sampleRect` drops one. when the pixels are gone or the block reaches past the field.

argtypedescription
selfHeightfieldThe heightfield.
ixnumberThe block's first texel column, from 0.
iznumberThe block's first texel row, from 0.
colsnumberTexels along world X, at least 2.
rowsnumberTexels along world Z, at least 2.

examples

local block = hf:texelGrid(0, 0, 65, 65)

texelLattice(self: Heightfield, xs: { number }, zs: { number }) →

Row-major heights of the field's own texels at a lattice of chosen columns and rows, read as they are without interpolation. `xs` names the texel column of each sample along X and `zs` the texel row of each sample along Z; a column or row may be named more than once, which is how a collider reaches from the outermost texel centre out to the rect's edge on ground that runs level there. A cut-out texel drops the way `sampleRect` drops one, and each sample names the layer covering its texel. being `#xs` and `rows` `#zs`, or nil when the pixels are gone or a column or row lies past the field.

argtypedescription
selfHeightfieldThe heightfield.
xs{ number }Texel columns, from 0, at least 2 of them.
zs{ number }Texel rows, from 0, at least 2 of them.

examples

local strip = hf:texelLattice({ 0, 0 }, { 0, 1, 2, 3 })

colliderRuns(min: number, size: number, texels: number, perRun: number) → void

The stops a collider's samples stand at along one axis of `texels` texels spanning `size` from `min`, cut into runs of evenly spaced stops: the half texel from the rect's edge to the first texel centre, the texel centres in runs of at most `perRun` cells, and the half texel from the last centre to the far edge. Each run's last stop is the next run's first. A stop is `{ position, texel }`; the stops on the rect's edges read the edge texels, which is the level ground `heightAt` answers past the outermost centres.

argtypedescription
minnumber
sizenumber
texelsnumber
perRunnumber

stop(s: number) → void

argtypedescription
snumber

colliderTiles(self: Heightfield, perTile: number?) →

The heightfield collider tiles that hold this ground exactly, over the whole rect. Their samples stand on the texel centres, read without interpolation, and each cell splits into the two triangles `cellHeight` answers, so a ray onto the collider meets the height `heightAt` answers anywhere. A half-texel strip along each edge carries the ground out from the outermost texel centres to the rect's edge, level the way `heightAt` answers it there. Neighbouring tiles share their edge samples. Read a tile's grid with `texelLattice(tile.xs, tile.zs)` and stand it at the centre of its rect with `extent = { sizeX, 1, sizeZ }`. 512 when omitted. rect in the field's coordinates and the texel column of each sample along X and texel row of each along Z.

argtypedescription
selfHeightfieldThe heightfield.
perTilenumber?The most samples a tile holds along an axis, at least 3;

examples

for _, tile in ipairs(hf:colliderTiles()) do local grid = hf:texelLattice(tile.xs, tile.zs) end

hasGroundAt(self: Heightfield, x: number, z: number) → boolean

Whether there is ground at a world position at all. A landscape can be CUT OUT — a cave mouth, a quarry, a shaft — and over a hole there is no surface: nothing is meshed there, and nothing should be stood there either. A landscape carrying no holes mask is solid everywhere.

argtypedescription
selfHeightfieldThe heightfield.
xnumberWorld X in metres.
znumberWorld Z in metres.

examples

if hf:hasGroundAt(x, z) then place(x, hf:heightAt(x, z), z) end

waterAt(self: Heightfield, x: number, z: number) → number

The world Y of the water surface covering a position, or nil where no surface covers it. A sea, a lake and a river are the same thing here: a level and where it reaches. A landscape with no water surface answers nil everywhere.

argtypedescription
selfHeightfieldThe heightfield.
xnumberWorld X in metres.
znumberWorld Z in metres.

examples

local sea = hf:waterAt(x, z); if sea ~= nil and sea > y then submerged() end

heavier(index: number, w: any, count: number, best: number, bestWeight: number) → void

The heavier of the layer held so far and layer `index` of weight `w`, leaving out an index past the `count` layers the landscape declares.

argtypedescription
indexnumber
wany
countnumber
bestnumber
bestWeightnumber

layerAt(self: Heightfield, x: number, z: number) →

Which painted layer covers a position most: the layer whose weight is largest in the weight-texture texel under it, the first of them on a tie. A layer's name is the surface it collides as, which is how a terrain's collider gives each layer its own ground. landscape that carries no layers.

argtypedescription
selfHeightfieldThe heightfield.
xnumberWorld X in metres.
znumberWorld Z in metres.

examples

local _, layer = hf:layerAt(x, z); if layer == "mud" then slowDown() end

depthAt(self: Heightfield, x: number, z: number) → number

How deep the water is over a position — the surface's world Y less the ground's, and zero where nothing covers it.

argtypedescription
selfHeightfieldThe heightfield.
xnumberWorld X in metres.
znumberWorld Z in metres.

examples

if hf:depthAt(x, z) > 1.5 then tooDeepToWade() end

texelAt(self: Heightfield, x: number, z: number) →

The texel a world position falls in, clamped to the field. Texels are addressed from 0, `ix` along world X and `iz` along world Z, and the key `setHeights` takes is `iz * width + ix`.

argtypedescription
selfHeightfieldThe heightfield.
xnumberWorld X in metres.
znumberWorld Z in metres.

examples

local ix, iz = hf:texelAt(120, -60)

texelCentre(self: Heightfield, ix: number, iz: number) →

The world position of a texel's centre, the point its height stands at.

argtypedescription
selfHeightfieldThe heightfield.
ixnumberTexel column, from 0.
iznumberTexel row, from 0.

examples

local x, z = hf:texelCentre(10, 12)

texelHeight(self: Heightfield, ix: number, iz: number) → number

The height one texel holds, in metres, read without interpolation.

argtypedescription
selfHeightfieldThe heightfield.
ixnumberTexel column, from 0.
iznumberTexel row, from 0.

examples

local h = hf:texelHeight(10, 12)

setHeights(self: Heightfield, edits: { [number]: number }) →

Write new heights into texels of the standing ground: a road bed, a levelled foundation, a crater. Each texel named takes its new height, the gradient of every texel the change reaches is derived again from its neighbours (the same central difference `terrain.to_texture` writes), and only the rows touched travel to the GPU. Bumps `gen`, so the geometry built from this heightfield is meshed again. The pixels are this heightfield's own copy, so the edit belongs to this reading alone: another terrain standing on the same landscape keeps its ground, and opening the landscape again elsewhere leaves this one's edits standing. They last until this heightfield reads its texture again (`refresh`, or a terrain rebuilt from its asset); `Terrain:setHeights` also rebuilds the collision under the change. texels (inclusive), or nil when nothing was written.

argtypedescription
selfHeightfieldThe heightfield.
edits{ [number]: number }`{ [iz * width + ix] = metres }`, texels addressed from 0.

examples

hf:setHeights({ [12 * hf.width + 10] = 4.5 })

at(ix: number, iz: number) → number

argtypedescription
ixnumber
iznumber

refresh(self: Heightfield) → boolean

Read the texture's pixels again and put them back on the GPU, so ground re-authored under a standing terrain is the ground it draws. Bumps `gen`, which is what tells everything built from this heightfield to rebuild. A texture that cannot be read keeps the pixels this heightfield already holds, and the ground goes on answering from them.

argtypedescription
selfHeightfieldThe heightfield.

examples

hf:refresh()

close(self: Heightfield) →

Let go of the pixels and the GPU buffers this heightfield holds. The texture asset itself is untouched — a heightfield is a reading of one, not the thing itself.

argtypedescription
selfHeightfieldThe heightfield.

examples

hf:close()

poseOf(entityId: string?) →

The pose an entity stands a landscape at, as `{ px, py, pz, qx, qy, qz, qw, sx, sy, sz }`: its world position, its turn (a unit quaternion) and its scale along its own axes. no such entity, or one standing at the origin unturned and unscaled.

argtypedescription
entityIdstring?The entity whose local space the landscape's coordinates are.

examples

local pose = terrainHeightfield.poseOf(id)

toLandscape(pose: { number }?, x: number, y: number, z: number) →

A world point in a landscape's coordinates: the point of the entity's local space it stands at.

argtypedescription
pose{ number }?The landscape's pose (`poseOf`), nil for the world's own.
xnumberWorld X in metres.
ynumberWorld Y in metres.
znumberWorld Z in metres.

examples

local lx, ly, lz = terrainHeightfield.toLandscape(pose, x, y, z)

toWorld(pose: { number }?, x: number, y: number, z: number) →

A point of a landscape's coordinates in the world: where the entity it stands under draws it.

argtypedescription
pose{ number }?The landscape's pose (`poseOf`), nil for the world's own.
xnumberX in the landscape's coordinates.
ynumberY in the landscape's coordinates.
znumberZ in the landscape's coordinates.

examples

local wx, wy, wz = terrainHeightfield.toWorld(pose, lx, hf:heightAt(lx, lz), lz)

verticalIn(pose: { number }) → void

The world's vertical, in a landscape's coordinates: the metres it runs along the landscape's X, Y and Z per metre of world Y.

argtypedescription
pose{ number }

upright(pose: { number }?) → boolean

Whether the world's vertical is a landscape's own up axis: true for a landscape moved, turned about the vertical or scaled, where the point of it under a world (x, z) is the one point its coordinates name there at every height; false for a tilted one, where the vertical line crosses its coordinates at a slant.

argtypedescription
pose{ number }?The landscape's pose (`poseOf`), nil for the world's own.

examples

if not terrainHeightfield.upright(pose) then lx, lz = terrainHeightfield.groundUnder(pose, x, z, heightOf, nil, step) end

read(lx: number, lz: number) → number

argtypedescription
lxnumber
lznumber

gap(t: number) → number

argtypedescription
tnumber

follow( ) → void

Down from the top of the reach in steps of half a sample, to the first step that crosses the ground, then halved down to the crossing.

levelUnder(pose: { number }?, x: number, z: number, level: number) → number

A world Y standing over a point of a landscape: the world Y the vertical line through world (x, z) crosses the landscape's level `level` at, a water surface's level being one.

argtypedescription
pose{ number }?The landscape's pose (`poseOf`), nil for the world's own.
xnumberWorld X in metres.
znumberWorld Z in metres.
levelnumberMetres, in the landscape's coordinates.

examples

local y = terrainHeightfield.levelUnder(pose, x, z, 4)

normalToWorld(pose: { number }?, nx: number, ny: number, nz: number) →

A surface normal of a landscape, in the world: carried through the inverse of the pose's scale, then its turn, and made unit length again.

argtypedescription
pose{ number }?The landscape's pose (`poseOf`), nil for the world's own.
nxnumberNormal X in the landscape's coordinates.
nynumberNormal Y.
nznumberNormal Z.

examples

local wx, wy, wz = terrainHeightfield.normalToWorld(pose, hf:normalAt(lx, lz))
⌬ Types
HeightfieldData = {Heightfield = HeightfieldData & {PyramidLevel = { offset: number, cols: number, rows: number }Pyramid = {

Problems

Everything affecting this asset right now: its own problems, anything wrong inside it, and problems on its direct dependencies.

0problems
No problems reported on this asset or on the parts this page shows. Parts it leaves out are not counted here.
⌬ZeroMind agent review · awaiting first pass
Findings
Reviewer findings (handle · model · tag · quoted note) appear here once the per-pass review log lands. Today only the rolled-up agent_score is exposed.
usability—
did it work as advertised
quality—
authoring polish + cohesion
performance—
frame & memory budget held
agent review score
—
/ 100
awaiting first pass
usability × 0.40
+ quality × 0.35
+ performance × 0.25
± compat factor

Usability ratings

Did the part work as advertised when consumers tried to drop it in. Separate from upvotes: those are taste; this is "did it function".

—%no reports yet
Sign in to report whether this part worked for you.
Discussion

Scoped to this part · feeds back into the world's score.

0comments
Sign in to post.sign in
No comments yet. Be the first.