---
title: "Voxel volumes: ground you can dig, carve and fill"
description: "A voxel volume is a block of space held as a grid of small cubes (voxels), each with a density and a material."
section: "Topics"
slug: "topics-voxel-volumes"
canonical: "https://origozero.ai/docs/topics-voxel-volumes"
updated: "2026-12-05T22:41:02.602622239+00:00"
tags: ["documentation", "guide"]
---

# Voxel volumes: ground you can dig, carve and fill

**Diggable ground in a scene is the `VolumeTerrain` component** (`systems/volumeTerrain`): put it on an entity and a block of this ground stands there, drawn and with colliders, and `terrain:dig(point, radius)`, `terrain:fill(...)`, the world-point `require("@builtin::systems.volumeTerrain.volumeTerrain").dig(point, radius)` and an editor viewport brush dig and fill it. The rest of this guide is the voxel data underneath, for building something of your own on it.

Each voxel stores two bytes:

- **density**: the signed distance into the solid, positive inside, negative in empty space, zero on the surface. It saturates about two voxels either side of the surface, so it describes a smooth surface between voxels rather than blocks.
- **material**: a number 1 to 255 for a solid (soil, rock, ore: what each number means is yours), 0 for empty space.

A volume is **sparse**: it names a **base** (empty space, solid, a flat ground or a heightfield, with material layers under the surface) and holds only the chunks an edit changed. A ground or heightfield base is solid all the way down, and a heightfield is interpolated between its samples and held at its edge samples past the grid. Untouched ground costs no memory. At the default 0.25 m voxel and 32-voxel chunk, a chunk is an 8 m cube of 64 KiB.

## Where the voxels sit

Voxel `(i, j, k)` stands at `origin + (i, j, k) * voxelSize` in world space (`origin` and `voxelSize` are `voxelVolume.create` options, default `0,0,0` and 0.25 m), and its cell is the cube of one voxel around that point. So with the defaults a voxel centre sits on every multiple of 0.25 m, and a face placed on a multiple of 0.25 m runs through a row of voxel centres. Chunk `(x, y, z)` holds voxels `x * chunkSize` to `x * chunkSize + chunkSize - 1` on each axis.

## Make one and dig

```lua
local v = voxelVolume.create({
    voxelSize = 0.25,                       -- metres
    base = {
        kind = "ground", height = 10,       -- solid below y = 10
        layers = { { material = 1, thickness = 0.5 },   -- 0.5 m of soil
                   { material = 2 } },                  -- rock below
    },
})

-- Dig a 1.5 m ball out of the ground.
local dug = voxelVolume.apply(v, { shape = "sphere", mode = "remove",
    center = vector.create(4, 10, 4), radius = 1.5 })
print(dug.removed, "m3 removed")
for _, m in dug.byMaterial do print("material", m.material, "removed", m.removed) end
```

`voxelVolume.apply` takes one operation: `shape` (`"sphere"` with `radius`, or `"box"` with `size`, its full extent), `center`, and `mode`:

| mode | does |
|---|---|
| `"remove"` | carves the shape out (dig, tunnel, crater) |
| `"add"` | fills the shape with solid `material` (build, fill a hole) |
| `"smooth"` | rounds off edges inside the shape by `strength` 0 to 1; it takes solid off sharp edges and puts some into hollows, so its `removed` and `added` both count |
| `"paint"` | gives every voxel inside the shape that is solid or on the surface `material`; its density, and so the volume, stays |

It returns the solid it took away and put in (cubic metres, also per material), how many voxels changed, and which chunks they are in.

## Ask it questions

```lua
local s = voxelVolume.sample(v, vector.create(4, 9, 4))          -- { density, material, solid }: material of the nearest solid voxel
local hit = voxelVolume.raycast(v, vector.create(4, 30, 4), vector.create(0, -1, 0), 100)
if hit then print(hit.position, hit.normal, hit.distance, hit.material) end
local m = voxelVolume.measure(v, vector.create(0, 0, 0), vector.create(8, 12, 8))
print(m.solid, "m3 of solid in the box")
```

`measure` counts the part of each voxel's cell its density fills, so a flat ground and an added or carved box measure exactly, and the solid a measure loses after a dig is exactly what the dig reported. A sphere measures within about 1% of the true ball at 0.25 m voxels. `raycast` reads the voxels directly: it needs no mesh and no collider.

## Materials sit on voxels

Each voxel holds one material, and `measure` and `apply` count each voxel's fill (the part of its cell its density fills) under that voxel's material. So a material boundary is held to whole voxels: a layer boundary or the edge of a `paint` lands on the nearest row of voxel centres, and a voxel whose centre sits exactly on it goes to the material below (for a layer) or inside (for paint). With the default origin a voxel centre sits on every multiple of `voxelSize`, so a boundary at a whole number of metres runs through a row of centres and that row's 0.25 m goes to one side. To put boundaries and faces you place on multiples of `voxelSize` between rows instead, shift the lattice by half a voxel: `origin = vector.create(0.125, 0.125, 0.125)` at 0.25 m voxels. A 1 m soil layer painted over rock then measures exactly 1 m.

## Same edits, same result, everywhere

Every edit runs in integer arithmetic, so the same base and the same operations in the same order give the same voxels on native and on the web. `voxelVolume.checksum(v)` is one number for the whole volume; two peers that applied the same operations compare it to find a volume that diverged. `voxelVolume.chunks(v)` lists each edited chunk with its own checksum. A checksum covers the voxels by their indices and chunk coordinates, so it compares volumes made with the same `voxelSize`, `chunkSize`, `origin` and base, and the brush arithmetic it measures is the engine's: compare checksums between engines of the same version.

## Save and send chunks

```lua
for _, c in voxelVolume.chunks(v) do
    local bytes = voxelVolume.snapshotChunk(v, c)   -- a buffer, usually under a few KB
    -- store it, or send it to a peer
end
local at = voxelVolume.restoreChunk(other, bytes)   -- raises if the bytes are damaged
```

A snapshot is the chunk run-length encoded, compressed with LZ4 and checksummed. `restoreChunk` refuses a damaged snapshot, one of another chunk size, or bytes that are not a snapshot, and changes nothing when it refuses.

## Draw it: the surface

A volume draws through a surface: one mesh per chunk, built from the voxels by Surface Nets on the engine's worker threads, so meshing costs the frame nothing on native or on the web. Each chunk mesh is an ordinary static mesh in the cluster pipeline, drawn as level-0 clusters from the moment it lands.

```lua
voxelVolume.mesh(v, {
    min = vector.create(0, 0, 0), max = vector.create(32, 16, 32),   -- the world box to cover
    palette = { [1] = vector.create(0.35, 0.25, 0.15), [2] = vector.create(0.5, 0.5, 0.5) },
})
-- once the chunks land, draw each one where its vertices are relative to
local drawn = {}
for _, c in voxelVolume.meshes(v).chunks do
    if c.state == "ready" then
        local e = entity.spawn(`ground_{c.x}_{c.y}_{c.z}`, { position = c.position, temporary = true })
        e.component.add("Model", { model = c.mesh })   -- c.mesh is the chunk's MeshHandle
        drawn[c.guid] = e
    end
end
```

From then on every `apply` and `restoreChunk` remeshes the chunks whose surface it changed, and only those: a dig at the corner of four chunks rebuilds four meshes. The new mesh replaces the old one under the same guid, so the entities above show the hole with no re-bind. A volume and its chunk meshes last as long as the script VM that made them, so the entities drawing them are `temporary`: the scene keeps none of them, and the script that builds the volume draws it again in the next session. A chunk that turns from `empty` to `ready` (a dig that opens a new face, a fill that rises into the air above) brings a new guid to draw. `voxelVolume.meshes(v).pending` counts the chunks still being built; at 0 every edit so far is on screen, and each chunk's `revision` names the volume revision its mesh shows.

A surface vertex sits inside the cell between eight voxel centres, so a chunk draws the surface from half a voxel below its first voxel to half a voxel below the next chunk's first: with the default origin, 0.25 m voxels and 32-voxel chunks, chunk `x` draws from `x * 8 - 0.125` m to `x * 8 + 7.875` m on each axis. The surface covers every chunk whose drawn span reaches into the box you give, and every chunk an edit reaches; with no box it covers the edits alone. So the drawn patch is whole chunks: to draw exactly a box, shift the lattice by half a voxel (`origin = vector.create(0.125, 0.125, 0.125)`, as for material boundaries above), so that chunk `x` draws from `x * 8` m to `x * 8 + 8` m, and pick a `chunkSize` whose span puts the box's edges on chunk edges (16-voxel chunks draw 4 m each). A 24 m box from -12 to 12 then draws exactly -12 to 12.

A chunk entity collides like any static mesh: give it a static `Physics` body and a `MeshCollider` with `fit = "exact"`, and its collider follows the mesh each dig replaces. The new mesh and its collider land together: the collider's triangles are cooked on the worker threads from the new mesh, and the chunk shows its new surface in the frame its new collider stands, so a ray, a body and the picture agree on every frame and no frame pays for the cook. Until a chunk's first mesh lands, the volume holds the ground there (`Physics.groundPending` names it as `voxelVolume:<id>`), so a player placed over a region still meshing waits for it instead of falling. Neighbouring chunks meet with no crack: each chunk reads a one-voxel border of its neighbours and builds the vertices it shares with them from the same voxels in integer arithmetic, so they agree to the bit, and a chunk mesh's `checksum` is the same on native and on the web. Each vertex has an outward normal from the density gradient and the colour `palette` gives its material (white for a material it leaves out). The palette reaches the screen as vertex colour, which the `Model`'s default material draws; a material you name on the `Model` shows the layers only if it reads vertex colour. `voxelVolume.unmesh(v)` drops the surface; `destroy` drops it with the volume.

## Lifetime and memory

A volume lives until `voxelVolume.destroy(v)` or until the script VM that made it closes. `voxelVolume.info(v)` and `voxelVolume.list()` state the bytes each holds, and `memory.report()` lists all of them under `voxelVolumes`. One operation covers at most 33,554,432 voxels, and one measure reads at most 268,435,456. A volume reaches 2^20 voxels from its origin on each axis (262,144 m at 0.25 m voxels): an edit, a measure, a sample or a ray's origin past that raises.
