Skip to content

Fire

Updated

A particle flame; many fires share one draw call.

Type
ent-fire
Script class
FireEntity
Family
Effects

A Fire entity draws an animated particle flame shaped like a cone: a campfire, a torch, a brazier, a burning barrel. The flame is computed entirely on the graphics card, and every fire in the world is an instance of one shared mesh, so a village full of torches costs about one draw call.

The fire’s position, rotation and scale come from its entity’s transform; its look comes from its required Fire component (comp-fire). In the editor every fire is visible but only the selected one animates; in play every fire animates.

Pick a Fire for flames. To light the surroundings with a warm glow, add a Point Light next to it.

The props that matter most:

  • height and radius set the size of the cone, in local metres.
  • particleCount is the number of particles, a whole number. It is the main cost setting.
  • size is the width of each particle. Larger particles make a fuller, softer flame.
  • color tints the flame.
  • speed is how fast the flame animates, in cycles per second. 0 freezes it and negative values play it backwards.
  • seed sets the starting phase. Give neighbouring fires different seeds so they do not flicker in step.
  1. Add a Fire entity to the scene (see Scenes and hierarchy) and give it a clear name, such as Campfire.
  2. Move it where the flame should start. The cone grows up from the entity’s position.
  3. Set height, radius and color for the look you want, and lower particleCount for small or distant fires.
  4. To light a glow, add a Point Light a little above the fire with a warm color and a short distance.
  5. To react to clicks, add a Selectable and a Signal Emitter to the Fire entity: each fire has an invisible box for picking.

Goal: make every torch of a corridor flicker on its own rhythm at startup.

TorchRow.jx
#pragma lifecycle(startup)
const TORCHES = ['Torch 1', 'Torch 2', 'Torch 3', 'Torch 4'];
function startup() {
for (const name of TORCHES) {
const entity = findEntity(name);
if (!entity) continue;
const fire = entity.getComponent(FireComponent);
fire.setProps({
seed: Random.integer(0, 10000),
speed: Random.range(0.9, 1.2),
color: '#ff5a10',
});
}
}
  • findEntity(name) returns the entity with that name, or undefined when it is not in the scene, which the continue skips.
  • getComponent(FireComponent) returns the entity’s Fire component; setProps changes only the keys passed to it.
  • A different seed per torch shifts each flame’s starting phase, and a slightly different speed keeps them from drifting back into step.

Goal: clicking the fire toggles it, with the flame growing in or dying down instead of popping.

The Fire entity Campfire has a Selectable and a Signal Emitter whose signal is campfire.

ToggleCampfire.jx
#pragma lifecycle(startup)
const FULL_HEIGHT = 2;
let lit = true;
let tween = null;
function startup() {
const fire = findEntity('Campfire').getComponent(FireComponent);
on('campfire', (event) => {
if (event.eventType !== EventType.Selectable.Select) return;
findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again
lit = !lit;
if (tween) tween.stop();
tween = Tween.value(fire.props.height, lit ? FULL_HEIGHT : 0, (value) => {
fire.setProps({ height: value });
}, { duration: lit ? 1.2 : 2, ease: lit ? Ease.OutCubic : Ease.InSine });
});
}
  • on('campfire', …) receives every event the Signal Emitter forwards. The script keeps only EventType.Selectable.Select, the click.
  • fire.props.height is the flame’s current height, so a click in the middle of a fade continues from where the flame is.
  • Tween.value(start, end, onValue, options) animates a plain number and passes each value to setProps. A running tween is stopped first so two never fight over the height.
  • At a height of 0 the flame is invisible but the fire still exists, so it can be relit at once.

Goal: the fire shrinks as it uses fuel, goes out when the fuel runs out, and grows again when another script sends campfire/fuel.

BurningFuel.jx
#pragma lifecycle(startup, update)
const BURN_PER_SECOND = 0.05;
let fire;
let fuel = 1;
const refresh = throttle(() => {
fire.setProps({
height: 0.4 + fuel * 1.6,
radius: 0.3 + fuel * 0.3,
particleCount: Math.round(80 + fuel * 200),
});
}, 250);
function startup() {
fire = findEntity('Campfire').getComponent(FireComponent);
on('campfire/fuel', (data) => {
fuel = Math.min(1, fuel + (data?.amount ?? 0.3));
if (!fire.enabled) fire.setEnabled(true);
refresh();
});
}
function update(dt) {
if (!fire.enabled) return;
fuel -= BURN_PER_SECOND * dt;
if (fuel <= 0) {
fuel = 0;
fire.setEnabled(false);
emit('campfire/out');
return;
}
refresh();
}
  • fuel runs from 1 to 0. It drops by BURN_PER_SECOND × dt each frame, so a full fire burns for 20 seconds.
  • throttle(fn, 250) returns a function that runs at most once every 250 ms: update calls it every frame, but the flame changes only four times a second.
  • particleCount must be a whole number, hence Math.round.
  • Another script feeds the fire with emit('campfire/fuel', { amount: 0.5 }); ?? 0.3 is the amount when none is sent.
  • When the fuel runs out, setEnabled(false) switches the Fire component off and emit('campfire/out') tells other scripts, for example to start smoke or stop a crackling sound.
  • A fire is drawn from a shared batch, so a Mask does not clip it and a Decal does not stamp it.
  • Outline and Silhouette draw around the fire’s invisible picking box, not around the flame.
  • Fade a fire in by tweening height from 0 before enabling it, so it does not pop in at full size.

What a new Fire 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.
Fire comp-fire Required One A particle flame; many fires share one draw call.

You can also add 9 optional components, such as Enable On Signal, Grabbable, Networkable, Networkable Trait, Outline, Pivotable.

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
radius 0.5 0 ≤ x ≤ 100
height 2 0 ≤ x ≤ 100
particleCount 240 0 ≤ x ≤ 2048
color "#ff2200"
size 0.4 0 ≤ x ≤ 10
speed 1 -10 ≤ x ≤ 10
seed 0

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

It reports the events every entity has: 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 Fire.

FireEntity.jx
#pragma lifecycle(startup)
function startup() {
// Find the entity by the name it has in the Hierarchy.
const entity = findEntity('My Fire');
if (!(entity instanceof FireEntity)) return;
// Its Fire component holds the settings listed under Props.
const component = entity.getComponent(FireComponent);
log('radius is', component.props.radius);
component.setProps({ radius: 0.75 });
}