Skip to content

Make your own effect ​

An effect is a timeline of parts (particles, a model, a light, a ground mark, a sound, a shake, a flash, a trail, other effects) with knobs that tune it without copying it. Write it in the view's fx.defs, or in the editor's Effects panel, which saves fx.json.

Quick start ​

ts
import { fx, type GameView } from '@v12-engine/client'

export const view: GameView = {
	fx: {
		defs: {
			'orc.blast': {
				params: { size: 1 },
				knobs: { size: { min: 0.2, max: 4, step: 0.1, about: 'How big the blast is' } },
				parts: [
					{ particles: { bursts: [{ at: 0, count: 60 }], shape: 'sphere', speed: [4, 9], lifetime: [0.3, 0.8], size: [0.6, 0.1], color: ['#fff2c0', '#ff6020'], gravity: 4 } },
					{ light: { color: '#ffb060', intensity: (p) => 4 * p.size, range: 8, fade: 0.6 } },
					{ shake: (p) => ({ strength: 0.4 * p.size, time: 0.3 }) },
					{ at: 0.1, decal: { size: (p) => 3 * p.size, time: 40 } },
					{ at: 0.2, fx: 'orc.smoke', for: 6 }
				]
			},
			'orc.smoke': fx.particles({ rate: 30, lifetime: [2, 4], speed: [0.5, 1], size: [[0, 0.6], [1, 2.4]], color: '#30302c', blend: 'alpha', texture: 'puff', wind: 1, lit: true })
		}
	}
}

A burst of 60 sparks, a light and a shake that grow with size, a scorch mark at 0.1 s, and 6 seconds of smoke from 0.2 s. Play it with world.fx('orc.blast', { at, params: { size: 2 } }) (Show an effect).

Common tasks ​

Time the parts ​

Each part starts at seconds in and lasts for seconds, or as long as the effect. An effect lasts its length, or until its parts end; one with a never-ending part (particles at a rate with no for: a fire) runs until stopped. loop: true repeats it.

Land a spark on the blow, not the swing ​

mark: 'hit' starts a part when the unit's clip reaches that mark (the model's own mark names work too).

ts
client.fx.define('footman.blow', {
	parts: [
		{ particles: { texture: 'flare', bursts: [{ at: 0, count: 12 }], lifetime: 0.3 }, mark: 'hit', point: 'weapon' },
		{ sound: 'thud', mark: 'hit', at: 0.05 }
	]
})
kit.fx.play('footman.blow', { on: footman.id }) // as the swing begins

A part whose mark won't come (no such clip, or not on a unit) starts at its at.

Wind a ribbon around a unit ​

orbit moves a part around where the effect is; a trail draws a ribbon behind it.

ts
const orbit = {
	radius: [
		[0, 0.7],
		[1, 0.15]
	],
	turns: 0.8,
	height: [0.3, 1.5],
	bob: 0.3,
	bobs: 1
}
const parts = [
	{ for: 4, orbit, trail: { color: '#6ff2ff', width: 0.3, soft: true, blend: 'add', strength: [1, 0] } },
	{ for: 4, orbit, particles: { texture: 'flare', rate: 40, over: { rate: [1, 0] }, lifetime: [0.5, 1] } }
]

A braid of three strands climbing again and again:

ts
const strand = (i: number) => ({
	for: 6,
	orbit: { radius: 0.6, figure: 2, depth: 0.4, phase: i / 3, turns: 1.2, climb: 1.4, height: [0.1, 2] },
	trail: { color: ['#6ff2ff', '#c48bff', '#ff8fd8'][i], width: 0.25, soft: true, blend: 'add', time: 1.5, gap: 0.9, linger: 1 }
})
const braid = [strand(0), strand(1), strand(2)]

Change an emitter over its part's time ​

over keys an emitter from 0 (part start) to 1 (part end). Each value multiplies its field.

ts
import type { FxPart } from '@v12-engine/client'

export const opening: FxPart = {
	for: 2,
	particles: {
		rate: 200,
		size: [0.2, 0.6],
		shape: { type: 'ring', radius: 2 },
		over: {
			rate: [
				[0, 1],
				[0.6, 1],
				[1, 0]
			], // as many, then fewer to none
			size: [1, 2.5], // later ones bigger
			color: ['#ffffff', '#ff4020'], // and redder
			speed: [2, 0.5], // and slower
			shape: [0.2, 1] // a ring opening
		}
	}
}

A particle keeps what it took when it was let go; it doesn't grow because the part went on.

Tune with knobs ​

params are an effect's knobs and their defaults. Any field of any part can read them: size: (p) => p.size * 2. They're given when played and changed while playing (handle.set). knobs describes each for the editor. fx.extend(base, params) makes a new effect from another with its own knobs.

Draw in code ​

make draws the effect (or one part) in code, every frame, with the renderer's glowing rods, rings and plates (FxContext.glow, said again each frame):

ts
'mine.rune': {
	params: { glow: 1 },
	make: ({ anchor, params, glow }) => {
		let turn = 0
		let bright = Number(params.glow)
		return {
			update: (dt) => {
				turn += dt
				glow.ring(anchor.x, anchor.y + 0.05, anchor.z, 1, 0.08, 0.6, 0.8, 1, bright)
				glow.rod(anchor.x, anchor.y, anchor.z, anchor.x + Math.cos(turn), anchor.y, anchor.z + Math.sin(turn), 0.05, 0.6, 0.8, 1, bright, true)
			},
			set: (p) => void (bright = Number(p.glow)),
			dispose: () => {}
		}
	}
}

It gets a place that moves with the effect (anchor), the knobs, a random shared by every screen, the ground's height, whether the player sees it, the renderer (renderer) and play for effects of its own. A beam's make asks FxContext.toward() where its other end is.

Keep effects in fx.json ​

The view lays the editor's file over the code's effects, field by field:

ts
import { FX_FILE, layFiles, presetFx } from '@v12-engine/client'
import fxFile from '../fx.json'

export const view = layFiles({ units: { looks: {} }, fx: { defs: presetFx(['fire', 'smoke', 'dust']) } }, [[FX_FILE, fxFile]])

Editor ​

The Effects panel (sparkles on the right rail, or ⌘K and an effect's name):

  1. Find an effect of the game or a ready-made one, or start one From a template (a hit spark, a trail, an aura, an explosion, a buff, a projectile).
  2. Pick where it plays: on the ground or On a model, by day or at night. Move carries it round a circle to show trails.
  3. Drag its parts' bars on the timeline (their at and for); Loop, hold, slow to a tenth, or scrub.
  4. Tune Knobs and Parts: curves and colour gradients are drawn and dragged; parts are added, duplicated, reordered, removed.
  5. Measure plays it at low, medium and high and reports particles, systems and frame cost.
  6. Save the map (Ctrl+S): changes go into fx.json.

More: On a unit's model, pick a Clip and set a part's mark to time it with the blow. Vary moves every number by up to 15% by a seed (varyFx(effect, seed, amount?)). Copy as code gives a line for the view's fx. A ready-made effect is taken into the game as { "extends": "campfire" } with your knobs; As the code's removes the file's change.

Reference ​

Parts (FxPart) ​

PartWhat it is
particlesAn emitter (ParticleDef, below)
modelA glb under models/: a clip, its size over life, fade in and out, spin
lightA scene light (LightDef fields), coming up and going out
decalA ground mark that stays time seconds; count marks over spread cells, size a range
soundA game sound (GameView.sounds), heard from where it is
shakeThe view shaken, less over its time
flashThe entity it's on, in a colour for a moment
trailA ribbon behind the part as it moves (FxTrail)
fxAnother effect, by name or inline, with knobs
makeYour own, in code
Timing and placeWhat it does
at, forStart and length in seconds (for may read a knob)
markStart at a clip mark, at seconds after (PartTiming.mark)
point, offsetA point of the entity's model, and an offset from it
whenPlayed only when the knobs say: (p) => p.size > 1.5
orbitGoes round where the effect is (FxOrbit)
pathYour own way: (age, life, params) => [x, y, z], after orbit

FxDef ​

params, knobs (KnobInfo: min, max, step, about), parts or make, length, loop, offset (from where it is played, turned and sized with it), seed (the same every play), priority, range, detail, extends, tags and words for search.

FxOrbit ​

FieldWhat it does
radiusHow far out (a curve over life allowed)
turnsTurns a second
phaseWhere on the circle it starts, as a share of a turn
heightHow high (a curve allowed)
bob, bobsHow far up and down, how many times a turn
wobbleHow its circle leans
figureSwings across this many times a turn (2: a figure of eight)
depthFlattens it (radius across times this)
spinTurns the whole figure about the up axis
climbSeconds to climb its height, over and over

FxTrail ​

color, width, time (seconds of path kept, 0.5), soft (a fading band) or a tube, blend (add for light), taper, strength over the part's life, gap (a jump farther than this starts it anew), linger (seconds the old part fades).

ParticleDef ​

Sizes and speeds in cells, times in seconds; every field can read the knobs.

FieldWhat it is
shapeWhere they start: point, sphere, hemisphere, box, cone, cylinder, ring, mesh (the model), ground
rate, burstsHow many a second; how many at once ([{ at: 0, count: 80 }])
lifetime, speedA number or [least, most]
direction, spreadWhich way they set off, and how far from it
gravity, drag, noise, windPulled down, slowed, thrown about, carried by the map's wind
size, color, alpha, rotation, velocityOver life: a value, [from, to], or keys ([[0, 0.2], [0.3, 1], [1, 1.5]]); colours [r, g, b, a] or '#ff8030'
texture, flipbookdot, soft, puff, spark, flare, ring, drop, square, stain, crack, rays, streak, bubble, splat, plus, leaf, or a picture under textures/
blend, billboardadd, alpha, multiply; facing the camera, up, stretched, flat
lit, softDarker at night; faded near the ground
local, prewarmMoves with its source; already running when seen
subEmitters off each particle, at its death or all its life (birth)
overrate, size, color, speed, shape over the part's time
gpuOn the graphics card where it can: auto for 500+ alive without CPU-only fields (sub, soft, ground, flat, mesh)

over.color moves a system to the CPU. A part with no end stays at its first over key. partLife(part, length, params) says how long a part runs; partStart(part, marks) where it starts; fxLength. The quality's fx sets how many particles (a third on low).

Cost ​

  • An effect off the screen, or past its range (60 cells) from the camera's focus, isn't stepped or drawn. Nearer, particles thin with distance (client.fx.detail, GameView.fx.detail, an effect's own detail). client.fx.pick(effect, { distance, fromFocus, onScreen, detail }) decides per effect (0 to 1).
  • client.fx.budget / GameView.fx.budget caps what's alive: particles thin past it; past twice it, or past its systems, lights or effects, the least important (FxDef.priority) and farthest go first.
  • Particle systems, lights, models and trail meshes are pooled (particlePool(scene)).
  • node scripts/benchPage.mjs --scenario battle in packages/bench measures frames, GPU time and particles at each quality preset.

In code ​

readFxFile, layFx(code, file), laidFile(view, FX_FILE), plainParts(parts, params), fxCode(name, entry), fxText(file); editor.takeFile(FX_FILE, file), editor.revealEffect(name); FX_TEMPLATES. The client's own visuals (blood, shots' flames, spell styles' sparks via sparkParticles(look) and Sparks, revealed areas via REVEAL_FX, weather splashes) all play through this same system.