FRAME beta
Variable of @v12-engine/render
FRAME = "\nstruct Frame {\n\tviewProjection: mat4x4f,\n\tcascades: array<mat4x4f, 3>,\n\teye: vec4f,\n\tsunWay: vec4f,\n\tsunColour: vec4f,\n\t// w: how far the ordered patterns are turned this frame (frameTurn)\n\tskyWay: vec4f,\n\tskyColour: vec4f,\n\tgroundColour: vec4f,\n\t// the far end of each cascade along the view\n\tsplits: vec4f,\n\t// 1 / a cascade's size in texels, a depth bias besides the cascades' own (0: theirs is in their matrices:\n\t// cascadeBias), 1 with shadows, a debug view\n\tshadow: vec4f,\n\t// 1 where a colour leaves a material linear (worldSpace), how many lamps are lit, and their slices along the\n\t// view: slice = log(depth) * z + w\n\tspace: vec4f,\n\t// the first and the last lamp that reaches into each slice, two slices a vector (none: first > last)\n\tlampSlices: array<vec4u, 8>,\n}\nstruct MistArea {\n\t// its middle (x, z), its radius twice or its half width and half depth\n\tarea: vec4f,\n\t// base, top, how thick a cell of it is, over how far it fades\n\theights: vec4f,\n\tcolour: vec4f,\n\t// how far its wisps drifted (x, z), 1 in wisps, 1 a circle\n\tdrift: vec4f,\n}\n// A lamp: its place and reach, its colour as bright as it is and a spot's power, a spot's way and the cosine of half\n// its angle (-1: a point's), the colour of its highlights\nstruct Lamp {\n\tat: vec4f,\n\tcolour: vec4f,\n\tcone: vec4f,\n\tshine: vec4f,\n}\nstruct World {\n\t// the map's width and depth in cells, 1 where the fog of war is on\n\tfog: vec4f,\n\t// beyond the map's playable bounds: the bounds (x0, z0, x1, z1), the colour (a: 1 where there are bounds),\n\t// the margins on each side, the cells over which it fades (0: the whole margin)\n\tbeyondBounds: vec4f,\n\tbeyondColour: vec4f,\n\tbeyondMargins: vec4f,\n\tbeyondFade: vec4f,\n\t// where along the view shadows end, and over how far they fade out before it\n\tshadowFade: vec4f,\n\t// the scene's fog: start, end, 1 where on\n\thaze: vec4f,\n\thazeColour: vec4f,\n\t// the clouds' shadows: drift x and z, cells a repeat, strength (0: none)\n\tcloud: vec4f,\n\t// 1 where the clouds are masked by the map's patches\n\tcloudMap: vec4f,\n\t// the mist on the ground: base, top, how thick a cell of it is at its base, 1 where there is one\n\tmistGround: vec4f,\n\t// its colour, and how many areas of mist there are\n\tmistGroundColour: vec4f,\n\tmistAreas: array<MistArea, 8>,\n}\n@group(0) @binding(0) var<uniform> frame: Frame;\n@group(0) @binding(1) var shadow0: texture_depth_2d;\n@group(0) @binding(2) var shadow1: texture_depth_2d;\n@group(0) @binding(3) var shadow2: texture_depth_2d;\n@group(0) @binding(4) var shadowSampler: sampler_comparison;\n@group(0) @binding(5) var<uniform> world: World;\n@group(0) @binding(6) var fogOfWar: texture_2d<f32>;\n@group(0) @binding(7) var mapSampler: sampler;\n@group(0) @binding(8) var cloudPicture: texture_2d<f32>;\n@group(0) @binding(9) var cloudMask: texture_2d<f32>;\n@group(0) @binding(10) var repeatSampler: sampler;\n// the lamps lit, in their order along the view, and a bit a lamp for each tile of the screen it covers\n@group(0) @binding(11) var<storage, read> lampList: array<Lamp>;\n@group(0) @binding(12) var<storage, read> lampTiles: array<u32>;\n\n// Four of the sun's depths about a place of a cascade, each 1 where the place is nearer the sun than it (the\n// comparison sampler's, a texel each): the 2 by 2 texels whose corner is at `at`, in the gather's order (x0 y1,\n// x1 y1, x1 y0, x0 y0)\nfn shadowGather(layer: i32, at: vec2f, depth: f32) -> vec4f {\n\tif (layer == 0) { return textureGatherCompare(shadow0, shadowSampler, at, depth); }\n\tif (layer == 1) { return textureGatherCompare(shadow1, shadowSampler, at, depth); }\n\treturn textureGatherCompare(shadow2, shadowSampler, at, depth);\n}\n// How much of the sun reaches a point: nine taps of the comparison sampler a texel apart, each four texels blended,\n// as the 4 by 4 texels under them read in four gathers and weighed as the nine taps weigh them (a texel of the middle\n// two rows and columns is in three taps across or down with a whole weight in all, an outer one in one, by its share\n// of it); fading to all of it over the last of the shadows' reach\nfn sunlit(at: vec3f, viewDepth: f32) -> f32 {\n\tif (frame.shadow.z < 0.5 || viewDepth > frame.splits.z) { return 1.0; }\n\tvar layer = 2;\n\tif (viewDepth < frame.splits.x) { layer = 0; } else if (viewDepth < frame.splits.y) { layer = 1; }\n\tlet place = frame.cascades[layer] * vec4f(at, 1.0);\n\tlet uv = vec2f(place.x * 0.5 + 0.5, 0.5 - place.y * 0.5);\n\tlet depth = place.z - frame.shadow.y * f32(layer + 1);\n\t// the middle tap's first texel (in texels, from 0) and how far past it the tap is\n\tlet texels = uv / frame.shadow.x - 0.5;\n\tlet first = floor(texels);\n\tlet f = texels - first;\n\t// the weights of the four columns (and rows), from the one before the middle tap's first\n\tlet low = 1.0 - f;\n\tlet a = shadowGather(layer, (first + vec2f(0.0, 0.0)) * frame.shadow.x, depth);\n\tlet b = shadowGather(layer, (first + vec2f(2.0, 0.0)) * frame.shadow.x, depth);\n\tlet c = shadowGather(layer, (first + vec2f(0.0, 2.0)) * frame.shadow.x, depth);\n\tlet d = shadowGather(layer, (first + vec2f(2.0, 2.0)) * frame.shadow.x, depth);\n\t// rows: a and b the first two (y0 = the one before, y1 = the first), c and d the last two (y0, y1 = the one after)\n\tvar lit = (a.w * low.x + a.z) * low.y + (a.x * low.x + a.y) + (b.w + b.z * f.x) * low.y + (b.x + b.y * f.x);\n\tlit += (c.w * low.x + c.z) + (c.x * low.x + c.y) * f.y + (d.w + d.z * f.x) + (d.x + d.y * f.x) * f.y;\n\tlit = lit / 9.0;\n\tlet fade = smoothstep(0.0, 1.0, clamp((world.shadowFade.x - viewDepth) / max(world.shadowFade.y, 0.0001), 0.0, 1.0));\n\treturn mix(1.0, lit, fade);\n}\n// A debug view of the frame's (Renderer.debug, the frame's shadow.w), what every kind draws in its place: 1 how\n// much of the sun reaches each pixel; 2 which of the shadows' cascades each pixel reads (the nearest red, then green,\n// then blue; past the last grey), shaded by how much of the sun reaches it (lit: the kind's own read of it); 3 the\n// wireframe's lines, shaded so; 4 what a draw adds to a pixel in the overdraw, red first, then green, then blue\n// (debugDraws.ts: the pipelines drawing a kind's sides, and adding)\nfn debugView(lit: f32, viewDepth: f32) -> vec4f {\n\tif (frame.shadow.w > 3.5) { return vec4f(1.0 / 3.0, 1.0 / 6.0, 1.0 / 12.0, 1.0); }\n\tif (frame.shadow.w > 2.5) { return vec4f(vec3f(0.45 + 0.55 * lit), 1.0); }\n\tif (frame.shadow.w < 1.5) { return vec4f(vec3f(lit), 1.0); }\n\tvar colour = vec3f(0.5);\n\tif (frame.shadow.z > 0.5) {\n\t\tif (viewDepth < frame.splits.x) { colour = vec3f(1.0, 0.3, 0.3); }\n\t\telse if (viewDepth < frame.splits.y) { colour = vec3f(0.3, 1.0, 0.3); }\n\t\telse if (viewDepth <= frame.splits.z) { colour = vec3f(0.3, 0.3, 1.0); }\n\t}\n\treturn vec4f(colour * (0.35 + 0.65 * lit), 1.0);\n}\n// How far a pattern of pixels drawn or left out (a fade's) is turned this frame, 0 to 1: an eighth more each frame, in\n// an order that keeps each far from the last, while the frames are blended (the temporal blend: each pixel on its share\n// of eight frames); 0 where they are not, the pattern standing still (patternTurn)\nfn frameTurn() -> f32 {\n\treturn frame.skyWay.w;\n}\n// How much of a colour is left by the scene's fog at a distance from the eye (1: all of it)\nfn worldHazeShare(distance: f32) -> f32 {\n\tif (world.haze.z < 0.5) { return 1.0; }\n\treturn clamp((world.haze.y - distance) / max(world.haze.y - world.haze.x, 0.0001), 0.0, 1.0);\n}\nfn worldHaze(colour: vec3f, share: f32) -> vec3f {\n\treturn mix(world.hazeColour.rgb, colour, share);\n}\n// The clouds' shadows over a place of the map (their picture's alpha, drifting), less in the scene's fog\nfn worldCloud(colour: vec3f, place: vec2f, hazeShare: f32) -> vec3f {\n\tif (world.cloud.w <= 0.0) { return colour; }\n\tvar shade = textureSampleLevel(cloudPicture, repeatSampler, (place - world.cloud.xy) / world.cloud.z, 0.0).a;\n\tif (world.cloudMap.x > 0.5) { shade *= textureSampleLevel(cloudMask, mapSampler, place / world.fog.xy, 0.0).a; }\n\treturn colour * (1.0 - shade * hazeShare * world.cloud.w);\n}\n// The fog of war over a place of the map, and the colour beyond the map's bounds\nfn worldFogOfWar(colour: vec3f, place: vec2f) -> vec3f {\n\tif (world.fog.z < 0.5) { return colour; }\n\tvar out = colour * textureSampleLevel(fogOfWar, mapSampler, place / world.fog.xy, 0.0).r;\n\tif (world.beyondColour.a > 0.5) {\n\t\tlet low = world.beyondBounds.xy - place;\n\t\tlet high = place - world.beyondBounds.zw;\n\t\tlet outside = max(max(low, high), vec2f(0.0));\n\t\tlet margin = select(world.beyondMargins.zw, world.beyondMargins.xy, low > vec2f(0.0));\n\t\tlet span = select(margin, min(vec2f(world.beyondFade.x), margin), world.beyondFade.x > 0.0);\n\t\tlet by = clamp((outside - margin + span) / max(span, vec2f(0.001)), vec2f(0.0), vec2f(1.0));\n\t\tlet t = smoothstep(0.0, 1.0, 1.0 - (1.0 - by.x) * (1.0 - by.y));\n\t\tout = mix(out, world.beyondColour.rgb, t);\n\t}\n\treturn out;\n}\n// A lit colour (gamma) into the space the picture's passes work in: linear where the picture is processed\nfn worldSpace(colour: vec3f) -> vec3f {\n\tif (frame.space.x < 0.5) { return colour; }\n\treturn pow(max(colour, vec3f(0.0)), vec3f(2.2));\n}\n// The lamps' light on a point: what each lamp of the pixel's tile and slice gives by how far it is and how the\n// point faces it, a spot's only within its cone. With a power, their highlights too (Blinn-Phong)\nstruct LampLight { diffuse: vec3f, shine: vec3f }\nfn lampsLit(at: vec3f, normal: vec3f, toEye: vec3f, power: f32) -> LampLight {\n\tvar lit = LampLight(vec3f(0.0), vec3f(0.0));\n\tif (frame.space.y < 0.5) { return lit; }\n\tlet clip = frame.viewProjection * vec4f(at, 1.0);\n\tif (clip.w <= 0.0) { return lit; }\n\tlet uv = clamp(clip.xy / clip.w * vec2f(0.5, -0.5) + 0.5, vec2f(0.0), vec2f(0.9999));\n\tlet tile = (u32(uv.y * 64.0) * 64u + u32(uv.x * 64.0)) * 8u;\n\tlet slice = u32(clamp(i32(floor(log(clip.w) * frame.space.z + frame.space.w)), 0, 15));\n\tlet pair = frame.lampSlices[slice / 2u];\n\tlet range = select(pair.xy, pair.zw, (slice & 1u) == 1u);\n\tif (range.x > range.y) { return lit; }\n\tfor (var word = range.x / 32u; word <= range.y / 32u; word++) {\n\t\tlet low = max(range.x, word * 32u) - word * 32u;\n\t\tlet high = min(range.y - word * 32u, 31u);\n\t\tvar mask = lampTiles[tile + word] & (0xffffffffu >> (31u - high)) & (0xffffffffu << low);\n\t\twhile (mask != 0u) {\n\t\t\tlet bit = firstTrailingBit(mask);\n\t\t\tmask &= mask - 1u;\n\t\t\tlet lamp = lampList[word * 32u + bit];\n\t\t\tlet to = lamp.at.xyz - at;\n\t\t\tlet far = length(to);\n\t\t\tvar reach = max(0.0, 1.0 - far / lamp.at.w);\n\t\t\tlet way = to / max(far, 0.0001);\n\t\t\tif (lamp.cone.w >= 0.0) {\n\t\t\t\tlet within = max(0.0, dot(lamp.cone.xyz, -way));\n\t\t\t\treach *= select(0.0, pow(within, lamp.colour.w), within >= lamp.cone.w);\n\t\t\t}\n\t\t\tlit.diffuse += max(dot(normal, way), 0.0) * lamp.colour.rgb * reach;\n\t\t\tif (power > 0.0) {\n\t\t\t\tlit.shine += pow(max(dot(normal, normalize(toEye + way)), 0.0), power) * lamp.shine.rgb * reach;\n\t\t\t}\n\t\t}\n\t}\n\treturn lit;\n}\n// The lamps' light alone: what a kind adds to its own after the clamp\nfn lamps(at: vec3f, normal: vec3f) -> vec3f {\n\treturn lampsLit(at, normal, vec3f(0.0), 0.0).diffuse;\n}\n// The mist (mist.ts of the client, its WGSL as it is): how much of each mist the line from the eye to a point passes\n// through, and the colour laid over by it\nfn mistHash(p: vec2f) -> f32 {\n\tlet q = bitcast<vec2u>(vec2i(floor(p))) * vec2u(1597334673u, 3812015801u);\n\tvar n = (q.x ^ q.y) * 1597334673u;\n\tn = (n >> 13u) ^ n;\n\tn = n * (n * n * 15731u + 789221u) + 1376312589u;\n\treturn f32(n) * (1.0 / 4294967296.0);\n}\nfn mistNoise(p: vec2f) -> f32 {\n\tlet i = floor(p);\n\tvar f = fract(p);\n\tf = f * f * (3.0 - 2.0 * f);\n\treturn mix(mix(mistHash(i), mistHash(i + vec2f(1.0, 0.0)), f.x), mix(mistHash(i + vec2f(0.0, 1.0)), mistHash(i + vec2f(1.0, 1.0)), f.x), f.y);\n}\nfn mistWisps(p: vec2f) -> f32 {\n\treturn (mistNoise(p) * 0.65 + mistNoise(p * 2.3 + 17.0) * 0.35) * 2.0;\n}\nfn mistGroundF(y: f32, base: f32, h: f32) -> f32 {\n\tlet u = clamp(y - base, 0.0, h);\n\treturn (min(y, base + h) - base) - (u * u) / (2.0 * h);\n}\nfn mistGroundDepth(eye: vec3f, to: vec3f, len: f32, ground: vec4f) -> f32 {\n\tlet h = max(ground.y - ground.x, 1e-3);\n\tlet dy = to.y - eye.y;\n\tvar average = clamp((ground.y - eye.y) / h, 0.0, 1.0);\n\tif (abs(dy) > 1e-4) {\n\t\taverage = (mistGroundF(to.y, ground.x, h) - mistGroundF(eye.y, ground.x, h)) / dy;\n\t}\n\treturn average * len * ground.z;\n}\nfn mistAreaDepth(eye: vec3f, dir: vec3f, len: f32, area: vec4f, heights: vec4f, drift: vec4f) -> f32 {\n\tlet soft = max(heights.w, 1e-3);\n\tvar t0 = 0.0;\n\tvar t1 = len;\n\tif (abs(dir.y) > 1e-5) {\n\t\tlet ta = (heights.x - eye.y) / dir.y;\n\t\tlet tb = (heights.y - eye.y) / dir.y;\n\t\tt0 = max(t0, min(ta, tb));\n\t\tt1 = min(t1, max(ta, tb));\n\t} else if (eye.y < heights.x || eye.y > heights.y) {\n\t\treturn 0.0;\n\t}\n\tif (drift.w > 0.5) {\n\t\tlet o = eye.xz - area.xy;\n\t\tlet d = dir.xz;\n\t\tlet r = area.z + soft;\n\t\tlet a = dot(d, d);\n\t\tlet b = dot(o, d);\n\t\tlet c = dot(o, o) - r * r;\n\t\tif (a < 1e-8) {\n\t\t\tif (c > 0.0) {\n\t\t\t\treturn 0.0;\n\t\t\t}\n\t\t} else {\n\t\t\tlet disc = b * b - a * c;\n\t\t\tif (disc < 0.0) {\n\t\t\t\treturn 0.0;\n\t\t\t}\n\t\t\tlet s = sqrt(disc);\n\t\t\tt0 = max(t0, (-b - s) / a);\n\t\t\tt1 = min(t1, (-b + s) / a);\n\t\t}\n\t} else {\n\t\tif (abs(dir.x) > 1e-5) {\n\t\t\tlet ta = (area.x - area.z - soft - eye.x) / dir.x;\n\t\t\tlet tb = (area.x + area.z + soft - eye.x) / dir.x;\n\t\t\tt0 = max(t0, min(ta, tb));\n\t\t\tt1 = min(t1, max(ta, tb));\n\t\t} else if (abs(eye.x - area.x) > area.z + soft) {\n\t\t\treturn 0.0;\n\t\t}\n\t\tif (abs(dir.z) > 1e-5) {\n\t\t\tlet ta = (area.y - area.w - soft - eye.z) / dir.z;\n\t\t\tlet tb = (area.y + area.w + soft - eye.z) / dir.z;\n\t\t\tt0 = max(t0, min(ta, tb));\n\t\t\tt1 = min(t1, max(ta, tb));\n\t\t} else if (abs(eye.z - area.y) > area.w + soft) {\n\t\t\treturn 0.0;\n\t\t}\n\t}\n\tif (t1 <= t0) {\n\t\treturn 0.0;\n\t}\n\tlet step = (t1 - t0) / 4.0;\n\tvar sum = 0.0;\n\tfor (var i = 0; i < 4; i++) {\n\t\tlet p = eye + dir * (t0 + step * (f32(i) + 0.5));\n\t\tlet edge = select(min(area.z - abs(p.x - area.x), area.w - abs(p.z - area.y)), area.z - length(p.xz - area.xy), drift.w > 0.5);\n\t\tvar w = clamp((edge + soft * 0.5) / soft, 0.0, 1.0) * clamp((heights.y - p.y) / soft, 0.0, 1.0);\n\t\tw = w * w * (3.0 - 2.0 * w);\n\t\tif (drift.z > 0.5) {\n\t\t\tw *= mistWisps((p.xz - drift.xy) / 6.0);\n\t\t}\n\t\tsum += w;\n\t}\n\treturn sum * step * heights.z;\n}\nfn worldMist(colour: vec3f, at: vec3f) -> vec3f {\n\tlet areas = i32(world.mistGroundColour.w);\n\tif (world.mistGround.w < 0.5 && areas == 0) { return colour; }\n\tlet mistFrom = frame.eye.xyz;\n\tlet mistRay = at - mistFrom;\n\tlet mistLen = length(mistRay);\n\tlet mistDir = mistRay / max(mistLen, 1e-4);\n\tvar mistDepth = 0.0;\n\tvar mistTint = vec3f(0.0);\n\tif (world.mistGround.w > 0.5) {\n\t\tlet d = mistGroundDepth(mistFrom, at, mistLen, world.mistGround);\n\t\tmistDepth += d;\n\t\tmistTint += world.mistGroundColour.rgb * d;\n\t}\n\tfor (var i = 0; i < areas; i++) {\n\t\tlet d = mistAreaDepth(mistFrom, mistDir, mistLen, world.mistAreas[i].area, world.mistAreas[i].heights, world.mistAreas[i].drift);\n\t\tmistDepth += d;\n\t\tmistTint += world.mistAreas[i].colour.rgb * d;\n\t}\n\tif (mistDepth > 1e-5) {\n\t\treturn mix(colour, mistTint / mistDepth, 1.0 - exp(-mistDepth));\n\t}\n\treturn colour;\n}\n// Every term of the world over a lit colour, in their order\nfn worldOver(colour: vec3f, at: vec3f) -> vec3f {\n\tlet share = worldHazeShare(distance(at, frame.eye.xyz));\n\treturn worldFogOfWar(worldMist(worldCloud(worldSpace(worldHaze(colour, share)), at.xz, share), at), at.xz);\n}\n"What every kind's shader of the view's pass begins with: the frame's group (0) and what is read from it. The sun through its cascades (sunlit), the lamps (lamps), and the world's terms a kind applies in this order after its own light: worldHazeShare and the scene's fog, worldSpace, worldCloud, worldMist, worldFogOfWar.
The light of a kind, as Babylon's standard material had it: the sky's and the sun's clamped to 1, the lamps' added after the clamp, all of it times the base colour: (clamp(hemi + sun) + lamps(at, normal)) * base.
Colour spaces (the client's, spec-post-lights.md A.4). A kind lights with its colours as they are stored (gamma) and applies the scene's fog so. Then worldSpace turns the colour into the space the picture's passes work in: linear (pow 2.2) where the look processes the picture (tone mapping, exposure, contrast, a vignette: the finish turns it back), unchanged where it does not, as Babylon's materials did with and without its image processing. The clouds' shadows, the mist, the fog of war and whatever else a kind lays over (the ground's rings, the sky in the water) come after worldSpace, in the picture's space, their colours taken as that space's. worldOver(colour, at) is all of it in order for a kind with nothing of its own between.