@driftengine/core 4.4.2 → 4.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) hide show
  1. package/CHANGELOG.json +46 -0
  2. package/README.md +1 -1
  3. package/dist/index.d.ts +5 -1
  4. package/dist/index.js +7 -0
  5. package/dist/render/backend/api.d.ts +17 -11
  6. package/dist/render/backend/webgl2/glassTintPrograms.d.ts +13 -0
  7. package/dist/render/backend/webgl2/glassTintPrograms.js +38 -0
  8. package/dist/render/backend/webgl2/renderer.d.ts +79 -8
  9. package/dist/render/backend/webgl2/renderer.js +360 -105
  10. package/dist/render/backend/webgpu/buffers.d.ts +19 -1
  11. package/dist/render/backend/webgpu/buffers.js +27 -1
  12. package/dist/render/backend/webgpu/depthPass.d.ts +1 -1
  13. package/dist/render/backend/webgpu/depthPass.js +2 -1
  14. package/dist/render/backend/webgpu/flatPass.d.ts +2 -0
  15. package/dist/render/backend/webgpu/flatPass.js +20 -1
  16. package/dist/render/backend/webgpu/glassTintPass.d.ts +28 -0
  17. package/dist/render/backend/webgpu/glassTintPass.js +205 -0
  18. package/dist/render/backend/webgpu/gpuDrivenPass.d.ts +60 -1
  19. package/dist/render/backend/webgpu/gpuDrivenPass.js +305 -5
  20. package/dist/render/backend/webgpu/instanced.d.ts +12 -1
  21. package/dist/render/backend/webgpu/instanced.js +20 -1
  22. package/dist/render/backend/webgpu/latePass.d.ts +26 -0
  23. package/dist/render/backend/webgpu/latePass.js +133 -0
  24. package/dist/render/backend/webgpu/lightVolumePass.js +5 -1
  25. package/dist/render/backend/webgpu/motionPass.d.ts +95 -0
  26. package/dist/render/backend/webgpu/motionPass.js +491 -0
  27. package/dist/render/backend/webgpu/pipelineCache.d.ts +10 -1
  28. package/dist/render/backend/webgpu/pipelineCache.js +11 -1
  29. package/dist/render/backend/webgpu/pointGlassTintPass.d.ts +32 -0
  30. package/dist/render/backend/webgpu/pointGlassTintPass.js +193 -0
  31. package/dist/render/backend/webgpu/pointShadowPass.d.ts +35 -6
  32. package/dist/render/backend/webgpu/pointShadowPass.js +105 -20
  33. package/dist/render/backend/webgpu/postPass.d.ts +2 -12
  34. package/dist/render/backend/webgpu/renderer.d.ts +171 -54
  35. package/dist/render/backend/webgpu/renderer.js +944 -390
  36. package/dist/render/backend/webgpu/scatterPass.js +2 -1
  37. package/dist/render/backend/webgpu/skinPaletteRing.d.ts +4 -0
  38. package/dist/render/backend/webgpu/skinPaletteRing.js +4 -0
  39. package/dist/render/backend/webgpu/snapshotMips.d.ts +29 -0
  40. package/dist/render/backend/webgpu/snapshotMips.js +52 -0
  41. package/dist/render/backend/webgpu/surfaceTexturePass.d.ts +5 -0
  42. package/dist/render/backend/webgpu/surfaceTexturePass.js +1 -1
  43. package/dist/render/glass.d.ts +45 -0
  44. package/dist/render/glass.js +39 -0
  45. package/dist/render/glassCasters.d.ts +40 -0
  46. package/dist/render/glassCasters.js +84 -0
  47. package/dist/render/glassShadow.d.ts +78 -0
  48. package/dist/render/glassShadow.js +125 -0
  49. package/dist/render/globalMediumPass.d.ts +2 -3
  50. package/dist/render/globalMediumPass.js +2 -8
  51. package/dist/render/gpudriven/lit.d.ts +12 -0
  52. package/dist/render/gpudriven/lit.js +23 -0
  53. package/dist/render/gpudriven/materialTable.d.ts +15 -5
  54. package/dist/render/gpudriven/materialTable.js +26 -6
  55. package/dist/render/gpudriven/pipeline.d.ts +1 -1
  56. package/dist/render/gpudriven/pipeline.js +4 -3
  57. package/dist/render/gpudriven/sunGlass.d.ts +35 -0
  58. package/dist/render/gpudriven/sunGlass.js +89 -0
  59. package/dist/render/lightBudget.d.ts +17 -5
  60. package/dist/render/lightBudget.js +17 -5
  61. package/dist/render/pointGlassTint.d.ts +59 -0
  62. package/dist/render/pointGlassTint.js +198 -0
  63. package/dist/render/pointShadowArray.d.ts +56 -3
  64. package/dist/render/pointShadowArray.js +127 -16
  65. package/dist/render/pointShadowMap.d.ts +23 -1
  66. package/dist/render/pointShadowMap.js +27 -1
  67. package/dist/render/recon/changeFrames.d.ts +15 -0
  68. package/dist/render/recon/changeFrames.js +21 -0
  69. package/dist/render/recon/lateRouting.d.ts +14 -0
  70. package/dist/render/recon/lateRouting.js +18 -0
  71. package/dist/render/recon/mover.d.ts +57 -0
  72. package/dist/render/recon/mover.js +86 -0
  73. package/dist/render/renderQuality.d.ts +35 -0
  74. package/dist/render/renderQuality.js +7 -0
  75. package/dist/render/rendererHarness.js +12 -0
  76. package/dist/render/sceneTarget.d.ts +7 -1
  77. package/dist/render/sceneTarget.js +58 -12
  78. package/dist/render/shaders/depth.d.ts +16 -1
  79. package/dist/render/shaders/depth.js +85 -9
  80. package/dist/render/shaders/flat/directionalShadow.js +91 -6
  81. package/dist/render/shaders/flat/glassTint.d.ts +6 -0
  82. package/dist/render/shaders/flat/glassTint.js +25 -0
  83. package/dist/render/shaders/flat/index.d.ts +13 -0
  84. package/dist/render/shaders/flat/index.js +4 -0
  85. package/dist/render/shaders/flat/lampGlass.d.ts +1 -0
  86. package/dist/render/shaders/flat/lampGlass.js +183 -0
  87. package/dist/render/shaders/flat/main.d.ts +1 -1
  88. package/dist/render/shaders/flat/main.js +115 -15
  89. package/dist/render/shaders/flat/pointShadow.d.ts +1 -1
  90. package/dist/render/shaders/flat/pointShadow.js +2 -0
  91. package/dist/render/shaders/flat/preamble.js +21 -6
  92. package/dist/render/shaders/generated/depth.wgsl.d.ts +227 -4
  93. package/dist/render/shaders/generated/depth.wgsl.js +227 -4
  94. package/dist/render/shaders/generated/flat.wgsl.d.ts +360 -312
  95. package/dist/render/shaders/generated/flat.wgsl.js +376 -328
  96. package/dist/render/shaders/generated/globalMedium.wgsl.d.ts +3 -13
  97. package/dist/render/shaders/generated/globalMedium.wgsl.js +3 -13
  98. package/dist/render/shaders/generated/lightVolume.wgsl.d.ts +3 -13
  99. package/dist/render/shaders/generated/lightVolume.wgsl.js +4 -14
  100. package/dist/render/shaders/generated/octahedralResolve.wgsl.d.ts +29 -0
  101. package/dist/render/shaders/generated/octahedralResolve.wgsl.js +29 -0
  102. package/dist/render/shaders/globalMedium.js +6 -6
  103. package/dist/render/shaders/gpudriven/blendRaster.wgsl.d.ts +7 -0
  104. package/dist/render/shaders/gpudriven/blendRaster.wgsl.js +86 -1
  105. package/dist/render/shaders/gpudriven/cull.wgsl.d.ts +1 -1
  106. package/dist/render/shaders/gpudriven/cull.wgsl.js +11 -2
  107. package/dist/render/shaders/gpudriven/materialTable.wgsl.d.ts +1 -1
  108. package/dist/render/shaders/gpudriven/materialTable.wgsl.js +3 -1
  109. package/dist/render/shaders/gpudriven/shade.wgsl.js +50 -1
  110. package/dist/render/shaders/gpudriven/sunGlass.wgsl.d.ts +9 -0
  111. package/dist/render/shaders/gpudriven/sunGlass.wgsl.js +133 -0
  112. package/dist/render/shaders/lightVolume.js +6 -6
  113. package/dist/render/shaders/octahedralResolve.d.ts +8 -0
  114. package/dist/render/shaders/octahedralResolve.js +48 -17
  115. package/dist/render/shaders/recon/lateDepth.wgsl.d.ts +1 -0
  116. package/dist/render/shaders/recon/lateDepth.wgsl.js +44 -0
  117. package/dist/render/shaders/recon/motion.wgsl.d.ts +1 -1
  118. package/dist/render/shaders/recon/motion.wgsl.js +84 -0
  119. package/dist/render/shadowCasters.d.ts +20 -1
  120. package/dist/render/shadowCasters.js +15 -1
  121. package/dist/render/shadowMap.d.ts +54 -5
  122. package/dist/render/shadowMap.js +190 -36
  123. package/dist/render/sunGlassTint.d.ts +25 -0
  124. package/dist/render/sunGlassTint.js +79 -0
  125. package/dist/render/translucentQueue.d.ts +6 -0
  126. package/dist/render/translucentQueue.js +15 -1
  127. package/package.json +6 -6
  128. package/src/index.ts +10 -0
  129. package/src/render/backend/api.ts +15 -9
  130. package/src/render/backend/webgl2/glassTintPrograms.ts +58 -0
  131. package/src/render/backend/webgl2/renderer.ts +401 -108
  132. package/src/render/backend/webgpu/buffers.ts +50 -2
  133. package/src/render/backend/webgpu/depthPass.ts +2 -1
  134. package/src/render/backend/webgpu/flatPass.ts +22 -1
  135. package/src/render/backend/webgpu/glassTintPass.ts +244 -0
  136. package/src/render/backend/webgpu/gpuDrivenPass.ts +349 -5
  137. package/src/render/backend/webgpu/instanced.ts +21 -1
  138. package/src/render/backend/webgpu/latePass.ts +152 -0
  139. package/src/render/backend/webgpu/lightVolumePass.ts +5 -1
  140. package/src/render/backend/webgpu/motionPass.ts +535 -0
  141. package/src/render/backend/webgpu/pipelineCache.ts +12 -1
  142. package/src/render/backend/webgpu/pointGlassTintPass.ts +233 -0
  143. package/src/render/backend/webgpu/pointShadowPass.ts +158 -26
  144. package/src/render/backend/webgpu/renderer.ts +1108 -421
  145. package/src/render/backend/webgpu/scatterPass.ts +2 -1
  146. package/src/render/backend/webgpu/skinPaletteRing.ts +4 -0
  147. package/src/render/backend/webgpu/snapshotMips.ts +80 -0
  148. package/src/render/backend/webgpu/surfaceTexturePass.ts +1 -1
  149. package/src/render/glass.ts +74 -0
  150. package/src/render/glassCasters.ts +164 -0
  151. package/src/render/glassShadow.ts +153 -0
  152. package/src/render/globalMediumPass.ts +4 -11
  153. package/src/render/gpudriven/lit.ts +31 -0
  154. package/src/render/gpudriven/materialTable.ts +28 -6
  155. package/src/render/gpudriven/pipeline.ts +4 -3
  156. package/src/render/gpudriven/sunGlass.ts +117 -0
  157. package/src/render/lightBudget.ts +19 -5
  158. package/src/render/pointGlassTint.ts +250 -0
  159. package/src/render/pointShadowArray.ts +188 -22
  160. package/src/render/pointShadowMap.ts +41 -0
  161. package/src/render/recon/changeFrames.ts +24 -0
  162. package/src/render/recon/lateRouting.ts +29 -0
  163. package/src/render/recon/mover.ts +118 -0
  164. package/src/render/renderQuality.ts +47 -0
  165. package/src/render/rendererHarness.ts +12 -0
  166. package/src/render/sceneTarget.ts +58 -11
  167. package/src/render/shaders/depth.ts +109 -9
  168. package/src/render/shaders/flat/directionalShadow.ts +97 -6
  169. package/src/render/shaders/flat/glassTint.ts +25 -0
  170. package/src/render/shaders/flat/index.ts +17 -0
  171. package/src/render/shaders/flat/lampGlass.ts +185 -0
  172. package/src/render/shaders/flat/main.ts +115 -15
  173. package/src/render/shaders/flat/pointShadow.ts +2 -0
  174. package/src/render/shaders/flat/preamble.ts +21 -6
  175. package/src/render/shaders/generated/depth.wgsl.ts +234 -4
  176. package/src/render/shaders/generated/flat.wgsl.ts +376 -328
  177. package/src/render/shaders/generated/globalMedium.wgsl.ts +3 -13
  178. package/src/render/shaders/generated/lightVolume.wgsl.ts +4 -14
  179. package/src/render/shaders/generated/octahedralResolve.wgsl.ts +30 -0
  180. package/src/render/shaders/globalMedium.ts +6 -6
  181. package/src/render/shaders/gpudriven/blendRaster.wgsl.ts +88 -1
  182. package/src/render/shaders/gpudriven/cull.wgsl.ts +11 -2
  183. package/src/render/shaders/gpudriven/materialTable.wgsl.ts +3 -1
  184. package/src/render/shaders/gpudriven/shade.wgsl.ts +50 -1
  185. package/src/render/shaders/gpudriven/sunGlass.wgsl.ts +139 -0
  186. package/src/render/shaders/lightVolume.ts +6 -6
  187. package/src/render/shaders/octahedralResolve.ts +50 -17
  188. package/src/render/shaders/recon/lateDepth.wgsl.ts +47 -0
  189. package/src/render/shaders/recon/motion.wgsl.ts +84 -0
  190. package/src/render/shadowCasters.ts +28 -1
  191. package/src/render/shadowMap.ts +231 -37
  192. package/src/render/sunGlassTint.ts +86 -0
  193. package/src/render/translucentQueue.ts +20 -1
@@ -334,7 +334,8 @@ export function createScatterDepthBindGroupLayout(
334
334
  {
335
335
  binding: previous.texture,
336
336
  visibility: VISIBILITY_FRAGMENT,
337
- texture: { sampleType: 'unfilterable-float' },
337
+ /* The depth program's peel read, an array since the sun's maps became one. */
338
+ texture: { sampleType: 'unfilterable-float', viewDimension: '2d-array' },
338
339
  },
339
340
  {
340
341
  binding: previous.sampler,
@@ -15,6 +15,10 @@ import { SkinPaletteTexture } from './skinPaletteTexture.ts';
15
15
  * **What would make it wrong** is a genuine crowd — a stadium, a swarm of rigged birds — and the
16
16
  * fix is then the one `skinPalette.ts` names: one texture holding every palette with a per-draw
17
17
  * row offset, which spends a uniform and a shader change to stop spending textures.
18
+ *
19
+ * **A reconstructing frame spends one more per skinned mover**: last frame's pose is uploaded into
20
+ * a slot of its own for the motion pass. So the same budget is about twelve such characters with a
21
+ * reflection and three shadow layers each.
18
22
  */
19
23
  export const MAX_SKIN_PALETTES = 128;
20
24
 
@@ -0,0 +1,80 @@
1
+ /**
2
+ * The mip chain of the frame's refraction copy, recorded into the frame's own encoder.
3
+ *
4
+ * **Frosted glass reads a blurrier level of what is behind it** (`glass.ts`), so on a frame that draws
5
+ * glass the copy needs every level filled from the one above. `generateMipChain` does that for an
6
+ * upload, on an encoder of its own that it submits at once — which here would run before the frame's
7
+ * encoder had even made the copy, and would build views and bind groups on every call. So this
8
+ * makes them once per copy texture and records one pass per level wherever the copy was taken.
9
+ *
10
+ * What it costs is a render pass per level on every frame that takes the copy — about a dozen for a
11
+ * 1080p copy, each a quarter of the one before, together about a third of the copy itself — and
12
+ * nothing on a frame that shows nothing through anything. **A frame that refracts and draws no glass
13
+ * pays it too**, and that is the trade: the copy is taken at the first draw that asks and shared by
14
+ * every later one, so a glass pane drawn after a refracting one would otherwise read levels nothing
15
+ * filled. WebGL2 fills its chain on the first glass draw instead, because a `generateMipmap` there
16
+ * ends no pass. What would change it is knowing at the copy whether glass follows in the frame.
17
+ */
18
+ import type { MipPipelines } from './surfaceTexturePass.ts';
19
+ import { mipBlitPipeline } from './surfaceTexturePass.ts';
20
+
21
+ export class SnapshotMips {
22
+ private readonly pipeline: GPURenderPipeline;
23
+ private readonly targets: GPUTextureView[] = [];
24
+ private readonly groups: GPUBindGroup[] = [];
25
+
26
+ constructor(
27
+ device: GPUDevice,
28
+ pipelines: MipPipelines,
29
+ texture: GPUTexture,
30
+ format: GPUTextureFormat,
31
+ /** One layer of an array texture, where the chain is a layer's rather than the texture's. */
32
+ layer: number | null = null,
33
+ private readonly label = 'refract.mips',
34
+ ) {
35
+ this.pipeline = mipBlitPipeline(device, pipelines, format);
36
+ const sampler = device.createSampler({
37
+ label: 'refract.mips.sampler',
38
+ magFilter: 'linear',
39
+ minFilter: 'linear',
40
+ });
41
+ const layout = this.pipeline.getBindGroupLayout(0);
42
+ const at =
43
+ layer === null ? {} : { dimension: '2d' as const, baseArrayLayer: layer, arrayLayerCount: 1 };
44
+ for (let level = 1; level < texture.mipLevelCount; level++) {
45
+ const source = texture.createView({ ...at, baseMipLevel: level - 1, mipLevelCount: 1 });
46
+ this.targets.push(texture.createView({ ...at, baseMipLevel: level, mipLevelCount: 1 }));
47
+ this.groups.push(
48
+ device.createBindGroup({
49
+ label: `refract.mips.${String(level)}`,
50
+ layout,
51
+ entries: [
52
+ { binding: 0, resource: source },
53
+ { binding: 1, resource: sampler },
54
+ ],
55
+ }),
56
+ );
57
+ }
58
+ }
59
+
60
+ /** Fill every level from level 0, in `encoder`, after whatever wrote level 0 there. */
61
+ record(encoder: GPUCommandEncoder): void {
62
+ for (let i = 0; i < this.targets.length; i++) {
63
+ const pass = encoder.beginRenderPass({
64
+ label: this.label,
65
+ colorAttachments: [
66
+ {
67
+ view: this.targets[i] as GPUTextureView,
68
+ loadOp: 'clear',
69
+ storeOp: 'store',
70
+ clearValue: [0, 0, 0, 0],
71
+ },
72
+ ],
73
+ });
74
+ pass.setPipeline(this.pipeline);
75
+ pass.setBindGroup(0, this.groups[i] as GPUBindGroup);
76
+ pass.draw(3);
77
+ pass.end();
78
+ }
79
+ }
80
+ }
@@ -128,7 +128,7 @@ export function generateMipChain(
128
128
  * Cached per format, because the two differ: an `-srgb` target encodes on write, and blitting a
129
129
  * linear chain through a pipeline declared for the other one is a gamma error per level.
130
130
  */
131
- function mipBlitPipeline(
131
+ export function mipBlitPipeline(
132
132
  device: GPUDevice,
133
133
  pipelines: MipPipelines,
134
134
  format: GPUTextureFormat,
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Glass: how much light a surface lets through, how milky it is, and the colour it takes.
3
+ *
4
+ * **A thin pane, not a volume.** Glass here is a surface that shows the scene behind it — by
5
+ * `transmission`, blurred by `frost`, tinted — keeps its own highlight and reflection, and glows
6
+ * with the lights behind it in proportion to how frosted it is. The one depth it has is the path
7
+ * absorption refraction already applies (`TranslucentMeshOptions.thicknessM`). What it gives up is
8
+ * everything a solid of glass does inside itself: a sculpture or a thick block under-darkens and
9
+ * does not bend light twice. What would make it wrong is a scene whose glass is mostly that.
10
+ *
11
+ * **Zero transmission is not glass**, whatever the frost says: a surface letting nothing through is
12
+ * opaque, and every branch the shader gates on transmission is skipped for it. This module is also
13
+ * the reference the shader's Fresnel is held to (`schlickFresnel`), stated once in TypeScript.
14
+ */
15
+ import type { Vec3 } from '../math/color.ts';
16
+
17
+ export interface GlassOptions {
18
+ /** Share of light that passes, 0 (opaque) to 1 (clear). */
19
+ readonly transmission: number;
20
+ /** How milky, 0 (see-through) to 1 (fully diffusing: what is behind is blurred away). */
21
+ readonly frost: number;
22
+ /** The colour light takes through it. White, the default, is clear glass. */
23
+ readonly tint?: Vec3;
24
+ }
25
+
26
+ /** A `GlassOptions` clamped and defaulted, in a caller-owned object so a draw allocates nothing. */
27
+ export interface ResolvedGlass {
28
+ transmission: number;
29
+ frost: number;
30
+ tint: [number, number, number];
31
+ }
32
+
33
+ const clamp01 = (value: number): number => Math.min(1, Math.max(0, value));
34
+
35
+ /**
36
+ * Resolve `options` into `out`, and say whether it is glass at all.
37
+ *
38
+ * Not glass — absent, or letting nothing through — zeroes `out` and tints it white, so a binder that
39
+ * uploads `out` either way uploads the state that switches the shader's glass branch off.
40
+ */
41
+ export function resolveGlass(options: GlassOptions | undefined, out: ResolvedGlass): boolean {
42
+ const transmission = clamp01(options?.transmission ?? 0);
43
+ if (options === undefined || transmission <= 0) {
44
+ out.transmission = 0;
45
+ out.frost = 0;
46
+ out.tint[0] = 1;
47
+ out.tint[1] = 1;
48
+ out.tint[2] = 1;
49
+ return false;
50
+ }
51
+ out.transmission = transmission;
52
+ out.frost = clamp01(options.frost);
53
+ const tint = options.tint;
54
+ out.tint[0] = tint === undefined ? 1 : clamp01(tint[0]);
55
+ out.tint[1] = tint === undefined ? 1 : clamp01(tint[1]);
56
+ out.tint[2] = tint === undefined ? 1 : clamp01(tint[2]);
57
+ return true;
58
+ }
59
+
60
+ /**
61
+ * Schlick's Fresnel for glass, `F0 = 0.04`: the share of light a pane reflects at a view angle.
62
+ *
63
+ * `cosView` is clamped at 0.05, the floor the shader's path length already uses, so a silhouette
64
+ * seen exactly edge-on is a very reflective pane rather than a division by zero.
65
+ */
66
+ export function schlickFresnel(cosView: number): number {
67
+ const c = Math.min(1, Math.max(0.05, cosView));
68
+ return 0.04 + 0.96 * (1 - c) ** 5;
69
+ }
70
+
71
+ /** How much of what is behind a pane is seen through it at a view angle. */
72
+ export function glassSeenShare(transmission: number, cosView: number): number {
73
+ return transmission * (1 - schlickFresnel(cosView));
74
+ }
@@ -0,0 +1,164 @@
1
+ /**
2
+ * The glass a shadow pass was offered, kept aside to be drawn twice once the opaque casters are in:
3
+ * where the nearest pane is, and what the panes let through. See `glassShadow.ts`.
4
+ *
5
+ * **Recorded rather than drawn at once**, because the two glass passes each need the whole set and
6
+ * a pass of their own, and the caster enumeration is the consumer's to run exactly once per pass.
7
+ * A pool of entries grown on demand and never shrunk, so a steady scene records into the entries
8
+ * the frame before it made — no allocation per frame, the house rule for a per-pass path.
9
+ *
10
+ * What it keeps by reference is only what outlives the pass that recorded it: the mesh, the batch
11
+ * and its instance data, a skinned caster's palette, and the material. The model matrix is copied,
12
+ * because a caller is entitled to reuse one scratch matrix between sink calls.
13
+ */
14
+ import type { ReadonlyMat4 } from 'gl-matrix';
15
+ import type { ResolvedGlass } from './glass.ts';
16
+ import type { InstancedHandle, MeshHandle } from './backend/api.ts';
17
+ import type { MeshInstances } from './instances.ts';
18
+ import type { SceneCasterMaterial } from './shadowCasters.ts';
19
+
20
+ const MESH = 0;
21
+ const INSTANCED = 1;
22
+ const SKINNED = 2;
23
+
24
+ interface GlassEntry {
25
+ kind: number;
26
+ mesh: MeshHandle | null;
27
+ batch: InstancedHandle | null;
28
+ data: MeshInstances | null;
29
+ palette: Float32Array | null;
30
+ readonly model: Float32Array;
31
+ material: SceneCasterMaterial | undefined;
32
+ readonly glass: ResolvedGlass;
33
+ }
34
+
35
+ /** Where a replay goes: each entry with its own glass, as the depth and tint passes want it. */
36
+ export interface GlassReplaySink {
37
+ mesh(
38
+ mesh: MeshHandle,
39
+ model: ReadonlyMat4,
40
+ material: SceneCasterMaterial | undefined,
41
+ glass: ResolvedGlass,
42
+ ): void;
43
+ instanced(
44
+ batch: InstancedHandle,
45
+ data: MeshInstances,
46
+ material: SceneCasterMaterial | undefined,
47
+ glass: ResolvedGlass,
48
+ ): void;
49
+ skinnedMesh(
50
+ mesh: MeshHandle,
51
+ model: ReadonlyMat4,
52
+ palette: Float32Array,
53
+ material: SceneCasterMaterial | undefined,
54
+ glass: ResolvedGlass,
55
+ ): void;
56
+ }
57
+
58
+ export class GlassCasterList {
59
+ private readonly pool: GlassEntry[] = [];
60
+ private used = 0;
61
+
62
+ /** How many glass casters this pass has recorded. */
63
+ get count(): number {
64
+ return this.used;
65
+ }
66
+
67
+ /** How many entries the pool holds, recorded or not: what a steady scene stops growing. */
68
+ get pooled(): number {
69
+ return this.pool.length;
70
+ }
71
+
72
+ recordMesh(
73
+ mesh: MeshHandle,
74
+ model: ReadonlyMat4,
75
+ material: SceneCasterMaterial | undefined,
76
+ glass: ResolvedGlass,
77
+ ): void {
78
+ const entry = this.take(MESH, material, glass);
79
+ entry.mesh = mesh;
80
+ entry.model.set(model as ArrayLike<number>);
81
+ }
82
+
83
+ recordInstanced(
84
+ batch: InstancedHandle,
85
+ data: MeshInstances,
86
+ material: SceneCasterMaterial | undefined,
87
+ glass: ResolvedGlass,
88
+ ): void {
89
+ const entry = this.take(INSTANCED, material, glass);
90
+ entry.batch = batch;
91
+ entry.data = data;
92
+ }
93
+
94
+ recordSkinned(
95
+ mesh: MeshHandle,
96
+ model: ReadonlyMat4,
97
+ palette: Float32Array,
98
+ material: SceneCasterMaterial | undefined,
99
+ glass: ResolvedGlass,
100
+ ): void {
101
+ const entry = this.take(SKINNED, material, glass);
102
+ entry.mesh = mesh;
103
+ entry.palette = palette;
104
+ entry.model.set(model as ArrayLike<number>);
105
+ }
106
+
107
+ /** Every recorded caster, in the order it was offered. */
108
+ replay(sink: GlassReplaySink): void {
109
+ for (let i = 0; i < this.used; i++) {
110
+ const entry = this.pool[i] as GlassEntry;
111
+ if (entry.kind === INSTANCED && entry.batch !== null && entry.data !== null) {
112
+ sink.instanced(entry.batch, entry.data, entry.material, entry.glass);
113
+ } else if (entry.kind === SKINNED && entry.mesh !== null && entry.palette !== null) {
114
+ sink.skinnedMesh(entry.mesh, entry.model, entry.palette, entry.material, entry.glass);
115
+ } else if (entry.mesh !== null) {
116
+ sink.mesh(entry.mesh, entry.model, entry.material, entry.glass);
117
+ }
118
+ }
119
+ }
120
+
121
+ /** Forget this pass's casters; the entries stay for the next one. */
122
+ clear(): void {
123
+ for (let i = 0; i < this.used; i++) {
124
+ const entry = this.pool[i] as GlassEntry;
125
+ /* Released so a disposed mesh is not held alive by a list nobody will replay again. */
126
+ entry.mesh = null;
127
+ entry.batch = null;
128
+ entry.data = null;
129
+ entry.palette = null;
130
+ entry.material = undefined;
131
+ }
132
+ this.used = 0;
133
+ }
134
+
135
+ private take(
136
+ kind: number,
137
+ material: SceneCasterMaterial | undefined,
138
+ glass: ResolvedGlass,
139
+ ): GlassEntry {
140
+ let entry = this.pool[this.used];
141
+ if (entry === undefined) {
142
+ entry = {
143
+ kind,
144
+ mesh: null,
145
+ batch: null,
146
+ data: null,
147
+ palette: null,
148
+ model: new Float32Array(16),
149
+ material,
150
+ glass: { transmission: 0, frost: 0, tint: [1, 1, 1] },
151
+ };
152
+ this.pool.push(entry);
153
+ }
154
+ this.used++;
155
+ entry.kind = kind;
156
+ entry.material = material;
157
+ entry.glass.transmission = glass.transmission;
158
+ entry.glass.frost = glass.frost;
159
+ entry.glass.tint[0] = glass.tint[0];
160
+ entry.glass.tint[1] = glass.tint[1];
161
+ entry.glass.tint[2] = glass.tint[2];
162
+ return entry;
163
+ }
164
+ }
@@ -0,0 +1,153 @@
1
+ /**
2
+ * Coloured light through glass: how one pane filters light, how panes combine, which receivers a
3
+ * pane tints, and how far frost spreads the colour — the reference the shaders state term for term.
4
+ *
5
+ * **Frost spreads light, it does not remove it.** Etched glass passes 88–91% of visible light
6
+ * against clear float glass's ~90%, so a frosted pane passes `transmission × (1 − F) × tint` like a
7
+ * clear one and only the filter widens. What it gives up: the spread stays inside the opaque frame's
8
+ * shadow, so light from a frosted pane does not reach past the outline of the frame around it. What
9
+ * would make it wrong is glass that genuinely absorbs as it diffuses — opal glass, milk glass — for
10
+ * which a lower `transmission` is the honest statement.
11
+ *
12
+ * **What else it does not reach**: light that bounces. DriftRay's probe bake and DriftLight's summed
13
+ * far field see a pane as absent, so the light a tinted window sends round a room by bouncing is its
14
+ * sun's own colour; only the direct light through the pane takes the tint.
15
+ */
16
+ import { schlickFresnel, type ResolvedGlass } from './glass.ts';
17
+
18
+ /** One tint texel: the light a stack of panes lets through, and how clear the stack is. */
19
+ export interface GlassTexel {
20
+ r: number;
21
+ g: number;
22
+ b: number;
23
+ clarity: number;
24
+ }
25
+
26
+ /** No glass: everything passes, perfectly clear. What a tint map is cleared to. */
27
+ export const CLEAR_TEXEL: Readonly<GlassTexel> = { r: 1, g: 1, b: 1, clarity: 1 };
28
+
29
+ /** How widely a fully frosted pane spreads its colour: a 30° half-angle cone behind it. */
30
+ export const FROST_SPREAD = Math.tan(Math.PI / 6);
31
+
32
+ /**
33
+ * The spread never exceeds this many base filter radii, and the tap count never changes.
34
+ *
35
+ * **Sixty-four, because the tint is read from the mip level the spread asks for**: each tap is then
36
+ * already an average over its own share of the spread, so taps far apart do not alias into grain.
37
+ * It was eight before the tint had mips, and on a sun map a few centimetres a texel that capped a
38
+ * frosted pane's spread at seven centimetres — frost that nobody saw. What the cap still bounds is
39
+ * a pane so frosted and so far above a floor that its colour would wash over a room.
40
+ */
41
+ export const FROST_RADIUS_CAP = 64;
42
+
43
+ /** One pane as a tint texel, lit at `cosLight` to its normal. Fresnel as `glass.ts` states it. */
44
+ export function paneTexel(glass: ResolvedGlass, cosLight: number, out: GlassTexel): GlassTexel {
45
+ const passes = glass.transmission * (1 - schlickFresnel(cosLight));
46
+ out.r = passes * glass.tint[0];
47
+ out.g = passes * glass.tint[1];
48
+ out.b = passes * glass.tint[2];
49
+ out.clarity = 1 - glass.frost;
50
+ return out;
51
+ }
52
+
53
+ /** Two stacks of glass on one ray: a product, so the order they are drawn in cannot matter. */
54
+ export function combinePanes(a: GlassTexel, b: GlassTexel, out: GlassTexel): GlassTexel {
55
+ out.r = a.r * b.r;
56
+ out.g = a.g * b.g;
57
+ out.b = a.b * b.b;
58
+ out.clarity = a.clarity * b.clarity;
59
+ return out;
60
+ }
61
+
62
+ /**
63
+ * What one filter tap contributes: the glass's texel where the receiver is behind the pane, and
64
+ * nothing where it is on the light's side of it — or is the pane, which the bias keeps untinted.
65
+ */
66
+ export function tapTint(
67
+ receiverDepth: number,
68
+ glassDepth: number,
69
+ bias: number,
70
+ glass: GlassTexel,
71
+ out: GlassTexel,
72
+ ): GlassTexel {
73
+ const behind = receiverDepth > glassDepth + bias;
74
+ out.r = behind ? glass.r : 1;
75
+ out.g = behind ? glass.g : 1;
76
+ out.b = behind ? glass.b : 1;
77
+ out.clarity = behind ? glass.clarity : 1;
78
+ return out;
79
+ }
80
+
81
+ /**
82
+ * A frosted pane's tint at a receiver: the outline from the taps at the base radius, the colour
83
+ * from the spread taps that found glass — nothing else.
84
+ *
85
+ * **Split because a tint cannot carry light out past a pane.** Blurring the tint map whole pulls in
86
+ * the white of the unobstructed ground around a pane, which brightens the patch — measured at 10%
87
+ * more light over a frosted window than a clear one — while the pane's own light, which should
88
+ * spread onto that ground, has no way to exceed full sun there. So the outline stays where the
89
+ * clear pane's is (`coverage`, the share of outline taps behind glass) and only the colour spreads,
90
+ * averaged over glass alone (`colourSum` over `colourCount`, the summed pane shares `unmixTint`
91
+ * reports — continuous, so a colour crossing between panes blends rather than stepping): neighbouring
92
+ * panes mix, a pattern blurs, and the light through the patch is what it was. What it gives up is
93
+ * spread past the outline, which the spec states; with no glass found by the spread the colour is
94
+ * clear.
95
+ */
96
+ export function spreadTint(
97
+ coverage: number,
98
+ colourSum: GlassTexel,
99
+ colourCount: number,
100
+ out: GlassTexel,
101
+ ): GlassTexel {
102
+ const r = colourCount > 0 ? colourSum.r / colourCount : 1;
103
+ const g = colourCount > 0 ? colourSum.g / colourCount : 1;
104
+ const b = colourCount > 0 ? colourSum.b / colourCount : 1;
105
+ out.r = 1 + (r - 1) * coverage;
106
+ out.g = 1 + (g - 1) * coverage;
107
+ out.b = 1 + (b - 1) * coverage;
108
+ out.clarity = 1;
109
+ return out;
110
+ }
111
+
112
+ /**
113
+ * A coarse level of the tint, with the open ground's white taken back out: returns the pane's share
114
+ * of the texel and writes the pane's own colour to `out`.
115
+ *
116
+ * **Why a level needs this**: it averages a pane with whatever lies beside it, and beside a pane is
117
+ * usually open ground, which the tint holds as white — so a frosted patch read coarse is paler than
118
+ * the pane, and measured 6% more light than the clear pane over the same floor. The clarity channel
119
+ * says how much of the texel was pane: open ground holds 1, the pane its own `centreClarity`, so
120
+ * the pane's share is `(1 − a) / (1 − centreClarity)` and its colour follows by subtraction. What it
121
+ * assumes is that the frost around a receiver is the frost at its centre, which is true within a
122
+ * pane and blends where two panes of different frost meet; a clear pane (`centreClarity` 1) is
123
+ * never read coarse, so it is returned as it stands.
124
+ */
125
+ export function unmixTint(mip: GlassTexel, centreClarity: number, out: GlassTexel): number {
126
+ if (centreClarity >= 1) {
127
+ out.r = mip.r;
128
+ out.g = mip.g;
129
+ out.b = mip.b;
130
+ out.clarity = mip.clarity;
131
+ return 1;
132
+ }
133
+ const share = Math.min(1, Math.max(0, (1 - mip.clarity) / (1 - centreClarity)));
134
+ if (share <= 0) {
135
+ out.r = 1;
136
+ out.g = 1;
137
+ out.b = 1;
138
+ out.clarity = 1;
139
+ return 0;
140
+ }
141
+ const ground = 1 - share;
142
+ out.r = Math.min(1, Math.max(0, (mip.r - ground) / share));
143
+ out.g = Math.min(1, Math.max(0, (mip.g - ground) / share));
144
+ out.b = Math.min(1, Math.max(0, (mip.b - ground) / share));
145
+ out.clarity = centreClarity;
146
+ return share;
147
+ }
148
+
149
+ /** The filter radius for a receiver `paneToReceiver` behind a pane of this clarity. */
150
+ export function frostRadius(clarity: number, paneToReceiver: number, baseRadius: number): number {
151
+ const spread = (1 - clarity) * FROST_SPREAD * Math.max(paneToReceiver, 0);
152
+ return Math.min(Math.max(baseRadius, spread), baseRadius * FROST_RADIUS_CAP);
153
+ }
@@ -19,9 +19,8 @@ export interface MediumLight {
19
19
  readonly sunShadow: number;
20
20
  /** World → light clip, **uncorrected**: the shader does its own `* 0.5 + 0.5`. */
21
21
  readonly lightViewProj: ReadonlyMat4;
22
- readonly staticShadowMap: WebGLTexture;
23
- readonly peeledShadowMap: WebGLTexture;
24
- readonly dynamicShadowMap: WebGLTexture;
22
+ /** The sun's static, moving and peeled maps, as one array. See `shadowMap.ts`. */
23
+ readonly sunShadows: WebGLTexture;
25
24
  readonly peeledEnabled: boolean;
26
25
  }
27
26
 
@@ -210,14 +209,8 @@ export class GlobalMediumPass {
210
209
  gl.bindTexture(gl.TEXTURE_2D, depth);
211
210
  gl.uniform1i(u['uDepth'] ?? null, 0);
212
211
  gl.activeTexture(gl.TEXTURE1);
213
- gl.bindTexture(gl.TEXTURE_2D, light.staticShadowMap);
214
- gl.uniform1i(u['uStaticShadowMap'] ?? null, 1);
215
- gl.activeTexture(gl.TEXTURE2);
216
- gl.bindTexture(gl.TEXTURE_2D, light.peeledShadowMap);
217
- gl.uniform1i(u['uPeeledShadowMap'] ?? null, 2);
218
- gl.activeTexture(gl.TEXTURE3);
219
- gl.bindTexture(gl.TEXTURE_2D, light.dynamicShadowMap);
220
- gl.uniform1i(u['uDynamicShadowMap'] ?? null, 3);
212
+ gl.bindTexture(gl.TEXTURE_2D_ARRAY, light.sunShadows);
213
+ gl.uniform1i(u['uSunShadows'] ?? null, 1);
221
214
 
222
215
  gl.uniformMatrix4fv(u['uDepthToWorld'] ?? null, false, this.depthToWorld);
223
216
  gl.uniform3f(u['uCameraPos'] ?? null, eye[0] ?? 0, eye[1] ?? 0, eye[2] ?? 0);
@@ -2,6 +2,7 @@
2
2
 
3
3
  import { EMISSIVE_SHADOW_SHARE, MIN_LOBE_ALPHA } from '../shaders/flat/lobes.ts';
4
4
  import { environmentWeight } from './ibl.ts';
5
+ import { schlickFresnel, type ResolvedGlass } from '../glass.ts';
5
6
 
6
7
  /**
7
8
  * **The lit expression had no reference until 2026-09-16, and that was a gap rather than a policy.**
@@ -327,3 +328,33 @@ export function litColour(
327
328
  }
328
329
  return out;
329
330
  }
331
+
332
+ /**
333
+ * Glass over its own shading: what is behind it shown by how much it lets through and how little it
334
+ * reflects at this angle, and the light behind it glowing through by how frosted it is — the forward
335
+ * path's glass branch (`flat/main.ts`) and `glass.ts`'s arithmetic, for the blended raster's twin.
336
+ *
337
+ * **`cosView` is taken unsigned**, because a pane passes the same light seen from either side.
338
+ * `sunGlow` is the light arriving at the pane's far side from the eye, which frost scatters toward
339
+ * it; a clear pane shows that light's source in `seen` instead. Allocates nothing: `out` is the
340
+ * caller's, and may be `own`.
341
+ */
342
+ export function glassShade(
343
+ own: readonly [number, number, number],
344
+ seen: readonly [number, number, number],
345
+ glass: ResolvedGlass,
346
+ cosView: number,
347
+ sunGlow: readonly [number, number, number],
348
+ out: [number, number, number],
349
+ ): [number, number, number] {
350
+ const t = glass.transmission;
351
+ const shown = t * (1 - schlickFresnel(Math.abs(cosView)));
352
+ const glowing = glass.frost * t;
353
+ for (let c = 0; c < 3; c++) {
354
+ const tint = glass.tint[c] as number;
355
+ const mine = own[c] as number;
356
+ out[c] =
357
+ mine + ((seen[c] as number) * tint - mine) * shown + (sunGlow[c] as number) * tint * glowing;
358
+ }
359
+ return out;
360
+ }
@@ -1,6 +1,7 @@
1
- /** A GPU-driven material as the shading pass reads it: five blocks, one of them integers. */
1
+ /** A GPU-driven material as the shading pass reads it: seven blocks, one of them integers. */
2
2
 
3
3
  import type { GpuDrivenMaterial } from '../backend/webgpu/gpuDrivenPass.ts';
4
+ import { resolveGlass, type ResolvedGlass } from '../glass.ts';
4
5
  import { DECODE_NO_PROGRAM } from '../shaders/gpudriven/decode.wgsl.ts';
5
6
  import type { GpuDrivenProgram } from './decodeTables.ts';
6
7
 
@@ -34,17 +35,18 @@ export interface GpuDrivenTextures {
34
35
 
35
36
  /**
36
37
  * Words a material entry: tint and emissive, the surface scalars, four program indices, the UV
37
- * scale and two strengths, the two ORM scales with the cutout and blend flags, and
38
- * the opacity with three spare.
38
+ * scale and two strengths, the two ORM scales with the cutout and blend flags, the opacity with the
39
+ * glass's transmission and frost and one spare, and the glass's tint with one spare.
39
40
  *
40
41
  * **It was eight until 2026-09-17**, and metalness took its last spare lane the same day, so the
41
42
  * programs and the scales needed three more blocks rather than squeezing into what was left. It
42
43
  * became twenty-four on 2026-09-18, when transparency arrived and there was nowhere left to put an
43
44
  * opacity: the cutout took lane 18 and the blend flag took 19, and lane 11 is a reserved program
44
45
  * slot rather than a gap. A sixteen-byte block is 1 kB across the whole table, against the 64 kB a
45
- * uniform buffer may hold.
46
+ * uniform buffer may hold. **Twenty-eight on 2026-09-29**, for glass: its transmission and frost
47
+ * took two of the opacity block's spares and its tint needed a block of its own.
46
48
  */
47
- export const GPU_DRIVEN_MATERIAL_FLOATS = 24;
49
+ export const GPU_DRIVEN_MATERIAL_FLOATS = 28;
48
50
 
49
51
  /**
50
52
  * Below this base-colour alpha the raster discards a fragment. Zero means no test.
@@ -74,6 +76,16 @@ export const MATERIAL_BLEND = 19;
74
76
  */
75
77
  export const MATERIAL_OPACITY = 20;
76
78
 
79
+ /**
80
+ * Glass (`glass.ts`): how much light passes, and how milky it is. Zero transmission is not glass,
81
+ * and a material that is glass is drawn in the blended half whatever `blend` says — a visibility
82
+ * buffer holds one surface a pixel, and glass is two.
83
+ */
84
+ export const MATERIAL_GLASS_TRANSMISSION = 21;
85
+ export const MATERIAL_GLASS_FROST = 22;
86
+ /** The colour light takes through the glass: three lanes, white where the material is not glass. */
87
+ export const MATERIAL_GLASS_TINT = 24;
88
+
77
89
  /**
78
90
  * Where a material's emissive program is: the fourth program lane, which the table reserved and
79
91
  * initialised to none before anything used it.
@@ -155,10 +167,20 @@ export function writeMaterialTable(
155
167
  float(m, 17, textures?.metallicScale ?? 1);
156
168
  /* Zero is off, so a material that says nothing about cutout writes the table it wrote before. */
157
169
  float(m, MATERIAL_ALPHA_CUTOFF, material.alphaCutoff ?? 0);
170
+ /* Glass A's clamps and its test of what is glass at all, so both pipelines agree on it. */
171
+ const glass = resolveGlass(material.glass, glassScratch);
158
172
  /* Zero is opaque, so a material that says nothing about blending stays in the opaque half. */
159
- float(m, MATERIAL_BLEND, material.blend === true ? 1 : 0);
173
+ float(m, MATERIAL_BLEND, material.blend === true || glass ? 1 : 0);
160
174
  /* One, so a material that asked to blend and said nothing else is as solid as an opaque one. */
161
175
  float(m, MATERIAL_OPACITY, material.opacity ?? 1);
176
+ float(m, MATERIAL_GLASS_TRANSMISSION, glassScratch.transmission);
177
+ float(m, MATERIAL_GLASS_FROST, glassScratch.frost);
178
+ float(m, MATERIAL_GLASS_TINT, glassScratch.tint[0]);
179
+ float(m, MATERIAL_GLASS_TINT + 1, glassScratch.tint[1]);
180
+ float(m, MATERIAL_GLASS_TINT + 2, glassScratch.tint[2]);
162
181
  });
163
182
  return buffer;
164
183
  }
184
+
185
+ /** One resolution every material's glass is read through. */
186
+ const glassScratch: ResolvedGlass = { transmission: 0, frost: 0, tint: [1, 1, 1] };
@@ -132,11 +132,11 @@ export const GPU_DRIVEN_PASSES = [
132
132
  'phaseTwoCull',
133
133
  'phaseTwoDraw',
134
134
  'blendCull',
135
- 'blendDraw',
136
135
  'visibilityCopy',
137
136
  'bin',
138
137
  'cursorCopy',
139
138
  'shade',
139
+ 'blendDraw',
140
140
  'blendResolve',
141
141
  ] as const;
142
142
 
@@ -230,10 +230,11 @@ function edgesFor(res: GpuDrivenResources): {
230
230
  /*
231
231
  * **It reads the depth the opaque half wrote and does not write it.** A pane behind a wall is
232
232
  * rejected at the wall; nothing here writes depth, so two panes do not hide each other and
233
- * there is something left to blend.
233
+ * there is something left to blend. **And it reads the shaded colour**, because glass shows the
234
+ * opaque picture behind it — which is why it is recorded after `shade` rather than before.
234
235
  */
235
236
  blendDraw: [
236
- [res.blendList, res.clusters, res.depth],
237
+ [res.blendList, res.clusters, res.depth, res.colour],
237
238
  [res.oitAccum, res.oitReveal],
238
239
  ],
239
240
  visibilityCopy: [[res.visibility], [res.visibilityCopy]],