Molten

Levels and chunks

A level is a place: lighting, the objects placed in it, and spawn points. It owns no players and no UI. Exactly one level is loaded at a time (or none).

A level is a folder: levels/<name>/ holds <name>.level and a chunks/ folder. The level's name is the folder name: levels/farmhouse/farmhouse.level is "farmhouse".

levels/
  farmhouse/
    farmhouse.level
    chunks/
      0_0.chunk
      1_0.chunk

The .level file

// levels/farmhouse/farmhouse.level (from samples/farmhouse)
export default level({
    chunkSize: 64,
    environment: { sunDirection: vec3(-0.3, -1, -0.4), ambient: vec3(0.15, 0.15, 0.2) },
    spawnPoints: [{ id: "spawn-1", position: dvec3(0, 0.9, 6) }],
});
  • chunkSize (meters, default 64) is the side of each chunk square.
  • environment sets the sun (sunDirection, sunColor, sunIntensity) and ambient light. A sun direction creates a level-owned object with a Light. skybox is accepted but not drawn yet.
  • spawnPoints lists named positions (and optional rotations). Cast reads them with spawnPosition(index), in file order.

Chunks

The world is split into a grid of .chunk files so that two people editing different areas rarely touch the same file, and so levels can be streamed later.

// levels/farmhouse/chunks/0_0.chunk (from samples/farmhouse)
export default chunk({
    format: "chunk-local-v1",
    coord: [0, 0],
    objects: [
        { id: "ground", type: Ground },
        { id: "crate-01", type: Crate, position: vec3(-3, 0, 0) },
        {
            id: "crate-02",
            type: Crate,
            position: vec3(3, 0, 0),
            rotation: euler(0, 30, 0),
            overrides: { mesh: { color: vec4(1, 0.3, 0.3, 1) } },
        },
        { id: "crate-03", type: Crate, position: vec3(0, 0, -4), scale: 2 },
    ],
});
  • The file name x_z.chunk must match coord: [x, z].
  • Chunk x_z covers the square of side chunkSize centered on (x * chunkSize, z * chunkSize). Chunk placement coordinates are float offsets from that center. The loader adds the center in double precision. Devtools puts new and moved objects in the chunk for their position, creating the file as needed.
  • Every chunk of a level is loaded when the level loads (streaming comes later).

Convert older chunks with castc fmt --convert-chunks FILE.... The required format: "chunk-local-v1" marker identifies converted files.

Large worlds

Levels can reach far from the origin: positions are double precision in Cast (dvec3), the C API, physics and the files' chunk centers. A scene 1,000 km out looks, moves and collides exactly as it does at the origin, with no jitter, and devtools edits it the same way.

The GPU still works in float. Each frame, rendering subtracts a render origin from every position in double precision before converting to float. The render origin is a point on the chunk grid (chunkSize) near the camera. It moves, by whole chunks, only when the camera gets more than 2 chunks away from it, and that move is invisible. Sounds are positioned from their offset to the listener the same way. Gameplay never sees the render origin; you keep using world positions. Only custom shaders notice it (see Assets).

Placed objects

Key Meaning
id Required. Unique within the level, never changes
type Required. An object type name
position vec3(...) offset from the chunk center, default zero
rotation euler(x, y, z) in degrees or quat(x, y, z, w)
scale A number (uniform) or vec3(...)
overrides Component values for this object: { health: { max: 120 } }

The ids matter: live reload compares chunks by id (added, removed, moved), devtools saves edits by id, and placedObject("id") finds the object from Cast.

When a level loads and unloads

  • switchScreen(S) loads the level S names (or unloads the current one if S has level: null). It does this every time, so switching to the same screen again restarts the level from its files.
  • pushScreen and popScreen never change the level.
  • Unloading destroys every object the level placed, plus every object spawned while it was loaded (including the player). Memory from the level arena is freed.

View this page's source on GitHub