Core Concepts

Scenes

JSON scene files shared by client and server, built per side and edited live in the level editor.

A scene is a JSON file in scenes/<name>.json in the project root (next to mavonengine.json). The client and the server load the same file, and each side only builds the objects it has a factory for: the client builds what is rendered, the server builds what it simulates (colliders, game logic).

The multiplayer template's map lives in scenes/main.json: sky, fog, lights and ground for the client, a tree field for the server.

Scene file format

{
  "version": 1,
  "name": "main",
  "settings": {
    "background": "#87ceeb",
    "fog": { "color": "#87ceeb", "density": 0.012 }
  },
  "objects": [
    {
      "id": "sun",
      "type": "Light:DirectionalLight",
      "name": "Sun",
      "transform": {
        "position": [20, 40, 20],
        "rotation": [0, 0, 0],
        "scale": [1, 1, 1]
      },
      "props": { "color": "#fff4e0", "intensity": 1.2, "castShadow": true }
    }
  ]
}
FieldDescription
versionScene format version, currently 1
nameShown in the editor
settings.backgroundCSS color of the background (client)
settings.fog{ color, density }, applied as FogExp2 (client)
objects[].idUnique within the scene; used as the entity key in the world on both sides
objects[].typeA registered scene object type
objects[].transformposition, rotation (radians, XYZ) and scale as [x, y, z]
objects[].propsType specific data

Scene names may use letters, digits, - and _.

Loading a scene

Client

const game = new Game(sources, physicsWorld)

// Fetches /scenes/main.json and loads it once the resources have loaded
game.openScene('main')

openScene() also takes a URL or the scene data itself, and returns a promise of the loaded GameScene. The Vite plugin serves scenes/*.json in dev and copies them into the build.

Server

const server = new Server(logger, physicsWorld, {
  // The project root, with mavonengine.json and scenes/. Defaults to plugins.root
  root: projectRoot,
})

server.start().then(() => {
  server.openScene('main')
})

The server reads <root>/scenes/main.json, or a path you pass. With { watch: true }, or by default while the client is served through Vite (client.dev), it reloads the scene whenever the file changes, for example when the editor saves it.

Reacting to a scene

Both sides trigger sceneLoading and sceneLoaded on the game, also on every reload:

game.on('sceneLoaded', (scene: GameScene) => {
  // scene.getEntries() lists every object; entry.object is set when this side built it
})

game.activeScene // the GameScene loaded last

loadScene(data, source?) on BaseGame replaces the active scene directly with scene data you already have.

Scene object types

A type says how an object is built on each side and how it is saved. Register it with registerSceneObject():

import { registerSceneObject } from '@mavonengine/core/Scene/SceneObjectRegistry'

// Server: build colliders and logic only
registerSceneObject<TreeField>({
  type: 'TreeField',
  label: 'Tree field',
  server: data => new TreeField(data),
})
// Client: build what is rendered and say what to save
registerSceneObject<Crate>({
  type: 'Crate',
  client: data => new Crate(new Vector3(...data.transform.position), data.props),
  serialize: crate => ({ color: crate.color }),
})
FieldDescription
typeKey used in the scene file
labelShown in the editor, defaults to type
client(data, ctx)Build the object on the client. Return a GameObject, or nothing to skip it
server(data, ctx)Build the object on the server
serialize(object)The props to save. The transform is saved for you. Without it, the props are kept as loaded
transformTarget(object)The Object3D the editor moves. Defaults to object.instance

Registering the same type on both sides merges the definitions. A side without a factory skips the object but keeps its data, so saving never drops it. Plugins register types with ctx.registerSceneObject(), keyed as <package>:<type>.

Scene objects are map data that every client loads itself, so the server does not send them in the state sync, unless they are networked entities (NetworkedGameObject).

Built-in types (client)

TypeProps
Primitive:<Name>Box, Capsule, Circle, Cone, Cylinder, Dodecahedron, Edges, Extrude, Icosahedron, Lathe, Octahedron, Plane, Polyhedron, Ring, Shape, Sphere, Tetrahedron, Torus, TorusKnot, Tube, Wireframe. Material and shadow props; width / height for Plane
Light:<Name>Three.js lights, e.g. Light:AmbientLight, Light:DirectionalLight. color, intensity, castShadow, …
Modelresource (name of a loaded gltfModel), castShadow, receiveShadow
prefab:<key>Any prefab of the prefab registry

Editing scenes

The level editor shows the active scene in its Scene panel, marks unsaved changes and saves with Ctrl+S (or Cmd+S) back to scenes/<name>.json through the dev server. The game server then reloads the file and every client gets the new map.

GameScene can also be changed from code: create(type, data), remove(id), setProps(id, props), setName(id, name), setSettings(settings), and serialize() to get the JSON back. It triggers objectAdded, objectRemoved, settingsChanged and changed.