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.
Before you start
Section titled “Before you start”- 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.
How masks work
Section titled “How masks work”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.
Mask Mesh
Section titled “Mask Mesh”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.
Image masks
Section titled “Image masks”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
rectangleto 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.
What gets clipped
Section titled “What gets clipped”- 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.
Editing from scripts
Section titled “Editing from scripts”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.
| Component | Type | What it does |
|---|---|---|
| Mask Mesh | comp-mask-mesh | Shows everything under it only through its 2D shape: windows, portals, peepholes, reveals. |
Examples
Section titled “Examples”Each example assumes a Mask entity named Portal with some content (a model, a room, a skybox sphere) placed under it.
1. Open a portal
Section titled “1. Open a portal”The portal starts closed and grows open over one second, then gently breathes.
#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 });}An elliptical portal pops open over a second and then gently breathes, showing a glowing world that exists only inside it: a Mask Mesh resized by tweens from a Behavior script.
Watch the portal open, then orbit to see the world behind it is clipped to the ellipse. Drag to orbit the camera, scroll to zoom.
- 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.OutBackovershoots slightly, so the portal pops open.awaitwaits for the opening to finish; it resolvesfalseif 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
setPropsrebuilds only the small flat shape, which is cheap enough to do every frame of a tween.
2. Peephole or hole: toggle on click
Section titled “2. Peephole or hole: toggle on click”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.
#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'); });}Clicking the portal's frame flips its Mask Mesh's invert: a window that shows another world inside the ellipse becomes a hole that shows it everywhere else.
Click the blue frame to turn the window into a hole, and again to turn it back. Drag elsewhere to orbit the camera, scroll to zoom.
mask.props.invertreads the current value. Readingpropsis fine; writing must go throughsetProps.- Changing
invertdoes not rebuild the shape at all, so it is instant.
3. Switch the world behind the portal
Section titled “3. Switch the world behind the portal”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.
#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));}Three worlds are groups under a circular Mask Mesh with one enabled at a time; Next world closes the portal with a tween, swaps the world and opens it again.
Press Next world to step through the desert, the glacier and space. Drag to orbit the camera, scroll to zoom.
- 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.valuereturns an awaitable handle, sosetRadiuscan simply return it and the caller canawaitit.- The
busyflag ignores new requests while a transition is running.
Snippets
Section titled “Snippets”1. A keyhole with an image mask
Section titled “1. A keyhole with an image mask”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.
#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 withwidthandheight.- The
Stringslot 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.
Common mistakes
Section titled “Common mistakes”- Scaling the Mask entity to resize the portal. That also scales everything inside it. Change
radius,widthorheightinstead. - 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 = 2changes nothing. UsesetProps. - Expecting shadows from masked objects. Masked meshes cast no shadow.