Skip to content

Mask

Updated

Shows everything under it only through its 2D shape: windows, portals, peepholes, reveals.

Type
ent-mask
Script class
MaskEntity
Family
Effects

A Mask entity is a portal. Everything placed under it in the hierarchy is visible only where the mask’s flat shape covers it on screen. As the camera moves, the content is revealed and hidden through the shape, like looking through a window into another world. Use it for portals between places, windows onto another scene, peepholes, magic mirrors and reveal effects.

The shape lies flat in the entity’s local X–Y plane and faces +Z: turn the entity to turn the portal. The portal and its content move together with the entity. Before the scene is drawn, an invisible copy of the shape marks the pixels it covers in a hidden screen buffer (the stencil), and each mesh under the mask is then drawn only on those pixels. Materials are not changed, so masks are cheap. In the editor a mask shows as a faint blue shape you can select; in play the shape itself is invisible.

Use a Mask to hide parts of what is drawn. To add an image onto surfaces, use a Decal instead.

The props that matter most, on its required Mask Mesh component (comp-mask-mesh):

  • shape is circle, ring, rectangle or ellipse.
  • radius (circle and ring) and innerRadius (ring) are in metres. A new Mask entity starts as a circle of radius 5.
  • width and height size a rectangle or an ellipse.
  • invert shows the content outside the shape instead of inside, so the shape becomes a hole.

The size comes from these props, not from the entity’s scale: scaling the entity would scale its content too.

  1. Add a Mask entity to the scene (see Scenes and hierarchy) and turn it so its +Z faces the viewer.
  2. Choose a shape and its size.
  3. Place the content to reveal under the Mask entity in the hierarchy: a model, a room, a large sphere for a sky.
  4. To trigger the portal from other scripts, add a Signal Emitter to the Mask entity or emit your own signals.

Goal: the portal starts closed and grows open over one second.

PortalOpen.jx
#pragma lifecycle(startup)
async function startup() {
const mask = findEntity('Portal').getComponent(MaskMeshComponent);
mask.setProps({ shape: 'ellipse', width: 0.01, height: 0.01 });
await Tween.value(0, 1, (t) => {
mask.setProps({ width: 0.01 + t * 2.4, height: 0.01 + t * 3.2 });
}, { duration: 1, ease: Ease.OutBack });
log('Portal open');
}
  • The ellipse is shrunk to almost nothing first, so the content is hidden from the first frame.
  • Tween.value(0, 1, …) produces a progress value from 0 to 1, turned here into a width and a height. Ease.OutBack overshoots slightly, so the portal pops open.
  • Each size change rebuilds only a small flat shape, cheap enough for every frame of a tween.
  • startup is async, so it may await the tween before logging.

Goal: pressing I turns the window inside out, so the content shows everywhere except the shape.

PortalInvert.jx
#pragma lifecycle(startup, update)
let mask;
function startup() {
mask = findEntity('Portal').getComponent(MaskMeshComponent);
}
function update() {
if (!Input.keyboard.isKeyReleased(Key.I)) return;
const inverted = !mask.props.invert;
mask.setProps({ invert: inverted });
log(inverted ? 'Now a hole' : 'Now a window');
}
  • Input.keyboard.isKeyReleased(Key.I) is true for one frame after the key goes up.
  • mask.props.invert reads the current value; changing it goes through setProps.
  • Changing invert does not rebuild the shape, so it is instant.

Goal: each portal/next signal closes the portal, swaps the world behind it and opens it again.

Under the Mask entity Portal, a Group per world (Desert, Glacier, Space) holds its content, with only Desert enabled. The mask is a circle.

PortalWorlds.jx
#pragma lifecycle(startup)
const WORLDS = ['Desert', 'Glacier', 'Space'];
const OPEN_RADIUS = 1.5;
let current = 0;
let busy = false;
function startup() {
const mask = findEntity('Portal').getComponent(MaskMeshComponent);
const worlds = WORLDS.map((name) => findEntity(name));
const resize = (to, seconds) =>
Tween.value(mask.props.radius, to, (r) => mask.setProps({ radius: r }), {
duration: seconds,
ease: Ease.InOutCubic,
});
on('portal/next', async () => {
if (busy) return;
busy = true;
await resize(0.01, 0.3);
worlds[current].setEnabled(false);
current = (current + 1) % worlds.length;
worlds[current].setEnabled(true);
await resize(OPEN_RADIUS, 0.5);
busy = false;
});
}
  • The worlds are looked up once and kept in worlds.
  • resize animates radius from its current value; a tween is awaitable and resolves when it ends.
  • The worlds are swapped while the portal is closed, so the change is never seen. Content enabled under a mask is clipped from its first frame.
  • busy ignores signals that arrive during a transition.
  • Only meshes under the mask are clipped. Meshes with a Cache Mesh, pooled water, fire, Gaussian splats, points, lines and sprites are not.
  • With nested masks, a mesh is clipped by the nearest Mask above it only. Up to 127 masks can be active at once.
  • Masked meshes cast no shadow, since a shadow would fall outside the portal.

What a new Mask 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.
Mask Mesh comp-mask-mesh Required One Shows everything under it only through its 2D shape: windows, portals, peepholes, reveals.

You can also add 1 optional components, such as 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
shape "circle"
radius 5 -10000 ≤ x ≤ 10000
innerRadius 0.5 -10000 ≤ x ≤ 10000
width 2 -10000 ≤ x ≤ 10000
height 2 -10000 ≤ x ≤ 10000
invert false

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 Mask.

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