Log inGet started
Package

terrainSystem.package

Newest version

This package holds more than this page lists. See all of its files in its world.

README.md

terrain_mesh

Builds one quadtree node's vertices from the heightfield's texels, in the engine's 23-word vertex layout, straight into the shared vertex pool the terrain draws from. One thread per node vertex.

Every vertex stands on a stop of the collider's lattice: the field's two edges and its texel centres along each axis, the stops the terrain's collider holds its samples at. The cut is counted on that lattice: a vertex is placed from whole indices counted from the grid corner, a cell step apart along each axis, and for a pool over the field's own rect the corner is half a texel outside the rect and the cell step the texel spacing times a power of two, so a vertex is already a lattice point and the snap onto the lattice moves none of the interior ones. A node whose cell step is the texel spacing draws exactly the collider's cells, split along the diagonal from the corner one texel along +Z to the corner one texel along +X, which is the split the collider and terrainHeightfield.cellHeight make. Every vertex is held to the rect the cut tiles, so the vertices past an edge stand on it and draw triangles with no area. Along an edge stitched to a coarser neighbour, the strip of cells next to the edge follows the neighbour's edge. A coarser node draws a subset of the same lattice.

The height of a node at the field's own density is read from the four texels of the cell around a stop through that same split, and the normal from the gradient the texels carry, blended across them. A node whose vertices stand 2^k stops apart reads level k of the heightfield's lattice pyramid instead (terrain_pyramid): one height at every 2^k-th stop, each a tent over the level below, with the gradient taken as the central difference of the level over 2^k texels. The far end of a vertex's morph is the parent's vertex at the same stop, one level up, so a node that has finished morphing draws what its parent holds.

Two edge behaviours make an LOD cut renderable:

  • Stitching. coarseMask names the edges whose neighbour is one level coarser. On such an edge the odd-indexed vertices snap onto the previous even one, so the fine edge becomes the polyline through the coarse neighbour's own vertices. The crack closes because the two edges are then the same line, not because something covers it.
  • Geomorphing. Over the far end of the distance band a node is a leaf across, every vertex slides onto the position it would hold one level coarser. By the time the cut merges the node into its parent, the node is already drawing the parent's geometry, so the merge moves nothing. The morph runs off each vertex's own distance to the camera rather than the node's, so a vertex two nodes share is placed identically by both.

A vertex stands at one height however many nodes hold it. Along an edge a coarser neighbour shares, a vertex reads the neighbour's level at both ends of its morph, so it is the neighbour's own vertex at that point. A corner reads the coarsest level among the nodes that meet there, which the caller states for each corner: a node two levels coarser can meet a fine one at a single point, with no edge between them.

A vertex is placed from its whole index alone, so every node that holds it, and every child whose vertex morphs onto it, computes the same bits for it and the same lattice point.