# graphics3d/toon/toonvfx (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/graphics3d/toon/toonvfx/

Base of toon 3D GPU effects. (The general-purpose part of the in-board effects that webgame-rimminer and aberrant each used separately)

Example: Show an explosion effect over a toon scene and draw one frame

```js
import { ToonRenderer, ToonVfx, ToonVfxLook, ToonVfxShape, ToonVfxDecalMode, MeshBuilder, createBoxGeometry, Matrix4, Vector3 } from "./vanilla.js";
const webGL2RenderingContext = document.createElement("canvas").getContext("webgl2");
const renderer = new ToonRenderer(webGL2RenderingContext);
const builder = new MeshBuilder();
builder.addPart(createBoxGeometry(8, 0.2, 8), Matrix4.createTranslation(0, -0.1, 0), "#708090", { hasOutline: false });
const floorMesh = renderer.createMesh(builder.readArrays());
const vfx = new ToonVfx(webGL2RenderingContext);
vfx.setRamps([["#ffffff", "#ffe080", "#ff8020", "#a02010", "#301008"]]);
vfx.prepare();
vfx.addFireball({ position: [0, 0.8, 0], radius: 0.9, life: 1.2, ramp: 0 });
vfx.addShockwave({ x: 0, z: 0, endRadius: 3, life: 0.6, ramp: 0 });
vfx.addDecal({ x: 0, z: 0, radius: 1.4, life: 8, heatSeconds: 1.5, mode: ToonVfxDecalMode.scorch, ramp: 0 });
vfx.emitParticles({ count: 40, position: [0, 0.6, 0], shape: ToonVfxShape.sphere, radius: 0.3, speed: [2, 5], gravity: 6, size: [0.05, 0.12], life: [0.4, 0.9], look: ToonVfxLook.streak, stretch: 0.05, ramp: 0, isGrounded: true });
const viewMatrix = Matrix4.createLookAt(Vector3.create(4, 3, 5), Vector3.create(0, 0.5, 0), Vector3.create(0, 1, 0));
const projectionMatrix = Matrix4.createPerspective(Math.PI / 4, 1, 0.1, 100);
vfx.update(1 / 60);
renderer.beginFrameWithMatrices(viewMatrix, projectionMatrix);
renderer.drawMesh(floorMesh, Matrix4.createIdentity());
renderer.endFrame();
vfx.draw(viewMatrix, projectionMatrix);
```

## ToonVfx

Base of toon 3D GPU effects. (The general-purpose part of the in-board effects that webgame-rimminer and aberrant each used separately)
- Texture: a single atlas built in code on the first draw, without image files. The left half is a 16-cell smoke flipbook, the right half is ToonVfxSlot cells and eight rows of color ramps.
  Ramp colors are decided by the game. (setRamps, each row is colors going from a white core to dark, an effect's ramp is the row number, -1 means color only without a ramp)
- Particles: GPU particles drawn with instancing. Written to the buffer once when emitted, and position, size and color are computed by the vertex shader from age. (Drag, gravity, stopping on the floor, streaks stretched along velocity, color ramps)
  When the cap (setParticleCapacity) is exceeded, the oldest emitted are overwritten first, and setCountRatio multiplies the emitted count by a fraction. (Phones and the lite path)
- Effect meshes: fireballs that boil with noise and cool into smoke (addFireball), shockwave rings that flow outward, tear and curl up, with a floor glow beneath (addShockwave),
  floor decals (addDecal, queueDecal), curling smoke plumes (addPlume), solid-of-revolution beams with flowing grain and a rippling surface (queueBeam),
  ribbons spread toward the camera (queueRibbon) and lightning made by winding them down (queueLightning), lightning that re-bends during its lifetime (addLightning), and point glints at a spot (queueGlint).
- Rim: the rim of glow particles, the glow and rim of ribbons, and the rim of fireballs and shockwaves cover the edges with the rim color (setRimColor) so they are visible on bright backgrounds too. (Default is additive light only)
- Brightness gain: setLookGain(look, gain) multiplies brightness per look of particles and glints. (Default 1, for making flame streaks, light rays and embers several times brighter in linear in the HDR version)
- Age and key: emitParticles, addFireball, addShockwave, addDecal, addPlume, addLightning, addFlash take options.age (seconds already elapsed, negative delays by that much) and
  options.key (event key, not emitted again if already emitted, forgotten by clear). Even if the scene re-emits events from state every frame they appear only once, and effects shown late start from that age.
  Shake (addShake) uses Shaker's exponential decay (9 per second), so passing strength x exp(-9 x age) for a late-shown event gives the same result.
- Foot shadow: queueBlobShadow is a round shade decal for that frame (ToonVfxDecalMode.shade).
- Screen: the flash (addFlash) covers the current viewport (inside the scissor if one is set) with its color at the end of draw and then fades.
  Camera shake (addShake) is computed with the engine Shaker, and the game adds the offset given by readShake to the camera's right and up.
- Which effect to emit for which event with what values (the recipe) is up to the game. The engine only takes parameters.
- Floor: if the game reports the floor height with setFloorHeightReader((x, z) => height), decals, shockwaves and smoke plumes are placed at that height, and isGrounded particles stop at that height. (0 if none)
- Clock: update(seconds) advances this base's clock. If the board stops, the effects stop too. Items passed with queue are drawn once in that frame and then cleared.
- Drawing: call draw(view, projection) after drawing the toon models. Depth test on, depth write off, blends with premultiplied alpha. Records the previous state and restores it.

Example: Fire an event only once by key even if the state re-fires it every frame

```js
import { ToonVfx, ToonVfxLook, ToonVfxShape, Matrix4, Vector3 } from "./vanilla.js";
const webGL2RenderingContext = document.createElement("canvas").getContext("webgl2");
const vfx = new ToonVfx(webGL2RenderingContext, 2000);
vfx.setRamps([["#ffffff", "#ffd070", "#ff7020", "#802010", "#200808"]]);
const viewMatrix = Matrix4.createLookAt(Vector3.create(0, 3, 6), Vector3.create(0, 0.5, 0), Vector3.create(0, 1, 0));
const projectionMatrix = Matrix4.createPerspective(Math.PI / 4, 1, 0.1, 100);
for (let frameIndex = 0; frameIndex < 3; ++frameIndex) {
	vfx.addFireball({ position: [0, 0.8, 0], radius: 0.7, life: 1, ramp: 0, key: "explosion-1" });
	vfx.emitParticles({ count: 24, position: [0, 0.8, 0], shape: ToonVfxShape.sphere, radius: 0.2, speed: [2, 4], look: ToonVfxLook.ember, ramp: 0, life: 0.8, key: "explosion-1-sparks" });
	vfx.queueBlobShadow({ x: 1, z: 0, radius: 0.4 });
	vfx.update(1 / 60);
	vfx.draw(viewMatrix, projectionMatrix);
}
const counts = vfx.getActiveCounts();
vfx.destroy();
```

- `constructor(webGL2RenderingContext, particleCapacity = TOON_VFX_DEFAULT_PARTICLE_CAPACITY)` (constructor, since 0.4.0): Constructor. (Builds the shaders, meshes and particle buffer, the atlas on the first draw)
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 context to draw effects with.
  - `particleCapacity` number: Maximum number of particles alive at once.
- `static readProgramSources()` (static, since 0.4.0): Returns the shader sources. (For prelinking with ShaderWarmer)
  - returns Array<object>: Array of { vertexShader, fragmentShader, attributeLocationTable }.

  Example: Link the effect shaders in advance when starting

  ```js
  import { ToonVfx, ShaderWarmer } from "./vanilla.js";
  const webGL2RenderingContext = document.createElement("canvas").getContext("webgl2");
  const shaderWarmer = new ShaderWarmer(webGL2RenderingContext, ToonVfx.readProgramSources());
  shaderWarmer.update();
  if (shaderWarmer.isFinished()) {
  	const vfx = new ToonVfx(webGL2RenderingContext);
  	vfx.prepare();
  }
  ```

- `createBuffer()` (method, since 0.4.0): Creates one buffer. (Adds it to the delete list)
  - returns WebGLBuffer: New buffer.
- `setRamps(ramps)` (method, since 0.4.0): Sets the color ramps. (Each row is an array of "#rrggbb" or [r, g, b] going from a white core to dark, up to eight rows, an effect's ramp follows this order)
  - `ramps` Array<Array<string | Array<number>>>: Colors per row going from a white core into darkness. (Up to eight rows, empty rows are dropped)

  Example: Give two ramp rows, fire and ice, and choose by row number

  ```js
  import { ToonVfx, ToonVfxLook } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  vfx.setRamps([
  	["#ffffff", "#ffe080", "#ff8020", "#801808", "#200804"],
  	["#ffffff", [0.7, 0.95, 1], [0.3, 0.6, 1], [0.05, 0.1, 0.3]],
  ]);
  vfx.emitParticles({ count: 20, position: [0, 1, 0], ramp: 0, look: ToonVfxLook.ember, speed: [1, 3], life: 0.8 });
  vfx.emitParticles({ count: 20, position: [2, 1, 0], ramp: 1, look: ToonVfxLook.star, speed: [0.5, 1], life: 0.6 });
  ```

- `prepare()` (method, since 0.4.0): Prebuilds the atlas. (So it does not stall on the first burst, when entering the board)
- `setParticleCapacity(particleCapacity)` (method, since 0.4.0): Sets the particle cap. (Clears alive particles)
  - `particleCapacity` number: Upper limit of particles alive at once. (1 or more)
- `getParticleCapacity()` (method, since 0.4.0): Returns the particle cap.
  - returns number: Maximum number of particles alive at once.
- `setCountRatio(countRatio)` (method, since 0.4.0): Sets the emitted particle count fraction. (Reduces only the count while keeping the composition on phones or the lite path, 0 ~ 1, at least one when emitting)
  - `countRatio` number: Fraction multiplied into the number of particles emitted. (0 ~ 1)
- `getCountRatio()` (method, since 0.4.0): Returns the emitted particle count fraction.
  - returns number: Fraction multiplied into the number of particles emitted. (0 ~ 1)
- `setGlintCapacity(glintCapacity)` (method, since 0.4.0): Sets the glint cap. (Number passed with queueGlint per frame, default 96, clears the glints passed this frame)
  - `glintCapacity` number: Upper limit of light points submitted per frame. (1 or more)
- `getGlintCapacity()` (method, since 0.4.0): Returns the glint cap.
  - returns number: Upper limit of light points submitted per frame.
- `setLookGain(look, gain)` (method, since 0.4.0): Sets the brightness gain per look. (Look of ToonVfxLook, multiplied into particle and glint brightness, default 1)
  - `look` number: Look. (ToonVfxLook)
  - `gain` number: Brightness multiplier. (0 or more, default 1)

  Example: Make only the spark streaks, light rays and embers brighter on an HDR target

  ```js
  import { ToonVfx, ToonVfxLook } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  vfx.setLookGain(ToonVfxLook.streak, 3);
  vfx.setLookGain(ToonVfxLook.star, 4);
  vfx.setLookGain(ToonVfxLook.ember, 2.5);
  const streakGain = vfx.getLookGain(ToonVfxLook.streak);
  ```

- `getLookGain(look)` (method, since 0.4.0): Returns the brightness gain per look.
  - `look` number: Look. (ToonVfxLook)
  - returns number: Brightness multiplier of that look. (1 if the look is out of range)
- `registerEventKey(key)` (method, since 0.4.0): Records an event key. (Always true if there is no key, records it and returns true for a new key, false for an already emitted key, forgotten by clear)
  - `key` number | string | undefined | null: Event key. (Always fires if none)
  - returns boolean: true if it may fire, false if the key already fired.

  Example: Do work outside the effects only once with the same event key

  ```js
  import { ToonVfx } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  let playCount = 0;
  for (let frameIndex = 0; frameIndex < 3; ++frameIndex) {
  	if (vfx.registerEventKey("door-7-open")) {
  		playCount += 1;
  	}
  }
  vfx.clear();
  const isNewAfterClear = vfx.registerEventKey("door-7-open");
  ```

- `setRimColor(rimColor)` (method, since 0.4.0): Sets the effect rim color. ([r, g, b] 0 ~ 1, edges of rimmed particles and glow ribbons, default is ToonRenderer's default outline color)
  - `rimColor` Array<number>: Rim color. ([r, g, b] 0 ~ 1)

  Example: Light grains and fireballs with a rim so they show on a bright background

  ```js
  import { ToonVfx, ToonVfxLook } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  vfx.setRimColor([0.12, 0.06, 0.03]);
  vfx.emitParticles({ count: 12, position: [0, 1, 0], look: ToonVfxLook.soft, color: [1, 0.8, 0.3], rim: 1, size: 0.3, speed: 1.5, life: 0.6 });
  vfx.addFireball({ position: [0, 1, 0], radius: 0.8, life: 1, rim: 0.7 });
  ```

- `getRimColor()` (method, since 0.4.0): Returns the effect rim color.
  - returns Array<number>: Color of the effect rim. ([r, g, b] 0 ~ 1)
- `setFloorHeightReader(floorHeightReader)` (method, since 0.4.0): Sets the floor height reader function. ((x, z) → height, 0 if null)
  - `floorHeightReader` function | null: Function returning the floor height for (x, z). (Always 0 if null)

  Example: Tell it the height of a bumpy floor

  ```js
  import { ToonVfx, ToonVfxLook } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  vfx.setFloorHeightReader((x, z) => {
  	return Math.sin(x * 0.5) * 0.3 + Math.cos(z * 0.5) * 0.2;
  });
  vfx.addDecal({ x: 1, z: 2, radius: 1, life: 4 });
  vfx.emitParticles({ count: 16, position: [1, 1, 2], isGrounded: true, gravity: 9, speed: [1, 2], look: ToonVfxLook.grit, color: [0.4, 0.3, 0.2] });
  const floorHeight = vfx.readFloorHeight(1, 2);
  vfx.setFloorHeightReader(null);
  ```

- `readFloorHeight(x, z)` (method, since 0.4.0): Floor height at that position.
  - `x` number: Scene x.
  - `z` number: Scene z.
  - returns number: Floor height at that position.
- `getTime()` (method, since 0.4.0): This base's clock. (Seconds)
  - returns number: Clock of this base. (Seconds, advanced by update)
- `getActiveCounts()` (method, since 0.4.0): Number of live effects. ({ particleCount (slots that may still be alive), fireballCount, shockwaveCount, decalCount, plumeCount, lightningCount, flashCount })
  - returns object: { particleCount, fireballCount, shockwaveCount, decalCount, plumeCount, lightningCount, flashCount }.

  Example: Check whether all effects have disappeared

  ```js
  import { ToonVfx } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  vfx.addFireball({ position: [0, 1, 0], radius: 1, life: 0.5 });
  vfx.emitParticles({ count: 10, position: [0, 1, 0], life: 0.3 });
  vfx.update(1);
  const counts = vfx.getActiveCounts();
  const isQuiet = counts.particleCount === 0 && counts.fireballCount === 0;
  ```

- `clear()` (method, since 0.4.0): Clears everything. (When the board changes, also forgets event keys)
- `clearQueue()` (method, since 0.4.0): Clears the items passed this frame. (After drawing)
- `update(deltaTime)` (method, since 0.4.0): Ages one frame. (Advances the clock, removes finished fireballs, rings, decals, plumes, lightning and flashes, damps the shake)
  - `deltaTime` number: Seconds.
- `emitParticles(options)` (method, since 0.4.0): Emits particles.
    options: { count, position([x, y, z], default [0, 0, 0]), shape(ToonVfxShape), radius, direction, spread(radians), length, isUpward, isInward,
               speed, rise, gravity, drag, size, sizeEnd(multiplied by the start size), sizeCurve, life, delay,
               color, ramp, look(ToonVfxLook), stretch, spin, floor, isGrounded, brightness, alpha, fadeIn, rim(rim of the glow look, 0 additive light only, 1 covers with the rim, negative is a faintly covering halo),
               age(seconds already elapsed, negative delays by that much), key(event key, not emitted if already emitted) }
    Range values (speed, rise, size, sizeEnd, life, delay, spin) are a single number or [min, max]. Color is [r, g, b] in 0 ~ 1, ramp is the row number (-1 if none).
    If floor is given it stops at that height, if isGrounded it stops at the floor height of the emission point. If isInward, velocity is ignored and it is sucked into the emission point as its age runs out.
  - `options` object: Emission values. (The form is the options in the header comment above)

  Example: Spark streaks bursting in a cone, ignored if fired again with the same key

  ```js
  import { ToonVfx, ToonVfxLook, ToonVfxShape } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  const sparkOptions = {
  	count: 30, position: [0, 1, 0],
  	shape: ToonVfxShape.cone, direction: [0, 1, 0], spread: 0.4,
  	speed: [3, 6], gravity: 9, drag: 1.5, size: [0.04, 0.08], sizeEnd: 0.3,
  	life: [0.5, 1], look: ToonVfxLook.streak, stretch: 0.04, color: [1, 0.7, 0.3],
  	isGrounded: true, age: 0.1, key: "spark-12",
  };
  vfx.emitParticles(sparkOptions);
  vfx.emitParticles(sparkOptions);
  ```

- `addFireball(options)` (method, since 0.4.0): Adds a fireball. (A sphere boiling with noise swells, cools into smoke and rises)
    options: { position([x, y, z], default [0, 0, 0]), radius, life, ramp, smokeColor, rise, growSeconds, boil, rim(fraction of the covering look, 0 additive light only), age, key }
  - `options` object: Fireball values. (The form is the options in the header comment above)

  Example: A fireball that swells, cools and rises

  ```js
  import { ToonVfx } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  vfx.setRamps([["#ffffff", "#ffe080", "#ff8020", "#801808", "#200804"]]);
  vfx.addFireball({ position: [0, 1, 0], radius: 1.2, life: 1.5, ramp: 0, rise: 1, growSeconds: 0.25, boil: 0.4, smokeColor: [0.2, 0.18, 0.16], key: "barrel-3" });
  ```

- `addShockwave(options)` (method, since 0.4.0): Adds a shockwave ring. (Spreads fast then slowly, the outer edge curls up and fades while torn by noise, the floor beneath brightens briefly, placed at floor height)
    options: { x, z, startRadius, endRadius, width, life, ramp, color, lift(curl-up height), height(offset above the floor), delay, rim(fraction of the covering look, 0 additive light only), age, key }
  - `options` object: Shockwave values. (The form is the options in the header comment above)

  Example: A shockwave band that spreads a little later and curls up

  ```js
  import { ToonVfx } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  vfx.addShockwave({ x: 0, z: 0, startRadius: 0.2, endRadius: 4, width: 0.8, life: 0.7, ramp: 0, color: [1, 0.9, 0.7], lift: 0.4, delay: 0.05, rim: 0.5 });
  ```

- `addDecal(options)` (method, since 0.4.0): Adds a floor decal. (Scorch cools from heat and fades in the last 1 second, can spread from startRadius to radius over growSeconds, placed at floor height)
    options: { x, z, radius, startRadius, growSeconds, life, heatSeconds, slot(ToonVfxSlot), mode(ToonVfxDecalMode), ramp, tint, alpha, angle, ring, flicker(flicker half-period in seconds, 0 none), age, key }
  - `options` object: Scorch values. (The form is the options in the header comment above, disappears at once if life is not given)

  Example: Soot that spreads and cools, and a flickering stain

  ```js
  import { ToonVfx, ToonVfxSlot, ToonVfxDecalMode } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  vfx.addDecal({ x: 0, z: 0, radius: 1.5, startRadius: 0.4, growSeconds: 0.3, life: 10, heatSeconds: 2, slot: ToonVfxSlot.scorch, mode: ToonVfxDecalMode.scorch, ramp: 0 });
  vfx.addDecal({ x: 3, z: 0, radius: 0.8, life: 6, mode: ToonVfxDecalMode.stain, tint: [0.2, 0.45, 0.1], alpha: 0.7, flicker: 0.25 });
  ```

- `queueBlobShadow(options)` (method, since 0.4.0): Passes a foot shadow drawn only this frame. (Shade fading roundly outward from alpha at the center, placed at floor height)
    options: { x, z, radius, alpha(default 0.45) }
  - `options` object: Shadow values. (The form is the options in the header comment above)

  Example: Submit a shadow under each character's feet every frame

  ```js
  import { ToonVfx, Matrix4, Vector3 } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  const viewMatrix = Matrix4.createLookAt(Vector3.create(0, 4, 6), Vector3.create(0, 0, 0), Vector3.create(0, 1, 0));
  const projectionMatrix = Matrix4.createPerspective(Math.PI / 4, 1, 0.1, 100);
  const characterPositions = [[0, 0], [1.5, -0.5], [-1, 1]];
  for (const position of characterPositions) {
  	vfx.queueBlobShadow({ x: position[0], z: position[1], radius: 0.5 });
  }
  vfx.draw(viewMatrix, projectionMatrix);
  ```

- `queueDecal(options)` (method, since 0.4.0): Passes a floor decal drawn only this frame. (For shapes decided every frame, same as addDecal but gives heat (0 ~ 1) directly instead of age, rotation and seed are 0 if not given)
  - `options` object: Scorch values. (Values of addDecal plus heat (0 ~ 1) and seed)

  Example: Submit a flowing ring and light mark at the chosen spot every frame

  ```js
  import { ToonVfx, ToonVfxSlot, ToonVfxDecalMode } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  vfx.queueDecal({ x: 0, z: 0, radius: 1.2, mode: ToonVfxDecalMode.ring, tint: [0.4, 0.9, 1], heat: 1, alpha: 1, ring: [0.8, 0.06, 3, 0.6], seed: 4 });
  vfx.queueDecal({ x: 2, z: 0, radius: 0.9, slot: ToonVfxSlot.glow, mode: ToonVfxDecalMode.glow, tint: [1, 0.6, 0.2], heat: 0.8, alpha: 0.9, angle: 0.5 });
  ```

- `addPlume(options)` (method, since 0.4.0): Adds a smoke plume. (The column rises, the cap curls up and grows, the underside of the cap heats up then cools and fades, stands at floor height)
    options: { x, z, height, capRadius, stemRadius, life, ramp, smokeColor, age, key }
  - `options` object: Smoke column values. (The form is the options in the header comment above)

  Example: A smoke column rising after a big explosion

  ```js
  import { ToonVfx } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  vfx.addPlume({ x: 0, z: 0, height: 4, capRadius: 1.6, stemRadius: 0.35, life: 5, ramp: 0, smokeColor: [0.3, 0.28, 0.26], key: "blast-9-plume" });
  ```

- `queueBeam(options)` (method, since 0.4.0): Passes a solid-of-revolution beam drawn only this frame.
    options: { start, end([x, y, z], grain flows from start toward end), radius, isCore(white light if core, otherwise the ramp's outer sheath), alpha(default 1), brightness, ramp, wobble, flowSpeed, grainRepeat(grain per unit of length), time, seed }
  - `options` object: Beam values. (The form is the options in the header comment above, submitted only if radius and alpha are above 0)

  Example: A beam with two layers, a white core and a ramp shell

  ```js
  import { ToonVfx, Matrix4, Vector3 } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  const viewMatrix = Matrix4.createLookAt(Vector3.create(4, 3, 4), Vector3.create(0, 1, -4), Vector3.create(0, 1, 0));
  const projectionMatrix = Matrix4.createPerspective(Math.PI / 4, 1, 0.1, 100);
  const start = [0, 1, 0];
  const end = [0, 1, -8];
  vfx.queueBeam({ start: start, end: end, radius: 0.12, isCore: true, alpha: 1, brightness: 1.5 });
  vfx.queueBeam({ start: start, end: end, radius: 0.3, alpha: 0.8, ramp: 0, wobble: 0.15, flowSpeed: 6, grainRepeat: 0.6, seed: 2 });
  vfx.draw(viewMatrix, projectionMatrix);
  ```

- `queueRibbon(options)` (method, since 0.4.0): Passes a ribbon drawn only this frame. (A strip of a point sequence spread toward the camera)
    options: { points([[x, y, z], ...], two or more), width, taper(0 uniform width, 1 both ends 0), color, ramp, alpha(default 1), brightness, grainRepeat, flow,
               glow(glow color [r, g, b], if given covers with a white core, that color around it and rim-colored edges, otherwise additive light), rim(when there is no glow, fraction of the look that covers the body with the rim color and keeps the core in color, 0 additive light only) }
  - `options` object: Ribbon values. (The form is the options in the header comment above, submitted only with two or more points and width and alpha (1 if omitted) above 0)

  Example: A swing trail as a blur ribbon and an additive light ribbon

  ```js
  import { ToonVfx } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  const trailPoints = [[-1, 1, 0], [-0.5, 1.3, -0.4], [0, 1.4, -0.6], [0.5, 1.3, -0.4], [1, 1, 0]];
  vfx.queueRibbon({ points: trailPoints, width: 0.25, taper: 1, alpha: 1, brightness: 1.2, glow: [0.4, 0.8, 1] });
  vfx.queueRibbon({ points: trailPoints, width: 0.1, color: [1, 0.9, 0.6], alpha: 0.8, grainRepeat: 2, flow: vfx.getTime() * 4 });
  ```

- `queueLightning(options)` (method, since 0.4.0): Passes this frame's lightning. (Several ribbons winding down around an axis, re-bends every 0.08 seconds, same shape for the same time and seed)
    options: { start, end, radius(winding radius), count, width, turns(winding turns), color, ramp, alpha, brightness, time(this base's clock if not given), seed, glow(ribbon glow color), rim(ribbon rim) }
  - `options` object: Lightning values. (The form is the options in the header comment above, radius and width must be given)

  Example: Three lightning bolts striking from the sky this frame

  ```js
  import { ToonVfx } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  vfx.queueLightning({ start: [0, 4, 0], end: [0, 0, 0], radius: 0.15, count: 3, width: 0.06, turns: 1.5, color: [0.8, 0.9, 1], alpha: 1, brightness: 1.5, seed: 7 });
  ```

- `queueGlint(options)` (method, since 0.4.0): Passes a glint drawn only this frame. (Same look as particles, stays in place without age, up to setGlintCapacity (default 96))
    options: { position([x, y, z], default [0, 0, 0]), size(default 0.2), color, ramp, look, brightness, alpha(default 1), angle, rim(same as the particle rim) }
  - `options` object: Light point values. (The form is the options in the header comment above, size must be given)

  Example: Light rays twinkling over a gem every frame

  ```js
  import { ToonVfx, ToonVfxLook } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  vfx.setGlintCapacity(32);
  const twinkle = 0.5 + 0.5 * Math.sin(vfx.getTime() * 6);
  vfx.queueGlint({ position: [0.5, 1.2, 0], size: 0.4 * twinkle, look: ToonVfxLook.star, color: [1, 0.95, 0.7], angle: 0.3, brightness: 1.5 });
  ```

- `addLightning(options)` (method, since 0.4.0): Adds lightning that re-bends during its lifetime. (Passed with queueLightning on each draw, fades in the last 0.1 seconds, appears that much later if age is negative)
    options: { start, end, life(seconds), age, radius, count, width, turns, color, ramp, alpha, brightness, seed(random if not given), glow, rim, key }
  - `options` object: Lightning values. (The form is the options in the header comment above)

  Example: Lightning that lives for 0.4 seconds, re-bending as it goes

  ```js
  import { ToonVfx, Matrix4, Vector3 } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  const viewMatrix = Matrix4.createLookAt(Vector3.create(0, 2, 6), Vector3.create(0, 2, 0), Vector3.create(0, 1, 0));
  const projectionMatrix = Matrix4.createPerspective(Math.PI / 4, 1, 0.1, 100);
  vfx.addLightning({ start: [0, 4, 0], end: [0.5, 0, 0.2], life: 0.4, count: 2, width: 0.08, radius: 0.2, glow: [0.5, 0.7, 1], key: "strike-5" });
  for (let frameIndex = 0; frameIndex < 3; ++frameIndex) {
  	vfx.update(1 / 60);
  	vfx.draw(viewMatrix, projectionMatrix);
  }
  ```

- `addFlash(options)` (method, since 0.4.0): Adds a screen flash. (Briefly covers the viewport with its color and fades quickly)
    options: { color([r, g, b], default white), strength(initial covering fraction, default 0.8), life(seconds, default 0.08), age, key }
  - `options` object: Flash values. (The form is the options in the header comment above)

  Example: Briefly cover the screen with light on an explosion

  ```js
  import { ToonVfx } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  vfx.addFlash({ color: [1, 0.9, 0.7], strength: 0.6, life: 0.12, key: "blast-9-flash" });
  ```

- `addShake(strength)` (method, since 0.4.0): Adds camera shake. (Maximum offset in scene length, computed by the engine Shaker with two phase-shifted sine waves and exponential decay, the stronger one if already shaking)
    Use CameraShake for 3 position axes and rotation, stacking and continuing by age. (applied with Camera3D.setViewOffset)
  - `strength` number: Maximum offset of the shake. (Scene length)

  Example: Add the shake to the camera's right and up

  ```js
  import { ToonVfx, Matrix4, Vector3 } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  vfx.addShake(0.2);
  vfx.update(1 / 60);
  const shake = vfx.readShake();
  const eye = Vector3.create(shake.x, 1.5 + shake.y, 6);
  const target = Vector3.create(shake.x, 1.5 + shake.y, 0);
  const viewMatrix = Matrix4.createLookAt(eye, target, Vector3.create(0, 1, 0));
  const isStill = !vfx.isShaking();
  ```

- `readShake()` (method, since 0.4.0): Current camera shake. (Scene length, x toward the camera's right and y toward the camera's up, added to both the camera position and the look-at point)
  - returns Vector2: Shake offset. (Scene length, x is the camera's right, y is the camera's up)
- `isShaking()` (method, since 0.4.0): Whether the camera is shaking.
  - returns boolean: true while shaking.
- `draw(viewMatrix, projectionMatrix)` (method, since 0.4.0): Draws. (In the order floor decals, rings, smoke plumes, fireballs, beams, ribbons, particles, flashes, living lightning is passed as this frame's ribbons, passed items are cleared after drawing)
    Reads the camera direction, right, up and position from the view matrix. If the projection is perspective, beams and ribbons face the camera position, if orthographic they face the camera direction.
  - `viewMatrix` Matrix4: View matrix of the camera.
  - `projectionMatrix` Matrix4: Projection matrix. (Perspective or orthographic)

  Example: Draw effects in an orthographic projection scene

  ```js
  import { ToonVfx, ToonVfxLook, Matrix4, Vector3 } from "./vanilla.js";
  const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
  const viewMatrix = Matrix4.createLookAt(Vector3.create(5, 5, 5), Vector3.create(0, 0, 0), Vector3.create(0, 1, 0));
  const projectionMatrix = Matrix4.createOrthographic(-4, 4, -4, 4, 0.1, 50);
  vfx.emitParticles({ count: 20, position: [0, 0.5, 0], look: ToonVfxLook.glow, color: [0.4, 0.8, 1], speed: 1, size: 0.4, life: 1 });
  vfx.queueBeam({ start: [-2, 0.5, 0], end: [2, 0.5, 0], radius: 0.1, isCore: true, alpha: 1 });
  vfx.update(0.1);
  vfx.draw(viewMatrix, projectionMatrix);
  ```

- `readFrame(viewMatrix, projectionMatrix)` (method, since 0.4.0): Camera of the frame being drawn. ({ viewProjectionElements, viewDirection(toward the camera in the scene), cameraRight, cameraUp, cameraPoint([position, 1] perspective, [camera direction, 0] orthographic) })
  - `viewMatrix` Matrix4: View matrix of the camera.
  - `projectionMatrix` Matrix4: Projection matrix. (Perspective or orthographic)
  - returns object: { viewProjectionElements, viewDirection, cameraRight, cameraUp, cameraPoint }.
- `useProgram(shaderProgram, frame)` (method, since 0.4.0): Uses the program and uploads the view, projection and atlas uniforms.
  - `shaderProgram` ShaderProgram: Effect shader to use.
  - `frame` object: Camera of the drawing frame returned by readFrame.
- `destroy()` (method, since 0.4.0): Deletes after use.

## Functions and constants

- `TOON_VFX_MAXIMUM_RAMPS = 8` (field, since 0.4.0): Maximum number of color ramp rows. (Eight rows in one ramp cell of the atlas)
  - returns number
- `TOON_VFX_DEFAULT_PARTICLE_CAPACITY = 3000` (field, since 0.4.0): Default maximum number of particles alive at once. (When exceeded, the oldest emitted are overwritten first)
  - returns number

## ToonVfxSlot (enum)

Cells of the atlas's right half. (Same as the shader's readSlot index, 6, 7, 11 ~ 15 are empty)

Example: Choose the cell to use for a floor scorch

```js
import { ToonVfx, ToonVfxSlot, ToonVfxDecalMode } from "./vanilla.js";
const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
vfx.addDecal({ x: 0, z: 0, radius: 1.2, life: 8, slot: ToonVfxSlot.dent, mode: ToonVfxDecalMode.scorch });
vfx.queueDecal({ x: 2, z: 0, radius: 1, slot: ToonVfxSlot.glow, mode: ToonVfxDecalMode.glow, tint: [1, 0.6, 0.2], heat: 1 });
```

- `soft` = `0`: Soft grain.
- `streak` = `1`: Flame streak. (The v 1 side is the head)
- `star` = `2`: Light ray.
- `shockBand` = `3`: Shock ring. (u is a tile continuing around the circumference, v goes from inner 0 to outer 1)
- `beamGrain` = `4`: Beam grain. (Tiled in both directions)
- `scorch` = `5`: Scorch. (Alpha is the darkening cover fraction, the red channel is heat)
- `ramps` = `8`: Eight color ramp rows.
- `dent` = `9`: Cratered ground.
- `glow` = `10`: Wide halo glow.

## ToonVfxLook (enum)

Particle look.

Example: Covering smoke and soil grains that stop on the floor

```js
import { ToonVfx, ToonVfxLook } from "./vanilla.js";
const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
vfx.emitParticles({ count: 8, position: [0, 0.5, 0], look: ToonVfxLook.smoke, color: [0.5, 0.48, 0.45], speed: 0.4, rise: 0.8, size: 0.6, sizeEnd: 2.5, life: [1.5, 2.5], spin: [-1, 1] });
vfx.emitParticles({ count: 20, position: [0, 0.2, 0], look: ToonVfxLook.grit, color: [0.35, 0.28, 0.2], speed: [2, 4], gravity: 9, isUpward: true, isGrounded: true, size: 0.06, life: 1.2 });
```

- `soft` = `0`: Soft grain. (Additive light)
- `streak` = `1`: Flame streak stretched along velocity. (Additive light, stretch is the stretch multiplied by velocity)
- `smoke` = `2`: Smoke flipbook. (Covering)
- `star` = `3`: Light ray. (Additive light)
- `ember` = `4`: Flickering ember. (Additive light)
- `glow` = `5`: Wide halo glow. (Additive light)
- `grit` = `6`: Non-emissive rock and dirt grain. (Covering)

## ToonVfxShape (enum)

Particle emission shape.

Example: Dust spreading in a floor ring and lights lined up along a line

```js
import { ToonVfx, ToonVfxLook, ToonVfxShape } from "./vanilla.js";
const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
vfx.emitParticles({ count: 32, position: [0, 0.1, 0], shape: ToonVfxShape.ring, radius: 0.5, speed: 3, drag: 4, look: ToonVfxLook.soft, size: 0.15, life: 0.5 });
vfx.emitParticles({ count: 40, position: [0, 1, 0], shape: ToonVfxShape.line, direction: [1, 0, 0], length: 5, radius: 0.05, speed: 0.5, look: ToonVfxLook.glow, size: 0.3, life: 0.4 });
```

- `point` = `point`: From one point in all directions.
- `sphere` = `sphere`: Outward within a ball of radius radius.
- `ring` = `ring`: Outward on a ring parallel to the floor.
- `disc` = `disc`: Outward within a disc parallel to the floor.
- `cone` = `cone`: Cone within the spread angle centered on direction.
- `line` = `line`: Toward direction, from a line along direction up to length (within radius around it).

## ToonVfxDecalMode (enum)

Floor decal mode.

Example: An oily sheen mark and a shadow under the feet

```js
import { ToonVfx, ToonVfxSlot, ToonVfxDecalMode } from "./vanilla.js";
const vfx = new ToonVfx(document.createElement("canvas").getContext("webgl2"));
vfx.addDecal({ x: 0, z: 0, radius: 1, life: 6, slot: ToonVfxSlot.soft, mode: ToonVfxDecalMode.sheen, tint: [0.05, 0.05, 0.08] });
vfx.queueDecal({ x: 2, z: 0, radius: 0.6, mode: ToonVfxDecalMode.shade, alpha: 0.5 });
```

- `scorch` = `0`: Darkens by alpha, and heat cools in the cracks of the red channel.
- `glow` = `1`: Additive light by the cell's alpha.
- `ring` = `2`: Noise rim flowing around the circumference. (ring is the radius fractions of (rim radius, rim width, noise fineness, flow speed))
- `stain` = `3`: Covers with the tint color by the cell's alpha, and the cell's center density (around the rim) is tinted toward the rim color (setRimColor). (Stains on bright floors, frost)
- `sheen` = `4`: Covers the cell's alpha body with tint and puts a white sheen around the body's edges. (Oil sheen)
- `shade` = `5`: Shade fading roundly from the center outward. (Darkens by alpha, ignores the cell, foot shadow queueBlobShadow)
