Mask
Updated
Shows everything under it only through its 2D shape: windows, portals, peepholes, reveals.
- Type
ent-mask- Script class
MaskEntity- Family
- Effects
Overview
Section titled “Overview”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):
shapeiscircle,ring,rectangleorellipse.radius(circle and ring) andinnerRadius(ring) are in metres. A new Mask entity starts as a circle of radius 5.widthandheightsize a rectangle or an ellipse.invertshows 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.
Set it up
Section titled “Set it up”- Add a Mask entity to the scene (see Scenes and hierarchy) and turn it so its +Z faces the viewer.
- Choose a
shapeand its size. - Place the content to reveal under the Mask entity in the hierarchy: a model, a room, a large sphere for a sky.
- To trigger the portal from other scripts, add a Signal Emitter to the Mask entity or emit your own signals.
Examples
Section titled “Examples”Open a portal
Section titled “Open a portal”Goal: the portal starts closed and grows open over one second.
#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.OutBackovershoots slightly, so the portal pops open.- Each size change rebuilds only a small flat shape, cheap enough for every frame of a tween.
startupisasync, so it mayawaitthe tween before logging.
Window or hole with a key
Section titled “Window or hole with a key”Goal: pressing I turns the window inside out, so the content shows everywhere except the shape.
#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)istruefor one frame after the key goes up.mask.props.invertreads the current value; changing it goes throughsetProps.- Changing
invertdoes not rebuild the shape, so it is instant.
Switch the world behind the portal
Section titled “Switch the world behind the portal”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.
#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. resizeanimatesradiusfrom 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.
busyignores 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.
Components
Section titled “Components”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- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| Mask Mesh | comp- |
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 |
- |
innerRadius |
0.5 |
- |
width |
2 |
- |
height |
2 |
- |
invert |
false |
Events
Section titled “Events”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.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
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.
From a script
Section titled “From a script”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.
#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 });}