@nanoforge-dev/graphics-3d renders a 3D scene in the game’s container with Three.js. It was chosen in the 3D graphics library benchmark.
- Client only.
__initthrows on a server (no container). - One graphics library per app. It uses the
graphicskey, like graphics-2d, so an app registers one or the other. - WebGPU first. It uses WebGPU where the browser has it, and WebGL2 elsewhere.
- Rendered by the engine. One frame per tick, after the ECS systems. A paused game keeps its last image.
Your first 3D scene in 5 minutes
Thecube-3d example is this page as a complete app.
1. Install
three is a peer dependency: the app installs it, so the game and Three.js add-ons share one copy. Also add @types/three (same version) if you import add-ons from three/addons.
2. Register the library
3. Add a light and an object
Everything Three.js offers is imported from@nanoforge-dev/graphics-3d/three. Add objects to graphics.scene to show them. Without a light, MeshStandardMaterial objects are black.
4. Place the camera
The library starts with a perspective camera at(0, 0, 5), looking at the origin.
5. Move things from a system
The scene is drawn after the systems run, so a system changes objects and the next frame shows them.ctx.app.delta is in milliseconds.
ctx.graphics. Do not keep the scene in a module-level variable.
Viewport
The canvas followsContext.viewport like the 2D stage:
- the camera’s full frame is the design resolution (
width×heightincreateClient); containletterboxes it,covercrops it,fillstretches it;- the canvas is resized on every window, fullscreen or pixel-ratio change.
renderer.setSize, and you never set a camera’s aspect yourself.
Context.graphics
Options
Teardown
When the game stops (or the editor restarts it), the library disposes everything in the scene: geometries, materials, their textures, and the scene’s background and environment. It also disposes the renderer, removes the canvas and deleteswindow.__THREE__.
Objects you removed from the scene yourself are not disposed by the library. Call their dispose() when you drop them.
Next
- Advanced: Three.js add-ons, custom shaders, cameras, picking, editor events.
- Performance: many objects, and
InstancedMesh. - Editor proposal: what the editor needs for a 3D Scene screen.