Primitive Mesh
Updated
Lets the entity’s meshes cast and/or receive shadows from lights.
- Type
ent-primitive-mesh- Script class
PrimitiveMeshEntity- Family
- Meshes
Overview
Section titled “Overview”A Primitive Mesh draws a built-in shape that MIRA generates from a few numbers: a box, a sphere, a cylinder, a capsule, a cone, a torus, a plane, a ring and more. Nothing is downloaded, so it appears at once, and a script can reshape it at any time.
It is made of two required parts. The Geometry component (comp-geometry) chooses the shape and its size; a material decides how its surface looks. A new Primitive Mesh is a 1 m green box with a Standard material, and it starts with a Shadow component that casts shadows but does not receive them. The material can be replaced by another kind (Lambert, Physical, Glass, a Shader…), but never removed.
Use a Primitive Mesh to block out a level, for simple props and markers, for shapes a script builds at run time, and as a tool for CSG cuts. For ground that things stand on, a Floor already has a collider. For detailed objects made in a modelling tool, use a glTF mesh.
The props that matter most:
shape(Geometry) picks the shape:'box','sphere','plane','cylinder','capsule','cone','torus','circle','ring'and four regular solids.params(Geometry) holds the shape’s dimensions in metres, such aswidth,heightanddepthfor a box orradiusfor a sphere. Missing entries take the shape’s defaults.color(material) is the base colour, andopacitybelow 1 makes it transparent.roughnessandmetalness(Standard material) describe the surface: rough or polished, metal or not.emissiveandemissiveIntensity(material) make it glow. Emission stays off whileemissiveIntensityis0.castandreceive(Shadow) decide whether it casts and receives shadows.
Set it up
Section titled “Set it up”- Add a Primitive Mesh entity to the scene (see Scenes and hierarchy) and name it, for example
Crate. - On its Geometry, choose a
shapeand set itsparams. To resize it later without rebuilding the mesh, scale the entity instead. - On its material, set
color,roughnessandmetalness, or replace the material with another kind. - Turn on
receiveon its Shadow if other objects should cast shadows onto it. - To make it clickable, add a Selectable and a Signal Emitter. To make it solid, add a collider.
Examples
Section titled “Examples”Spin a box and recolour it on click
Section titled “Spin a box and recolour it on click”Goal: a box that turns every frame and takes a random colour each time it is clicked.
The Behavior entity is a child of the box. The box has a Selectable and a Signal Emitter whose signal is box.
#pragma lifecycle(startup, update)
let box;let material;
function startup() { box = self.parent; material = box.getComponent(MaterialComponent);
on('box', (event) => { if (event.eventType !== EventType.Selectable.Select) return; findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again material.setProps({ color: `#${Random.color().getHexString()}` }); });}
function update(dt) { box.rotation.y += dt;}selfis the Behavior entity running the script; itsparentis the box.getComponent(MaterialComponent)returns the box’s material whatever its kind, so the script keeps working if the Standard material is later replaced by a Lambert one.update(dt)runs every frame. Addingdt, the seconds since the last frame, turns the box at one radian a second at any frame rate.Random.color()returns aTHREE.Color;getHexString()turns it into the hex string thecolorprop expects.
Make a beacon pulse
Section titled “Make a beacon pulse”Goal: a sphere whose glow rises and falls forever, to draw the eye to an objective.
#pragma lifecycle(startup)
function startup() { const beacon = findEntity('Beacon'); beacon.getComponent(GeometryComponent).setProps({ shape: 'sphere', params: { radius: 0.3 } });
const material = beacon.getComponent(MaterialComponent); material.setProps({ color: '#1b6fff', emissive: '#4aa3ff' });
Tween.value(0, 2, (value) => material.setProps({ emissiveIntensity: value }), { duration: 0.8, ease: Ease.InOutSine, repeat: Infinity, yoyo: true, });}- The Geometry is set once at startup. Passing an explicit
paramsobject uses only those params; the sphere’s segment counts keep their defaults. emissiveis the glow colour. It shows only onceemissiveIntensityrises above0.Tween.valueanimates a plain number and writes each value withsetProps.repeat: Infinitywithyoyo: trueplays it forward, then backward, forever.- Each
setPropsrebuilds the material once. For many beacons, prefer a few changes a second over one every frame.
Use a cheaper material on phones and headsets
Section titled “Use a cheaper material on phones and headsets”Goal: keep the polished look on desktop, but swap a group of props to a lighter Lambert material on mobile devices.
#pragma lifecycle(startup)
const PROPS = ['Barrel 1', 'Barrel 2', 'Barrel 3', 'Crate 1', 'Crate 2'];
function startup() { if (!Device.isMobile() && !Device.isHeadset()) return;
for (const name of PROPS) { const entity = findEntity(name); if (!entity) continue;
const current = entity.getComponent(MaterialComponent); entity.addComponent(new LambertMaterialComponent({ color: current.props.color })); } log('Switched', PROPS.length, 'props to Lambert');}Device.isMobile()andDevice.isHeadset()describe the device the app runs on. On desktop the script stops at once.- An entity holds one material. Adding a Lambert material replaces the Standard one, so the mesh is never left without a surface.
current.props.colorreads the old colour before the swap, so each prop keeps its look.- Lambert is a cheap matte material, a good fit for phones and headsets when many objects share the screen.
- Changing the Geometry rebuilds the mesh. To animate a size, scale the entity (
entity.scale) or change the geometry in a few steps, never every frame. - A plane, circle or ring shows one face. Set the material’s
sideto'double'to see it from both sides. - Keep segment counts low on shapes you repeat many times: each segment adds triangles.
- A cylinder is centred on its origin. Lift it by half its height to stand it on the ground.
Components
Section titled “Components”What a new Primitive Mesh 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. |
| Shadow | comp-shadow |
Starts with | One | Lets the entity’s meshes cast and/or receive shadows from lights. |
| Geometry | comp-geometry |
Required | One | A built-in primitive shape: box, sphere, plane, cylinder, ring and more. |
| Standard Material | comp- |
Starts with | One | Physically based metal/roughness material; the default for most surfaces. |
You can also add 63 optional components, such as Animation Graph, Animation Timeline, Basic Material, Billboard, Box Collider, Cache Mesh.
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({ … }).
Shadow
Section titled “Shadow”| Prop | Default | Range |
|---|---|---|
cast |
true |
|
receive |
false |
Geometry
Section titled “Geometry”| Prop | Default | Range |
|---|---|---|
shape |
"box" |
|
params |
{"width":1,"height":1,"depth":1} |
Standard Material
Section titled “Standard Material”| Prop | Default | Range |
|---|---|---|
color |
"#25c205" |
|
opacity |
1 |
0 ≤ x ≤ 1 |
wireframe |
false |
|
side |
"front" |
|
emissive |
"#ffffff" |
|
emissive |
0 |
0 ≤ x ≤ 1000 |
flatShading |
false |
|
roughness |
0.8 |
0 ≤ x ≤ 1 |
metalness |
0 |
0 ≤ x ≤ 1 |
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.
Shadow::change
Like every entity, it also reports: 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.
Your first script: a Behavior under a sphere bobs it around its starting height, with the height and period as editor slots.
Watch the ball bob. 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 Primitive Mesh.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Primitive Mesh'); if (!(entity instanceof PrimitiveMeshEntity)) return;
// Its Shadow component holds the settings listed under Props. const component = entity.getComponent(ShadowComponent); log(component.props);}