Skip to content

Masks

Updated

A mask is a portal. Everything placed under a Mask entity 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, exactly like looking through a window into another world. Use masks for portals between places, windows onto a different scene, peepholes, magic mirrors, and reveal effects that grow from nothing.

  • Hierarchy. Entities can be parents of other entities. A mask clips its descendants: the entities under it. See Working with entities.
  • Local axes. The mask’s shape lies flat in its entity’s own X–Y plane and faces +Z. Turn the entity to turn the portal.
  • Tweens. Some examples animate values with Tween. See Animation.

The technique is called stencil masking. Before the scene is drawn, an invisible copy of the mask’s shape marks the pixels it covers in a hidden screen buffer, the stencil. Then each mesh under the mask is drawn only on those marked pixels. Nothing about the content’s materials changes, so masks are cheap.

  • The portal and what it shows move together with the mask entity.
  • The shape’s size comes from its props, not from the entity’s scale: scaling the entity would scale its content too.
  • Masked meshes cast no shadow, since a shadow would land outside the portal.
  • Up to 127 masks can be active at once.
  • In the editor a mask shows as a faint blue shape you can select. It costs nothing in play.

MaskMeshComponent · comp-mask-mesh · required on a Mask entity (ent-mask)

Prop Type Default Description
shape 'circle' | 'ring' | 'rectangle' | 'ellipse' 'circle' The portal’s shape, in the entity’s local X–Y plane, facing +Z.
radius number 1 Circle and ring: the outer radius, in metres.
innerRadius number 0.5 Ring: the inner radius.
width number 2 Rectangle and ellipse: the width.
height number 2 Rectangle and ellipse: the height.
invert boolean false Show the content outside the shape instead of inside: the shape becomes a hole.
src string none An image whose transparency cuts the portal inside the shape.

A new Mask entity created in the editor starts as a circle of radius 5. Dimensions accept values from −10000 to 10000 metres.

src takes a URL or a project asset reference to a PNG, JPEG, WebP, AVIF, GIF or SVG image, or a data:image/ URL. Any other file type is refused when it is set.

  • The image is stretched over the shape’s bounds, and its alpha (transparency) cuts the portal: pixels less than half opaque are cut away, with hard edges.
  • The shape still sets the outline. Use rectangle to let the image alone decide, for a keyhole or a star.
  • The portal stays closed until the image loads, so nothing ever shows unclipped. An image that fails to load leaves the plain shape and logs a warning.
  • Meshes under the mask: primitives, models, images, video, documents, text, and Water Mesh without a Cache Mesh.
  • Not clipped: anything drawn in a shared batch outside the entity (meshes with a Cache Mesh, pooled water, fire), and things that are not meshes: Gaussian splats, points, lines and sprites.
  • Nested masks: a mesh is clipped by the nearest Mask above it only.
  • New content built under the mask (a model that finished loading, an entity switched on) is clipped from its first frame.
  • A disabled or hidden Mask releases its content: everything under it shows unclipped.

Always change a mask through setProps. An edit applies in place and what the mask clips stays clipped. Only edits to shape, radius, innerRadius, width or height rebuild the shape, a small flat geometry, so animating the size is affordable.

ComponentTypeWhat it does
Mask Meshcomp-mask-meshShows everything under it only through its 2D shape: windows, portals, peepholes, reveals.

Each example assumes a Mask entity named Portal with some content (a model, a room, a skybox sphere) placed under it.

The portal starts closed and grows open over one second, then gently breathes.

PortalOpen.jx
#pragma lifecycle(startup)
async function startup() {
const mask = findEntity('Portal').getComponent(MaskMeshComponent);
mask.setProps({ shape: 'ellipse', width: 0.01, height: 0.01 });
const finished = 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 });
if (!finished) return;
Tween.value(0, 1, (t) => {
mask.setProps({ width: 2.4 + t * 0.08, height: 3.2 + t * 0.1 });
}, { duration: 1.6, ease: Ease.InOutSine, repeat: Infinity, yoyo: true });
}
  • The script first shrinks the ellipse to almost nothing, so the content is hidden on the first frame.
  • Tween.value(0, 1, onValue, options) produces a number from 0 to 1 over the duration and hands it to the callback, which turns it into a width and a height. Ease.OutBack overshoots slightly, so the portal pops open.
  • await waits for the opening to finish; it resolves false if the tween was stopped, in which case the script stops too.
  • The second tween repeats for ever with yoyo, playing every other run backwards, for a slow breathing effect.
  • Each setProps rebuilds only the small flat shape, which is cheap enough to do every frame of a tween.

invert turns the portal inside out: the content shows everywhere except the shape. That makes a hole cut through a world. The Portal entity has a Selectable and a Signal Emitter with the signal portal/clicked.

PortalInvert.jx
#pragma lifecycle(startup)
function startup() {
const mask = findEntity('Portal').getComponent(MaskMeshComponent);
on('portal/clicked', (event) => {
if (event.eventType !== EventType.Selectable.Select) return;
findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again
const inverted = !mask.props.invert;
mask.setProps({ invert: inverted });
log(inverted ? 'Now a hole' : 'Now a window');
});
}
  • mask.props.invert reads the current value. Reading props is fine; writing must go through setProps.
  • Changing invert does not rebuild the shape at all, so it is instant.

One mask, several worlds. Put a Group for each world under the Portal (Desert, Glacier, Space), with only one enabled. This script shows the next world each time the signal portal/next is emitted, with a quick close-and-open transition.

PortalWorlds.jx
#pragma lifecycle(startup)
const WORLDS = ['Desert', 'Glacier', 'Space'];
let current = 0;
let busy = false;
function setRadius(mask, to, duration) {
const from = mask.props.radius;
return Tween.value(from, to, (r) => mask.setProps({ radius: r }), { duration: duration, ease: Ease.InOutCubic });
}
async function showNext(mask, worlds) {
if (busy) return;
busy = true;
const open = mask.props.radius;
await setRadius(mask, 0.01, 0.3);
worlds[current].setEnabled(false);
current = (current + 1) % worlds.length;
worlds[current].setEnabled(true);
await setRadius(mask, open, 0.4);
busy = false;
}
function startup() {
const portal = findEntity('Portal');
const mask = portal.getComponent(MaskMeshComponent);
mask.setProps({ shape: 'circle' });
const worlds = WORLDS.map((name) => findEntity(name));
worlds.forEach((world, i) => world.setEnabled(i === current));
on('portal/next', () => showNext(mask, worlds));
}
  • The worlds are ordinary entities under the mask, so they are clipped automatically, including whatever is switched on later.
  • setEnabled(false) switches a world off completely: it is not drawn, and its scripts and animations pause.
  • The portal shrinks to a point, the worlds are swapped while nothing is visible, and the portal grows back to its previous radius. The swap is never seen.
  • Tween.value returns an awaitable handle, so setRadius can simply return it and the caller can await it.
  • The busy flag ignores new requests while a transition is running.

An image’s transparency can cut any outline: a keyhole, a star, a logo. This script sets up a keyhole the player can widen for a closer look by pressing Space.

Keyhole.jx
#pragma slot(ref=keyholeImage, type=String, default="https://cdn.example.com/masks/keyhole.png")
#pragma lifecycle(startup, update)
let mask;
let peeking = false;
function startup() {
mask = findEntity('Portal').getComponent(MaskMeshComponent);
mask.setProps({ shape: 'rectangle', width: 0.6, height: 1, src: keyholeImage, invert: false });
}
function update() {
if (!Input.keyboard.isKeyReleased(Key.Space)) return;
peeking = !peeking;
mask.setProps(peeking ? { width: 1.8, height: 3 } : { width: 0.6, height: 1 });
}
  • shape: 'rectangle' lets the image alone decide the outline. The image is stretched over the rectangle, so match its proportions with width and height.
  • The String slot lets you paste another image URL in the editor without touching the code.
  • Until the image loads the portal stays closed, so the room behind it never flashes into view.
  • Pressing Space toggles a closer look by enlarging the keyhole: two cheap shape rebuilds per press.
  • Scaling the Mask entity to resize the portal. That also scales everything inside it. Change radius, width or height instead.
  • Content beside the mask, not under it. Only descendants are clipped. Check that the content is a child (or grandchild) of the Mask entity.
  • Cached or pooled content. Meshes with a Cache Mesh, pooled water and fire are drawn in shared batches and are not clipped. Remove the Cache Mesh from masked content.
  • Splats through a portal. Gaussian splats are not meshes and are never clipped.
  • Disabling the mask to hide a world. A disabled mask releases its content, which then shows unclipped. Disable the content instead.
  • Writing to props. mask.props.radius = 2 changes nothing. Use setProps.
  • Expecting shadows from masked objects. Masked meshes cast no shadow.