Skip to content

Light

Light is the unified type covering the three glTF KHR_lights_punctual kinds: directional (parallel rays for sun, sky, fill), point (radiates from a position with inverse-square falloff: lamps, candles, fireballs), and spot (point light constrained to a cone: flashlights, headlamps, theater lighting). The kind is set once at construction via Light::directional, Light::point, or Light::spot.

The unified surface keeps the public API simple: one type, one Scene::add method, one Pass::add method, and one binding per platform. Every setter returns Result<Self, LightError> for chaining, and they fall into two groups:

The constructors and every setter but set_range refuse NaN and an infinity with LightError::NotFinite, which names the call and the item, and a refused setter leaves the light as it was. The other languages also refuse a number past the 32-bit float range there, such as 1e39, before the call runs: an InvalidInputError in JavaScript and Python, FragmentColorError in Swift and FragmentColorException in Kotlin. set_range reads an infinity as an unlimited range and refuses NaN with LightError::NegativeRange.

The matching getters use Option<T> for the kind-specific fields: position, direction, range, inner_cone_angle, outer_cone_angle return Some only on the kinds that carry the value, None otherwise.

Lights hold Arc-shared state, so a single handle can be absorbed by multiple Passes, and a later setter reaches every shader the Light lights at the next render.

A Light lights as long as a Scene, a Pass or a handle of yours holds it. Each Pass and each Scene holds the lights added to it, so scene.add(&light) keeps lighting after the local light handle drops. A Model holds no lights. When it joins a pass, its shader takes every light that pass holds, and so does a shader added with Pass::add_shader. Once the last pass, scene and handle holding a Light are gone, the shaders it reached draw without it.

Material::pbr shades each fragment with the lights that reach it. At each render the renderer sorts every light with a finite range into a grid of clusters over the pass’s camera view, and a fragment loops over the lights of its own cluster. The cost of a fragment grows with the lights that overlap it, not with every light in the scene.

A light without a range reaches every fragment, so every fragment pays for it. That covers each directional light and every point or spot light left at the default range of 0. Keep those to a few suns and fill lights, and give lamps, torches and other local lights a range.

A still scene sorts its lights once. Moving a light, changing its range or cone, or moving the camera sorts them again at the next render. A new color or intensity sorts nothing and uploads only the lights from the first one that changed to the last.

The device sets the cap. A device with compute shaders and storage buffers in fragment shaders, as on WebGPU, Vulkan, Metal and DirectX 12, takes as many lights as its largest storage buffer binding holds: about two million at the common 128 MiB. Every OpenGL backend, WebGL2 included, takes the lights through a uniform block, and so does any other device, so the cap is what the device’s largest uniform block holds, from 253 lights in 16 KiB up to 1,021.

Past the cap, pass.add(&light) returns PassError::LightCapReached { cap }, and so does adding a Model whose shader would take more lights than that when it joins the pass. Lights added before the renderer opened a device are checked at render, which returns RendererError::PassError with the same error.

Each shader keeps its own list of lights, and the cap applies to that list. Two Passes that share a Material, and so its shader, share its lights:

  • Adding the same Light to Pass A and Pass B lists it once on the shared shader. Both Passes render with it.
  • Adding one Light to Pass A and another to Pass B lists both on the shared shader, so both lights illuminate both Passes. Think of a Material as the lighting model and the Passes as views into the same lit world.

For independent lighting setups, build a separate Material per scene (each Material::pbr() makes a new shader with its own list) or pass your own shader to Material::custom. One scene with one Material covers the typical case, and the shared list is what lets Scene::add(&light) reach every Pass that renders the Scene.

name what it does
directional parallel rays from a world-space direction
point radiates from a world-space position with distance falloff
spot point light constrained to a cone

Construct a directional light. direction is the world-space vector the light travels along (so [0, -1, 0] is straight down, the noon sun); color is linear-RGB intensity ([1, 1, 1] for full white, scaled values for dimmer or tinted lights).

Use this for sun / moon / fill / key lights: anything where every shaded surface should receive parallel rays from a fixed direction. For positioned sources, see Light::point and Light::spot.

A directional light carries no position. Calling set_position, set_range, or set_cone_angles on a directional light returns LightError::FieldNotApplicable.

An item of direction or color that is NaN or an infinity returns LightError::NotFinite, which names the call, the vector and the item: Light::directional: direction: item 1 must be finite, got NaN. The other languages also refuse an item past the 32-bit float range, such as 1e39, with a message like Light.directional: direction: item 1 must be a finite number that fits a 32-bit float, got 1e39. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException.

A direction of length 0, such as [0, 0, 0], points the light nowhere and returns LightError::ZeroDirection in every language: Light::directional: direction has length 0, so the light points nowhere. JavaScript and Python raise it as InvalidInputError, too.

use fragmentcolor::Light;
let sun = Light::directional([0.3, -1.0, -0.4], [1.0, 0.95, 0.9])?;

Construct a point light. position is the world-space location the light radiates from; color is linear-RGB intensity ([1, 1, 1] for full white). Falloff is inverse-square distance. Bring set_range in to cap the influence radius, and set_intensity to scale the color uniformly.

Use this for lamps, candles, fireballs: anything where light radiates outward from a fixed point. For a parallel-ray sun-like light see Light::directional; for a cone-constrained beam see Light::spot.

A point light carries no direction or cone. Calling set_direction or set_cone_angles on a point light returns LightError::FieldNotApplicable.

An item of position or color that is NaN or an infinity returns LightError::NotFinite, which names the call, the vector and the item, such as Light::point: position: item 0 must be finite, got inf. The other languages also refuse an item past the 32-bit float range, such as 1e39, with the error a color item gets: an InvalidInputError in JavaScript and Python, FragmentColorError in Swift and FragmentColorException in Kotlin.

A point light with a range lights only what lies within it, which lets the renderer skip it everywhere else. Without one, every fragment pays for it.

use fragmentcolor::{Camera, Light, Material, Mesh, Model, Pass, Renderer, Vertex};
let renderer = Renderer::new();
let target = renderer.create_texture_target([64, 64]).await?;
let wall = Mesh::new();
wall.add_vertices([
Vertex::pbr([-2.0, -2.0, 0.0]),
Vertex::pbr([2.0, -2.0, 0.0]),
Vertex::pbr([2.0, 2.0, 0.0]),
Vertex::pbr([-2.0, 2.0, 0.0]),
]);
wall.set_indices([0, 1, 2, 0, 2, 3]);
let camera = Camera::perspective(1.047, 1.0, 0.1, 100.0)?
.look_at([0.0, 0.0, 4.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0])?;
let bulb = Light::point([0.0, 0.5, 1.0], [1.0, 0.95, 0.8])?.set_intensity(15.0)?;
bulb.set_range(3.0)?;
let pass = Pass::new("lamp");
pass.add(&Model::new(wall, Material::pbr()))?;
pass.add(&camera)?;
pass.add(&bulb)?;
renderer.render(&pass, &target)?;

Construct a spot light. position is the world-space location the light radiates from; direction is the world-space vector the cone aims along (the cone axis is -direction); color is linear-RGB intensity.

Falloff is inverse-square distance with a smooth cone roll-off between an inner and outer cone angle (defaults: 0 and π/4). Tighten the beam with set_cone_angles, cap the influence with set_range, and scale the radiance with set_intensity.

Use this for flashlights, headlamps, theater lighting, mood key-lights: anything where the lit volume is a cone aimed at a target. For an unconstrained point source see Light::point; for parallel-ray illumination see Light::directional.

A spot light carries every kind-specific field, so every kind-specific setter (set_position, set_direction, set_range, set_cone_angles) applies to it.

An item of position, direction or color that is NaN or an infinity returns LightError::NotFinite, which names the call, the vector and the item, such as Light::spot: direction: item 2 must be finite, got NaN. The other languages also refuse an item past the 32-bit float range, such as 1e39. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException.

A direction of length 0, such as [0, 0, 0], points the light nowhere and returns LightError::ZeroDirection in every language: Light::spot: direction has length 0, so the light points nowhere. JavaScript and Python raise it as InvalidInputError, too.

use fragmentcolor::Light;
let torch = Light::spot([0.0, 1.8, 1.0], [0.0, -0.3, -1.0], [1.0, 0.9, 0.7])?
.set_intensity(5.0)?
.set_cone_angles(0.15, 0.4)?;

Read which kind this Light was constructed as: a LightKind value, Directional, Point, or Spot, in every language.

Use this when inspecting a Light built elsewhere (e.g. loaded from a glTF scene) to decide which kind-specific setters or getters to call.

use fragmentcolor::{Light, LightKind};
let sun = Light::directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0])?;
let bulb = Light::point([0.0, 2.5, 0.0], [1.0, 1.0, 1.0])?;
let torch = Light::spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0])?;

Read the linear-RGB color the light emits. Defined for every kind. The returned value is the [r, g, b] triple stored on the Light. Call set_color to update it. The scalar intensity multiplier is separate and multiplies the color uniformly in the shader.

use fragmentcolor::Light;
let warm_lamp = Light::point([0.0, 2.0, 0.0], [1.0, 0.7, 0.4])?;

Read the scalar intensity multiplier. Defined for every kind. The shader multiplies the color by this value when accumulating the light’s contribution, so doubling intensity doubles every channel uniformly. Call set_intensity to update it.

use fragmentcolor::Light;
let bright = Light::point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0])?.set_intensity(5.0)?;

Update the linear-RGB color the light emits. Defined for every kind; returns Ok(self) for chaining. The new value propagates live to every shader the Light has already been wired into; no re-attach needed.

A channel that is NaN or an infinity returns LightError::NotFinite, which names the call and the item, and the light keeps the color it had. The other languages also refuse a channel past the 32-bit float range, such as 1e39, with a message like Light.setColor: item 0 must be a finite number that fits a 32-bit float, got 1e39. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException.

use fragmentcolor::Light;
let lamp = Light::point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0])?;
// Warm-tint the lamp later. Every Pass that absorbed `lamp` sees the
// new color on the next render.
lamp.set_color([1.0, 0.7, 0.4])?;

Update the scalar intensity multiplier. Defined for every kind; returns Ok(self) for chaining. The shader multiplies the color by this value when accumulating, so doubling intensity doubles every channel uniformly. The new value propagates live to every shader the Light has already been wired into.

NaN or an infinity returns LightError::NotFinite, which names the call, and the light keeps the intensity it had. The other languages also refuse a number past the 32-bit float range, such as 1e39, with a message like Light.setIntensity: value must be a finite number that fits a 32-bit float, got 1e39. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException.

use fragmentcolor::Light;
let torch = Light::spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0])?;
torch.set_intensity(8.0)?;

Read the world-space position the light radiates from. Returns Some([x, y, z]) for a point or spot light, None for a directional light (directional rays travel from infinitely far away, so there’s no position to report). Call set_position to update it.

use fragmentcolor::Light;
let lamp = Light::point([0.0, 2.5, 0.0], [1.0, 1.0, 1.0])?;
let sun = Light::directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0])?;

Read the world-space direction the light travels along. Returns Some([x, y, z]) for a directional or spot light, None for a point light (point sources radiate omnidirectionally, so there’s no direction to report). Call set_direction to update it.

use fragmentcolor::Light;
let sun = Light::directional([0.3, -1.0, -0.4], [1.0, 1.0, 1.0])?;
let lamp = Light::point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0])?;

Read the influence-radius cap. Returns Some(value) for a point or spot light, None for a directional light (parallel rays have no distance falloff). A value of 0.0 means “unlimited”, matching glTF KHR_lights_punctual’s default; positive values cut the contribution off smoothly past that distance. Call set_range to update it.

use fragmentcolor::Light;
let lamp = Light::point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0])?;
let sun = Light::directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0])?;

Read the inner cone half-angle (radians). Returns Some(value) only for a spot light, None for directional and point lights. The inner cone is the band where the light reaches full intensity; beyond it the contribution smoothly rolls off until it reaches zero at the outer cone (see outer_cone_angle). Call set_cone_angles to update both at once.

use fragmentcolor::Light;
let torch = Light::spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0])?
.set_cone_angles(0.15, 0.4)?;
let lamp = Light::point([0.0, 0.0, 0.0], [1.0, 1.0, 1.0])?;

Read the outer cone half-angle (radians). Returns Some(value) only for a spot light, None for directional and point lights. The outer cone is where the light’s contribution reaches zero. Beyond this angle the fragment receives nothing from this light. Between the inner and outer cones the contribution smoothly rolls off; see inner_cone_angle. Call set_cone_angles to update both at once.

use fragmentcolor::Light;
let torch = Light::spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0])?
.set_cone_angles(0.15, 0.4)?;
let sun = Light::directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0])?;

Update the world-space position. Defined for point and spot lights only; returns Ok(self) for chaining. Calling this on a directional light returns Err(LightError::FieldNotApplicable { kind: Directional, field: "set_position" }) because directional rays carry no position. The new value propagates live to every shader the Light has already been wired into; no re-attach needed.

An item that is NaN or an infinity returns LightError::NotFinite, which names the call and the item, on any kind, and the light stays where it was. The other languages also refuse an item past the 32-bit float range, such as 1e39. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException.

use fragmentcolor::Light;
let lamp = Light::point([0.0, 0.0, 0.0], [1.0, 1.0, 1.0])?;
lamp.set_position([3.0, 1.5, -2.0])?;

Update the world-space direction. Defined for directional and spot lights only; returns Ok(self) for chaining. Calling this on a point light returns Err(LightError::FieldNotApplicable { kind: Point, field: "set_direction" }) because point sources radiate omnidirectionally. The new value propagates live to every shader the Light has already been wired into; no re-attach needed.

An item that is NaN or an infinity returns LightError::NotFinite, which names the call and the item, on any kind, and the light keeps the direction it had. The other languages also refuse an item past the 32-bit float range, such as 1e39. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException. A direction of length 0, such as [0, 0, 0], points the light nowhere and returns LightError::ZeroDirection, which JavaScript and Python raise as InvalidInputError too, and the light keeps its direction.

use fragmentcolor::Light;
let sun = Light::directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0])?;
sun.set_direction([0.3, -0.8, -0.5])?;

Update the influence-radius cap. Defined for point and spot lights only; returns Ok(self) for chaining. Calling this on a directional light returns Err(LightError::FieldNotApplicable { kind: Directional, field: "set_range" }) because parallel rays have no distance falloff. Passing a negative value or NaN returns Err(LightError::NegativeRange(value)) regardless of kind, and the light keeps its range.

A value of 0.0 means “unlimited”, matching glTF KHR_lights_punctual’s default; positive values cut the contribution off smoothly past that distance. The new value propagates live to every shader the Light has already been wired into.

use fragmentcolor::Light;
let lamp = Light::point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0])?;
lamp.set_range(8.0)?;

Update the inner and outer cone half-angles (radians). Defined for spot lights only; returns Ok(self) for chaining. Calling this on a directional or point light returns Err(LightError::FieldNotApplicable { kind, field: "set_cone_angles" }).

The inner half-angle is the band where the light reaches full intensity; between the inner and outer the contribution smoothly rolls off; beyond the outer the fragment receives nothing from this light. Pass equal values for a hard-edged spot, or pass (0.0, π/4) for the default soft 45° cone.

The new values propagate live to every shader the Light has already been wired into; no re-attach needed.

An angle that is NaN or an infinity returns LightError::NotFinite, which names the call and the angle, Light::set_cone_angles: inner must be finite, got NaN, on any kind, and the light keeps both angles it had. The other languages also refuse an angle past the 32-bit float range, such as 1e39, with a message like Light.setConeAngles: inner_radians must be a finite number that fits a 32-bit float, got 1e39. JavaScript throws an Error named InvalidInputError, Python raises InvalidInputError, Swift throws FragmentColorError and Kotlin throws FragmentColorException.

use fragmentcolor::Light;
let torch = Light::spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0])?;
torch.set_cone_angles(0.15, 0.4)?;