Skip to content

Build a multi-root game β€” a 3D root with a 2D overlay

tutorial lesson Intermediate ⏱ 10 min vgai 0.1.0

Prerequisites: Take the editor tour Β· Scaffold a project

What you'll learn

  • Declare a two-world game (threejs + pixijs) in vgai.game.json
  • Navigate the per-world hierarchy groups and inspect entities in each world
  • Persist a play-mode edit to a world's own overlay file

What you'll build: A 3D scene with a 2D pixijs HUD overlay stacked above it, edited live in one session.

Walkthrough β€” Build a multi-root game β€” a 3D root with a 2D overlay Β· generated by site/media/lessons/learn-multiworld.lesson.ts (5 steps)

A vgai game is a manifest-backed adapter-root list: vgai.game.json can declare any number of threejs, pixijs, and react adapters, and one Game mounts them as stacked layers β€” one shared loop, per-root zOrder, each root with its own scene and its own components. In this lesson you build the classic shape: a 3D world with a 2D HUD overlay above it, then edit both through one editor session. (Concept reference: Multi-world games & play control.)

  1. Press Play. One Game mounts the root list: the Three.js root renders on the bottom canvas, and the pixijs overlay stacks above it (zOrder: 10) on its own transparent canvas β€” you see the yellow sprite floating over the 3D scene.

    The 3D world with the yellow 2D sprite stacked above it
    Two worlds, one Game: the pixijs overlay's canvas stacks above the threejs canvas.
  2. Read the hierarchy. The hierarchy shows native Three.js and PixiJS roots, with each root’s entities beneath it.

    Hierarchy showing the PixiJS root with HudBadge beneath it
    Native adapter roots organize their own entities.
  3. Select the cube in the threejs world. The inspector shows its transform like any entity β€” the same panels you know from single-world editing.

  4. Select the sprite in the pixijs overlay. The same hierarchy + inspector route to the 2D world: HudBadge shows 2D position, scale, alpha, and tint.

    The HudBadge sprite selected, 2D inspector visible
    The same panels drive the 2D world β€” position, alpha, tint.
  5. Edit Position-Y and save. Change the sprite’s Position Y to 240. In a multi-world session, play-mode edits persist to per-world overlay files β€” this one lands in .vgai/overlays/overlay.json in your project, keyed by the world id.

    The sprite moved down after the Position-Y edit
    The edit persists to the overlay world's own file: .vgai/overlays/overlay.json.

Cross-world code works the same way: game.queryByComponent(Cls) spans every first-party world in one call (narrow with {worldId} or {kind}). And the world node is the entity in every kind β€” Object3D in threejs worlds, PIXI.Container in pixijs worlds.

Recap

New functionality

  • Declared a two-world game (threejs + pixijs overlay) in vgai.game.json
  • Played it β€” two stacked canvases, one Game, one loop
  • Inspected entities in both worlds and persisted a 2D edit to the world's overlay file

New concepts & skills

  • a game is a manifest-backed adapter-root list mounted by one Game
  • roots stack as layers by zOrder; upper canvases are transparent
  • the hierarchy presents native adapter roots
  • multi-root play edits persist per root

Next lesson β†’ Pause, resume & step

On Your Own!

Extend what you built:

  • Add a second pixijs overlay at zOrder 20 and watch the layer order
  • Give the 2D scene more sprites and tint them from the inspector
  • Add a react world to the list β€” see the React worlds tutorial