Skip to content

Lighting and weather ​

Lamps, torches and glowing runes that come up at night, glow, the finished picture (bloom, colour grade, ambient occlusion, passes of your own, parts of the map with a look of their own), shadows, and the sky, weather, wind, mist and clouds of a map.

Quick start ​

ts
export const view: GameView = {
	units: { looks: {} },
	ground: {
		// every torch of the shared art lights the ground from its flame, and glows more at night
		scenery: {
			'dungeon/torch.glb': {
				lights: [{ name: 'flame', offset: [0, 0.85, 0], color: [1, 0.72, 0.42], intensity: 1.4, range: 9, flicker: 0.08 }],
				glow: { day: 0.1, night: 1 }
			}
		}
	},
	world: {
		// a little colder at night, lamps blooming
		picture: { night: { tint: [0.55, 0.65, 1], tintAmount: 0.18, saturation: 0.85, bloom: 0.35, bloomThreshold: 0.75 } }
	}
}

Torches light up as the sun goes down. You need a game with a day (Rules.day), or lights that are on always, and the player's quality lights at Nearest or All (Low has none).

Common tasks ​

Put a light on a unit's model ​

ts
// a caster's staff, at the hand that holds it
visual.model = {
	...visual.model,
	lights: [{ name: 'staff', at: 'handslot.r', offset: [0, 0.35, 0], color: [0.62, 0.76, 1], intensity: 0.9, range: 4, flicker: 0.04 }]
}
// decide per unit, a few times a second: dark while being built
visual.lights = (entity, declared) => (entity.construction === undefined ? declared : [])

at is a point or a node (bone) of the model, so a torch in a hand moves with the clips. A unit's lights go out while the player can't see it.

Add a light to the map ​

ts
world.setEnvironment({
	lights: [
		{
			id: 1,
			name: 'middle rune',
			x: 64,
			z: 64,
			height: 0.6,
			color: [0.45, 0.6, 1],
			intensity: 1.6,
			range: 10,
			flicker: { amount: 0.15, speed: 0.5 },
			halo: 0.5
		}
	]
})
client.lights.named('middle rune') // find it in code

halo draws a glowing ball where the light is. In the editor, place map lights with the light tool (I).

Add a light from code ​

ts
lights: (lights) => {
	const lamp = lights.add({ x: 40, y: 3, z: 52, color: [1, 0.78, 0.5], intensity: 1.5, range: 8, flicker: 0.15 })
	return () => lamp.dispose()
} // GameView.world.lights: called once as the scene is made

const beacon = client.lights.add({ x: 40, y: 3, z: 52, color: [1, 0.3, 0.2], range: 12, when: 'always' })
beacon.set({ on: false })

Make the night darker ​

ts
world.setEnvironment({ light: { night: { sun: 0.25, ambient: 0.25 } } })

The darker the night (MapLight.night), the more lamps stand out.

Make it rain ​

ts
world.setEnvironment({ weather: { kind: 'rain', amount: 1 } }, { over: 20 }) // every player, eased in over 20 s
client.setEnvironment({ weather: { kind: 'snow', amount: 0.5 } }) // this screen only

The client's rain and snow come from presetWeather(); nothing falls of a kind the view doesn't have.

Switch to a named look ​

ts
world: {
	environments: {
		;(dusk, storm)
	}
} // GameView.world.environments
world.setEnvironment(view.world.environments.storm, { over: 20 })
gameScene({ environment: 'dusk' })

A named look replaces the sky, clouds, cloud layer, light, mist, wind, weather and water (LOOK_FIELDS), and keeps the map's sounds, music, grounds and cliffs (withEnvironment).

Set the wind ​

ts
world.setEnvironment({ wind: { around: 90, strength: 0.6, gusts: 0.3 } })

Grass sways and leans downwind (waves roll over a field: Make grass follow the wind), rain slants, snow drifts, mist drifts, and burning grass spreads downwind. Read it with client.wind (WindNow); windDirection(around) turns degrees into a vector.

Add mist ​

ts
world.setEnvironment({
	mist: { ground: { base: 0, top: 2, density: 0.4, color: [0.7, 0.75, 0.8] }, areas: [{ id: 1, shape: { region: 'swamp' }, top: 3, density: 0.8 }] }
})

Add a pass of your own over the picture ​

ts
world: {
	passes: [
		// after the picture is finished, before a cinematic's transitions and bars (`before: 'screen'`, the default)
		{ name: 'poster', wgsl: 'fn shade(uv: vec2f, colour: vec4f) -> vec4f { return vec4f(floor(colour.rgb * 5.0) / 5.0, colour.a); }' },
		// before the tone: it sees the picture linear, brighter than white; made when it is first wanted
		{ name: 'outline', make: async () => (await import('./outline.wgsl?raw')).default, before: 'tone' }
	]
} // GameView.world.passes

const scratches = client.picture.addPass(makeScratches, { before: 'end' }) // while the game runs
scratches.enabled = false
scratches.remove()

A pass is WGSL that writes fn shade(uv: vec2f, colour: vec4f) -> vec4f. Each goes before one of the client's own in the picture's chain (before): taa (before the temporal blend), tone (before the picture is finished), screen (after it, the default) or end (over the screen's bars too). It goes on once its shader is ready, so no frame waits for it or flashes black, and it keeps its place as transitions, the screen's bars and the picture's own passes come and go. It shows in the editor's view too. See Passes of your own.

Give part of the map a look of its own ​

ts
world.setEnvironment({
	pictureZones: [
		{
			id: 1,
			name: 'Bog',
			shape: { region: 'swamp' },
			blend: 12,
			look: { saturation: 0.7, tint: [0.5, 0.8, 0.4], tintAmount: 0.25 },
			fog: { start: 8, end: 50 }
		},
		{ id: 2, shape: { kind: 'circle', x: 90, z: 40, radius: 20 }, look: { bloom: 0.8 }, light: { tint: [1, 0.7, 0.5], amount: 0.6 } }
	]
})

As the camera's target goes into a zone, its look comes in over the map's: none at its edge, all of it blend cells in. In the editor, draw zones in Look › Picture. See Picture zones.

Choose which things cast shadows ​

ts
world: {
	shadows: (shadows) => {
		// heroes cast at any distance; everything else as the looks say
		shadows.casts = (caster) => (caster.mesh.metadata?.hero ? true : undefined)
	}
}

Editor ​

  1. Open the Look panel.
  2. Light sets the sun and At night; Lights sets how all map lights go (MapEnvironment.lamps).
  3. Press I for the light tool, click to place, set it in the Inspector. A doodad's light is under Its light.
  4. Glow, Picture (with Its own at night and See it at night), Shadows (Distance, Fade, Parts at most) and Weather are folds of their own.
  5. Picture also has the zones: draw one (Draw a rectangle/circle, or Over a region…), set how far it blends in, its own mist, a lean of the light and its look with the picture's own rows. Its eye button shows its look wherever the camera is. The game's own passes are listed under the zones: untick one to see the picture without it.
  6. Preset puts a named look on the map. Time of day previews an hour.

See The editor › Lights.

Reference ​

A light (LightDef) ​

FieldWhat
kindpoint, or spot with a direction, angle and focus
color0..1 each
intensityabout 0..3
rangecells it reaches
specularhow much it shines on smooth things
whenat night (default), always, between two hours ({ from: 20, to: 6 }), or a function
ona switch
flickeran amount, or { amount, speed }
prioritywhich are drawn first when not all can be
followa node it rides
haloa glowing ball this wide

handle.set(change) changes any of it, handle.dispose() removes it.

Where lights come from ​

SourceDeclaredPer thing
a modelModelDef.lights (at a point or node, offset)UnitLook.lights(entity, declared)
sceneryGameView.ground.scenery[file].lights (by offset alone)GameView.ground.sceneryLights(doodad, declared), MapDoodad.light
the mapMapEnvironment.lights (MapLightSource: id, name, x, z, height)client.lights.named(name) (<Map>Lights in the map's *.names.ts)
codeGameView.world.lights(lights)client.lights.add(def)

In the editor, map lights are editor.doc.look.addLight(at, options), look.setLight(id, change), look.removeLights(ids), each an undo step.

How lights are drawn ​

Lights are clustered (@v12-engine/render's lamps): the screen is cut into tiles and slices of depth, each knowing the lamps that reach into it, so a pixel pays only for those that reach it and no material knows how many there are. None casts a shadow. Lamps are added after the day's light is clamped, so a lamp brightens even at noon. On a 512 × 512 map at night, 500 lights cost about 0.8–1.7 ms of GPU a frame, and nothing on the CPU.

Glow (client.glow, LiteGlow) ​

What a model's maker made emissive (a lamp's glass, a skeleton's eyes) glows past its edges. Only glowing meshes are drawn again, so a map with nothing glowing pays nothing.

WhereWhat
ModelDef.glowa number, false, or { day, night, color }
GameView.ground.scenery[file].glowthe same for scenery
GameView.world.glow{ strength, night, spread }, or a function given the world's glow once (LiteGlow: look, quality, night)
MapEnvironment.glowtimes the game's

The quality's glow draws it at half the pixels, a quarter, or off.

The finished picture (client.picture) ​

After the scene is drawn: tone mapping (none, standard, aces, neutral), exposure, contrast, saturation, a tint, a colour grade (a look-up table in textures/, .3dl or a picture), bloom, vignette, sharpening (MapPictureLook). Set by GameView.world.picture (MapPicture: a look by day and one at night, blended as lamps come up), a map's MapEnvironment.picture over it, and the map's zones where the camera looks. Nothing is made unless something is asked. Passes of your own go in with picture.addPass (below). In code: picture.adjust(look, { daylight, hour }) every frame; picture.now is the look drawn now. At 1080p, tone and colours cost about 0.2 ms, bloom 1 ms, all of it 1.7 ms.

Ambient occlusion: a look's occlusion (0 none) and occlusionRadius (cells) darken creases and the feet of things (drawn at a part of the screen's pixels from a 16-bit copy of the view's depth, blurred there and brought up to the screen's pixels by depth, with no copy of the colour). The quality's occlusion: low (a sixteenth of the pixels, 8 samples) or high (a quarter, 16), picture.occlusion in code. On the Oathbound (2462 x 1964) high costs about 1.2 ms.

Passes of your own (addPass, GameView.world.passes) ​

picture.addPass(pass, options) adds a pass: its WGSL (a string, or { wgsl }), or what makes it (PicturePassMaker: told where it goes and whether it sees the picture linear; it may return a promise). GameView.world.passes (PicturePassDef: wgsl or make, and the options) adds them to every scene of the world, the editor's too.

The WGSL writes fn shade(uv: vec2f, colour: vec4f) -> vec4f: colour is the picture at uv (from the top left), what it returns is written there. It may read source (the picture, texture_2d<f32>), linear (a sampler), depth (the view's depth, texture_depth_2d), post.size and post.camera (near, far, the view's tangents), and game.size and game.time (seconds since the page began, and the frame's count). What the card refuses is said once and the pass left out; the rest of the picture is drawn. Options (PicturePassOptions):

OptionWhat it doesDefault
beforetaa, tone, screen or end: which of the client's passes it goes before'screen'
orderAmong passes at the same place: lower first, then as added0
essentialDrawn with the picture off too (QualityKnobs.picture: 'off'): a look the game can't do withoutfalse
nameWhat the editor lists it asthe pass's

The picture's chain, in order: passes before: 'taa', the temporal blend (antialias: 'taa'), passes before: 'tone', the picture's own (ambient occlusion, bloom, tone, grade, FXAA), passes before: 'screen', a cinematic's transition, the screen's frame (videos, blur, bars), passes before: 'end'. The scene's colour goes through the temporal blend and the ambient occlusion in half floats, so what is brighter than white (a torch, a fire) reaches the bloom and the tone mapping as it is.

A pass before: 'taa' gets the scene straight from its drawing, jittered, as wide as the screen. Before tone the picture is linear and can be brighter than white. Up to four passes are drawn at one place. The PicturePass you get back has ready, drawn, enabled and remove(). A maker that throws or rejects is logged and skipped. picture.passes lists them.

Picture zones (MapEnvironment.pictureZones) ​

Parts of the map with a look of their own (MapPictureZone): a shape (AreaRef: a shape of its own, or a region by name), a look over the map's picture (MapPicture, by day and night), and if it says, its own fog (MapFog fields over the map's, grown from none where the map has none) and a light lean (tint, amount: the sun and the light from all around multiplied towards the colour). Each comes in as the camera's target goes in: none at its edge, all of it blend cells in (8, PICTURE_ZONE_BLEND; 0 for at once), eased. Zones that overlap are laid in order, the later over the earlier, under adjust and a cinematic's look.

Cost: the camera's target is checked against each zone's box, and nothing is worked out or changed while it is in none: about 200 ns a frame for 64 zones (Node, Apple silicon). Inside a zone's blend the look is blended (a few microseconds), and the mist and light are set only on frames where how far in changed.

The map check says a zone over a region that isn't there, a blend outside 0 to 1000, look values out of bounds (as the map's picture's), a mist that ends before it starts, a light amount outside 0 to 1, and two zones of an id (picture-zone-*).

Shadows (GameView.world.shadows, MapEnvironment.shadows) ​

FieldDefaultWhat
culltruefalse draws every shadow
small2a thing whose longest side is at most this many cells is small
near70small things farther than this cast no shadow
smallFade10they stop one by one over this many cells before near
unitsCascades2shadow parts a unit or building is drawn into at most (0: no limit)
distance0how far shadows reach; 0 follows the view (24 to 90 cells, in steps of 8)
cascadespresetparts at most
fade12cells shadows ease out over before distance

As a function, it gets the world's shadows (LiteSceneShadows): its look, and casts(caster) told the batch and its copy, its size, distance, cascade, unit, its doodads, and kept. client.sceneShadows is the same while playing (dropped: shadows left out last frame). Shadows can also come from a coarser level of detail: see Detail and far scenery.

Weather (MapWeather, WeatherLook) ​

FieldWhat
kindrain, snow, or your own (GameView.world.weather)
amounthow much
directiondegrees around and a tilt; with the wind by default
color, opacity, size, speedthe look, as factors
splashes0 to 1, at medium quality and up
swingside to side as it falls
settlea tint over the ground, the more the more falls
sound, volumea loop, louder the more falls
areasplaces it falls (AreaRef and a share of the amount), WEATHER_AREAS (8) at most

Particles move on the processor and are drawn by the effects. A weather kind in code is a LiteWeatherFactory (given the renderer and the effects; its view has set(amount), frame, setDetail, dispose), under GameView.world.weather or liteView's weather. falling(look, weather) and fallAcross give the combined look and direction.

Changing the look over time ​

world.setEnvironment(change, { over: seconds }) eases every screen into the new look: weather, wind and mist grow or fade, numbers and colours go part way, a new kind of weather fades the old out first, and what can't blend (a sound, the light) changes at once. The sky's picture and the water switch half way. The world has the new look at once, so a save or a joining player sees the end. client.setEnvironment(change, { over }) for one screen; blendEnvironment(from, to, t) is the blend.

Sky and mist by the hour ​

GameView.world.daylight (DaylightLook: sky(hour, day), mist(hour, day)) colours the sky dome and the mist by the hour where a map names no colour. DAYLIGHT_COLORS is the client's preset (skyAtHour, mistAtHour). A sky of your own in code replaces the client's clouds (GameView.world.sky).

Cloud layer (MapEnvironment.cloudLayer, MapCloudLayer) ​

A plane at a height with clouds made in code (cloudPicture: cover, scale), drifting (drift, or with the wind), swinging (swing), over the whole map or in patches (MapCloudPatch: AreaRef, cover, softness). Its shadows darken everything under it (off at low quality). With volumetric, it is drawn as a volume at high effects quality (a box thickness cells tall, ray-marched, at half resolution); at other qualities the flat layer shows the same clouds.

Mist (MapEnvironment.mist, MapMist) ​

Beyond distance fog (MapFog): ground mist below a height (base, top, density, color), and areas (a shape or region, between two heights, with softness and drift). Every material draws it. MIST_AREAS (8) at most; areas are left out at low quality.

Wind (MapEnvironment.wind, MapWind) ​

around (degrees, as the sun's), strength (0 to 1), gusts. Read by your own parts through client.wind, CellLayerFrame.wind, or the third argument of WeatherView.frame.