@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
@@ -1,49 +1,106 @@
1
1
  /**
2
- * Single-cascade directional shadow map: one depth texture, one extra draw of
3
- * the static world per update.
2
+ * The sun's shadow: one depth array whose layers are the static world, its movers, and the static
3
+ * world's second occluder (the peel), drawn one at a time and sampled together.
4
4
  *
5
- * Filtering belongs to the receiving material shader, so the depth target can
6
- * stay a single-sample texture shared by every mesh pass.
5
+ * **One array rather than three maps since 4.5.0**, for the reason the lamps' twelve cubemaps
6
+ * became one: a texture unit. The lit pass binds all three through one `sampler2DArray`, which is
7
+ * what paid for the two tints glass casts with. The layers share one size and one format, so
8
+ * nothing about how each is drawn or read moved — only where it lives.
7
9
  *
8
- * Note this is a depth *render target*, not an art texture: AGENTS.md's
9
- * "zero textures" rule is about the art direction (no image assets in the
10
- * payload), and nothing here adds a byte to it.
10
+ * Filtering belongs to the receiving material shader, so the depth target can stay a
11
+ * single-sample texture shared by every mesh pass. This is a depth *render target*, not an art
12
+ * texture: AGENTS.md's "zero textures" rule is about the payload, and nothing here adds to it.
11
13
  */
12
14
  import { SHADOW_DEPTH_CLEAR } from './depthConvention.ts';
13
15
  import { DEFAULT_RENDER_QUALITY } from './renderQuality.ts';
14
16
 
15
- export class ShadowMap {
17
+ /** Where each of the sun's maps lives in the array. The shaders read these same numbers. */
18
+ export const SUN_STATIC_LAYER = 0;
19
+ export const SUN_DYNAMIC_LAYER = 1;
20
+ /** Present only when more than one static depth layer is asked for. */
21
+ export const SUN_PEELED_LAYER = 2;
22
+ /**
23
+ * Where the nearest pane's depth lives once glass has cast: after the three maps, at fixed places,
24
+ * because a shader cannot ask an array how many layers it has here — naga refuses the third
25
+ * component of an arrayed size query. A profile with no peel grows past an unused third layer to
26
+ * reach them, which is one map's memory in a profile that already declined one.
27
+ */
28
+ export const SUN_GLASS_STATIC_LAYER = 3;
29
+ export const SUN_GLASS_MOVING_LAYER = 4;
30
+
31
+ /** The three passes a scene opens, by the names `beginShadowPass` takes. */
32
+ export type SunShadowLayer = 'static' | 'static-peel' | 'dynamic';
33
+
34
+ /** The array layer a pass draws into. */
35
+ export function sunLayerIndex(layer: SunShadowLayer): number {
36
+ return layer === 'static'
37
+ ? SUN_STATIC_LAYER
38
+ : layer === 'dynamic'
39
+ ? SUN_DYNAMIC_LAYER
40
+ : SUN_PEELED_LAYER;
41
+ }
42
+
43
+ export class SunShadowArray {
16
44
  readonly size: number;
17
- readonly texture: WebGLTexture;
45
+ /** Replaced once, when glass first casts: see `growForGlass`. */
46
+ private current: WebGLTexture;
47
+ private layerCount: number;
48
+ private glass = false;
18
49
 
19
50
  private readonly framebuffer: WebGLFramebuffer;
51
+ /**
52
+ * **The peel is drawn here and copied into its layer when the pass ends.** It reads the static
53
+ * layer while it is drawn, and WebGL2 calls a texture both sampled and attached a feedback loop
54
+ * whatever layer each names — so drawing the peel into the array would draw nothing at all.
55
+ * What it costs is one more map's memory where a peel is asked for, and one blit per peel.
56
+ */
57
+ private readonly peelTexture: WebGLTexture | null;
58
+ private readonly peelFramebuffer: WebGLFramebuffer | null;
59
+ private peeling = false;
20
60
 
21
- constructor(gl: WebGL2RenderingContext, size = DEFAULT_RENDER_QUALITY.directionalShadowMapSize) {
61
+ constructor(
62
+ gl: WebGL2RenderingContext,
63
+ size = DEFAULT_RENDER_QUALITY.directionalShadowMapSize,
64
+ peeled = false,
65
+ ) {
22
66
  this.size = size;
67
+ this.layerCount = peeled ? 3 : 2;
23
68
 
24
- const texture = gl.createTexture();
25
- if (texture === null) throw new Error('ShadowMap: createTexture failed');
26
- this.texture = texture;
27
-
28
- gl.bindTexture(gl.TEXTURE_2D, texture);
29
- gl.texStorage2D(gl.TEXTURE_2D, 1, gl.DEPTH_COMPONENT24, size, size);
30
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
31
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
32
- // Clamp so anything outside the light frustum samples the border and is
33
- // treated as lit, rather than wrapping into a spurious shadow.
34
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
35
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
69
+ const texture = createSunTexture(gl, size, this.layerCount);
70
+ this.current = texture;
36
71
 
37
72
  const framebuffer = gl.createFramebuffer();
38
- if (framebuffer === null) throw new Error('ShadowMap: createFramebuffer failed');
73
+ if (framebuffer === null) throw new Error('SunShadowArray: createFramebuffer failed');
39
74
  this.framebuffer = framebuffer;
40
-
41
75
  gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
42
- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, texture, 0);
76
+ gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, texture, 0, SUN_STATIC_LAYER);
43
77
  // Depth-only: without this WebGL expects a colour attachment.
44
78
  gl.drawBuffers([gl.NONE]);
45
79
  gl.readBuffer(gl.NONE);
46
80
 
81
+ if (peeled) {
82
+ const peelTexture = gl.createTexture();
83
+ const peelFramebuffer = gl.createFramebuffer();
84
+ if (peelTexture === null || peelFramebuffer === null) {
85
+ throw new Error('SunShadowArray: the peel target could not be created');
86
+ }
87
+ gl.bindTexture(gl.TEXTURE_2D, peelTexture);
88
+ gl.texStorage2D(gl.TEXTURE_2D, 1, gl.DEPTH_COMPONENT24, size, size);
89
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
90
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
91
+ gl.bindFramebuffer(gl.FRAMEBUFFER, peelFramebuffer);
92
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, peelTexture, 0);
93
+ gl.drawBuffers([gl.NONE]);
94
+ gl.readBuffer(gl.NONE);
95
+ gl.bindTexture(gl.TEXTURE_2D, null);
96
+ this.peelTexture = peelTexture;
97
+ this.peelFramebuffer = peelFramebuffer;
98
+ } else {
99
+ this.peelTexture = null;
100
+ this.peelFramebuffer = null;
101
+ }
102
+
103
+ gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
47
104
  const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
48
105
  if (status !== gl.FRAMEBUFFER_COMPLETE) {
49
106
  /*
@@ -52,29 +109,121 @@ export class ShadowMap {
52
109
  * because the context cannot be lost before it exists.
53
110
  */
54
111
  if (gl.isContextLost()) return;
55
- throw new Error(`ShadowMap: framebuffer incomplete (0x${status.toString(16)})`);
112
+ throw new Error(`SunShadowArray: framebuffer incomplete (0x${status.toString(16)})`);
56
113
  }
57
114
 
58
115
  /*
59
- * **Cleared once, here, to the far plane, because nothing else says what the texture holds.**
60
- * WebGL zeroes it, and a zero compared with `LEQUAL` is an occluder at the light itself, so
61
- * a layer no pass has drawn put the whole world in shade. ANGLE on Vulkan hands the same
62
- * texture back reading 1, which is lit, and that is what every capture here ran on: a scene
63
- * with a second depth layer it never peeled lost its sun only under ANGLE's GL backend, the
64
- * one Chrome on Linux runs by default. The frame's clear value is borrowed and put back.
116
+ * **Every layer cleared once, here, to the far plane, because nothing else says what it
117
+ * holds.** WebGL zeroes the texture, and a zero compared with `LEQUAL` is an occluder at the
118
+ * light itself, so a layer no pass has drawn put the whole world in shade. ANGLE on Vulkan
119
+ * hands the same texture back reading 1, which is lit, and that is what every capture here
120
+ * ran on: a scene with a second depth layer it never peeled lost its sun only under ANGLE's
121
+ * GL backend, the one Chrome on Linux runs by default. The frame's clear value is put back.
65
122
  */
66
123
  const frameClear = gl.getParameter(gl.DEPTH_CLEAR_VALUE) as number;
67
124
  gl.clearDepth(SHADOW_DEPTH_CLEAR);
68
- gl.clear(gl.DEPTH_BUFFER_BIT);
125
+ gl.viewport(0, 0, size, size);
126
+ for (let layer = 0; layer < this.layers; layer++) {
127
+ gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, texture, 0, layer);
128
+ gl.clear(gl.DEPTH_BUFFER_BIT);
129
+ }
69
130
  gl.clearDepth(frameClear);
70
131
 
71
132
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
72
- gl.bindTexture(gl.TEXTURE_2D, null);
133
+ gl.bindTexture(gl.TEXTURE_2D_ARRAY, null);
134
+ }
135
+
136
+ get texture(): WebGLTexture {
137
+ return this.current;
138
+ }
139
+
140
+ get layers(): number {
141
+ return this.layerCount;
142
+ }
143
+
144
+ /** Whether the two glass layers exist: from the first glass a sun pass is offered, for good. */
145
+ get hasGlass(): boolean {
146
+ return this.glass;
73
147
  }
74
148
 
75
- /** Bind as the render target and clear. Caller then draws the casters. */
76
- begin(gl: WebGL2RenderingContext): void {
149
+ /** The glass depth layer beside a map: fixed places after the three maps. */
150
+ glassLayer(which: 'static' | 'dynamic'): number {
151
+ return which === 'static' ? SUN_GLASS_STATIC_LAYER : SUN_GLASS_MOVING_LAYER;
152
+ }
153
+
154
+ /**
155
+ * Add the two glass layers, once, carrying every layer already held across.
156
+ *
157
+ * **Carried rather than re-baked**, because the sun's static map is the consumer's to bake and
158
+ * it may bake it once: the glass that grows the array arrives in the very pass that drew that
159
+ * map, so a copy is what keeps it. A depth blit per layer, same size and format, and the glass
160
+ * layers cleared to the far plane — nothing nearer than the sun until a pane is drawn there.
161
+ * Returns whether it grew, so the caller knows every bind of the old texture is stale.
162
+ */
163
+ growForGlass(gl: WebGL2RenderingContext): boolean {
164
+ if (this.glass) return false;
165
+ const old = this.current;
166
+ const oldLayers = this.layerCount;
167
+ const layers = SUN_GLASS_MOVING_LAYER + 1;
168
+ const texture = createSunTexture(gl, this.size, layers);
169
+ const read = gl.createFramebuffer();
170
+ if (read === null) throw new Error('SunShadowArray: createFramebuffer failed');
171
+ gl.bindFramebuffer(gl.READ_FRAMEBUFFER, read);
172
+ gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, this.framebuffer);
173
+ for (let layer = 0; layer < oldLayers; layer++) {
174
+ gl.framebufferTextureLayer(gl.READ_FRAMEBUFFER, gl.DEPTH_ATTACHMENT, old, 0, layer);
175
+ gl.framebufferTextureLayer(gl.DRAW_FRAMEBUFFER, gl.DEPTH_ATTACHMENT, texture, 0, layer);
176
+ gl.blitFramebuffer(
177
+ 0,
178
+ 0,
179
+ this.size,
180
+ this.size,
181
+ 0,
182
+ 0,
183
+ this.size,
184
+ this.size,
185
+ gl.DEPTH_BUFFER_BIT,
186
+ gl.NEAREST,
187
+ );
188
+ }
189
+ for (let layer = oldLayers; layer < layers; layer++) {
190
+ gl.framebufferTextureLayer(gl.DRAW_FRAMEBUFFER, gl.DEPTH_ATTACHMENT, texture, 0, layer);
191
+ gl.clearBufferfv(gl.DEPTH, 0, FAR_PLANE);
192
+ }
193
+ gl.bindFramebuffer(gl.READ_FRAMEBUFFER, null);
194
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
195
+ gl.deleteFramebuffer(read);
196
+ gl.deleteTexture(old);
197
+ this.current = texture;
198
+ this.layerCount = layers;
199
+ this.glass = true;
200
+ return true;
201
+ }
202
+
203
+ /** Bind `layer` as the render target and clear it. Caller then draws the casters. */
204
+ begin(gl: WebGL2RenderingContext, layer: SunShadowLayer): void {
205
+ this.peeling = layer === 'static-peel' && this.peelFramebuffer !== null;
206
+ if (this.peeling) {
207
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this.peelFramebuffer);
208
+ } else {
209
+ this.attach(gl, sunLayerIndex(layer));
210
+ }
211
+ this.clearAndRaster(gl);
212
+ }
213
+
214
+ /** Bind a glass depth layer as the render target and clear it: where the nearest pane is. */
215
+ beginGlass(gl: WebGL2RenderingContext, which: 'static' | 'dynamic'): void {
216
+ this.peeling = false;
217
+ this.attach(gl, this.glassLayer(which));
218
+ this.clearAndRaster(gl);
219
+ }
220
+
221
+ private attach(gl: WebGL2RenderingContext, layer: number): void {
77
222
  gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
223
+ gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, this.current, 0, layer);
224
+ }
225
+
226
+ private clearAndRaster(gl: WebGL2RenderingContext): void {
78
227
  gl.viewport(0, 0, this.size, this.size);
79
228
  gl.clear(gl.DEPTH_BUFFER_BIT);
80
229
  /*
@@ -93,11 +242,56 @@ export class ShadowMap {
93
242
  end(gl: WebGL2RenderingContext): void {
94
243
  gl.disable(gl.POLYGON_OFFSET_FILL);
95
244
  gl.cullFace(gl.BACK);
245
+ if (this.peeling) {
246
+ /* The peel into its layer: same size, same format, so a nearest depth blit is a copy. */
247
+ gl.bindFramebuffer(gl.READ_FRAMEBUFFER, this.peelFramebuffer);
248
+ gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, this.framebuffer);
249
+ gl.framebufferTextureLayer(
250
+ gl.DRAW_FRAMEBUFFER,
251
+ gl.DEPTH_ATTACHMENT,
252
+ this.current,
253
+ 0,
254
+ SUN_PEELED_LAYER,
255
+ );
256
+ gl.blitFramebuffer(
257
+ 0,
258
+ 0,
259
+ this.size,
260
+ this.size,
261
+ 0,
262
+ 0,
263
+ this.size,
264
+ this.size,
265
+ gl.DEPTH_BUFFER_BIT,
266
+ gl.NEAREST,
267
+ );
268
+ gl.bindFramebuffer(gl.READ_FRAMEBUFFER, null);
269
+ this.peeling = false;
270
+ }
96
271
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
97
272
  }
98
273
 
99
274
  dispose(gl: WebGL2RenderingContext): void {
100
275
  gl.deleteFramebuffer(this.framebuffer);
101
- gl.deleteTexture(this.texture);
276
+ gl.deleteTexture(this.current);
277
+ if (this.peelFramebuffer !== null) gl.deleteFramebuffer(this.peelFramebuffer);
278
+ if (this.peelTexture !== null) gl.deleteTexture(this.peelTexture);
102
279
  }
103
280
  }
281
+
282
+ /** The far plane, as `clearBufferfv` takes it: a layer no pane has reached is clear of glass. */
283
+ const FAR_PLANE = new Float32Array([SHADOW_DEPTH_CLEAR]);
284
+
285
+ function createSunTexture(gl: WebGL2RenderingContext, size: number, layers: number): WebGLTexture {
286
+ const texture = gl.createTexture();
287
+ if (texture === null) throw new Error('SunShadowArray: createTexture failed');
288
+ gl.bindTexture(gl.TEXTURE_2D_ARRAY, texture);
289
+ gl.texStorage3D(gl.TEXTURE_2D_ARRAY, 1, gl.DEPTH_COMPONENT24, size, size, layers);
290
+ gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
291
+ gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
292
+ // Clamp so anything outside the light frustum samples the border and is
293
+ // treated as lit, rather than wrapping into a spurious shadow.
294
+ gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
295
+ gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
296
+ return texture;
297
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * What the sun's light keeps of itself through glass: an RGBA8 array of two layers, static and
3
+ * moving, beside the sun's depth array. RGB is the product of every pane's
4
+ * `transmission × (1 − F) × tint`, A the product of their clarities (`glassShadow.ts`).
5
+ *
6
+ * **Cleared to white and drawn with a multiplying blend and no depth test**, so every pane on a ray
7
+ * contributes and the order they come in cannot matter. **Mipmapped and filtered**, unlike the
8
+ * depths beside it: a frosted pane's colour is read at the level its spread asks for, so a wide
9
+ * spread is smooth rather than a scatter of taps — the chain is rebuilt after every draw of a layer.
10
+ * `'half'` makes it half the depth map's size and nothing else changes: the light matrix is the
11
+ * same, so a texel is a texel of the same place, only larger.
12
+ */
13
+ export class SunGlassTint {
14
+ readonly size: number;
15
+ readonly texture: WebGLTexture;
16
+ private readonly framebuffer: WebGLFramebuffer;
17
+
18
+ constructor(gl: WebGL2RenderingContext, size: number) {
19
+ this.size = size;
20
+ const texture = gl.createTexture();
21
+ const framebuffer = gl.createFramebuffer();
22
+ if (texture === null || framebuffer === null) {
23
+ throw new Error('SunGlassTint: the tint target could not be created');
24
+ }
25
+ this.texture = texture;
26
+ this.framebuffer = framebuffer;
27
+ gl.bindTexture(gl.TEXTURE_2D_ARRAY, texture);
28
+ gl.texStorage3D(gl.TEXTURE_2D_ARRAY, mipLevels(size), gl.RGBA8, size, size, 2);
29
+ gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
30
+ gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
31
+ gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
32
+ gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
33
+ gl.bindTexture(gl.TEXTURE_2D_ARRAY, null);
34
+ /* Both layers born clear: a layer no pane has reached lets everything through. */
35
+ gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
36
+ for (let layer = 0; layer < 2; layer++) {
37
+ gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, texture, 0, layer);
38
+ gl.clearBufferfv(gl.COLOR, 0, CLEAR);
39
+ }
40
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
41
+ this.fillMips(gl);
42
+ }
43
+
44
+ /** Bind `layer` (0 static, 1 moving), clear it to white, and set the multiplying state. */
45
+ begin(gl: WebGL2RenderingContext, layer: number): void {
46
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
47
+ gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, this.texture, 0, layer);
48
+ gl.viewport(0, 0, this.size, this.size);
49
+ gl.clearBufferfv(gl.COLOR, 0, CLEAR);
50
+ gl.disable(gl.DEPTH_TEST);
51
+ gl.depthMask(false);
52
+ gl.disable(gl.POLYGON_OFFSET_FILL);
53
+ gl.enable(gl.BLEND);
54
+ gl.blendEquation(gl.FUNC_ADD);
55
+ gl.blendFuncSeparate(gl.DST_COLOR, gl.ZERO, gl.DST_ALPHA, gl.ZERO);
56
+ }
57
+
58
+ /** Put back what `begin` changed, and rebuild the mip chain the frost reads. */
59
+ end(gl: WebGL2RenderingContext): void {
60
+ gl.disable(gl.BLEND);
61
+ gl.depthMask(true);
62
+ gl.enable(gl.DEPTH_TEST);
63
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
64
+ this.fillMips(gl);
65
+ }
66
+
67
+ /** Every level from level 0, both layers: bound on the active unit and handed back. */
68
+ private fillMips(gl: WebGL2RenderingContext): void {
69
+ const previous = gl.getParameter(gl.TEXTURE_BINDING_2D_ARRAY) as WebGLTexture | null;
70
+ gl.bindTexture(gl.TEXTURE_2D_ARRAY, this.texture);
71
+ gl.generateMipmap(gl.TEXTURE_2D_ARRAY);
72
+ gl.bindTexture(gl.TEXTURE_2D_ARRAY, previous);
73
+ }
74
+
75
+ dispose(gl: WebGL2RenderingContext): void {
76
+ gl.deleteFramebuffer(this.framebuffer);
77
+ gl.deleteTexture(this.texture);
78
+ }
79
+ }
80
+
81
+ const CLEAR = new Float32Array([1, 1, 1, 1]);
82
+
83
+ /** A full chain down to one texel. */
84
+ function mipLevels(size: number): number {
85
+ return Math.floor(Math.log2(Math.max(1, size))) + 1;
86
+ }
@@ -3,6 +3,8 @@ import type { ReadonlyMat4 } from 'gl-matrix';
3
3
  /* Type-only, and from the backend that declares it — the same import `backend/api.ts` makes,
4
4
  for the same reason: the shape belongs to the draw verb and erases at compile time. */
5
5
  import type { TranslucentMeshOptions } from './backend/webgl2/renderer.ts';
6
+ import { resolveGlass } from './glass.ts';
7
+ import type { ResolvedGlass } from './glass.ts';
6
8
 
7
9
  /**
8
10
  * The frame's translucent draws, held so they can be submitted more than once.
@@ -51,10 +53,14 @@ export interface TranslucentDraw {
51
53
  /** What survives one metre of the medium. Null where the draw named none; otherwise a copy. */
52
54
  refractTint: Float32Array | null;
53
55
  thicknessM: number;
56
+ /** Glass, resolved, or null for a draw that is not glass. See `glass.ts`. */
57
+ glass: ResolvedGlass | null;
54
58
  }
55
59
 
56
60
  /** A record with its storage, reused across frames. */
57
61
  interface PooledDraw extends TranslucentDraw {
62
+ /** Where `glass` points when set: allocated once with the record, never released. */
63
+ readonly glassStore: ResolvedGlass;
58
64
  /** Allocated once and pointed at by `tint` when a draw has one. */
59
65
  readonly tintStore: Float32Array;
60
66
  /** The same arrangement for the refraction tint: allocated once, never released. */
@@ -77,6 +83,8 @@ function newRecord(): PooledDraw {
77
83
  refractTint: null,
78
84
  thicknessM: 0,
79
85
  refractTintStore: new Float32Array(3),
86
+ glass: null,
87
+ glassStore: { transmission: 0, frost: 0, tint: [1, 1, 1] },
80
88
  };
81
89
  }
82
90
 
@@ -84,6 +92,7 @@ export class TranslucentQueue {
84
92
  private readonly pool: PooledDraw[] = [];
85
93
  private count = 0;
86
94
  private refracting = 0;
95
+ private glassing = 0;
87
96
 
88
97
  /** How many draws this frame has recorded. */
89
98
  get length(): number {
@@ -98,6 +107,11 @@ export class TranslucentQueue {
98
107
  return this.refracting > 0;
99
108
  }
100
109
 
110
+ /** Whether anything recorded this frame is glass, so the copy it reads needs its mip chain. */
111
+ get glassy(): boolean {
112
+ return this.glassing > 0;
113
+ }
114
+
101
115
  /** How many records are pooled, which a test uses to assert a steady scene stops allocating. */
102
116
  get capacity(): number {
103
117
  return this.pool.length;
@@ -137,7 +151,11 @@ export class TranslucentQueue {
137
151
  record.tint = record.tintStore;
138
152
  }
139
153
  record.refraction = options.refraction ?? 0;
140
- if (record.refraction > 0) this.refracting++;
154
+ /* Glass reads the copy too, whether or not it bends: see `glass.ts`. */
155
+ const glassy = resolveGlass(options.glass, record.glassStore);
156
+ record.glass = glassy ? record.glassStore : null;
157
+ if (glassy) this.glassing++;
158
+ if (record.refraction > 0 || glassy) this.refracting++;
141
159
  record.thicknessM = options.thicknessM ?? 0;
142
160
  const refractTint = options.refractTint ?? null;
143
161
  if (refractTint === null) {
@@ -168,5 +186,6 @@ export class TranslucentQueue {
168
186
  reset(): void {
169
187
  this.count = 0;
170
188
  this.refracting = 0;
189
+ this.glassing = 0;
171
190
  }
172
191
  }