High-res edition, Nov 2026 · as built
Tessera
A Wee book about a voxel fantasy console for the browser: what it is, how a frame gets made, what a cart looks like inside, and how to write your first one.
A tessera is one tile of a mosaic. In Tessera every voxel is a tile. This edition describes the high-res console as it is built: a 256 × 256 × 128 world drawn at 768 × 432. For the deep dive into each part, read Tessera High-Res, explained.
The puppy-island beach, painted once at load into a 480 × 270 canvas and scaled up with chunky pixels, in the spirit of Tessera's own 768 × 432 picture. Its colours come from the guide's first draft palette. This is an illustration, not the real renderer.
01What Tessera is
Tessera is a general-purpose fantasy console: a pretend games machine with fixed, small limits that lives in a web page. Instead of a flat screen of pixels it has a box of 3D voxels, and you program it in Lua. The limits are the point. A fixed world, 255 colours and four sound channels keep carts quick to make and easy to share.
The first cart is a port of Puppy Coast: Hat Chase, the three.js game, and it plays at 60 fps on a desktop GPU and on a phone. Chapter 9 shows how that game maps onto Tessera piece by piece.
02A box of tiles
The whole display is one block of voxels, 256 wide, 256 deep and 128 tall. Tessera puts z up, so x and y are positions on the ground (x east, y north) and z is height. Each voxel holds one byte: 0 means empty, and 1 to 255 pick a colour from the palette.
idx() in src/core/vram.js), which matches how a 3D texture is laid out.pal(76, …) recolours every sand voxel at once without touching the 8 MiB layers. Index 0 is always empty.The palette
Each cart can define its own 255 colours. A cart that defines none gets the default Tessera palette, laid out as a 16 × 16 grid so you can find colours by row and column.
src/core/palette.js), used by any cart that defines no palette of its own. Read an index as row and column in hex: sand is 0x4C (row 4 amber, column C), sea is 0xA8. Because every hue row runs dark to light in the same 16 steps, shading something darker is just "subtract one".03One frame
Tessera keeps two layers. The map layer is the world that stays put: terrain, buildings, the lighthouse. You build it once in _init. The draw layer is everything that moves: characters, the sea, sparkles. It lives in its own layer, which cls() empties at the start of every frame, and is redrawn on top of the map.
cls() and refilled by _draw, so moving things never need erasing. Each layer is cut into 2,048 bricks of 16 × 16 × 16 voxels, and the renderer uploads only the bricks that changed since the last frame (src/render/renderer.js). In the shader a non-empty draw voxel wins; otherwise the ray sees the map voxel. The HUD is a third texture the same shader lays on top. Memory and uploads has the details.Timing
A cart has three functions. _init runs once when the cart loads. _update runs exactly 30 times a second and should hold the game rules. _draw runs once for every frame the screen shows and should only draw.
_draw (coral) runs once per displayed frame: twice per update at 60 Hz, four times at 120 Hz. Shading shows which update each draw follows.04Making the picture
Tessera does not turn voxels into triangles. A WebGL2 fragment shader shoots one ray per pixel of a fixed 768 × 432 picture into the two 3D textures and walks it voxel by voxel with a DDA (a digital differential analyser: step to whichever cell edge is nearer, x or y or z, and check that cell). The camera is orthographic, so every ray points the same way, about 35° down, just like the Hat Chase camera today. The small picture is then scaled up with nearest-neighbour sampling, which gives the chunky look.
Shading, in four cheap steps
- Face light: top, side and front faces get fixed brightness levels, so shapes read clearly.
- Sun shadow: one extra ray toward the sun, as in Figure 7.
- Ambient occlusion: peek at a few neighbouring voxels and darken corners a little.
- Fog (optional): fade distant voxels toward a colour set by
fog(c, density). - Frame reuse: when nothing changed since the last frame, the renderer shows the last picture again instead of raymarching.
On top sits a 768 × 432 2D overlay for HUD text and icons in a built-in 5 × 7 pixel font. The overlay is drawn after the raymarch, so text is always crisp and never hidden behind voxels.
05Inside the machine
The console is written in JavaScript, split into small modules under src/. Only shell/ touches the page; the rest is tested on its own.
| Module | Job | Note |
|---|---|---|
core/core.js | the machine: the ports above; every load starts a fresh machine | no DOM: unit-tested in Node |
core/api.js · core/lua.js | the Lua API; the wasmoon host with a 2 s watchdog and cart line numbers on errors | a cart error shows the Tessera error screen; the console never crashes |
core/vram.js | the two layers, 16³ brick tracking, primitives (box, sphere, line, models) | only changed bricks are marked for upload |
render/renderer.js · shaders.js | WebGL2 raymarcher over two R8UI 3D textures | reuses the last picture when nothing changed |
render/overlay.js · font.js | 768 × 432 HUD in a 5 × 7 pixel font | drawn after the raymarch |
io/input.js | keyboard, standard gamepads and touch → the input array | arrows/WASD, Z/Space, X/E, M mute |
io/audio.js | 4-channel synth: square, triangle, saw, noise; envelopes; pitch slides | plays the sound event queue |
cart/tess.js · png.js · pngfile.js | parse and write .tess; encode and decode .tess.png | pure functions |
shell/ | boot screen, cart picker, pause menu, error screen, code editor (F4), stats (F3) | the only part that owns the page |
Build: Vite with vite-plugin-singlefile inlines everything, including the Lua WebAssembly, into one tessera.html that works offline.
Built to be swapped
The core sits behind a "machine level" boundary. Only plain data crosses it: cart text in, button states in, and out come the two 8 MiB layers, the palette, camera numbers, HUD commands and sound events. Nothing on the outside knows which language the core is written in.
.wasm later. The ports are the TesseraCore object in src/core/core.js; slot 32 of the input array carries the mute key.box() crosses the bridge once and fills thousands of voxels on the host side, which is why the API favours big calls. A cart that calls vset 10,000 times per frame crosses 10,000 times. With Lua and the machine in the same .wasm, the crossing (coral) disappears. Measured on this console, fast argument passing brought a typical seven-argument call to about 3 µs (Lua calls and speed); a benchmark decides when the Rust core is worth building.In code, the boundary is small enough to write on one card:
// The machine boundary: the object createCore() returns (src/core/core.js).
// A later Rust core fills the same ports.
const core = await createCore();
core.load(cartText); // fresh machine, parse, run the Lua: {ok} or {ok: false, error}
core.init(); // runs _init once
core.tick(); // one 30 Hz tick: runs _update
core.draw(); // runs _draw into the draw layer
core.input; // Float32Array(33): 4 players x (6 buttons + 2 axes), then mute
core.map; // Uint8Array, 256*256*128 = 8,388,608 bytes
core.drawLayer; // Uint8Array, same size, emptied by cls()
core.palette; // Uint8Array(768): 256 x RGB
core.view; // Float32Array(16): camera, zoom, yaw, sun, sky, fog
core.overlay; // HUD commands for this frame
core.sounds; // sfx / tone events since the shell last took them
core.storage; // dset / dget values
core.error; // {line, message} for the Tessera error screen, or null
In a later Rust version, the layers would be views straight into WebAssembly memory, so nothing is copied before upload.
06Carts
A cart is everything a game needs in one file: its name, colours, voxel models, sounds and code. It comes in two forms that hold the same thing.
.tess cart is plain text, so it diffs cleanly in git and can be read in any editor (or in Tessera's own code editor, F4). The model shown is the real hat from the built carts/hatchase.tess: 10 wide, 10 deep, 12 tall, its colours indices into the cart's own __pal__. The __sfx__ line is an example: Hat Chase makes its sounds with tone(). The port converted every source model at twice the detail, so each original cube became 2 × 2 × 2 voxels.TESS, version, length and a CRC-32) tells the reader where the data ends and whether it survived. Hat Chase compresses to 28.6 KiB, well inside the 64 KiB.07The API
Every function a cart can call, grouped by job (src/core/api.js). Square brackets mark optional arguments. Coordinates are voxels (x, y across, z up) for the 3D calls and pixels for the HUD. Buttons: 0 left, 1 right, 2 up, 3 down, 4 A (Z or Space), 5 B (X or E), and the same on a standard gamepad.
Map layer persists between frames
mset(x,y,z,c)- set one map voxel
mget(x,y,z)- read one map voxel
mclear([c])- empty (or fill) the whole map
mbox(x0,y0,z0, x1,y1,z1, c)- fill a box on the map
mmodel(id,x,y,z,[rot],[flip])- stamp a model into the map
mheight(x,y)- top of the map column at x,y
Draw layer cleared every frame
cls()- start the frame: empty the draw layer
vset(x,y,z,c) · vget(x,y,z)- set / read one voxel this frame
box(x0,y0,z0, x1,y1,z1, c,[hollow])- solid or hollow box
sphere(x,y,z,r,c)- filled ball
line3d(…)- a line of voxels
model(id,x,y,z,[rot],[flip],[frame])- draw a model (animation frame)
particle(x,y,z,c,[life])- short-lived speck
Models
msize(id)- a model's size
mvox(id,[frame])- a model's voxels, as a table
newmodel(id,w,d,h,[frames])- make an empty model
mpset(id,x,y,z,c,[frame])- set a voxel inside a model
Palette
pal(i,r,g,b)- set colour i (1–255)
palt()- reset to the cart's palette
pswap(a,b)- swap two colours
Camera and light
camera(x,y,z,[zoom],[yaw])- where the view looks
sun(dx,dy,dz,[strength])- sun direction for light and shadow
sky(top,bottom)- background gradient
fog(c,density)- distance fog
HUD (2D overlay) 768 × 432 pixels
print(s,x,y,c,[outline],[scale])- 5 × 7 pixel-font text
rect(…) · rectfill(…)- rectangles
circfill(…)- filled circle
spr2d(…)- 2D icon
textw(s)- width of s in pixels
project(x,y,z) → sx,sy- voxel position to screen pixel
Input up to 4 players
btn(b,[p])- is button b held
btnp(b,[p])- did b just go down
axis(n,[p])- stick axis n, −1..1
Collision & Space fast host physics queries
raycast3d(x0,y0,z0, dx,dy,dz, [max_d], [layer])- 3D DDA raycast: hit, x, y, z, nx, ny, nz, c, dist
check_box(x0,y0,z0, x1,y1,z1, [layer])- test if AABB contains any voxel: hit, x, y, z, c
sweep_box(x0,y0,z0, x1,y1,z1, dx,dy,dz, [max_d])- sweep AABB along vector: hit, dist, x, y, z, nx, ny, nz, c
sweep_down(x,y,z, [w], [d], [max_drop])- drop footprint down: hit, ground_z, dist, x, y, z, c
vheight(x,y, [w], [d], [layer])- highest solid voxel in footprint (−1 if empty)
Sound & Music 4 channels
sfx(n,[ch])- play sound n from __sfx__
tone(wave,f0,f1,dur,vol,[ch])- one pitch slide
mute()- toggle sound; M key too
music(id,[fade],[mask])- start a __music__ song
music_stop([fade]) · music_pos() · music_vol([v])- stop, query row, or set volume
Misc
t()- seconds since start
rnd(n)- random 0..n
flr(x) · mid(a,b,c)- floor · middle value
noise3(x,y,z)- smooth 3D noise
stat(n)- 0 fps · 1 update ms · 2 draw ms · 3 composite ms · 4 last input
dset(i,v) · dget(i)- save slots
printh(s)- print to the host console
quality([q])- graphics: "hq" (smooth, native-res) or "standard"; returns what is in use
m and last; draw calls are redone every frame.08Your first cart
Six steps from an empty cart to a hat you can steer, with sound and a save. Each step adds a little to the last. The pictures are sketches of what you should see; paste a step into the code editor (F4) to see the real thing.
Step 1Hello, box
Every cart is Lua 5.4. Tessera calls
_drawonce per displayed frame. Clear, draw one box, print one line.function _draw() cls() -- empty the draw layer box(120,120,0, 135,135,15, 76) -- a 16-voxel sand cube print("hello, tessera", 8, 8, 15) -- HUD text, white endYou should see: A sand-coloured cube in the middle of the floor, and white pixel text in the top-left corner.
Step 2Make it bounce
t()is the time in seconds. Lua has no+=, and maths lives inmath.function _draw() cls() local z = 16 + math.abs(math.sin(t() * 3)) * 80 sphere(128,128,z, 16, 23) -- radius 16, red print("boing", 8, 8, 15) endYou should see: A red ball bouncing up and down about once a second. Nothing needs erasing:
cls()empties the draw layer every frame.Step 3Build a world once
Things that never move go on the map layer in
_init. It stays put;cls()only empties the draw layer on top of it.function _init() mbox(0,196,0, 255,255,0, 168) -- sea to the north (high y) mbox(0,0,0, 255,195,1, 76) -- sand, 2 voxels deep for x = 226,243 do -- one striped hut local c = (x // 2 % 2 == 0) and 23 or 15 mbox(x,140,2, x,159,24, c) end end function _draw() cls() -- the map stays endYou should see: A beach: a strip of sea at the back, sand everywhere else, and one red-and-white striped hut.
Step 4Move the hat
Read buttons in
_update(30 times a second) and draw in_draw. Buttons 0–3 are left, right, up, down; up is north, which is +y.hx, hy = 128, 96 function _update() if btn(0) then hx = hx - 2 end if btn(1) then hx = hx + 2 end if btn(2) then hy = hy + 2 end -- up = north = +y if btn(3) then hy = hy - 2 end end function _draw() cls() model("hat", hx, hy, mheight(hx, hy)) camera(hx, hy, 0) -- follow the hat endYou should see: The red hat sits on the sand. Arrow keys or WASD (or a drag on a touch screen) slide it around and the camera follows. (Add your
_initfrom step 3 and ahatmodel in the cart's__models__.)Step 5Make a sound
btnpis true only on the tick a button goes down.toneplays a quick pitch slide.function _update() -- ... movement from step 4 ... if btnp(4) then -- A: Z or Space tone("square", 880, 440, 0.15, 0.5) end if btnp(5) then sfx(0) end -- B: sound 0 from __sfx__ endYou should see: Press Z or Space for a short falling “boop”. X or E plays sound 0 from the cart. M mutes.
Step 6Save your cart
Open the pause menu to export a
.tess.pngpicture cart with a screenshot on the front, or keep the.tesstext file. Drag either one onto Tessera to load it, or open?cart=with a bundled cart's name.-- high scores survive between plays: function _init() best = dget(0) -- save slot 0 end function win(score) if score > best then best = score; dset(0, best) end endYou should see: Two files:
hat.tess(readable text, good for git) andhat.tess.png(a picture you can share). Both load the same cart.
Starting outside the browser with npm
You can scaffold, develop, pack, and export a cart from your own editor without cloning the engine repo using the public npm packages @stephenwil/create-tessera and @stephenwil/tessera:
npm create @stephenwil/tessera@latest my-game
cd my-game
npm install
npm run dev # live-reload console at http://localhost:3000
npm run check # validate manifest and Lua structure
npm run pack # emit dist/my-game.tess and dist/my-game.tess.png
npm run build # export standalone offline dist/my-game.html
# Or list and start from any playable showcase cart
npx @stephenwil/tessera@latest samples
npx @stephenwil/tessera@latest pull bramblecourt --project my-arena
npm create @stephenwil/tessera@latest my-arena -- --template bramblecourt
Building carts with AI agents (MCP)
AI coding agents with terminal access can run the npm CLI directly. For tool-driven clients (Claude Desktop, Cursor, T3 Code), connect the built-in Model Context Protocol (MCP) server (npx -y @stephenwil/tessera@latest mcp or npx -y @stephenwil/tessera-mcp@latest) to expose 8 structured tools for scaffolding, syntax checking, MagicaVoxel palette budgeting, picture-cart packing, HTML export, and live API reference lookup.
09Porting Hat Chase
Hat Chase already had the Tessera shape: a 128 × 128 beach, voxel models made of coloured cubes, an orthographic camera and small synth sounds. The port moved each part to the matching Tessera feature, then doubled everything for the high-res world. The cart's source lives in the private tessera-hatchase repo; this repo keeps the built carts/hatchase.tess.
world.js (the same rules world.lua runs in the cart) and colouring each column with a default-palette index. In Tessera everything here except the sea goes on the map layer once, in _init. North (the sea) is at the top, at high y: the built port sets y = 127 − old z, then doubles every axis, so each source column becomes 2 × 2 voxels and each height doubles.| Part | Hat Chase (three.js) | In Tessera |
|---|---|---|
| Terrain | world.js builds column segments into one InstancedMesh | world.lua runs the same rules and fills the map layer once in _init, every box doubled |
| Voxel models | [x, y, z, "#hex"] lists in models.js | __models__, re-converted from the source at 2 × detail; hex colours become the cart's __pal__ |
| Leg animation | leg groups rotated by Math.sin each frame | model frames (Betsy has frames=6), chosen with model(…, frame) |
| Turning | dogs rotate to any angle | dogs are drawn at quarter turns: odd angles fatten thin parts of a model |
| Animated sea | updateWater rewrites 128 × 52 instances per frame | redrawn into the draw layer every frame; only its changed bricks are uploaded |
| Follow camera | OrthographicCamera, offset (0, 72, 100): about 36° down | camera(x,y,z,[zoom],[yaw]); the raymarcher is orthographic at ~35° |
| Shadows | PCF shadow map, 2048 × 2048 | one shadow ray per pixel toward sun() |
| Round moods | midday, afternoon, sunset light colours | sun(), sky() and pal() |
| HUD | DOM elements (banner, shells, stars) | overlay: print, rectfill, spr2d |
| Popups | DOM text placed over the 3D scene | project(x,y,z) gives the pixel, then print |
| Paw prints, sand | particle meshes | particle(x,y,z,c,[life]) |
| Sound | WebAudio tone() and noise() | tone() on the 4-channel synth (noise is a wave) |
| Controls | arrows/WASD run, Space special, E sniff; drag to run on touch | buttons 0–3, A (Z/Space), B (X/E), gamepad, and the shell's touch controls |
| Rounds, stars, shells | JavaScript state in main.js | plain Lua tables; no fail state, as now |
- Only Betsy's left front paw is white. The axis change is a rotation, so her left stays her left.
- Never state Betsy's or Sunday's age.
- No golden scallop shell.
10Built and next
Built
- Name Tessera; carts
.tessand.tess.png(export from the pause menu, drag-drop to load) - 256 × 256 × 128 volume, 1 byte per voxel, map and draw layers of 8 MiB each
- Per-cart palette of 255 colours, 0 empty, default palette as fallback
- WebGL2 orthographic DDA raymarcher at 768 × 432 (plus
quality("hq")), brick skipping, brick uploads, frame reuse, and a 5 × 7 font overlay - Fixed 30 Hz
_update;_drawevery displayed frame - Lua 5.4 via wasmoon, with a watchdog, cart line numbers on errors, and host-side collision probes (
raycast3d,check_box,sweep_box,sweep_down,vheight) - Keyboard, gamepad (menus too) and touch; 4-channel synth; mute
- Code editor (F4) with Lua colouring and jump-to-error; voxel model editor (F5); stats overlay (F3)
- Public npm developer kit (
@stephenwil/create-tessera,@stephenwil/tesseraCLI) and MCP server (@stephenwil/tessera-mcp) for AI coding agents - Hat Chase, 60 fps on a desktop GPU and a phone, plus more carts in
carts/
Next
- Music tracker authoring polish for the
__music__section and pattern player - Zero-allocation 3D math primitives (
vec3,quat) - Headless tick simulator / frame capture MCP tool for AI visual inspection
- Rust core with Lua in the same
.wasm: when a benchmark says it is worth it - Still open:
model()rotation at odd angles fattens thin models; touch needs a real-tablet check