Voxel Terrain

BGE.DrawableVoxelTerrain draws hills from a heightmap, the way NovaLogic's Comanche (1992) did: for each screen column, a ray steps over the map from near to far, and wherever the ground rises into view it draws a vertical strip. There are no polygons, so it's cheap enough for real hills on a Roku, at the cost of a blocky, low-resolution look (quality sets how many columns). Flat ground is better done with a DrawablePlane.

It needs a Camera3d, and a renderer draws one terrain at most.

Maps

A BGE.VoxelMap is a square grid, 64 to 1024 cells along each side (a power of 2), with a height (0-255) and a colour for every cell. Colours are indexes into a palette of up to 256 RGB colours. The map also keeps smaller copies of itself (mip levels) for drawing far terrain without flicker.

Past its edges a map either repeats (edgeMode: "wrap", so it should tile) or is a flat floor of one height and colour (edgeMode: "flat", e.g. sea round an island).

Baking a map from images

Paint a grayscale heightmap (black is 0, white 255) and a colour map the same size in any paint program, then bake them into a .bvox file:

CODE
python3 tools/voxel-map.py heights.png colors.png island.bvox --edge flat --flat-height 60 --flat-color 2A5DA8

The colour map can have any number of colours: more than 256 are reduced to 256. The tool needs only Python 3. Load the file in your game:

CODE
map = BGE.VoxelMaps.fromFile("pkg:/maps/island.bvox")

Loading is quick (a 256 map takes ~2 ms on a Roku Ultra), so do it whenever you need it.

Other ways to make one

  • BGE.VoxelMaps.fromArrays(size, heights, colors, palette, options) from integer arrays you've built in code.
  • BGE.VoxelMapGenerator makes random tileable maps: diamond-square heights coloured by height bands (water, sand, grass, forest, rock, snow by default) and shaded by slope. It's slow (~1.3 s for 256x256 on a Roku Ultra, long enough to trip the watchdog on slower devices), so generate a map once and keep it: map.saveToFile("tmp:/world.bvox").

Adding the terrain

CODE
terrain = new BGE.DrawableVoxelTerrain(entity, map, {
  cellSize: 4,        ' world units per map cell
  heightScale: 1.5,   ' world units per height unit
  fade: {color: &h8EC5F0, start: 0.5}
})
entity.addDrawable("terrain", terrain)

The entity's position (plus the drawable's offset) is map cell (0, 0). Map x and z are world x and z, and a height of 255 is 255 * heightScale above the entity.

To stand something on the terrain, terrain.heightAt(worldX, worldZ) gives the ground's world y there, blended between cells.

setMap(map) swaps the map. Call it too after changing a map's heights or colours in place, or the terrain keeps drawing what it had.

What draws over what

The terrain draws after the ground planes and before level walls (walls always draw over it). Everything sorted by depth (sprites, models) is tested against the hills: one wholly behind a hill isn't drawn, and one partly behind it is drawn and then the nearer hillside is drawn back over it. This is exact to a screen column.

Nothing is drawn above the terrain, so put a skybox or a backdrop behind it.

Fog and fading

With Camera3d.fog on, far terrain fades into the fog colour across the camera's drawDistanceFadeFraction band, like objects and walls. With it off, set fade to fade it into another colour (usually the sky's) from start (a fraction of the draw distance) to the far end. Without either, the terrain just stops at the draw distance.

Roll and pitch

Roll works at any angle: the terrain is drawn level into a square bitmap as wide as the screen's diagonal, then turned onto the screen. That bitmap is kept while rolled: ~8.6 MB at 1280x720, ~19 MB at 1920x1080, which counts against the device's texture memory.

Pitch is harder: the terrain's columns stay upright, so it can't truly look up or down. pitchMode picks how it copes:

  • horizonShift (default): the horizon moves up or down. This is cheap, but only right near level. Past about 15 degrees up or down, objects float above or sink into the ground.
  • exact: every step is projected with the pitched camera, so objects sit on the ground at any pitch. It costs about a third more. Objects near the screen edges drift sideways a little at steep pitches, since the columns can't lean.

Or make the camera match the terrain instead: with camera.pitchMode = "shear" the whole view pitches the way horizonShift does (it stays level and the image moves up or down), so objects, walls and planes sit exactly on the hills at any pitch, at horizonShift's cost. Vertical lines stay vertical, which suits the retro look; it reads as looking up or down up to about 30 degrees. The terrain ignores its own pitchMode under a shear camera.

Quality and cost

The cost is almost exactly columns x depth steps, about 4 µs each on a Roku Ultra. Quality sets both (voxelColumns, voxelBandSteps, see Render Quality); the draw distance sets how many steps there are. While the camera is still, the terrain draws the same strips again without marching.

Flying and turning over a 256 map, terrain only, on a Roku Ultra at 1280x720:

LevelColumnshorizonShiftexacthorizonShift, rolled
Basic5048 fps38 fps39 fps
Low6439 fps31 fps
Medium8030 fps23 fps24 fps
High10024 fps18 fps
Ultra10021 fps16 fps17 fps

On a Roku Express (HD, so the engine caps the draw distance at 1000 units: ~74 depth steps a column where the Ultra takes ~99) the same flight runs at about half the Ultra's frame rate. Each column step costs ~8.5 µs, about twice the Ultra's:

LevelhorizonShiftexacthorizonShift, rolled
Basic23 fps17 fps23 fps
Low18 fps15 fps
Medium15 fps11 fps15 fps
High13 fps9 fps
Ultra11 fps8 fps11 fps

On an Express, start at Basic or Low and keep the objects in view few.

Objects cost the same as anywhere else, plus the hill test for each one in view: ~0.2 ms for a directScaled sprite on a Roku Ultra, ~0.3 ms for other kinds. The test is relatively dearer on an Express: ~0.9 ms per sprite (35 ms for 40 at Medium). The rendererTest app's Voxel Terrain demo (demo=voxel-terrain) shows all of this, and with sweep=1 it prints these numbers for your own device.