Phaser 4.3 World3D: a small, fast, retro 3D world inside one Game Object, WebGL only (this.add.world3D). Covers building models in code from shapes (addBox, addSphere, addCylinder, addPlane, addWall, addBillboard, addContainer, Mesh3D.add, createLathe, createExtrude), terrain (addTerrain, getHeightAt), textures and normal maps, 3D lights (setAmbientColor, addLight, addSpotLight, setTiledLights), fog, sky, reflections, shadows, glow, decals, 3D particles, the Camera3D and its orbit, free and follow controls, and picking with raycast. Use when making a 3D scene, a first or third person game, a dungeon crawler, a racing or flying game, a model from primitives, or a 3D backdrop for a 2D game. It has no model loader, skeletal animation or physics. Triggers on: 3D, World3D, world3D, Mesh3D, Billboard3D, Camera3D, Light3D, first person, FPS, third person, dungeon crawler, low poly, PS1, retro 3D, 3D model, lathe, extrude, heightmap, terrain, skybox, decal, spot light, raycast, orbit camera, z
Capabilities
Quick start
The space
Every one returns a Mesh3D (or a Billboard3D, or a Terrain3D, which extend it)
Building a model from shapes
There is no model loader
Two faces in the same place flicker
If two faces lie in the same plane and overlap, the GPU can't tell which is in front, and they flicker in…
Getting started
Full instructionsInstall the skill, then ask your AI tool for the task in plain words. The full SKILL.md below shows what it covers.
Instructions: SKILL.mdv1.0.0
World3D
A World3D is one Game Object which holds and draws a 3D world: shapes, billboards, lights and a 3D camera. It draws the world to a texture of its own, then shows that as an Image does, so it sits in a Scene with your 2D objects, and can be scaled, tinted and given filters. The things in it are Mesh3D objects, which are small and are not Game Objects.
Source: src/gameobjects/world3d/ (World3D.js, Mesh3D.js, Billboard3D.js, Geometry3D.js, Light3D.js, Camera3D.js, Terrain3D.js, ParticleEmitter3D.js, the three *Control3D.js files, and typedefs/ for every config). Related: phaser4-core, loading-assets, lighting (the 2D lights, which are separate), tilemaps.
Quick start
This needs no assets: a shape with no texture is plain white, for you to tint.
class Yard extends Phaser.Scene
{
create ()
{
// WebGL only. In Canvas a World3D draws nothing.
if (this.renderer.type !== Phaser.WEBGL)
{
return;
}
const { width, height } = this.scale;
// x and y are its middle in the Scene. The size is the size of its texture.
const world = this.add.world3D(width / 2, height / 2, width, height);
// The ambient color starts as white, which hides every light. Lower it.
world.setAmbientColor(0x404858);
world.setDirectionalLight(-0.5, -1, -0.3, 0xfff0d8, 1);
world.setBackgroundColor(0x202838);
// A floor is a plane. A box is placed by its middle, so y is half its height.
world.addPlane(0, 0, 0, 40, 40).setTint(0x4a7a4a);
this.crate = world.addBox(0, 0.5, 0, 1, 1, 1).setTint(0xc08040);
world.addLight(2, 2, 1, 8, 0xff8040, 2);
// y is up, and the camera looks along the negative z axis until you turn it.
world.camera.setPosition(0, 2.5, 6).lookAt(0, 0.5, 0);
this.world = world;
}
update (time, delta)
{
if (this.crate)
{
this.crate.rotationY += delta * 0.001;
}
}
}
The space
- Right-handed, y up. The camera, and every mesh, faces along its own negative z axis. Units are your own: use 1 for a metre or a tile.
- Rotations are in radians, as
rotationX,rotationY,rotationZ. The one exception iscamera.fov, which is in degrees. - Each shape is placed by a different point. Getting this wrong buries things in the floor, or leaves them floating:
| Method | Placed by | Faces |
|---|---|---|
addBox(x, y, z, width, height, depth, texture, frame) |
Its middle | |
addSphere(x, y, z, radius, texture, frame, segments = 16) |
Its middle | |
addCylinder(x, y, z, radius, height, texture, frame, segments = 16) |
Its middle. It stands upright | |
addCone(x, y, z, radius, height, texture, frame, segments = 16) |
Its middle, half way up | |
addPlane(x, y, z, width, depth, texture, frame) |
Its middle | Up. Invisible from below |
addQuad(x, y, z, width, height, texture, frame) |
Its middle | Positive z. One sided |
addWall(x1, z1, x2, z2, height, y, texture, frame) |
y is its bottom |
Its front is on your right, going from the first point to the second. One sided |
addBillboard(x, y, z, texture, frame) |
The middle of its bottom edge: it stands on its position | The camera |
addMesh(x, y, z, geometry, texture, frame) |
Where the geometry's own 0, 0, 0 is | |
addContainer(x, y, z, children) |
Draws nothing. A parent for other meshes |
Every one returns a Mesh3D (or a Billboard3D, or a Terrain3D, which extend it). Call setDoubleSided() on a plane, quad or wall which is seen from both sides.
Building a model from shapes
There is no model loader. A model is a container, with shapes added to it. A child's position, rotation and scale are measured from its parent.
const car = world.addContainer(0, 0, 0);
car.add([
world.addBox(0, 0.6, 0, 1.6, 0.5, 3.2).setTint(0xd03030),
world.addBox(0, 1.05, 0.3, 1.3, 0.5, 1.4).setTint(0x90b0d0)
]);
// A cylinder stands upright, so a wheel is one turned on to its side,
// inside a container which is the thing that rolls.
const wheels = [ [ -0.9, -1 ], [ 0.9, -1 ], [ -0.9, 1 ], [ 0.9, 1 ] ].map(([ x, z ]) =>
{
const wheel = world.addContainer(x, 0.4, z);
wheel.add(world.addCylinder(0, 0, 0, 0.4, 0.3, null, null, 12).setRotation(0, 0, Math.PI / 2).setTint(0x202020));
car.add(wheel);
return wheel;
});
// In update: move the whole car, and roll each wheel. Forward is negative z.
car.z -= 2 * delta / 1000;
wheels.forEach(wheel => { wheel.rotationX -= 5 * delta / 1000; });
// Another one, with all its parts
const second = car.clone().setPosition(4, 0, 0);
parent.add(child)takes a mesh or an array, andremove(child)lets one go.parent,children. Destroying a parent destroys its children. A child can have children, to any depth.setSizedoesn't reach the children,setScaledoes. The size from anaddmethod is the mesh's own. To grow a whole model, scale its container.- A child is still batched with every other mesh of its shape and texture: 100 cars cost the draw calls of one.
clone()copies a mesh and its children. - To turn a part around a point which isn't its middle, such as a door on its hinge or a gun barrel on its mount, put it in a container at that point, offset the part inside it, and turn the container.
getWorldPoint(x, y, z, out)takes a point in the space of a mesh to the world (the end of a barrel, to fire from),getLocalPointgoes the other way,getWorldPosition(out),getForward(out), andlookAt(x, y, z)aims a mesh at a point in the world.getBounds()gives{ minX, minY, minZ, maxX, maxY, maxZ }around a mesh and its children, in the world. Use it to stand one model on another.
Shapes from an outline
// Lathe: points are x = distance from the axis, y = height. An x of 0 at an end closes that end.
world.createLathe('vase', [ 0, 0, 0.3, 0, 0.5, 0.5, 0.2, 1.1, 0.3, 1.3, 0, 1.3 ], 16);
// Extrude: an outline with x to the right and y up, given a depth along z. An optional array of holes.
world.createExtrude('ramp', [ 0, 0, 4, 0, 4, 2 ], 3);
world.addMesh(0, 0, 0, 'vase').setTint(0xc08060);
world.addMesh(5, 0, 0, 'ramp');
- Both make a geometry the size you draw it, kept under the key. Make it once, and use it for many meshes.
- A lathe stands on its position, if its outline starts at a height of 0.
- The depth of an extrude is half to each side of its position, not all to one side. An extrude 6 deep, placed at z = 0, runs from -3 to 3.
- A wheel, a column, a tower, a dome, a bottle and a chess piece are lathes. A star, an arrow, a gear, an arch, a ramp, stairs (their side view) and an I-beam are extrudes.
- Flat shading: add
flatto a built-in shape key for faces the light picks out one by one:world.addMesh(0, 1, 0, 'sphere8flat'),'cylinder6flat'. A rock issphere6flatwith an uneven scale. The number is the segments. Passfalseas the last argument ofcreateLatheorcreateExtrudefor the same, orcreateFlatGeometry(key, geometry). createGeometry(key, vertices, uvs, indices, normals, colors)makes a shape from your own vertices. Onlyverticesis needed. A geometry holds up to 65,535 vertices.
Two faces in the same place flicker
If two faces lie in the same plane and overlap, the GPU can't tell which is in front, and they flicker in stripes (z-fighting). When you build from shapes:
- Don't let the top of one box lie level with a floor it overlaps. Stop it just short (0.02 is enough), or cover the join with another shape.
- Let shapes meet edge to edge, or push one well into the other. Never leave them overlapping by a face.
- For a mark on a surface, such as a sign, a stain or a road line, use
addDecal, which is made for it. Don't float a quad a little above the surface.
Mesh3D
It is not a Game Object: no setInteractive, no events, no depth, no physics body, and it isn't on the display list. Every setter returns the mesh.
| Place and turn | x, y, z, setPosition, rotationX/Y/Z, setRotation(x, y, z), lookAt(x, y, z) |
| Size | setSize(width, height, depth), setScale(x, y, z), scaleX/Y/Z |
| Look | setTint(0xRRGGBB), setAlpha(value), setVisible(bool), setBlendMode('ADD') |
| Texture | setTexture(key, frame), setFrame(frame), setTextureRepeat(x, y), setTextureScroll(x, y) |
| Drawing | setLighting(false), setDoubleSided(), setTransparent(), setReflection(amount), setGlow(amount), setCastShadow(bool) |
| Yours | name, data, body (Phaser never reads them) |
| Remove | destroy() |
- Phaser tweens work on them:
this.tweens.add({ targets: door, rotationY: 1.5, duration: 600 }). - A solid mesh leaves out the pixels of its texture with an alpha under 0.5, which suits sprites and fences. A mesh with an alpha under 1, a blend mode, or
setTransparent()is drawn after the solid ones, sorted from far to near. Keep those few: they batch badly. - There is no physics. Do collision yourself (a tilemap,
getHeightAt,raycast, distances), or step a 3D physics library and callmesh.setTransform(body.position, body.quaternion).
Textures
setTextureRepeat(8)repeats the frame across the mesh: a floor 32 across with a repeat of 32 shows the texture once a unit.setTextureScrollmoves it, for water or a conveyor.- Frames of an atlas work everywhere. Meshes which share a geometry and a texture image are one draw call, so pack the textures of a world into one atlas when you can.
world.drawCallssays how the last frame went. addBoxtakes an array of frames in place of one, all from one texture: six, for the right, left, top, bottom, front and back, or three, for the top, the bottom and the sides.- Normal maps need no call. Load the texture with one,
this.load.image('brick', [ 'brick.png', 'brick-n.png' ]), and every mesh which shows it is lit with it. They use the OpenGL layout, with green up.world.normalStrengthscales them all, and 0 turns them off. - A whole texture with a power of two size gets mipmaps, which stop it shimmering in the distance, if the game config has
render: { mipmapFilter: 'LINEAR_MIPMAP_LINEAR' }. - For the chunky look, make the world small and scale it up:
this.add.world3D(x, y, 320, 240).setDisplaySize(width, height), withpixelArt: true. For smooth edges, make it twice the size andsetScale(0.5).
Lights and fog
world.setAmbientColor(0x202028); // starts as 0xffffff
world.setDirectionalLight(-0.5, -1, -0.3, 0xfff4dc, 1.2); // the direction it shines in. Off by default
world.setFog(0x202028, 10, 60); // color, near, far. clearFog() removes it
const lamp = world.addLight(0, 3, 0, 10, 0xffaa66, 2); // x, y, z, radius, color, intensity
const beam = world.addSpotLight(0, 6, 0, 14, 0xffffff, 3, 0.6, 0.4).lookAt(4, 0, 2); // ..., angle, softness
lamp.startFollow(mesh, 0, 0.5, 0); // goes where the mesh goes. The offset turns with it
- Lights add to the ambient color, which starts as white. Until you lower it, a light changes nothing you can see.
- A light fades to nothing at its
radius. An intensity above 1 is fine, and is how to get strong color. - A spot light shines straight down until you call
setDirection(x, y, z)orlookAt(x, y, z).setCone(angle, softness)turns any light into one, and an angle of 0 turns it back. On a light which follows a mesh, the direction is in the space of the mesh, so a headlight turns with its car. - Only the
world.maxLightsnearest to the camera are drawn, which is 8 by default. Every pixel of every lit mesh works out every one of them, so raise it with care, and give lights a small radius. - For many lights, such as one on each rocket, torch or lamp, call
world.setTiledLights(). Every light in view is then drawn, up to 255, and a pixel works out only those which reach it. The world looks the same, andmaxLightsisn't used.world.lightsDroppedshould stay at 0: if it doesn't, too many lights overlap, so shrink their radius.clearTiledLights()turns it off. - A
Light3DhassetPosition,setRadius,setColor,setIntensityandsetVisible.world.removeLight(light)removes one. - These are not the Scene's 2D lights:
this.lightsandsetLighting(true)do nothing here.mesh.setLighting(false)makes a mesh ignore the 3D lights, and show its texture at full brightness.
Sky, reflections, shadows and glow
Each is off until you call it, and costs nothing until then.
| Call | Notes |
|---|---|
world.setSky('panorama') |
A 2:1 panorama behind everything, which turns with the camera. clearSky(). The sky is world.sky, a mesh. |
mesh.setReflection(0.3) |
0 to 1. Shows the sky, or the texture given to world.setEnvironment(key), in the color of the mesh: white at 1 is chrome. It reflects that picture, not the world. |
world.setShadows(32, 1024) |
The directional light casts shadows over a square size units across, in front of the camera, or around world.shadowTarget = player. Smaller is sharper. Needs the directional light on. A map of 1024 uses 8 MB of GPU memory, and 2048 uses 32 MB. clearShadows(). |
world.setGlow() then mesh.setGlow(1) |
The mesh spreads its color past its edges, as a lamp, lava or a laser bolt does. Use setLighting(false) on it too, and put a light at the same place if it should light what is around it. An emitter has glow in its config. clearGlow(). |
Point and spot lights cast no shadows. A billboard casts one only after setCastShadow(true).
Decals and picking
this.input.on(Phaser.Input.Events.POINTER_DOWN, (pointer) =>
{
const hit = world.hitTestPointer(pointer);
if (hit)
{
// hit.mesh, hit.distance, hit.x/y/z, and the way the face it met points
world.addDecal(hit.x, hit.y, hit.z, hit.normalX, hit.normalY, hit.normalZ, 0.5, 'marks', 'scorch');
}
});
addDecal(x, y, z, normalX, normalY, normalZ, size, texture, frame, rotation)lays a mark on a surface, lit as the surface is, with no flicker. A floor is0, 1, 0. Decals aren't removed for you: keep a list, and destroy the oldest.- To mark a mesh which moves, make the decal its child, at
mesh.getLocalPoint(hit.x, hit.y, hit.z). world.raycast(ray, maxDistance, meshes)does the same for any ray:{ origin, direction }, bothPhaser.Math.Vector3.world.camera.getRay(x, y)andworld.getPointerRay(pointer)make one. Use it for shots and line of sight.- Both test a box around each mesh, not its triangles: exact for boxes, walls and floors, and loose for a sphere or a lathe. They test every mesh, so don't call them every frame in a large world without passing a list of meshes.
getRayandprojectuse the camera as it was last drawn. If you moved it this frame, or are still increate, callworld.camera.update()first.world.isPointerOver(pointer)says if a Pointer is over the World3D at all.
Billboards and particles
const tree = world.addBillboard(3, 0, -5, 'atlas', 'tree').setSize(2, 3);
const enemy = world.addBillboard(0, 0, -8, 'knight').setSizeToFrame(0.02);
enemy.play('walk'); // a Phaser animation, as a Sprite plays one
const fire = world.addParticles(0, 0.5, 0, 'flares', {
frame: 'yellow', lifespan: { min: 400, max: 800 }, frequency: 30, speed: { min: 0.5, max: 1.5 },
spread: 15, size: { start: 0.5, end: 0.1 }, alpha: { start: 1, end: 0 },
tint: { start: 0xffe080, end: 0xff2000 }, blendMode: 'ADD', maxParticles: 60
});
- A billboard is 1 unit high and stands on its position.
setOrigin(0.5, 0.5)centres it, for an orb or a corona. It turns around the y axis only, as a tree does:setUpright(false)makes it face the camera fully. It also hassetFlip(x, y)andsetRotation(radians). - An emitter's particles are moved by the GPU, and each emitter is one draw call.
directiondefaults to up,spreadis in degrees,sizeis in units of the world, and gravity isgravityY: -9. It never holds more thanmaxParticles.explode(count, x, y, z)throws a burst, andstartFollow(mesh, x, y, z)makes a trail. The config istypedefs/ParticleEmitter3DConfig.js: its keys are not the 2D emitter's.
Terrain and tilemaps
// x, y, z, width, height, depth, heights, texture, frame, segments
const terrain = world.addTerrain(0, 0, 0, 200, 20, 200, 'heightmap', 'grass', null, 96);
terrain.setTextureRepeat(40);
player.y = terrain.getHeightAt(player.x, player.z);
heightsis the key of an image in which brighter is higher, or rows of numbers from 0 to 1, the first row at the lowest z. It is one mesh and one draw call.getHeightAt(x, z)gives the y of the ground under a position in the world. It allows for the terrain's position and scale, but not a tilt.world.addTilemapLayer(tilemap, layer, tileSize, height, y)makes a box for each tile of a Tilemap layer, or a flat plane ifheightis 0: a dungeon from a map. Use the Tilemap for collision.
The camera and its controls
world.camera is a Camera3D: setPosition, setRotation, lookAt(x, y, z), moveForward(distance, fly), moveRight, moveUp, fov (degrees, 60), near (0.1), far (500), setPerspective(fov, near, far), setOrthographic(height, near, far), project(x, y, z) for the pixel a point is drawn at.
world.createOrbitControl({ target: { x: 0, y: 1, z: 0 }, minPitch: 0, autoRotate: 0.1 }); // drag, wheel, pinch
world.createFreeControl({ fly: false, speed: 4, pointerLock: true }); // W, A, S, D and the mouse
world.createFollowControl({ target: car, distance: 8, height: 3, lerp: 0.1 }); // stays behind a mesh
- The world updates them itself. Use one at a time. Each has
active, anddestroy(). - They know nothing of walls or the ground. After the free control moves the camera, put it back yourself in
update. For the follow control, set itsminYeach frame, such as fromgetHeightAt. - Its configs are in
typedefs/OrbitControl3DConfig.js,FreeControl3DConfig.jsandFollowControl3DConfig.js.
Keeping it fast
- A mesh which doesn't change costs nothing each frame. Only move what moves.
- One draw call for each geometry and texture image, however many meshes. Reuse geometry keys, share an atlas, and read
world.drawCalls. - Fewer segments are faster and suit the style:
addSphere(x, y, z, r, null, null, 8). - For a very large level,
world.setCulling(120)skips the parts the camera can't see, in cubes of that size. Use a cube the size of a large room or more: small cubes are slower than none. world.remove(mesh),world.removeAll()andmesh.destroy()free things. Destroying the World3D frees everything in it.
Gotchas
- WebGL only. Guard on
this.renderer.type === Phaser.WEBGL. Builds made fromsrc/phaser-no-3d.jsdon't have it:this.add.world3Dis then undefined. - Lights do nothing at first, as the ambient color starts as white. Call
setAmbientColorwith something dark. - Shapes are placed by their middle, so a box 1 high on the floor has a y of 0.5. Billboards, lathes and walls stand on their position. An extrude's depth is centred.
- Faces in the same plane flicker. See "Two faces in the same place flicker".
- Planes, quads and walls are one sided. If one vanishes, you are behind it: turn it, or call
setDoubleSided(). - Forward is negative z, for the camera and for
lookAtandgetForwardon a mesh. Build a model with its front towards negative z. - Meshes aren't Game Objects. No input events, no
setDepth, no Scene lights, no filters on one mesh. Filters go on the World3D as a whole. setSizeisn't inherited,setScaleis.- The world is a texture of a fixed size. Call
world.resize(width, height)if the game is resized. The World3D itself is an Image:setPosition,setScale,setDisplaySize,setDepthandsetScrollFactor(0)place it in the Scene. - Glow, shadows, the sky and tiled lights use GPU memory only once you call them, and
clearGlow,clearShadows,clearSkyandclearTiledLightsgive it back.
Explore more skills
Browse all- scenesPhaser 4 Scenes: the lifecycle (init, preload, create, update), the Scene config, this.scene (start, restart, launch,…
- camerasPhaser 4 cameras: this.cameras (CameraManager) and this.cameras.main; scroll, zoom, rotation, bounds and worldView;…
- Audio and SoundSound and music in Phaser 4 with the Sound Manager (this.sound): this.sound.play for one-shot effects, this.sound.add…
- Game Setup and ConfigCreate and configure a Phaser 4 game: new Phaser.Game, the GameConfig and its typedefs, the renderer type and the AUTO…
- Time and TimersPhaser 4 time: the Scene Clock (this.time), delayedCall, addEvent and the TimerEvent config (delay, repeat, loop,…
- tweensPhaser 4 tweens: this.tweens.add and this.add.tween, property values (relative, random, arrays, from/to, per-property…
Security
Reviews (0)
More from Phaser
View source →Frequently asked questions about world3d
What does the world3d skill do?
Phaser 4.3 World3D: a small, fast, retro 3D world inside one Game Object, WebGL only (this.add.world3D). Covers building models in code from shapes (addBox, addSphere, addCylinder, addPlane, addWall, addBillboard, addContainer, Mesh3D.add, createLathe, createExtrude), terrain (addTerrain, getHeightAt), textures and normal maps, 3D lights (setAmbientColor, addLight, addSpotLight, setTiledLights), fog, sky, reflections, shadows, glow, decals, 3D particles, the Camera3D and its orbit, free and follow controls, and picking with raycast. Use when making a 3D scene, a first or third person game, a dungeon crawler, a racing or flying game, a model from primitives, or a 3D backdrop for a 2D game. It has no model loader, skeletal animation or physics. Triggers on: 3D, World3D, world3D, Mesh3D, Billboard3D, Camera3D, Light3D, first person, FPS, third person, dungeon crawler, low poly, PS1, retro 3D, 3D model, lathe, extrude, heightmap, terrain, skybox, decal, spot light, raycast, orbit camera, z It's a reusable SKILL.md instruction set that loads into your AI coding assistant on demand, no prompt engineering, no copy-pasting every session.
How do I install the world3d skill?
Run `npx @skills-hub-ai/cli install phaser-world3d` from your terminal. The CLI writes the SKILL.md to the correct location for your AI tool (e.g. ~/.claude/skills/phaser-world3d/ for Claude Code or ~/.cursor/skills/ for Cursor with --target cursor) and adds it to your project's .skills.json lockfile.
Which AI tools does world3d work with?
world3d runs in Claude Code. It follows the open Agent Skills standard (SKILL.md), so the same skill works in every supported tool without modification.
Is the world3d skill free?
Yes. Every skill on skills-hub.ai is free and open-source. There are no premium tiers, paywalls, or usage limits. You only pay for whatever AI assistant you're already using.
How do I use world3d after installing it?
In Claude Code, type `/phaser-world3d` (or whatever slash command the skill registers) and the AI follows the skill's instructions immediately. You can also reference it by name in natural language, your AI loads the skill into context when relevant.
Can I share the world3d skill with my team?
Yes. Commit your project's .skills.json lockfile and teammates run `npx @skills-hub-ai/cli install` (no args) to install every skill at the exact version you pinned. Organization-scoped installs work via skills-hub.ai organizations.
Badge for your README
Show that this skill is on Skills Hub. The badge links back to this page and shows the live install count.
[](https://skills-hub.ai/skills/phaser-world3d)<a href="https://skills-hub.ai/skills/phaser-world3d"><img src="https://skills-hub.ai/api/badges/skill/phaser-world3d" alt="Install on Skills Hub"></a>More options in the badge docs.