On this page

← All documentation

Native rendering with stage.*

Draw sprites, shapes, text and particles in the game's own renderer, in the same frame as the notes, with animations and triggers that run without your script.

A mod can draw in two ways. hud.* places nodes in the web overlay, above everything. stage.* places elements in the game's native renderer (wgpu), in the same frame as the notes: sparks at the receptor of a hit note, a glow on a held lane, a sprite that follows the playfield when a map script moves it. Use stage.* when an effect must be exact to the frame or must move with the playfield; use hud.* for text and panels.

You do not draw frame by frame. You declare elements, animations and triggers once, and the renderer plays them from the song clock. Skins and map scripts can use stage.* as well.

A complete example

This mod throws sparks at the receptor of every hit, in the color of the judgement tier, pulses a lane when its key goes down, and makes a "MISS" rise and fade on every miss. The folder holds spark.png.

/// <reference path="../../../mods/sdk/modding.d.ts" />

// Native drawing (`stage.*`): sparks at the receptor of every hit, in the color of the
// judgement tier; a pulse on a lane when its key goes down; a "MISS" that rises and fades
// on every miss. The stage is declared once per play. The game's renderer plays it from
// the song clock, in the frame that shows the judgement: no script runs per frame, and
// the elements follow the playfield wherever the skin or a map script moves it.
//
// Install it by copying this folder into the mods folder (Mods page → Open folder).

/** Sparks one emitter keeps alive; the stage allows 1024 particles in all. */
const SPARKS_PER_TIER = 100;

function build(start: SongStartEvent) {
  // Start from an empty stage: the previous play's elements are gone.
  stage.clear();

  // One emitter per hit tier (a hit tier has a window), at most seven. An emitter
  // placed on a receptor without `column` bursts at the column of the judgement that
  // plays it.
  const hitTiers = start.judgements.tiers.filter((tier) => tier.lateMs !== null && tier.lateMs !== undefined).slice(0, 7);
  for (const tier of hitTiers) {
    stage.emitter({
      id: `sparks-${tier.index}`,
      layer: "above",
      at: { space: "receptor" },
      image: "spark.png",
      size: 0.02,
      color: tier.color,
      endColor: "#ffffff00",
      lifetimeMs: 420,
      speed: 0.7,
      speedJitter: 0.5,
      direction: 0,
      spread: 150,
      gravity: 1.6,
      burst: 12,
      maxParticles: SPARKS_PER_TIER,
      shrink: true,
    });
    stage.trigger({
      id: `hit-${tier.index}`,
      on: { judgement: { tier: tier.index, miss: false } },
      target: `sparks-${tier.index}`,
      play: "burst",
    });
  }

  // A sprite on each receptor, invisible at rest, that swells and fades when the
  // lane's key goes down. `from`/`to` only name what changes; the rest keeps the
  // element's values, which come back when the animation ends.
  for (const lane of start.playfield.lanes) {
    stage.sprite({
      id: `pulse-${lane.column}`,
      layer: "lanes",
      at: { space: "receptor", column: lane.column },
      image: "spark.png",
      size: { width: lane.width * 1.4, height: lane.width * 1.4 },
      color: "#8fd3ff",
      opacity: 0,
      animations: {
        press: { durationMs: 160, easing: "easeOut", from: { opacity: 0.8, scale: 0.7 }, to: { opacity: 0, scale: 1.3 } },
      },
    });
    stage.trigger({ id: `press-${lane.column}`, on: { press: { column: lane.column } }, target: `pulse-${lane.column}`, play: "press" });
  }

  // Text is drawn with the game's font, on one line of at most 128 characters.
  stage.text({
    id: "miss",
    layer: "above",
    at: { space: "playfield", y: 0.1 },
    text: "MISS",
    size: 0.05,
    color: "#ff5a5a",
    align: "center",
    opacity: 0,
    animations: {
      rise: { durationMs: 700, easing: "easeOut", from: { opacity: 1, y: 0 }, to: { opacity: 0, y: -0.08 } },
    },
  });
  stage.trigger({ id: "missed", on: { judgement: { miss: true } }, target: "miss", play: "rise" });
}

export default defineMod({
  id: "example.stage-burst",
  name: "Stage burst",
  version: "1.0.0",
  apiVersion: 2,
  author: "Prism",
  description: "Sparks on every hit, a pulse on each pressed lane and a rising MISS, drawn by the game's renderer.",
  // Without this permission every `stage.*` call throws; the player can withdraw it from the Mods page.
  permissions: ["stage"],
  // Only the PNGs listed here are decoded and usable by `stage.sprite` and `stage.emitter`.
  images: ["spark.png"],
  setup() {
    ctx.on("game.songStart", build);
  },
});

Copy the folder stage-burst into your mods folder (Mods page, Open folder) and start a map. A second, smaller example is content/examples/mods/hit-bursts.

Permission

Declare permissions: ["stage"] in defineMod (the same in defineSkin and defineChart). Without it every stage.* call throws. The Mods page marks such a mod ("Draws in the native renderer"), and the player can withdraw the permission mod by mod. When it is withdrawn, every stage.* call throws and the mod's stage is cleared at once.

A mod that builds its stage in setup gets it back when the mods reload; one that builds it at game.songStart gets it back at the next play.

Elements

Every element belongs to the script that created it and has an id of 1 to 64 characters. Creating an element with an existing id replaces it in place, keeping its running animations and particles.

Function Element
stage.sprite({ id, at, image, size, color?, opacity?, rotation?, scale?, layer?, animations? }) a PNG of the package, declared in images
stage.rect({ id, at, size, color?, radius?, opacity?, rotation?, scale?, layer?, animations? }) a filled rectangle; radius rounds the corners (a circle or a pill when it reaches half the short side)
stage.text({ id, at, text, size, color?, align?, opacity?, rotation?, scale?, layer?, animations? }) one line of at most 128 characters, in the game's font; size is in screen heights
stage.emitter({ id, at, size, lifetimeMs, maxParticles, ... }) a particle emitter (see Emitters)
stage.trigger({ id, on, target, play? | stop? }) a trigger (see Triggers)
stage.play({ target, animation }), stage.stop({ target, animation }) starts or stops an animation from a handler
stage.remove(id), stage.clear() removes an element or a trigger, or everything

color is #rgb, #rgba, #rrggbb or #rrggbbaa. size of a sprite or a rectangle is { width, height } in screen heights, each 0 to 4. opacity is 0 to 1, scale 0 to 16, rotation in degrees clockwise (-3600 to 3600).

The images a mod can use are those of defineMod({ images: [...] }): PNG files of the folder, 64 at most. All of them are decoded when a map launches, with the same limits as skin images, and packed in the playfield's atlas. A skin or a map script uses its own images. A sprite or an emitter that names an image that is not declared throws.

Positions

at is { space?, column?, x?, y? }. x and y move the point of space.

space Origin x, y
screen (default) the top-left corner of the screen fractions of the width and of the height
playfield the center of the box of the columns screen heights, to the right and down
lane the center of column column the same
receptor the receptor of column column the same

Every position but screen follows the playfield: it moves, turns and scales with it, including when a map script moves it (rotation, zoom, offsetX of the columns). x and y are within -4 to 4. column is below 32; lane and receptor need a column, except on an emitter. An emitter without a column on lane or receptor throws its particles from the column of the event that triggers it.

Layers

layer is below (behind the playfield, over the map's background), lanes (over the columns and receptors, under the notes) or above (the default, over the notes). All of them are under the HUD overlay. Within a layer, elements are drawn in the order of their scripts, then of their creation.

Emitters

An emitter throws particles: burst of them when a trigger plays its burst, and rate per second continuously.

Field Meaning Bounds
size size of a particle, in screen heights above 0, up to 0.5
image a declared PNG; a plain square without it
color, endColor color at birth and at death; endColor is color by default
lifetimeMs life of a particle above 0, up to 5000
speed, speedJitter speed in screen heights per second; speedJitter varies it by a fraction 0 to 8; 0 to 1
direction, spread center of the cone in degrees clockwise from up (0 is up); width of the cone (360 is every direction, the default) -3600 to 3600; 0 to 360
gravity acceleration in screen heights per second squared, positive downward -16 to 16
burst particles per burst 0 to 256
rate particles per second 0 to 1000
maxParticles particles alive at once; the oldest is replaced when it is full 1 to 512
fade, shrink opacity fades out, size shrinks to 0 over the life; fade is on by default, shrink off

Animations and triggers

Animations

Nothing runs in your script per frame. An element declares its animations once: animations: { name: { durationMs, easing?, repeat?, from, to } }. from and to give x, y (an offset in screen heights, -4 to 4), scale, rotation, opacity and color. A value you leave out stays the element's own, and returns to it when the animation ends. With repeat the animation starts again until it is stopped. durationMs is above 0, up to 10000. easing is linear (default), easeIn, easeOut or easeInOut. An element has 8 animations at most, and burst is a name kept for emitters. An emitter plays only burst.

Triggers

A trigger starts (play) or stops (stop) an animation of its target element when on happens. For an emitter, play: "burst" throws burst particles. on holds exactly one field:

on Happens when
{ judgement: { tier?, column?, miss? } } a note is judged; a filter you leave out matches everything
{ press: { column? } }, { release: { column? } } a lane key goes down, comes up
{ holdStart: { column? } }, { holdEnd: { column? } } a lane starts, stops holding a long note

Cues from a handler

stage.play and stage.stop do the same as a trigger from a script handler, for an event no trigger covers. They take effect from the next frame, but they go through the mods thread: they are not frame-exact like a trigger.

ctx.on("game.judgement", (judgement) => {
  if (judgement.combo > 0 && judgement.combo % 100 === 0) {
    stage.play({ target: "milestone", animation: "pulse" });
  }
});

When it runs

The render thread evaluates animations, particles and triggers at each frame, from the song clock (a pause freezes them) and from the judgements the frame shows. A judgement made by the input thread is queued, without a lock or a wait, before the game state shows it, so a burst starts in the very frame the note disappears. The renderer reads your published stage without waiting for your script, which cannot make a frame late.

Everything is drawn in the playfield's single instance batch, with the game's shader (no custom shader): a sprite, a rectangle or a particle is one quad, a text is one quad per glyph. A frame without a change of scene allocates nothing; a dead particle frees its place.

Budgets and limits

What Limit
Elements per script (emitters included) 128
Emitters per script 16
Live particles per script 1024 in all: the sum of the maxParticles, 512 at most per emitter, 256 per burst
Triggers per script 64
Cues queued by stage.play/stage.stop 64; the oldest is dropped when it is full
Animations per element 8, each up to 10 s
Text 128 characters
id (element, trigger, animation) 1 to 64 characters
at.x, at.y, animation offsets -4 to 4
size (width, height) 0 to 4
column below 32
images 64 PNG per script

Errors you will see

A value out of bounds throws with the field's name in the script. Catch it with try, as for hud.*.

Message Cause
`stage.sprite` needs the `stage` permission: declare `permissions: ["stage"]` the permission is missing from the declaration
`stage.sprite`: the player turned off native drawing for `example.stage-burst` the player withdrew the permission
image "spark.png" is not declared in `images`: only declared images are loaded a sprite or an emitter names an image missing from images
size.width: 5 is outside [0, 4] a number out of bounds (the name is the field's path)
lifetimeMs: 9000 is outside (0, 5000] a value that must be above 0 and at most its maximum
at.column: a `lane` or `receptor` position needs a column lane/receptor without column on a sprite, rectangle or text
column 40 does not exist: there are at most 32 lanes a column out of range
the stage holds 128 elements, its limit too many elements
the stage holds 16 emitters, its limit too many emitters
the stage's emitters would keep 1100 particles alive; the limit is 1024 the sum of the maxParticles is too high
the stage holds 64 triggers, its limit too many triggers
a trigger needs exactly one of `play` and `stop` a trigger with both or neither
`on` needs exactly one of judgement, press, release, holdStart and holdEnd an on with none or several events
element `miss` does not exist / element `miss` has no animation `rise` stage.play/stage.stop with an unknown target or animation
animation name `burst` is kept for emitters an animation named burst on a sprite, rectangle or text
animations: 9 animations; the limit is 8 too many animations on one element

Pitfalls

  • Call stage.clear() first when you rebuild at game.songStart. The stage of the previous play is still there until you clear or replace it.
  • Declare the PNG in images. Putting a file in the folder is not enough: only the listed images are decoded.
  • An element's opacity is its resting value. Give a trigger-driven element opacity: 0 and animate it in, as the example does for the lane pulse and the MISS text.
  • Share the particle budget: 7 emitters of 150 particles are already 1050. Size maxParticles for the burst you need, not for the maximum.
  • A receptor sprite per column needs the number of columns of the map: read it from start.playfield.lanes at game.songStart, not from a constant.
  • stage.text draws one line of at most 128 characters, in the game's font: it has no wrapping and no font option. Write text that fits.
  • Text and panels that carry information belong to hud.*; the stage is for effects.

See also

Source in the game repository: crates/modding/src/stage.rs,crates/stage/src/element.rs,crates/stage/src/scene.rs,crates/stage/src/spec.rs,content/examples/mods/stage-burst/main.ts,content/examples/mods/hit-bursts/main.ts,content/mods/sdk/modding.d.ts