Skip to content

Spot Light

Updated

A cone of light aimed by the entity’s rotation.

Type
ent-spot-light
Script class
SpotLightEntity
Family
Lights

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:

  • angle is the half-angle of the cone, in radians, from 0 to Math.PI / 2. A new entity starts at Math.PI / 6, 30 degrees.
  • penumbra is the softness of the cone’s edge, from 0 (sharp) to 1. A new entity starts at 0.1.
  • intensity (candela, 0 to 100000) and color set the brightness and tint.
  • distance is the maximum range in metres, 0 to 1000; 0 means no limit. decay sets the falloff; 2 is physically correct.
  • On the Light Shadow, mapSize sets the shadow resolution and range how far the shadow reaches.
  1. Add a Spot Light entity to the scene (see Scenes and hierarchy) and move it where the lamp is.
  2. Rotate the entity so its cone points at what it should light.
  3. Set angle and penumbra for the shape of the pool of light, then intensity, color and distance.
  4. Keep its Light Shadow for shadows; cover only the play area with the shadow’s range for sharper shadows.
  5. For a visible beam or a projected image, add a Spot Light Beam or a Spot Light Projector to the entity.

Goal: set up a tight, soft-edged spot at startup and turn it 45 degrees.

StageSpot.jx
#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);
}
  • setProps changes only the keys passed: color and decay keep their values.
  • angle is in radians: Math.PI / 8 is a 22.5-degree half-angle.
  • The cone is aimed by rotating the entity, not by a prop of the component. DEG_TO_RAD converts degrees to radians.

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.

FollowSpot.jx
#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.

Goal: sweep the light from side to side, and flare it brighter when another script emits alarm.

Searchlight.jx
#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.value and setProps, never by tweening light.props.
  • The alarm listener is async, so it can await the 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 angle casts no shadow and shows no beam until it has a usable cone again.
  • Switch the light with intensity rather than by disabling it, which recompiles every lit material.

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-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
Spot Light comp-spot-light Required One A cone of light aimed by the entity’s rotation.
Light Shadow comp-light-shadow 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({ … }).

Prop Default Range
color "#ffffff"
intensity 1 0 ≤ x ≤ 100000
distance 0 0 ≤ x ≤ 1000
decay 2 0 ≤ x ≤ 10
angle 0.5235987755982988 0 ≤ x ≤ 1.5707963267948966
penumbra 0.1 0 ≤ x ≤ 1
Prop Default Range
mapSize 256 1 ≤ x ≤ 2048
bias 0
radius 1 0 ≤ x ≤ 16
range 0 0 ≤ x ≤ 1000

With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.

  • SpotLight::change
  • LightShadow::change

Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.

Run it in the browser, read its scripts, and download it as an XR Creator project.

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.

SpotLightEntity.jx
#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 });
}