Light
Description
Section titled “Description”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:
- Universal:
set_colorandset_intensityapply to every kind. - Kind-specific:
set_position,set_direction,set_range, andset_cone_anglesapply to the kinds that carry the field. Calling one on the wrong kind returnsLightError::FieldNotApplicable { kind, field }.
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.
Lifetime
Section titled “Lifetime”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.
Cost model
Section titled “Cost model”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.
How many lights a shader takes
Section titled “How many lights a shader takes”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.
Lights bind to the shader, not the Pass
Section titled “Lights bind to the shader, not the Pass”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
Lightto Pass A and Pass B lists it once on the shared shader. Both Passes render with it. - Adding one
Lightto 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.
Constructors
Section titled “Constructors”| 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 |
Methods
Section titled “Methods”Light::directional
Section titled “Light::directional”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.
Example
Section titled “Example”use fragmentcolor::Light;
let sun = Light::directional([0.3, -1.0, -0.4], [1.0, 0.95, 0.9])?;import { Light } from "fragmentcolor";
const sun = Light.directional([0.3, -1.0, -0.4], [1.0, 0.95, 0.9]);from fragmentcolor import Light
sun = Light.directional([0.3, -1.0, -0.4], [1.0, 0.95, 0.9])import FragmentColor
let sun = try Light.directional([0.3, -1.0, -0.4], [1.0, 0.95, 0.9])import org.fragmentcolor.*
val sun = Light.directional(listOf(0.3f, -1.0f, -0.4f), listOf(1.0f, 0.95f, 0.9f))Light::point
Section titled “Light::point”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.
Example
Section titled “Example”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)?;import { Camera, Light, Material, Mesh, Model, Pass, Renderer, Vertex } from "fragmentcolor";
const renderer = new Renderer();const target = await renderer.createTextureTarget([64, 64]);
const wall = new Mesh();wall.addVertices([ 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.setIndices([0, 1, 2, 0, 2, 3]);
const camera = Camera.perspective(1.047, 1.0, 0.1, 100.0).lookAt([0.0, 0.0, 4.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0]);const bulb = Light.point([0.0, 0.5, 1.0], [1.0, 0.95, 0.8]).setIntensity(15.0);bulb.setRange(3.0);
const pass = new Pass("lamp");pass.add(new Model(wall, Material.pbr()));pass.add(camera);pass.add(bulb);renderer.render(pass, target);from fragmentcolor import Camera, Light, Material, Mesh, Model, Pass, Renderer, Vertex
renderer = Renderer()target = renderer.create_texture_target([64, 64])
wall = Mesh()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])
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])bulb = Light.point([0.0, 0.5, 1.0], [1.0, 0.95, 0.8]).set_intensity(15.0)bulb.set_range(3.0)
rpass = Pass("lamp")rpass.add(Model(wall, Material.pbr()))rpass.add(camera)rpass.add(bulb)renderer.render(rpass, target)import FragmentColor
let renderer = Renderer()let target = try await renderer.createTextureTarget([64, 64])
let wall = Mesh()wall.addVertices([ try Vertex.pbr([-2.0, -2.0, 0.0]), try Vertex.pbr([2.0, -2.0, 0.0]), try Vertex.pbr([2.0, 2.0, 0.0]), try Vertex.pbr([-2.0, 2.0, 0.0]),])wall.setIndices([0, 1, 2, 0, 2, 3])
let camera = try Camera.perspective(1.047, 1.0, 0.1, 100.0).lookAt([0.0, 0.0, 4.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0])let bulb = try Light.point([0.0, 0.5, 1.0], [1.0, 0.95, 0.8]).setIntensity(15.0)try bulb.setRange(3.0)
let pass = Pass("lamp")try pass.add(Model(wall, Material.pbr()))try pass.add(camera)try pass.add(bulb)try renderer.render(pass, target)import org.fragmentcolor.*
val renderer = Renderer()val target = renderer.createTextureTarget(listOf(64u, 64u))
val wall = Mesh()wall.addVertices(listOf(Vertex.pbr(listOf(-2.0f, -2.0f, 0.0f)), Vertex.pbr(listOf(2.0f, -2.0f, 0.0f)), Vertex.pbr(listOf(2.0f, 2.0f, 0.0f)), Vertex.pbr(listOf(-2.0f, 2.0f, 0.0f))))wall.setIndices(listOf(0u, 1u, 2u, 0u, 2u, 3u))
val camera = Camera.perspective(1.047f, 1.0f, 0.1f, 100.0f).lookAt(listOf(0.0f, 0.0f, 4.0f), listOf(0.0f, 0.0f, 0.0f), listOf(0.0f, 1.0f, 0.0f))val bulb = Light.point(listOf(0.0f, 0.5f, 1.0f), listOf(1.0f, 0.95f, 0.8f)).setIntensity(15.0f)bulb.setRange(3.0f)
val pass = Pass("lamp")pass.add(Model(wall, Material.pbr()))pass.add(camera)pass.add(bulb)renderer.render(pass, target)Light::spot
Section titled “Light::spot”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.
Example
Section titled “Example”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)?;import { Light } from "fragmentcolor";
const torch = Light.spot([0.0, 1.8, 1.0], [0.0, -0.3, -1.0], [1.0, 0.9, 0.7]).setIntensity(5.0).setConeAngles(0.15, 0.4);from fragmentcolor import Light
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)import FragmentColor
let torch = try Light.spot([0.0, 1.8, 1.0], [0.0, -0.3, -1.0], [1.0, 0.9, 0.7]).setIntensity(5.0).setConeAngles(0.15, 0.4)import org.fragmentcolor.*
val torch = Light.spot(listOf(0.0f, 1.8f, 1.0f), listOf(0.0f, -0.3f, -1.0f), listOf(1.0f, 0.9f, 0.7f)).setIntensity(5.0f).setConeAngles(0.15f, 0.4f)Light::kind
Section titled “Light::kind”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.
Example
Section titled “Example”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])?;import { Light, LightKind } from "fragmentcolor";
const sun = Light.directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0]);const bulb = Light.point([0.0, 2.5, 0.0], [1.0, 1.0, 1.0]);const torch = Light.spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0]);from fragmentcolor import Light, LightKind
sun = Light.directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0])bulb = Light.point([0.0, 2.5, 0.0], [1.0, 1.0, 1.0])torch = Light.spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0])import FragmentColor
let sun = try Light.directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0])let bulb = try Light.point([0.0, 2.5, 0.0], [1.0, 1.0, 1.0])let torch = try Light.spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0])import org.fragmentcolor.*
val sun = Light.directional(listOf(0.0f, -1.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f))val bulb = Light.point(listOf(0.0f, 2.5f, 0.0f), listOf(1.0f, 1.0f, 1.0f))val torch = Light.spot(listOf(0.0f, 1.8f, 1.0f), listOf(0.0f, -1.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f))Light::color
Section titled “Light::color”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.
Example
Section titled “Example”use fragmentcolor::Light;
let warm_lamp = Light::point([0.0, 2.0, 0.0], [1.0, 0.7, 0.4])?;import { Light } from "fragmentcolor";
const warm_lamp = Light.point([0.0, 2.0, 0.0], [1.0, 0.7, 0.4]);from fragmentcolor import Light
warm_lamp = Light.point([0.0, 2.0, 0.0], [1.0, 0.7, 0.4])import FragmentColor
let warm_lamp = try Light.point([0.0, 2.0, 0.0], [1.0, 0.7, 0.4])import org.fragmentcolor.*
val warm_lamp = Light.point(listOf(0.0f, 2.0f, 0.0f), listOf(1.0f, 0.7f, 0.4f))Light::intensity
Section titled “Light::intensity”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.
Example
Section titled “Example”use fragmentcolor::Light;
let bright = Light::point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0])?.set_intensity(5.0)?;import { Light } from "fragmentcolor";
const bright = Light.point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0]).setIntensity(5.0);from fragmentcolor import Light
bright = Light.point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0]).set_intensity(5.0)import FragmentColor
let bright = try Light.point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0]).setIntensity(5.0)import org.fragmentcolor.*
val bright = Light.point(listOf(0.0f, 2.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f)).setIntensity(5.0f)Light::set_color
Section titled “Light::set_color”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.
Example
Section titled “Example”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])?;import { Light } from "fragmentcolor";
const 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.setColor([1.0, 0.7, 0.4]);from fragmentcolor import Light
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])import FragmentColor
let lamp = try 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.try lamp.setColor([1.0, 0.7, 0.4])import org.fragmentcolor.*
val lamp = Light.point(listOf(0.0f, 2.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f))
// Warm-tint the lamp later. Every Pass that absorbed `lamp` sees the// new color on the next render.lamp.setColor(listOf(1.0f, 0.7f, 0.4f))Light::set_intensity
Section titled “Light::set_intensity”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.
Example
Section titled “Example”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)?;import { Light } from "fragmentcolor";
const torch = Light.spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0]);
torch.setIntensity(8.0);from fragmentcolor import Light
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)import FragmentColor
let torch = try Light.spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0])
try torch.setIntensity(8.0)import org.fragmentcolor.*
val torch = Light.spot(listOf(0.0f, 1.8f, 1.0f), listOf(0.0f, -1.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f))
torch.setIntensity(8.0f)Light::position
Section titled “Light::position”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.
Example
Section titled “Example”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])?;import { Light } from "fragmentcolor";
const lamp = Light.point([0.0, 2.5, 0.0], [1.0, 1.0, 1.0]);const sun = Light.directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0]);from fragmentcolor import Light
lamp = Light.point([0.0, 2.5, 0.0], [1.0, 1.0, 1.0])sun = Light.directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0])import FragmentColor
let lamp = try Light.point([0.0, 2.5, 0.0], [1.0, 1.0, 1.0])let sun = try Light.directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0])import org.fragmentcolor.*
val lamp = Light.point(listOf(0.0f, 2.5f, 0.0f), listOf(1.0f, 1.0f, 1.0f))val sun = Light.directional(listOf(0.0f, -1.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f))Light::direction
Section titled “Light::direction”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.
Example
Section titled “Example”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])?;import { Light } from "fragmentcolor";
const sun = Light.directional([0.3, -1.0, -0.4], [1.0, 1.0, 1.0]);const lamp = Light.point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0]);from fragmentcolor import Light
sun = Light.directional([0.3, -1.0, -0.4], [1.0, 1.0, 1.0])lamp = Light.point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0])import FragmentColor
let sun = try Light.directional([0.3, -1.0, -0.4], [1.0, 1.0, 1.0])let lamp = try Light.point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0])import org.fragmentcolor.*
val sun = Light.directional(listOf(0.3f, -1.0f, -0.4f), listOf(1.0f, 1.0f, 1.0f))val lamp = Light.point(listOf(0.0f, 2.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f))Light::range
Section titled “Light::range”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.
Example
Section titled “Example”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])?;import { Light } from "fragmentcolor";
const lamp = Light.point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0]);const sun = Light.directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0]);from fragmentcolor import Light
lamp = Light.point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0])sun = Light.directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0])import FragmentColor
let lamp = try Light.point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0])let sun = try Light.directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0])import org.fragmentcolor.*
val lamp = Light.point(listOf(0.0f, 2.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f))val sun = Light.directional(listOf(0.0f, -1.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f))Light::inner_cone_angle
Section titled “Light::inner_cone_angle”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.
Example
Section titled “Example”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])?;import { Light } from "fragmentcolor";
const torch = Light.spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0]).setConeAngles(0.15, 0.4);const lamp = Light.point([0.0, 0.0, 0.0], [1.0, 1.0, 1.0]);from fragmentcolor import Light
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)lamp = Light.point([0.0, 0.0, 0.0], [1.0, 1.0, 1.0])import FragmentColor
let torch = try Light.spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0]).setConeAngles(0.15, 0.4)let lamp = try Light.point([0.0, 0.0, 0.0], [1.0, 1.0, 1.0])import org.fragmentcolor.*
val torch = Light.spot(listOf(0.0f, 1.8f, 1.0f), listOf(0.0f, -1.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f)).setConeAngles(0.15f, 0.4f)val lamp = Light.point(listOf(0.0f, 0.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f))Light::outer_cone_angle
Section titled “Light::outer_cone_angle”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.
Example
Section titled “Example”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])?;import { Light } from "fragmentcolor";
const torch = Light.spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0]).setConeAngles(0.15, 0.4);const sun = Light.directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0]);from fragmentcolor import Light
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)sun = Light.directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0])import FragmentColor
let torch = try Light.spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0]).setConeAngles(0.15, 0.4)let sun = try Light.directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0])import org.fragmentcolor.*
val torch = Light.spot(listOf(0.0f, 1.8f, 1.0f), listOf(0.0f, -1.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f)).setConeAngles(0.15f, 0.4f)val sun = Light.directional(listOf(0.0f, -1.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f))Light::set_position
Section titled “Light::set_position”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.
Example
Section titled “Example”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])?;import { Light } from "fragmentcolor";
const lamp = Light.point([0.0, 0.0, 0.0], [1.0, 1.0, 1.0]);lamp.setPosition([3.0, 1.5, -2.0]);from fragmentcolor import Light
lamp = Light.point([0.0, 0.0, 0.0], [1.0, 1.0, 1.0])lamp.set_position([3.0, 1.5, -2.0])import FragmentColor
let lamp = try Light.point([0.0, 0.0, 0.0], [1.0, 1.0, 1.0])try lamp.setPosition([3.0, 1.5, -2.0])import org.fragmentcolor.*
val lamp = Light.point(listOf(0.0f, 0.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f))lamp.setPosition(listOf(3.0f, 1.5f, -2.0f))Light::set_direction
Section titled “Light::set_direction”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.
Example
Section titled “Example”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])?;import { Light } from "fragmentcolor";
const sun = Light.directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0]);sun.setDirection([0.3, -0.8, -0.5]);from fragmentcolor import Light
sun = Light.directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0])sun.set_direction([0.3, -0.8, -0.5])import FragmentColor
let sun = try Light.directional([0.0, -1.0, 0.0], [1.0, 1.0, 1.0])try sun.setDirection([0.3, -0.8, -0.5])import org.fragmentcolor.*
val sun = Light.directional(listOf(0.0f, -1.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f))sun.setDirection(listOf(0.3f, -0.8f, -0.5f))Light::set_range
Section titled “Light::set_range”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.
Example
Section titled “Example”use fragmentcolor::Light;
let lamp = Light::point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0])?;lamp.set_range(8.0)?;import { Light } from "fragmentcolor";
const lamp = Light.point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0]);lamp.setRange(8.0);from fragmentcolor import Light
lamp = Light.point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0])lamp.set_range(8.0)import FragmentColor
let lamp = try Light.point([0.0, 2.0, 0.0], [1.0, 1.0, 1.0])try lamp.setRange(8.0)import org.fragmentcolor.*
val lamp = Light.point(listOf(0.0f, 2.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f))lamp.setRange(8.0f)Light::set_cone_angles
Section titled “Light::set_cone_angles”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.
Example
Section titled “Example”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)?;import { Light } from "fragmentcolor";
const torch = Light.spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0]);torch.setConeAngles(0.15, 0.4);from fragmentcolor import Light
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)import FragmentColor
let torch = try Light.spot([0.0, 1.8, 1.0], [0.0, -1.0, 0.0], [1.0, 1.0, 1.0])try torch.setConeAngles(0.15, 0.4)import org.fragmentcolor.*
val torch = Light.spot(listOf(0.0f, 1.8f, 1.0f), listOf(0.0f, -1.0f, 0.0f), listOf(1.0f, 1.0f, 1.0f))torch.setConeAngles(0.15f, 0.4f)