Drawing your own
When the client's drawing isn't enough: renderers of your own, phases in the frame, replacing what the client draws (shots, items, units), and the toolkit a view draws with (particles, clips, timers, debris).
Quick start
import { cylinder, type GameView, type LiteRendererFactory } from '@v12-engine/client'
// made once from the render kit (`LiteRenderKit`) as the world stands
const flags: LiteRendererFactory = (kit) => {
// every copy of one shape drawn one way is one batch: made the first time its key is asked
const poles = kit.draw.shape('flag-pole', () => cylinder({ height: 3, diameter: 0.15 }))
const copies = new Map<number, ReturnType<typeof poles.add>>()
return {
sync: (changes) => {
// every snapshot: what came into view, what left it, what happened
for (const entity of changes.removed) copies.get(entity.id)?.remove(), copies.delete(entity.id)
for (const entity of kit.state.entities.values())
if (entity.state.components?.flag && !copies.has(entity.id))
copies.set(entity.id, poles.add({ x: entity.pos.x, y: kit.terrain.heightAt(entity.pos.x, entity.pos.z) + 1.5, z: entity.pos.z }, { colour: [0.9, 0.8, 0.2] }))
},
frame: (alpha, dt) => {
/* every frame: move, paint (`copy.set`, `copy.paint`) */
},
dispose: () => poles.dispose()
}
}
export const view: GameView = { units: { looks: {} }, renderers: [flags] }The tower defense pulses the end of its road this way, the Oathbound its beacons' fires and its patrols' cones (apps/my-game/src/look/beacon.ts, alarm.ts).
Common tasks
Run code at a point of the frame
framePhases: [{ name: 'banners', after: 'render', run: (frame, client) => drawBanners(frame.dt) }]
client.frame.add(phase, { before: 'camera' }) // the same in codeShare an API between renderers
const ROADS = service<RoadsApi>('roads')
kit.provide(ROADS, api) // in one renderer
kit.require(ROADS) // in another: an error naming it where nothing provides it
kit.optional(ROADS)The client's own are services too: UNITS, MARKS, BLOOD, SPELLS, STATICS, RINGS, GROUND_RINGS.
Replace what the client draws
replace: { projectiles: myShots, groundItems: myItems, blood: null } // null: not at allAlso units, statics (trees and mines), rings, marks. A replacement of the first four provides its service (kit.provide(UNITS, …)). For items on the ground, set metadata.itemId on what you draw so the pointer picks it up.
Shoot your own projectiles
looks: { spiritTower: { ...tower, shot: { draw: ({ projectile, shooter, from, kit }) => ({ move: (at, heading) => {}, dispose: () => {} }) } } }A shot is a ball, a model, a trail, hidden, or your draw; shotFor decides per shot from the rules' look.
Burst some particles
const sparks = new Sparks(kit.fx!) // the world's effects, where it plays any
sparks.burst(x, y, z, { color: [1, 0.8, 0.3], count: 30, way: 'up' })
const smoke = sparks.stream(at, look) // until released
sparks.dispose()Play a clip on a unit
client.playClip(id, 'Roar', { seconds: 1.2, marks: [{ at: 0.55, run: () => client.renderer.kit.shake(x, z, 0.6) }], onEnd: (how) => {} })
client.renderer.kit.clipsOf(id) // its clips
client.stopClip(id) // stops it where it is: back to standing or walking, blendedloop plays it until it is stopped. stopClip stops a clip played once or a loop, and returns whether there was one; it never stops what the game plays itself (a swing, a cast, a death). onEnd is told why the clip ended (ClipStop):
how | When |
|---|---|
'done' | it played to its end |
'stopped' | stopClip (or cut.stopClip) stopped it |
'replaced' | something else took the body: another clip, an attack, a death, a fall |
releaseClip is the older call: it stops a loop only.
Freeze the picture
const unfreeze = client.freeze() // every animation, effect, particle, the weather, the sky, water and wind stand still
unfreeze() // they go on from where they were
client.frozen // whether anything holds it frozenThe pause menu freezes it while it holds the game. Freezing is the screen alone: the camera and the interface still move, and the simulation keeps its own pace (pace), so a game on a server goes on underneath. A phase of your own (framePhases) reads frame.frozen and should move the world by no time then.
Time something on screen
client.timers.after(2, () => {})
client.timers.every(0.5, tick)
client.timers.tween(1, (t) => fade(t), { ease: 'inOutQuad' })Screen only, by frames, cleared with the game. What changes the game uses world.timers.
Break a unit apart
looks: { skeleton: { ...skeleton, onDeath: (entity, death) => (death.attack ? 'debris' : 'fall') } } // debris: DebrisPiece[] says the pieces
client.renderer.kit.breakApart(at, pieces, { from, strength })Hide or recolour one part of a unit
A part of a unit's model is found by its name in the file, and hidden or tinted alone (its parts kept apart: merge: false on its model, or GameView.units.merge):
const { kit } = client.renderer
kit.partsOf(id) // the names of its model's parts
kit.setPart(id, 'Knight_HelmetVisor', { hidden: true }) // whether its model has the part
kit.setPart(id, 'Knight_Cape', { tint: [0.6, 0.1, 0.1] })
kit.setPart(id, 'Knight_HelmetVisor', undefined) // as it was
``` See [Assets › Keep a character's parts apart](/world/assets#keep-a-characters-parts-apart).
### Go further than the kit
The kit draws shapes (`kit.draw`: `box`, `cylinder`, `disc`, `plane`, `sphere`, `strip`, `torus`, `merged`), lights (`kit.lamps`) and effects (`kit.fx`). The renderer itself (`@v12-engine/render`) has no scene graph to reach into: what is drawn is batches of the client's materials, so what a game changes goes through the client's ways:
| To | See |
| ----------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| Give a model's material a colour, a picture | [Materials](/world/materials) |
| Draw a pass of your own over the picture | [Lighting › Passes of your own](/world/lighting#passes-of-your-own-addpass-gameview-world-passes) |
| Give an area of the map a look of its own | [Lighting › Picture zones](/world/lighting#picture-zones-mapenvironment-picturezones) |
| Outline or highlight what the player should see | [Selection › Outline or highlight a unit](/ui/selection#outline-or-highlight-a-unit) |
| Play a clip on a part of the body | [Assets › A clip on a part of the body](/world/assets#a-clip-on-a-part-of-the-body) |
| Make pipelines before they are first drawn | [Warm-up and first-seen hitches](/world/warm-up) |
## Reference
### `LiteRenderKit` (`kit`)
What a renderer of the game's (`renderers`, a `LiteRendererFactory`) is made with:
| Member | What |
| -------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
| `kit.state`, `kit.terrain` | the game as the player knows it, the ground (heights, the water's) |
| `kit.draw.shape(key, make, look)`, `once` | a set of copies of one shape drawn one way (`KitLook`: `unlit`, `seenThrough`, `bothSides`, `casts`…) |
| `set.add(place, paint)`, `copy.set`, `paint` | a copy placed (`KitPlace`: `x`, `y`, `z`, `yaw`, `pitch`, `roll`, `scale`) and painted (`colour`, `alpha`, `glow`) |
| `kit.lamps.add(def)` | a light besides the sun (`LightDef`), a handle back |
| `kit.fx` | the effects by name, where the world plays any |
| `kit.ring(x, z, colour, from, to)`, `rings` | a ring that grows and fades; the ground's own circles by name |
| `kit.drawn(id)`, `kit.visualOf(entity)` | where an entity is drawn now, and how tall it stands |
| `kit.root` | the game's element, for what a renderer puts on the page |
| `kit.timers`, `kit.eases`, `kit.log` | timers, eases, where it says what went wrong |
| `kit.provide`, `require`, `optional` | services |
What acts on the entities' models is the client's kit, `client.renderer.kit`: `tint`, `flash`, `setAlpha`, `hide`, `letGo`, `playClip`, `stopClip`, `clipsOf`, `onMark`, `point`, `setMaterial(id, name, def)` ([Materials](/world/materials#give-one-unit-a-material-of-its-own)), `partsOf`, `setPart`, `afterClips`, `breakApart`, `shake`, `bleed`, `sound`, `hover`. `client.outline(target, look)` outlines or highlights a unit, a doodad or an item ([Selection](/ui/selection#outline-or-highlight-a-unit)).
### Frame phases (`CLIENT_PHASES`)
`measure`, `simulate`, `apply`, `settle`, `light`, `follow`, `cinematic`, `camera`, `world`, `marks`, `render`, `overlay`, `hud`. A renderer's `frame` runs in `world`. `frame.dt` and `frame.alpha` are the frame's.
### Unit looks in code
| Field | What |
| ------------------------ | ------------------------------------------------------------------------------------------------ |
| `onDeath(entity, death)` | `'fall'`, `'shatter'`, `'debris'`, `'none'`, `'keep'`, or `{ clip, shatter, debris, strength }` |
| `modelFor`, `tintFor` | asked again as the entity's state comes in |
| `loop` | the clip it keeps playing |
| `attackClip`, `castClip` | |
| `shot`, `shotFor` | a projectile's look |
| `motion` | lunge, bob, roll, rock (0 none), and `construction` (grow out of the ground, as built, or yours) |
| `pose` | place it your way every frame, after the client |
| `illusion` | an illusion's tint, or `false` |
| `debris`, `shatter` | what it breaks into on death |
`ModelDef.tint` multiplies a model's colours once for every copy (buildings too); `tintFor` is per unit. A unit given to another player is drawn in their colour.
### Resources (trees, mines)
`worked` is what one does while worked (`sway` by default, `none`, or your code); it dies by `onDeath` and `debris`, else falls over or vanishes.
### Placement preview (`input.placement`)
The client's placement preview shows the building's own model, see-through, in the colour of whether it fits, each blocked cell marked (`GameSession.placeCells`, the engine's `placementCells`), and walls as their line of pieces. Set `{ fits, blocked, opacity, model, cells }`, or give your own `(context) => ({ show(def, point, fits, blocked?), showLine?(def, pieces), hide(), dispose() })`.
### Items on the ground (`units.items`)
`looks` and `for` (by item id or in code): a model, a colour, a size, or `hidden`. A small chest otherwise.
### Spell styles and markers
`GameView.fx.abilities.styles` by name, over the client's eleven of the same name (`ring`, `tornado`, `smash`, `tether`, `sonar`, `missile`, `burst`, `slam`, `mark`, `glow`, `barrage`), which are always there. An ability's visual names its style (`effects: { judgement: { color, style: 'campaign.holyFire' } }`); a style hears wind-up, release, arrival and every frame. Also: `GameView.units.rallyFlag` (`RallyFlagLook`: `{ model, color, scale }`, a building's `rallyFlag` in `looks.json`), `GameView.units.buffMarker` (`BuffMarkerLook`), `unitHudLook.ownerColors`. `presetView(view)` takes all the presets at once (`presetView(view, { rallyFlag: false })` leaves one out). See [Effects and particles](/fx/).
### Particles (`Sparks`)
`burst(x, y, z, look)` for what ends by itself, `stream(at, look)` until `release`d, `dispose()`. A look (`SparkLook`) says colour, count, way (`up`, `down`, `out`, `away`, `about`), size, speed, life, streaks or dust. Each is an effect of the effect system, kept to its budget.
### Loading units late
The loading screen loads the unit types the game starts with and what they make (`files.preload`, `all` for everything). `files.later` leaves big ones out of it; a unit's model is read as it is first drawn (`client.loadUnits` has nothing to do, and is kept for code that calls it). The warm-up makes ahead what would be made as a unit is first seen ([Warm-up](/world/warm-up)). See [Loading screens](/scenes/loading).