Spot Light
Updated
A cone of light aimed by the entity’s rotation.
- Type
ent-spot-light- Script class
SpotLightEntity- Family
- Lights
Overview
Section titled “Overview”A Spot Light shines a cone of light along the entity’s local -Z axis: a stage light, a flashlight, a desk lamp, a searchlight. You aim it by rotating the entity.
A new Spot Light entity starts with two components: the required Spot Light (comp-spot-light) that holds its colour, brightness and cone, and an enabled Light Shadow (comp-light-shadow) that makes it cast shadows. Two optional components add to it: a Spot Light Beam shows the cone in the air, and a Spot Light Projector shines an image through it, like a gobo. In the editor the light shows an icon and, while selected, its cone; play builds none of these.
Pick a Spot Light when the light has a direction and a shape. It is also the best choice for real-time shadows: a spot’s shadow costs one extra render of the shadow casters a frame, against six for a Point Light. For parallel sunlight, use a Directional Light.
The props that matter most:
angleis the half-angle of the cone, in radians, from0toMath.PI / 2. A new entity starts atMath.PI / 6, 30 degrees.penumbrais the softness of the cone’s edge, from0(sharp) to1. A new entity starts at0.1.intensity(candela,0to100000) andcolorset the brightness and tint.distanceis the maximum range in metres,0to1000;0means no limit.decaysets the falloff;2is physically correct.- On the Light Shadow,
mapSizesets the shadow resolution andrangehow far the shadow reaches.
Set it up
Section titled “Set it up”- Add a Spot Light entity to the scene (see Scenes and hierarchy) and move it where the lamp is.
- Rotate the entity so its cone points at what it should light.
- Set
angleandpenumbrafor the shape of the pool of light, thenintensity,coloranddistance. - Keep its Light Shadow for shadows; cover only the play area with the shadow’s
rangefor sharper shadows. - For a visible beam or a projected image, add a Spot Light Beam or a Spot Light Projector to the entity.
Examples
Section titled “Examples”A narrow stage spot
Section titled “A narrow stage spot”Goal: set up a tight, soft-edged spot at startup and turn it 45 degrees.
#pragma lifecycle(startup)
function startup() { const spot = findEntity('Stage Spot'); spot.getComponent(SpotLightComponent).setProps({ angle: Math.PI / 8, penumbra: 0.3, intensity: 40, distance: 15, }); spot.rotation.y += DEG_TO_RAD(45);}setPropschanges only the keys passed:coloranddecaykeep their values.angleis in radians:Math.PI / 8is a 22.5-degree half-angle.- The cone is aimed by rotating the entity, not by a prop of the component.
DEG_TO_RADconverts degrees to radians.
A follow spot
Section titled “A follow spot”Goal: keep the spot aimed at a performer who moves around the stage.
The spot is directly under the scene root; the performer entity is named Performer.
#pragma lifecycle(startup, update)
let spot;let performer;const target = new THREE.Vector3();const aim = new THREE.Vector3();
function startup() { spot = findEntity('Stage Spot'); performer = findEntity('Performer');}
function update() { performer.getWorldPosition(target); // lookAt turns +Z toward a point; the light shines along -Z, so look away from the target. aim.copy(spot.position).multiplyScalar(2).sub(target); spot.lookAt(aim);}getWorldPosition(target)writes the performer’s world position into a vector created once and reused every frame.- An entity’s
lookAt(point)turns its +Z toward the point. The light shines along -Z, so the script looks at the point mirrored through the spot,2 × spot − target, which aims -Z straight at the performer. - Rotating the entity costs nothing extra: the light, its shadow and any beam follow it.
A searchlight that sweeps and flares
Section titled “A searchlight that sweeps and flares”Goal: sweep the light from side to side, and flare it brighter when another script emits alarm.
#pragma lifecycle(startup)
async function startup() { const entity = findEntity('Searchlight'); const light = entity.getComponent(SpotLightComponent);
Tween.to(entity.rotation, { y: entity.rotation.y + DEG_TO_RAD(60) }, { duration: 4, ease: Ease.InOutSine, repeat: Infinity, yoyo: true, });
on('alarm', async () => { const normal = light.props.intensity; await Tween.value(normal, normal * 4, (value) => light.setProps({ intensity: value }), { duration: 0.2 }); await Tween.value(normal * 4, normal, (value) => light.setProps({ intensity: value }), { duration: 1 }); });}Tween.to(entity.rotation, …)animates the entity’s rotation directly: 60 degrees one way, then back (yoyo: true), forever (repeat: Infinity).- The light’s own props are changed with
Tween.valueandsetProps, never by tweeninglight.props. - The
alarmlistener isasync, so it canawaitthe flare up before fading back down.
- Rolling the entity about its aim rolls the light’s shadow and whatever a Spot Light Projector projects, so a light aimed straight down still has a defined orientation.
- A Spot Light Projector needs an enabled Light Shadow: the image is projected through the shadow map.
- A Spot Light with a zero
anglecasts no shadow and shows no beam until it has a usable cone again. - Switch the light with
intensityrather than by disabling it, which recompiles every lit material.
Components
Section titled “Components”What a new Spot Light is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.
| Component | Type | Role | Per entity | What it does |
|---|---|---|---|---|
| Layer | comp-layer |
Required | One | Puts the entity on a render/raycast layer so cameras and rays can include or skip it. |
| Lock | comp-lock |
Required | One | Editor-only flag that stops the entity from being selected or moved. |
| Enable | comp-enable |
Required | One | Turns the entity and its children on or off: hides them and stops their behavior. |
| Transform | comp- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| Spot Light | comp- |
Required | One | A cone of light aimed by the entity’s rotation. |
| Light Shadow | comp- |
Starts with | One | Adds real-time and baked shadows to the light beside it. |
You can also add 8 optional components, such as Animation Graph, Animation Timeline, Motion Path Controller, Networkable, Networkable Trait, Signal Emitter.
The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).
Spot Light
Section titled “Spot Light”| Prop | Default | Range |
|---|---|---|
color |
"#ffffff" |
|
intensity |
1 |
0 ≤ x ≤ 100000 |
distance |
0 |
0 ≤ x ≤ 1000 |
decay |
2 |
0 ≤ x ≤ 10 |
angle |
0. |
0 ≤ x ≤ 1. |
penumbra |
0.1 |
0 ≤ x ≤ 1 |
Light Shadow
Section titled “Light Shadow”| Prop | Default | Range |
|---|---|---|
mapSize |
256 |
1 ≤ x ≤ 2048 |
bias |
0 |
|
radius |
1 |
0 ≤ x ≤ 16 |
range |
0 |
0 ≤ x ≤ 1000 |
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
SpotLight::changeLightShadow::change
Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
A script under a spot light tweens its intensity down to 0 and back up to 3, two seconds each way, for as long as it runs.
Watch the spot fade out and back in. Drag to orbit the camera, scroll to zoom.
From a script
Section titled “From a script”Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My Spot Light.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Spot Light'); if (!(entity instanceof SpotLightEntity)) return;
// Its Spot Light component holds the settings listed under Props. const component = entity.getComponent(SpotLightComponent); log('intensity is', component.props.intensity); component.setProps({ intensity: 1.5 });}