wgpu3d
wgpu3d is a small WebGPU library for 3D scenes you can play in: drive a car down a street, walk a character over crates, or build a village out of boxes and triangles. It is the rendering and driving code from City Drive, pulled out of the game and given a simple API.
https://gpuengine.github.io/wgpu3d-0.1.0.min.js
The version is in the file name and in the header comment of the file, and VERSION is exported. Every release gets its own file name, so a page that links to it only changes when you change the link. wgpu3d is free software under the GNU General Public License, version 2 or later (SPDX-License-Identifier: GPL-2.0-or-later): you can use, change and share it, including commercially, as long as projects that ship it are shared under compatible terms and keep the header comment in the file. The source is on GitHub.
Quick start
wgpu3d is a single ES module with no dependencies. Add a canvas, map the name wgpu3d to the file, and write a module script:
<canvas id="scene" style="width: 100%; height: 480px"></canvas>
<script type="importmap">
{ "imports": { "wgpu3d": "/wgpu3d-0.1.0.min.js" } }
</script>
<script type="module" src="game.js"></script>
import { createApp, createCar, createGround, VehicleController } from "wgpu3d";
const app = await createApp({ canvas: document.querySelector("#scene"), environment: "sunset" });
app.add(createGround());
const car = createCar({ paint: "#1d6fd8" });
car.addBehavior(new VehicleController());
app.add(car);
app.follow(car);
app.start();
That is a complete program: a car on a field with sun shadows, a sky and a chase camera. Click the canvas and drive with WASD or the arrow keys, or drag on a phone.
Tutorials
Each tutorial has a live demo running the code shown on the page.
Your first wgpu3d scene
Objects, meshes, materials, behaviors and an orbit camera in thirty lines.
Make a drivable car
A street with buildings and lamps, a car with a vehicle controller and a chase camera.
A tiny platform game
Walk, run and jump onto crates to collect coins, with a score and a timer.
Build a procedural village
Write your own meshes with MeshBuilder and grow a different village from every seed.
Every function and option is listed in the API reference.
What is in the box
- Renderer: sky gradient with a sun disc, sun light with soft shadow maps, sky and ground ambient light, glossy highlights, glowing surfaces, distance fog and 4× antialiasing. Five lighting presets: morning, noon, sunset, dusk and night.
- Scene graph: objects with position, yaw, pitch, roll and scale, parent and child objects, tags, and behaviors that run every frame.
- MeshBuilder: boxes, cylinders, cones, spheres, tiled planes, lofts, triangles and quads, with per-vertex color, gloss and glow.
- Playable objects: a vehicle controller ported from City Drive's driving model, a character controller that walks, runs, jumps and stands on boxes, and simple box and sphere collisions.
- Cameras: a chase camera that trails a vehicle and an orbit camera you drag, pinch and scroll.
- Input: keyboard bindings plus a touch stick (drag to move, tap to jump) that works with no extra code.
- Prefabs: the City Drive car, a character, trees, buildings, roads, crates, coins, lamp posts and ground.
How it relates to City Drive
City Drive is a big, specialised program: an endless procedural city streamed by Web Workers, with traffic, weather and dozens of building generators. wgpu3d keeps the parts any small 3D game needs and drops the rest. The vertex format (position, normal, colour and a surface value per vertex), the yaw, pitch and roll convention, the box occlusion trick, the texel-snapped sun shadow camera and its 2×2 filtered lookup, the sky and ambient light, the car body and the driving model all come from the game. Everything is rewritten around a scene of objects instead of one large streamed vertex buffer.
What it does not do
- No textures or model loading. Everything is coloured geometry, which keeps the file small and the look consistent.
- One light: the sun. Glowing materials look lit but do not light their surroundings.
- No physics engine. Collisions are circles against boxes and spheres on the ground plane, which is enough for driving and platforming but not for stacking or tumbling objects.
- WebGPU only, so it needs a recent Chrome, Edge or Safari, or Firefox on Windows. Call
isSupported()to show a fallback message.
Official documentation
Go deeper with the specifications and reference documentation:
- developer.mozilla.org
MDN: WebGPU API
The browser API wgpu3d is built on.
- caniuse.com
Can I use: WebGPU
Which browsers can run wgpu3d scenes.
- developer.chrome.com
WebGPU troubleshooting tips
What to check when isSupported() returns false.
- w3.org
WebGPU specification
The W3C standard behind every call wgpu3d makes.