Skip to content

CSG

Updated

Constructive solid geometry (CSG) builds new shapes by combining simple ones. You cut a door and a window out of a wall, drill holes through a block, or round a cube by keeping only the part that overlaps a sphere. In MIRA the result is live: move a cutting shape and its parent is re-cut on the next frame, so scripts can carve, open and reshape objects while the world runs.

  • Primitive meshes. CSG works only on primitives (boxes, spheres, cylinders and the other built-in shapes), not on loaded models. See Models and meshes.
  • Hierarchy. CSG uses parent and child entities: a child shape acts on its parent. See Working with entities.
  • Transforms. Every entity has a position, rotation and scale, relative to its parent. Moving a tool means changing its transform.
  • Tool. A primitive mesh with an enabled CSG component, placed under another primitive mesh. It is not drawn itself in play; its shape is applied to its parent.
  • Parent (the host). The primitive the tools act on. It is drawn with every tool’s shape applied.
  • Operation. What a tool does: subtract cuts its shape out, union joins its shape on, intersect keeps only the overlap.
Wall (box)
├── Door (box, CSG subtract)
├── Window (cylinder, CSG subtract)
└── Ledge (box, CSG union)

CSGComponent · comp-csg · on a Primitive Mesh (ent-primitive-mesh)

Prop Type Default Description
operation 'subtract' | 'union' | 'intersect' 'subtract' What the tool does to its parent: cut itself out, join on, or keep only the overlap.
  1. A primitive mesh with an enabled CSG, under another primitive mesh, is a tool. Its parent is drawn with the tool’s operation applied.
  2. Tools apply in the order they sit under their parent. A tool with tools under it is cut by them first, so a tool can itself be a combined shape.
  3. In play, a tool draws nothing. In the editor it shows as an orange wireframe so you can select and move it.
  4. A tool’s placement is its transform relative to its parent: move, turn or scale it like any entity. Moving the parent moves the whole result without re-cutting.
  5. The parent is re-cut when a tool moves (by the gizmo, a script, a tween, physics or the network), when a tool’s geometry or operation changes, and when a tool is enabled, disabled, added, removed or moved to another parent. Several changes in one frame cause one re-cut.
  6. Cut faces take the parent’s material. The result is one mesh with one material, drawn in one call.
  7. Picking, outlines, shadows and the parent’s mesh-based colliders all use the cut shape.
  8. A disabled CSG does nothing and the mesh draws as usual. So does a CSG on a primitive that is not under another primitive.
  9. Plane, circle and ring have no inside. As tools they cut nothing, and a plane, circle or ring parent is not cut.
  10. A parent with tools is not batched by a Cache Mesh: its cut shape is its own.
  11. In multiplayer, each player cuts locally from the tools’ transforms and props. Add the CSG network trait (NetworkTraitType.CSG) to share operation changes made by scripts.
  • A scene without CSG pays nothing. A tool that does not move costs a transform check per frame.
  • A re-cut takes a few milliseconds for simple shapes and grows with the triangle counts of the parent and its tools. Keep segment counts modest on tools that move every frame.
  • A parent with a Mesh Collider or Convex Hull Collider rebuilds its collider on every re-cut. Avoid animating tools on such a parent.
ComponentTypeWhat it does
CSGcomp-csgCuts this primitive mesh out of its parent primitive mesh, joins it on, or keeps only their overlap.

The simplest live cut: a sphere named Cutter, under a box, slides from side to side, carving a moving dent.

SlidingCutter.jx
#pragma lifecycle(startup, update)
let cutter;
function startup() {
cutter = findEntity('Cutter');
cutter.getComponent(CSGComponent).setProps({ operation: 'subtract' });
}
function update(dt, time) {
cutter.position.x = Math.sin(time) * 1.2;
}
  • startup finds the tool once and makes sure it subtracts.
  • update(dt, time) receives the seconds since the last frame and the seconds since play started. Math.sin(time) swings between −1 and 1, so the cutter moves 1.2 m either side.
  • Changing the tool’s position is enough: the parent is re-cut on the next frame, once, however many tools moved.
  • Keep the cutter’s segment counts low (for example a sphere with widthSegments 16), because the parent is re-cut every frame while it moves.

A wall has a box tool named Doorway under it. Its CSG is saved disabled, so the wall starts solid. Clicking the switch (an entity with a Selectable and a Signal Emitter with the signal switch/clicked) opens and closes the doorway.

Doorway.jx
#pragma lifecycle(startup)
function startup() {
const cut = findEntity('Doorway').getComponent(CSGComponent);
on('switch/clicked', (event) => {
if (event.eventType !== EventType.Selectable.Select) return;
findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again
cut.setEnabled(!cut.enabled);
log(cut.enabled ? 'Doorway open' : 'Doorway sealed');
});
}
  • cut.enabled reads whether the component is on; setEnabled switches it. A disabled CSG does nothing, so the wall is whole again.
  • Each switch causes one re-cut, which costs a few milliseconds once, not every frame.
  • If the wall has a Box Collider, players still bump into the full box. For a walk-through doorway, use colliders that match the opening, or a Mesh Collider (which re-cuts with the shape, at a cost).

A script can add tools while the world runs. Here each press of the H key drills a new round hole at a random spot of the wall named Panel, using a disabled template cylinder named Drill that already has a CSG set to subtract.

Driller.jx
#pragma slot(ref=maxHoles, type=Integer, min=1, max=50, default=12)
#pragma lifecycle(startup, update)
let panel;
let drill;
let holes = 0;
function startup() {
panel = findEntity('Panel');
drill = findEntity('Drill');
}
function update() {
if (!Input.keyboard.isKeyReleased(Key.H)) return;
if (holes >= maxHoles) {
log('The panel cannot take more holes');
return;
}
spawn(drill, {
parent: panel,
position: { x: Random.range(-0.8, 0.8), y: Random.range(-0.8, 0.8), z: 0 },
rotation: { x: M_HALF_PI, y: 0, z: 0 },
enabled: true,
});
holes++;
}
  • spawn(drill, { parent: panel, … }) copies the template, CSG included, and places the copy under the panel, which makes it a tool. Its position is relative to the panel.
  • A cylinder stands along Y; rotating it a quarter turn (M_HALF_PI) around X points it through the panel.
  • maxHoles caps the number of tools, because every re-cut grows with the number and complexity of tools.
  • Each new hole causes one re-cut, on the next frame.

The same two shapes give three different solids. This script cycles a tool named Shaper through the three operations each time the signal shape/next is emitted, which makes a good learning toy.

OperationCycle.jx
#pragma lifecycle(startup)
const OPERATIONS = ['subtract', 'union', 'intersect'];
function startup() {
const csg = findEntity('Shaper').getComponent(CSGComponent);
let index = OPERATIONS.indexOf(csg.props.operation);
on('shape/next', () => {
index = (index + 1) % OPERATIONS.length;
csg.setProps({ operation: OPERATIONS[index] });
log('Operation:', OPERATIONS[index]);
});
}
  • csg.props.operation reads the current operation, so the cycle starts from what was saved.
  • setProps({ operation }) changes it and causes one re-cut.
  • With a box parent and a sphere tool: subtract scoops a sphere out of the box, union makes a box with a ball stuck on, and intersect keeps a rounded cube.
  • In multiplayer, add the CSG network trait so other players see the operation change too.

Tool moves made by tweens re-cut like any other. This script pushes a sphere tool named Scoop through a block once and back, carving a channel and filling it in again.

Carve.jx
#pragma lifecycle(startup)
async function startup() {
const scoop = findEntity('Scoop');
const start = scoop.position.x;
await Tween.to(scoop.position, { x: start + 2 }, { duration: 2, ease: Ease.InOutSine });
await Tween.to(scoop.position, { x: start }, { duration: 2, ease: Ease.InOutSine });
log('Carving pass done');
}
  • Tween.to(scoop.position, { x: … }) moves the tool’s position number directly; the parent is re-cut on each frame it moves.
  • await chains the two moves, the second starting where the first ended.
  • The block shows a dent that travels with the sphere, because a subtract tool removes only where it currently is.
  • Using a model. CSG cuts primitives only. A glTF or FBX model can be neither a tool nor a parent.
  • A tool that is not a child. The tool must be directly under the primitive it acts on. A tool beside it does nothing.
  • Flat tools or parents. Planes, circles and rings have no inside: they cut nothing and cannot be cut.
  • Heavy tools moving every frame. A high-segment sphere moving constantly re-cuts every frame. Lower its segments, or move it only when needed.
  • Animated tools on a Mesh Collider parent. The collider is rebuilt on every re-cut. Use simpler colliders, or keep such tools still.
  • Expecting the tool to show in play. Tools are invisible in play. Add a separate visible mesh if you want to see the cutter.
  • Expecting batching. A parent with tools never joins a Cache Mesh batch.