Cameras
Look at a scene through a perspective or orthographic camera, map points between the world and the screen, orbit with the mouse, and fly.
A Camera is an Obj, so it has a position and rotation like anything else in the scene graph. From its world matrix it keeps a viewMatrix, which takes the world into the camera's view, and a projectionMatrix, which flattens that view onto the screen. Meshes read both when they draw.
import {
Renderer,
DollyCamera,
Obj,
Mesh,
Box,
Torus,
GridHelper,
Lights,
LitProgram,
Vec2,
Vec3
} from 'wtc-gl'
// Drag to orbit, right-drag to pan. DollyCamera eases towards where the
// input puts it, so it's updated every frame.
export default function (container) {
const renderer = new Renderer({
dpr: Math.min(devicePixelRatio, 2),
alpha: true,
premultipliedAlpha: true,
antialias: true,
clearColour: [0, 0, 0, 0]
})
const { gl } = renderer
container.appendChild(gl.canvas)
const camera = new DollyCamera(
{
element: gl.canvas,
target: new Vec3(0, 0.9, 0),
autoRotate: true,
// The wheel would zoom rather than scroll the page
enableZoom: false
},
{ fov: 35 }
)
camera.setPosition(3.5, 2.5, 5)
const scene = new Obj()
const lights = new Lights()
const grid = new GridHelper(gl, { size: 6, divisions: 12 })
grid.setParent(scene)
const box = new Mesh(gl, {
geometry: new Box(gl, { width: 0.8, height: 0.8, depth: 0.8 }),
program: new LitProgram(gl, { lights, colour: [0.29, 0.62, 1] })
})
box.position.y = 0.4
box.setParent(scene)
const ring = new Mesh(gl, {
geometry: new Torus(gl, {
radius: 0.7,
tube: 0.12,
radialSegments: 16,
tubularSegments: 64
}),
program: new LitProgram(gl, {
lights,
colour: [0.95, 0.42, 0.26],
specular: 0.5
})
})
ring.position.y = 1.75
ring.setParent(scene)
return {
frame(time) {
ring.rotation.x = time * 0.6
ring.updateRotation()
camera.update()
renderer.render({ scene, camera })
},
resize(width, height) {
renderer.dimensions = new Vec2(width, height)
camera.perspective({ aspect: width / height })
},
dispose() {
camera.removeHandlers()
for (const { geometry, program } of [grid, box, ring]) {
geometry.remove()
program.remove()
}
renderer.dispose()
gl.canvas.remove()
}
}
}Perspective
A camera is perspective unless you give it orthographic planes. Things further away look smaller, as they do to the eye.
import { Camera, Vec3 } from 'wtc-gl'
const camera = new Camera({ fov: 35, near: 0.1, far: 100 })
camera.position.reset(0, 2, 6)
camera.lookAt(new Vec3(0, 0, 0))| Option | Default | What it does |
|---|---|---|
fov | 45 | The vertical field of view, in degrees. |
aspect | 1 | The width over the height of what it draws into. |
near | 0.1 | The distance to the near clipping plane. |
far | 100 | The distance to the far clipping plane. Anything beyond it is clipped. |
camera.perspective() changes any of these, keeps the rest, and rebuilds the projection. Call it whenever the canvas changes shape:
camera.perspective({ aspect: width / height })If you set fov, aspect, near or far directly, call camera.updateProjectionMatrix() afterwards.
Keep near as large and far as small as the scene allows. The depth buffer's precision is spread between them, and a tiny near makes surfaces that are close together flicker through each other.
Orthographic
Give a camera left, right, top and bottom planes and it's orthographic: parallel lines stay parallel, and things are the same size however far away they are. That suits 2D work, diagrams and isometric views. A camera whose planes match the canvas in CSS pixels draws one unit per pixel, with the origin in the centre:
const camera = new Camera({
left: -width / 2,
right: width / 2,
top: height / 2,
bottom: -height / 2,
near: 0.1,
far: 1000
})
camera.position.z = 100camera.orthographic() changes the planes, near, far and zoom, keeping the rest. zoom divides the planes, so 2 shows half as much at twice the size:
camera.orthographic({ zoom: 2 })camera.type is 'perspective' or 'orthographic'. Calling perspective() or orthographic() switches it.
Placing the camera
Set position and call lookAt(target), as above. A camera can also be a child of something in the scene, to follow it; the renderer updates the scene's matrices before the camera's, so it sees where its parent is that frame. A camera doesn't need to be in the scene being rendered at all.
Between the world and the screen
project takes a point in the world to normalised device coordinates, where the visible area runs from −1 to 1 on x and y, and z runs from −1 on the near plane to 1 on the far plane. unproject goes back. Both write into their second argument, and without one they change the point you pass in:
const screen = camera.project(new Vec3(0, 1, 0))
// In CSS pixels from the top left of the canvas
const x = (screen.x * 0.5 + 0.5) * width
const y = (0.5 - screen.y * 0.5) * heightThe lighting demo places HTML labels under its shapes this way. A z outside −1 to 1 means the point is in front of the near plane, past the far plane or behind the camera.
Unprojecting a screen position at z = −1 and z = 1 gives the near and far ends of the ray under it:
const nx = (x / width) * 2 - 1
const ny = 1 - (y / height) * 2
const near = camera.unproject(new Vec3(nx, ny, -1))
const far = camera.unproject(new Vec3(nx, ny, 1))Raycast does this for you and finds the meshes the ray hits.
Both use projectionViewMatrix, which the renderer brings up to date as it renders. If you've moved the camera since the last render, call camera.updateMatrixWorld() first.
Frustum culling
The frustum is the part of the world a camera can see. With frustumCull on, as it is by default, render() skips meshes whose bounding sphere is entirely outside it, which saves drawing what can't be seen. A drawable that isn't a Mesh has no bounds, so it's never culled.
The bounding sphere is measured from the geometry's position data the first time it's needed. A vertex shader that moves vertices, or position data you change later, can take a mesh outside its measured bounds, and it'll be culled while still on screen. Either measure again, box first, as the sphere is centred on the box, or turn culling off for that mesh:
geometry.computeBoundingBox()
geometry.computeBoundingSphere()
// or
const mesh = new Mesh(gl, { geometry, program, frustumCulled: false })You can test against the frustum yourself. updateFrustum() updates its planes from the camera's matrices, which render() does for you:
camera.updateFrustum()
camera.frustumIntersects(mesh) // the mesh's bounds against the frustum; true for other drawables
camera.frustumIntersectsSphere(centre, radius)InstancedMesh culls each instance rather than the whole mesh; see Instancing.
Orbiting with DollyCamera
DollyCamera is a camera that orbits a target in response to input on an element:
- Drag with the left button or one finger to orbit.
- Scroll the wheel, drag with the middle button or pinch to zoom.
- Drag with the right button or two fingers to pan.
The example below has limits: it stays between 4 and 12 units from its target, and above the floor. Try orbiting underneath, or click it and zoom all the way in and out, and the readout shows the limits holding. Its zoom only turns on once it's been clicked, so scrolling past doesn't zoom it, and panning is off.
import {
Renderer,
DollyCamera,
Obj,
Mesh,
Box,
Sphere,
GridHelper,
Lights,
LitProgram,
Vec2,
Vec3
} from 'wtc-gl'
// A DollyCamera with limits. It stays between 4 and 12 units from the
// target, and between straight down and just above the floor, so it can't
// go underneath. Zoom only turns on once the example is clicked, and off
// again when the pointer leaves, so scrolling past doesn't zoom it.
export default function (container) {
const renderer = new Renderer({
dpr: Math.min(devicePixelRatio, 2),
alpha: true,
premultipliedAlpha: true,
antialias: true,
clearColour: [0, 0, 0, 0]
})
const { gl } = renderer
container.appendChild(gl.canvas)
const camera = new DollyCamera(
{
element: gl.canvas,
target: new Vec3(0, 0.5, 0),
minDistance: 4,
maxDistance: 12,
// From 10° off straight down to 5° above the floor, in radians
minPolarAngle: Math.PI / 18,
maxPolarAngle: Math.PI / 2 - Math.PI / 36,
enablePan: false,
enableZoom: false
},
{ fov: 35 }
)
camera.setPosition(5, 3, 6)
// Zoom with the wheel only after a click, until the pointer leaves
const zoomOn = () => (camera.enableZoom = true)
const zoomOff = () => (camera.enableZoom = false)
gl.canvas.addEventListener('pointerdown', zoomOn)
gl.canvas.addEventListener('pointerleave', zoomOff)
// A readout of the distance and tilt, to watch the limits hold
const readout = document.createElement('div')
Object.assign(readout.style, {
position: 'absolute',
left: '12px',
bottom: '12px',
padding: '4px 8px',
borderRadius: '4px',
background: 'rgb(0 0 0 / 0.5)',
color: '#fff',
font: '12px/1.5 ui-monospace, monospace',
pointerEvents: 'none'
})
container.appendChild(readout)
const scene = new Obj()
const lights = new Lights()
const grid = new GridHelper(gl, { size: 10, divisions: 20 })
grid.setParent(scene)
// A sphere on the target, ringed by blocks of different heights
const meshes = [grid]
const add = (geometry, colour, x, y, z) => {
const mesh = new Mesh(gl, {
geometry,
program: new LitProgram(gl, { lights, colour, specular: 0.3 })
})
mesh.position.reset(x, y, z)
mesh.setParent(scene)
meshes.push(mesh)
}
add(new Sphere(gl, { radius: 0.5 }), [0.95, 0.42, 0.26], 0, 0.5, 0)
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2
const height = 0.4 + (i % 3) * 0.5
add(
new Box(gl, { width: 0.5, height, depth: 0.5 }),
[0.29, 0.62, 1],
Math.cos(angle) * 2.2,
height / 2,
Math.sin(angle) * 2.2
)
}
let text = ''
return {
frame() {
camera.update()
renderer.render({ scene, camera })
// The polar angle is measured down from straight above the target
const distance = camera.spherical.radius.toFixed(1)
const tilt = Math.round(90 - (camera.spherical.phi * 180) / Math.PI)
const next = `distance ${distance} · ${tilt}° above the floor${
camera.enableZoom ? '' : ' · click to zoom'
}`
if (next !== text) readout.textContent = text = next
},
resize(width, height) {
renderer.dimensions = new Vec2(width, height)
camera.perspective({ aspect: width / height })
},
dispose() {
camera.removeHandlers()
gl.canvas.removeEventListener('pointerdown', zoomOn)
gl.canvas.removeEventListener('pointerleave', zoomOff)
for (const { geometry, program } of meshes) {
geometry.remove()
program.remove()
}
renderer.dispose()
gl.canvas.remove()
readout.remove()
}
}
}It takes its own options first and the usual camera options second. Both are optional, and as with Camera you only pass the camera options you want to change, so new DollyCamera({ element }) works. Place it with setPosition, and call update() every frame: input sets where the camera is heading, and update() eases it there, applies the limits and points it at the target.
import { DollyCamera, Vec3 } from 'wtc-gl'
const camera = new DollyCamera(
{
element: renderer.gl.canvas,
target: new Vec3(0, 0.5, 0),
minDistance: 3,
maxDistance: 20,
maxPolarAngle: Math.PI / 2
},
{ fov: 35 }
)
camera.setPosition(0, 3, 8)
function frame() {
requestAnimationFrame(frame)
camera.update()
renderer.render({ scene, camera })
}| Option | Default | What it does |
|---|---|---|
element | document.body | The element that listens for input. |
target | (0, 0, 0) | The point it orbits and looks at. Panning moves it. |
enabled | true | Whether input moves the camera. |
ease | 0.25 | How much of the remaining distance it moves each update, from 0 to 1. Lower is smoother. |
inertia | 0.5 | How much of each rotation and pan carries on into the next update, from 0 to 1. |
enableRotate, rotateSpeed | true, 0.5 | Orbiting, and how fast. |
enableZoom, zoomSpeed | true, 1 | Zooming, and how fast. |
zoomStyle | 'dolly' | 'dolly' moves the camera towards the target; anything else changes the field of view instead. |
enablePan, panSpeed | true, 0.5 | Panning, and how fast. |
autoRotate, autoRotateSpeed | false, 1 | Turn around the target on its own. At speed 1, a full turn takes 3600 updates: a minute at 60fps. |
minDistance, maxDistance | 0, Infinity | How close to and far from the target it can go. |
minPolarAngle, maxPolarAngle | 0, Math.PI | Limits on the angle down from straight above the target, in radians. Math.PI / 2 keeps it level with the target or above. |
minAzimuthAngle, maxAzimuthAngle | -Infinity, Infinity | Limits on the angle around the target's y axis, in radians. |
Setting camera.position directly has no lasting effect, as the next update() puts the camera back on its orbit. Use setPosition(x, y, z), or set position and call forcePosition().
When you're done with it, removeHandlers() stops it listening for input.
TIP
The camera stops the page scrolling while the wheel or a touch is over its element, so it can zoom and orbit. On a canvas that's part of a longer page, set enableZoom: false, as the example above does, so the wheel still scrolls the page. Touches over the element don't scroll the page while the camera is enabled.
Flying with FlyCamera
FlyCamera flies forward on its own and is steered like a plane. It turns about its own axes, so it can loop, roll and fly upside down, and it looks down its local −Z like any camera.
import {
Stage,
FlyCamera,
InstancedMesh,
Cylinder,
LitProgram,
Mat4
} from 'wtc-gl'
// A FlyCamera flying through a field of prisms. The field wraps around the
// camera, so it never runs out: each prism is kept within a box centred on
// the camera, and shrinks away near the box's edge so none of them pop.
// Keys go to the example itself, not the window, so it only flies with the
// keyboard once it's been clicked.
export default function (container) {
const stage = new Stage({
container,
controls: false,
shadow: false,
autoplay: false,
clearColour: [0.93, 0.94, 0.96, 1]
})
stage.lights.direction = [0.4, 0.8, 0.3]
container.tabIndex = 0
stage.canvas.style.touchAction = 'none'
const fly = new FlyCamera(
{ element: stage.canvas, keyTarget: container, speed: 3 },
{ fov: 60, far: 40 }
)
stage.camera = fly
// Prisms scattered through a box, each with its own turn and colour
const SIZE = 36
const COUNT = 600
const prisms = new InstancedMesh(stage.gl, {
geometry: new Cylinder(stage.gl, { radialSegments: 3 }),
program: new LitProgram(stage.gl, {
lights: stage.lights,
instanced: true,
flatShading: true
}),
count: COUNT
})
stage.add(prisms, { castShadow: false })
const colours = [
[0.1, 0.42, 0.45],
[0.93, 0.45, 0.33],
[0.95, 0.72, 0.25]
]
const places = []
for (let i = 0; i < COUNT; i++) {
places.push([
(Math.random() - 0.5) * SIZE,
(Math.random() - 0.5) * SIZE,
(Math.random() - 0.5) * SIZE
])
// A random turn, kept as the matrix to scale and place each frame
const axis = [Math.random(), Math.random(), 1]
prisms.setMatrixAt(i, Mat4.fromRotation(Math.random() * Math.PI * 2, axis))
prisms.setColourAt(i, colours[i % 3])
}
const turns = prisms.matrices.slice()
// Wrap each prism into the box around the camera, shrinking near its edge
const m = new Mat4()
const wrap = (v, c) =>
c + ((((v - c + SIZE / 2) % SIZE) + SIZE) % SIZE) - SIZE / 2
function place() {
const { x, y, z } = fly.position
for (let i = 0; i < COUNT; i++) {
const px = wrap(places[i][0], x)
const py = wrap(places[i][1], y)
const pz = wrap(places[i][2], z)
const d = Math.max(Math.abs(px - x), Math.abs(py - y), Math.abs(pz - z))
const s = Math.min(1, Math.max(0, (SIZE / 2 - d) / 4))
const o = i * 16
m.a11 = turns[o] * s
m.a12 = turns[o + 1] * s
m.a13 = turns[o + 2] * s
m.a21 = turns[o + 4] * s
m.a22 = turns[o + 5] * s
m.a23 = turns[o + 6] * s
m.a31 = turns[o + 8] * s
m.a32 = turns[o + 9] * s
m.a33 = turns[o + 10] * s
m.a41 = px
m.a42 = py
m.a43 = pz
prisms.setMatrixAt(i, m)
}
}
return {
frame(time, delta) {
fly.update(delta)
place()
stage.render()
},
resize() {
stage.resize()
},
dispose() {
fly.removeHandlers()
stage.destroy()
}
}
}| Input | What it does |
|---|---|
| Hold and drag | Steer like a joystick: the further from where you pressed, the faster it turns. A quick click doesn't steer. |
| W S or ↑ ↓ | Pitch the nose up and down, or with accelerationMode, boost and brake into reverse while held. |
| A D or ← → | Turn left and right. |
| Q E | Roll. |
| R F | Speed up and slow down, as far as stopping. |
| Space | Stop, and start again at the same speed. |
Held keys build up to their full rate over rampTime, and turning dies away once they're let go, the same at any frame rate. Call update(delta) every frame with the frame's time in seconds:
import { FlyCamera, Stage } from 'wtc-gl'
const stage = new Stage({ controls: false })
const fly = new FlyCamera(
{ element: stage.canvas, speed: 4, accelerationMode: true },
{ fov: 60, far: 200 }
)
stage.camera = fly
stage.onBeforeRender = (time, delta) => fly.update(delta)| Option | Default | What it does |
|---|---|---|
element | document.body | The element that listens for pointer input. |
keyTarget | window | What listens for key input. |
enabled | true | Whether input steers it. It keeps flying either way. |
speed, maxSpeed | 1, speed * 10 | The cruising speed in units a second, and the fastest speeding up can take it. |
maxReverseSpeed | maxSpeed / 2 | The fastest it can reverse, holding S in accelerationMode. 0 stops it reversing. |
acceleration | the starting speed | How quickly the speed changes while a speed key is held, in units a second². |
turnSpeed, rollSpeed | 1, 1 | The turn and roll rates at full input, in radians a second. |
rampTime | 0.9 | The seconds a held key takes to reach its full rate. |
damping | 6 | How quickly turning dies away once let go. Higher stops sooner. |
stickRadius | 200 | The drag distance, in CSS pixels, for the full turn rate. |
holdDelay | 0.1 | The seconds a press is held before dragging steers. |
accelerationMode | false | W, S and the up and down arrows boost and brake while held instead of pitching. Dragging still pitches. |
accelerationDecay | 4 | How quickly a boost or brake returns to speed once let go. Higher is sooner. |
keys | see above | Keys to replace the defaults, by action, such as { up: ['i'], down: ['k'] }. Single letters match either case. |
Everything but element and keyTarget can be changed after it's made, and setKeys remaps keys later. paused is the space key's stop, and speed is kept while paused.
In accelerationMode, holding W or ↑ speeds up from the cruising speed, as far as maxSpeed, and holding S or ↓ slows to a stop and then reverses, as far as maxReverseSpeed. Letting go returns quickly to the cruising speed, while R and F still change it. currentSpeed is the speed it's flying at, boost included, and below 0 while reversing. For a camera that only moves while a key is held, set speed to 0, as the fly demo does in its boost mode.
Keys are ignored while someone is typing in a field, and the arrows and space don't scroll the page while the camera is enabled. On a page with more on it than the scene, pass the scene's container as keyTarget and give it a tabIndex, as the example above does, so the keys only fly the camera once the scene has been clicked. Set touch-action: none on the element so touch drags steer rather than scroll.
When you're done with it, removeHandlers() stops it listening for input. The fly demo flies through endless space with one.
See also
- Your first 3D scene and Scene graph.
- Raycast, for picking what's under the pointer.
- Camera, CameraOptions, DollyCamera and DollyCameraOptions in the API reference.