A winter cabin, lit by hand
A small real-time scene on the WebGPU API: an OBJ cabin, Phong lighting written in WGSL, a free camera, a few physics toggles and snow drifting in the wind.
- Context
- MSc group coursework, 3D graphics
- Date
- Nov 2025
- Runs on
- Browsers with WebGPU
- Stack
- WebGPU, WGSL, JavaScript, wgpu-matrix
Live demo
Run the scene in your browser
About 5.6 MB, mostly the cabin mesh. WASD to move, hold and drag to look, buttons trigger the animations.
The 26 November 2025 build, unchanged apart from a back link, a controls hint and a notice for browsers without WebGPU. It only loads when you ask, so this page stays light.
What is in it
- OBJ loading
- A small parser reads positions, normals and texture coordinates from the OBJ files and builds vertex buffers for the pipeline.
- Free camera
- WASD or arrow keys to move, pointer lock while the left button is held to look around, scroll to zoom.
- Animations
- Freefall drops the cabin from above under constant gravity, Float bobs it on a sine wave, Shake jitters it, and it rotates continuously unless paused.
- Snow
- A 2D canvas layer over the scene with drifting flakes and a wind term, behind a backdrop image.
Freefall, per frame
const gravity = -9.8; fallVelocity += gravity * deltaSeconds; fallOffsetY += fallVelocity * deltaSeconds;
The lighting
Lighting is computed per fragment. Ambient light is a constant floor; diffuse light scales with the angle between the surface normal and the light; specular highlights depend on how closely the reflected light lines up with the viewer, sharpened by a shininess exponent. The three terms modulate the sampled texture.
// Ambient let ambient = ka * ia; // Diffuse let lightDir = normalize(lightPosition - position); let diff = max(dot(normal, lightDir), 0); let diffuse = kd * id * diff; // Specular let viewDir = normalize(fsInput.viewPos.xyz - position); let reflectDir = reflect(-lightDir, normal); let spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); let specular = ks * is * spec; // Full Phong light calculation var output = colorTexture * vec4f(ambient + diffuse + specular, 1.0);
Scope and credit
This was group coursework, and the scene is a modest one: a single model, one light, no shadows or post-processing. I include it because it is the one place I have written GPU pipeline code by hand, from buffer layouts and bind groups to the shader. The backdrop image is not my own work.