Skip to content

Camera

A Camera packages the two things every 3D render needs into one object: a projection (how the view frustum maps to clip space) and a view (where the camera sits and what it looks at). Pass it to Pass::add to wire its camera.view_proj and camera.position into every shader the pass renders; the Camera holds Arc-shared state, so subsequent look_at calls propagate to every shader the Camera has been wired into.

Internally a Camera carries:

  • A proj matrix built by Camera::perspective or Camera::orthographic. Both use glam’s right-handed builders (Mat4::perspective_rh, Mat4::orthographic_rh), which match wgpu’s NDC depth range [0, 1].
  • A view matrix initialized to identity (eye at origin, looking down -Z, with +Y up). Call look_at to position the camera in world space.
  • The world-space position of the eye, kept alongside the view matrix so shaders that need it (specular highlights, fresnel) don’t have to invert the view matrix on every frame.

Parameters that leave no usable view return an error, and a Camera that already exists keeps its state. perspective, orthographic, look_at and set_aspect return a CameraError in Rust, throw an Error named CameraError in JavaScript, raise CameraError (a FragmentColorError and a ValueError) in Python, throw FragmentColorError in Swift and throw FragmentColorException in Kotlin.

A shader whose camera.view_proj or camera.position can’t hold the Camera’s value, such as an f16 position past ±65504 or a vec4<f32> position for the Camera’s three numbers, keeps the value it had. Renderer::render returns the shader’s ShaderError::TypeMismatch until a value of that key lands, from the Camera or from Shader::set. A shader that declares neither key, such as one whose camera is a plain mat4x4<f32>, takes nothing from the Camera, with no error.

The Camera is the user’s domain, not the Material’s: a Material is “what the surface looks like under any light from any viewpoint”, a Camera is “which viewpoint we’re using right now”.


Construct a Camera with a perspective projection. fovy_radians is the vertical field of view (use degrees.to_radians() if you’re starting from degrees); aspect is width / height; near and far clip the depth range.

Built on glam::Mat4::perspective_rh, which targets wgpu’s NDC depth range [0, 1]. A pass that draws models gets a depth buffer for that range on its own.

The view component starts at identity: eye at the world origin, looking down -Z, with +Y up. Chain look_at to position the camera before binding it.

Every value must be finite, fovy_radians must lie between 0 and π, aspect can’t be 0, and near and far must be positive and different. Anything else returns Err(CameraError::Perspective): such values give a matrix of infinities or NaN, a flipped or collapsed image, or no usable depth range.

use fragmentcolor::Camera;
let camera = Camera::perspective(1.047, 16.0 / 9.0, 0.1, 100.0)?;

Construct a Camera with an orthographic projection. The six arguments are the frustum planes in view space: left, right, bottom, top, near, far. Use this when you need a flat 2D look (UI overlays, 2D-in-3D gameplay, isometric scenes) or for shadow-map style passes where you want parallel projection.

Built on glam::Mat4::orthographic_rh, which targets wgpu’s NDC depth range [0, 1]. A pass that draws models gets a depth buffer for that range on its own.

The view component starts at identity: eye at the world origin, looking down -Z, with +Y up. Chain look_at to position the camera before binding it.

All six planes must be finite, and left and right, bottom and top, and near and far must differ. Planes that close the frustum to nothing return Err(CameraError::Orthographic).

use fragmentcolor::Camera;
// A 16:9 viewport, 10 world units tall, depth range 0.1..100.
let camera = Camera::orthographic(-8.0, 8.0, -4.5, 4.5, 0.1, 100.0)?;

Update the camera’s aspect ratio (width / height) in place. The projection matrix recomputes and propagates to every shader the Camera was added to, plus the Pass-level camera snapshot the renderer reads for transparency depth-sorting. No need to drop and recreate the Camera handle.

Typical use: window-resize handler. On WindowEvent::Resized, call camera.set_aspect(width as f32 / height as f32) and the next frame renders without distortion.

Returns a handle to the same Camera (Arc-shared backing) for chaining. An aspect of 0, NaN or infinity returns Err(CameraError::Aspect) and leaves the projection as it was. A minimized window reports a width or height of 0, so skip the call until the window has an area again.

Behavior by projection kind:

  • Perspective: rebuilds from fovy_radians / near / far with the new aspect. The vertical FOV is preserved, horizontal grows or shrinks.
  • Orthographic: keeps the current vertical extent and rescales the horizontal extents so (right - left) / (top - bottom) matches the new aspect, centered on the existing horizontal midpoint. The frustum height stays put; the width tracks the window.
use fragmentcolor::Camera;
let camera = Camera::perspective(1.047, 1.0, 0.1, 100.0)?;
// Window resize: 1920×1080 → wide-screen aspect.
camera.set_aspect(1920.0 / 1080.0)?;

Position the camera in world space. position is where the camera sits, target is the point it aims at, and up is the world-space up vector that orients the roll (almost always [0, 1, 0]). Matches Light’s position vocabulary so both scene types describe “where this object is in the world” the same way.

Returns a handle to the same Camera (Arc-shared backing) so it chains cleanly off a perspective or orthographic constructor and can be called again after the Camera has been added to a Pass. The new view propagates live to every Material the Pass renders.

The vectors must be finite, target must differ from position, and up can’t be zero or lie on the line of view, forward or back. Any of those leaves no way to orient the camera, so the call returns Err(CameraError::View) and the Camera keeps its view.

The view is built in double precision, from a normalized up, and stored as f32. So an up counts as on that line only when the sine of its angle to it is below 1e-12, and one just off the line still gets the roll its numbers describe. A top-down camera 10,000 units up, with its target offset by 0.001, has a sine of 1e-7 and gets an exact roll.

The result is a right-handed view matrix that pairs with the right-handed projection produced by perspective and orthographic.

The world-space position is cached on the Camera and exposed via position so shaders that need it (specular highlights, fresnel) don’t have to invert the view matrix on every frame.

use fragmentcolor::Camera;
let camera = Camera::perspective(1.047, 16.0 / 9.0, 0.1, 100.0)?
.look_at([0.0, 1.0, 5.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0])?;

Read the combined proj * view matrix as its four columns of four numbers, the [[f32; 4]; 4] Rust returns, and four lists of four in every other language (four Float32Arrays in JavaScript). Column-major order matches WGSL’s mat4x4<f32> storage, so the result is ready to feed directly into a Shader’s camera.view_proj uniform via Shader::set(...) if you need direct control. For the common case, pass the Camera to Pass::add. The Pass seeds camera.view_proj + camera.position on every Material attached to it and keeps them in sync with later updates.

use fragmentcolor::Camera;
let camera = Camera::perspective(1.047, 16.0 / 9.0, 0.1, 100.0)?
.look_at([0.0, 0.0, 5.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0])?;
let view_proj = camera.view_proj();

Read the world-space eye position as [x, y, z]. This is the value set by the most recent look_at call, or [0, 0, 0] if the camera has only been constructed (the default view is identity, with the eye at the origin).

Shaders that need the eye position (specular highlights, fresnel, parallax) typically pull it from the camera.position uniform seeded by Pass::add when the Camera is absorbed. Caching it here keeps every frame cheap (no view-matrix inversion on the GPU side).

use fragmentcolor::Camera;
let camera = Camera::perspective(1.047, 16.0 / 9.0, 0.1, 100.0)?
.look_at([3.0, 2.0, 8.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0])?;
let eye = camera.position();