@driftengine/core 4.4.2 → 4.6.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 (339) hide show
  1. package/CHANGELOG.json +97 -0
  2. package/README.md +1 -1
  3. package/dist/geometry/boxCsg.d.ts +28 -0
  4. package/dist/geometry/boxCsg.js +139 -0
  5. package/dist/geometry/csg.d.ts +36 -0
  6. package/dist/geometry/csg.js +329 -0
  7. package/dist/geometry/meshBuilder.js +13 -0
  8. package/dist/geometry/meshConcat.js +1 -0
  9. package/dist/geometry/pixelFont.js +2 -0
  10. package/dist/geometry/solid.d.ts +61 -0
  11. package/dist/geometry/solid.js +152 -0
  12. package/dist/geometry/solidFlat.d.ts +35 -0
  13. package/dist/geometry/solidFlat.js +428 -0
  14. package/dist/geometry/solidHarness.d.ts +19 -0
  15. package/dist/geometry/solidHarness.js +66 -0
  16. package/dist/geometry/solidNormals.d.ts +17 -0
  17. package/dist/geometry/solidNormals.js +86 -0
  18. package/dist/geometry/solidRound.d.ts +41 -0
  19. package/dist/geometry/solidRound.js +323 -0
  20. package/dist/geometry/solidWriter.d.ts +16 -0
  21. package/dist/geometry/solidWriter.js +42 -0
  22. package/dist/index.d.ts +28 -3
  23. package/dist/index.js +20 -1
  24. package/dist/render/backend/api.d.ts +34 -14
  25. package/dist/render/backend/drawFog.d.ts +26 -0
  26. package/dist/render/backend/drawFog.js +30 -0
  27. package/dist/render/backend/materialChanges.d.ts +2 -1
  28. package/dist/render/backend/materialChanges.js +6 -1
  29. package/dist/render/backend/webgl2/glassTintPrograms.d.ts +13 -0
  30. package/dist/render/backend/webgl2/glassTintPrograms.js +38 -0
  31. package/dist/render/backend/webgl2/instanced.d.ts +24 -5
  32. package/dist/render/backend/webgl2/instanced.js +72 -15
  33. package/dist/render/backend/webgl2/renderer.d.ts +201 -16
  34. package/dist/render/backend/webgl2/renderer.js +596 -224
  35. package/dist/render/backend/webgpu/buffers.d.ts +29 -1
  36. package/dist/render/backend/webgpu/buffers.js +61 -4
  37. package/dist/render/backend/webgpu/depthPass.d.ts +1 -1
  38. package/dist/render/backend/webgpu/depthPass.js +7 -2
  39. package/dist/render/backend/webgpu/drawCommand.d.ts +5 -0
  40. package/dist/render/backend/webgpu/drawCommand.js +1 -0
  41. package/dist/render/backend/webgpu/flatPass.d.ts +6 -1
  42. package/dist/render/backend/webgpu/flatPass.js +57 -9
  43. package/dist/render/backend/webgpu/glassTintPass.d.ts +28 -0
  44. package/dist/render/backend/webgpu/glassTintPass.js +206 -0
  45. package/dist/render/backend/webgpu/gpuDrivenPass.d.ts +60 -1
  46. package/dist/render/backend/webgpu/gpuDrivenPass.js +305 -5
  47. package/dist/render/backend/webgpu/instanceCullPass.d.ts +34 -0
  48. package/dist/render/backend/webgpu/instanceCullPass.js +144 -0
  49. package/dist/render/backend/webgpu/instanced.d.ts +23 -2
  50. package/dist/render/backend/webgpu/instanced.js +41 -4
  51. package/dist/render/backend/webgpu/latePass.d.ts +30 -0
  52. package/dist/render/backend/webgpu/latePass.js +140 -0
  53. package/dist/render/backend/webgpu/lightVolumePass.js +5 -1
  54. package/dist/render/backend/webgpu/motionPass.d.ts +95 -0
  55. package/dist/render/backend/webgpu/motionPass.js +491 -0
  56. package/dist/render/backend/webgpu/pipelineCache.d.ts +10 -1
  57. package/dist/render/backend/webgpu/pipelineCache.js +11 -1
  58. package/dist/render/backend/webgpu/pointGlassTintPass.d.ts +32 -0
  59. package/dist/render/backend/webgpu/pointGlassTintPass.js +193 -0
  60. package/dist/render/backend/webgpu/pointShadowPass.d.ts +35 -6
  61. package/dist/render/backend/webgpu/pointShadowPass.js +105 -20
  62. package/dist/render/backend/webgpu/postPass.d.ts +3 -13
  63. package/dist/render/backend/webgpu/renderer.d.ts +271 -64
  64. package/dist/render/backend/webgpu/renderer.js +1395 -580
  65. package/dist/render/backend/webgpu/scatterPass.js +2 -1
  66. package/dist/render/backend/webgpu/sdfTextPass.js +6 -1
  67. package/dist/render/backend/webgpu/shaders/instanceCull.wgsl.d.ts +18 -0
  68. package/dist/render/backend/webgpu/shaders/instanceCull.wgsl.js +59 -0
  69. package/dist/render/backend/webgpu/skinPaletteRing.d.ts +4 -0
  70. package/dist/render/backend/webgpu/skinPaletteRing.js +4 -0
  71. package/dist/render/backend/webgpu/snapshotMips.d.ts +29 -0
  72. package/dist/render/backend/webgpu/snapshotMips.js +52 -0
  73. package/dist/render/backend/webgpu/surfaceTexturePass.d.ts +22 -3
  74. package/dist/render/backend/webgpu/surfaceTexturePass.js +78 -39
  75. package/dist/render/driftLight/bake.d.ts +7 -0
  76. package/dist/render/driftLight/bake.js +61 -56
  77. package/dist/render/driftLight/denseField.d.ts +46 -0
  78. package/dist/render/driftLight/denseField.js +109 -0
  79. package/dist/render/driftLight/lightField.d.ts +10 -5
  80. package/dist/render/driftLight/lightField.js +25 -19
  81. package/dist/render/driftLight/presence.d.ts +50 -0
  82. package/dist/render/driftLight/presence.js +43 -0
  83. package/dist/render/driftLight/uniforms.d.ts +3 -3
  84. package/dist/render/driftLight/uniforms.js +5 -6
  85. package/dist/render/driftLight/worldLightField.d.ts +41 -0
  86. package/dist/render/driftLight/worldLightField.js +66 -0
  87. package/dist/render/fixtureAtlas.d.ts +20 -0
  88. package/dist/render/fixtureAtlas.js +49 -0
  89. package/dist/render/glass.d.ts +45 -0
  90. package/dist/render/glass.js +39 -0
  91. package/dist/render/glassCasters.d.ts +40 -0
  92. package/dist/render/glassCasters.js +84 -0
  93. package/dist/render/glassShadow.d.ts +78 -0
  94. package/dist/render/glassShadow.js +125 -0
  95. package/dist/render/globalMediumPass.d.ts +2 -3
  96. package/dist/render/globalMediumPass.js +2 -8
  97. package/dist/render/gpudriven/lit.d.ts +12 -0
  98. package/dist/render/gpudriven/lit.js +23 -0
  99. package/dist/render/gpudriven/materialTable.d.ts +15 -5
  100. package/dist/render/gpudriven/materialTable.js +26 -6
  101. package/dist/render/gpudriven/pipeline.d.ts +1 -1
  102. package/dist/render/gpudriven/pipeline.js +4 -3
  103. package/dist/render/gpudriven/sunGlass.d.ts +35 -0
  104. package/dist/render/gpudriven/sunGlass.js +89 -0
  105. package/dist/render/instanceCull.d.ts +63 -0
  106. package/dist/render/instanceCull.js +160 -0
  107. package/dist/render/instances.d.ts +15 -0
  108. package/dist/render/lightBudget.d.ts +36 -35
  109. package/dist/render/lightBudget.js +38 -37
  110. package/dist/render/lightGrid.d.ts +42 -0
  111. package/dist/render/lightGrid.js +87 -0
  112. package/dist/render/mesh.d.ts +18 -24
  113. package/dist/render/mesh.js +51 -68
  114. package/dist/render/occlusion.d.ts +10 -0
  115. package/dist/render/occlusion.js +36 -7
  116. package/dist/render/pointGlassTint.d.ts +59 -0
  117. package/dist/render/pointGlassTint.js +198 -0
  118. package/dist/render/pointLightSelection.d.ts +7 -0
  119. package/dist/render/pointLightSelection.js +29 -4
  120. package/dist/render/pointShadowArray.d.ts +56 -3
  121. package/dist/render/pointShadowArray.js +127 -16
  122. package/dist/render/pointShadowMap.d.ts +23 -1
  123. package/dist/render/pointShadowMap.js +27 -1
  124. package/dist/render/rainField.d.ts +13 -0
  125. package/dist/render/rainField.js +27 -5
  126. package/dist/render/recon/changeFrames.d.ts +15 -0
  127. package/dist/render/recon/changeFrames.js +21 -0
  128. package/dist/render/recon/lateDepth.d.ts +11 -0
  129. package/dist/render/recon/lateDepth.js +70 -0
  130. package/dist/render/recon/lateRouting.d.ts +14 -0
  131. package/dist/render/recon/lateRouting.js +18 -0
  132. package/dist/render/recon/mover.d.ts +57 -0
  133. package/dist/render/recon/mover.js +86 -0
  134. package/dist/render/recon/resolve.d.ts +34 -4
  135. package/dist/render/recon/resolve.js +161 -20
  136. package/dist/render/renderQuality.d.ts +35 -0
  137. package/dist/render/renderQuality.js +7 -0
  138. package/dist/render/rendererHarness.js +16 -0
  139. package/dist/render/sceneTarget.d.ts +9 -1
  140. package/dist/render/sceneTarget.js +60 -12
  141. package/dist/render/shaders/depth.d.ts +16 -1
  142. package/dist/render/shaders/depth.js +94 -15
  143. package/dist/render/shaders/flat/directionalShadow.js +91 -6
  144. package/dist/render/shaders/flat/driftLight.d.ts +1 -1
  145. package/dist/render/shaders/flat/driftLight.js +32 -7
  146. package/dist/render/shaders/flat/glassTint.d.ts +6 -0
  147. package/dist/render/shaders/flat/glassTint.js +25 -0
  148. package/dist/render/shaders/flat/index.d.ts +13 -0
  149. package/dist/render/shaders/flat/index.js +9 -3
  150. package/dist/render/shaders/flat/lampGlass.d.ts +1 -0
  151. package/dist/render/shaders/flat/lampGlass.js +183 -0
  152. package/dist/render/shaders/flat/main.d.ts +1 -1
  153. package/dist/render/shaders/flat/main.js +190 -38
  154. package/dist/render/shaders/flat/pointShadow.d.ts +1 -1
  155. package/dist/render/shaders/flat/pointShadow.js +2 -0
  156. package/dist/render/shaders/flat/preamble.js +96 -26
  157. package/dist/render/shaders/flat/surfaceEffects.d.ts +29 -0
  158. package/dist/render/shaders/flat/surfaceEffects.js +152 -0
  159. package/dist/render/shaders/generated/depth.wgsl.d.ts +230 -7
  160. package/dist/render/shaders/generated/depth.wgsl.js +230 -7
  161. package/dist/render/shaders/generated/flat.wgsl.d.ts +1644 -1596
  162. package/dist/render/shaders/generated/flat.wgsl.js +1683 -1621
  163. package/dist/render/shaders/generated/globalMedium.wgsl.d.ts +3 -13
  164. package/dist/render/shaders/generated/globalMedium.wgsl.js +3 -13
  165. package/dist/render/shaders/generated/lightVolume.wgsl.d.ts +3 -13
  166. package/dist/render/shaders/generated/lightVolume.wgsl.js +21 -17
  167. package/dist/render/shaders/generated/octahedralResolve.wgsl.d.ts +29 -0
  168. package/dist/render/shaders/generated/octahedralResolve.wgsl.js +29 -0
  169. package/dist/render/shaders/generated/rush.wgsl.d.ts +21 -16
  170. package/dist/render/shaders/generated/rush.wgsl.js +21 -16
  171. package/dist/render/shaders/generated/sdfText.wgsl.d.ts +2 -2
  172. package/dist/render/shaders/generated/sdfText.wgsl.js +2 -2
  173. package/dist/render/shaders/generated/text.wgsl.d.ts +1 -1
  174. package/dist/render/shaders/generated/text.wgsl.js +1 -1
  175. package/dist/render/shaders/gi/driftLight.wgsl.d.ts +1 -1
  176. package/dist/render/shaders/gi/driftLight.wgsl.js +27 -4
  177. package/dist/render/shaders/globalMedium.js +6 -6
  178. package/dist/render/shaders/gpudriven/blendRaster.wgsl.d.ts +7 -0
  179. package/dist/render/shaders/gpudriven/blendRaster.wgsl.js +86 -1
  180. package/dist/render/shaders/gpudriven/cull.wgsl.d.ts +1 -1
  181. package/dist/render/shaders/gpudriven/cull.wgsl.js +11 -2
  182. package/dist/render/shaders/gpudriven/materialTable.wgsl.d.ts +1 -1
  183. package/dist/render/shaders/gpudriven/materialTable.wgsl.js +3 -1
  184. package/dist/render/shaders/gpudriven/shade.wgsl.js +50 -1
  185. package/dist/render/shaders/gpudriven/sunGlass.wgsl.d.ts +9 -0
  186. package/dist/render/shaders/gpudriven/sunGlass.wgsl.js +133 -0
  187. package/dist/render/shaders/lightVolume.js +6 -6
  188. package/dist/render/shaders/octahedralResolve.d.ts +8 -0
  189. package/dist/render/shaders/octahedralResolve.js +48 -17
  190. package/dist/render/shaders/recon/lateDepth.wgsl.d.ts +1 -0
  191. package/dist/render/shaders/recon/lateDepth.wgsl.js +69 -0
  192. package/dist/render/shaders/recon/motion.wgsl.d.ts +1 -1
  193. package/dist/render/shaders/recon/motion.wgsl.js +84 -0
  194. package/dist/render/shaders/recon/resolve.wgsl.d.ts +8 -7
  195. package/dist/render/shaders/recon/resolve.wgsl.js +161 -27
  196. package/dist/render/shaders/rush.js +11 -1
  197. package/dist/render/shaders/sdfText.d.ts +1 -1
  198. package/dist/render/shaders/sdfText.js +3 -2
  199. package/dist/render/shaders/text.d.ts +1 -1
  200. package/dist/render/shaders/text.js +18 -9
  201. package/dist/render/shadowCasters.d.ts +20 -1
  202. package/dist/render/shadowCasters.js +15 -1
  203. package/dist/render/shadowMap.d.ts +54 -5
  204. package/dist/render/shadowMap.js +190 -36
  205. package/dist/render/sunGlassTint.d.ts +25 -0
  206. package/dist/render/sunGlassTint.js +79 -0
  207. package/dist/render/surfaceEffects.d.ts +83 -0
  208. package/dist/render/surfaceEffects.js +75 -0
  209. package/dist/render/surfaceScene.d.ts +8 -0
  210. package/dist/render/surfaceScene.js +8 -0
  211. package/dist/render/surfaceTexture.d.ts +18 -2
  212. package/dist/render/surfaceTexture.js +68 -15
  213. package/dist/render/temporalAa.d.ts +25 -6
  214. package/dist/render/temporalAa.js +45 -15
  215. package/dist/render/textureSource.d.ts +30 -0
  216. package/dist/render/textureSource.js +53 -0
  217. package/dist/render/translucentQueue.d.ts +6 -0
  218. package/dist/render/translucentQueue.js +15 -1
  219. package/dist/render/vertexDefaults.js +2 -0
  220. package/dist/world/hlod.d.ts +104 -0
  221. package/dist/world/hlod.js +214 -0
  222. package/package.json +6 -6
  223. package/src/geometry/boxCsg.ts +186 -0
  224. package/src/geometry/csg.ts +384 -0
  225. package/src/geometry/meshBuilder.ts +15 -0
  226. package/src/geometry/meshConcat.ts +1 -0
  227. package/src/geometry/pixelFont.ts +2 -0
  228. package/src/geometry/solid.ts +189 -0
  229. package/src/geometry/solidFlat.ts +522 -0
  230. package/src/geometry/solidHarness.ts +83 -0
  231. package/src/geometry/solidNormals.ts +103 -0
  232. package/src/geometry/solidRound.ts +443 -0
  233. package/src/geometry/solidWriter.ts +58 -0
  234. package/src/index.ts +61 -1
  235. package/src/render/backend/api.ts +36 -11
  236. package/src/render/backend/drawFog.ts +31 -0
  237. package/src/render/backend/materialChanges.ts +9 -2
  238. package/src/render/backend/webgl2/glassTintPrograms.ts +58 -0
  239. package/src/render/backend/webgl2/instanced.ts +80 -14
  240. package/src/render/backend/webgl2/renderer.ts +700 -250
  241. package/src/render/backend/webgpu/buffers.ts +92 -5
  242. package/src/render/backend/webgpu/depthPass.ts +7 -2
  243. package/src/render/backend/webgpu/drawCommand.ts +6 -0
  244. package/src/render/backend/webgpu/flatPass.ts +62 -11
  245. package/src/render/backend/webgpu/glassTintPass.ts +245 -0
  246. package/src/render/backend/webgpu/gpuDrivenPass.ts +349 -5
  247. package/src/render/backend/webgpu/instanceCullPass.ts +165 -0
  248. package/src/render/backend/webgpu/instanced.ts +42 -4
  249. package/src/render/backend/webgpu/latePass.ts +161 -0
  250. package/src/render/backend/webgpu/lightVolumePass.ts +5 -1
  251. package/src/render/backend/webgpu/motionPass.ts +535 -0
  252. package/src/render/backend/webgpu/pipelineCache.ts +12 -1
  253. package/src/render/backend/webgpu/pointGlassTintPass.ts +233 -0
  254. package/src/render/backend/webgpu/pointShadowPass.ts +158 -26
  255. package/src/render/backend/webgpu/renderer.ts +1671 -670
  256. package/src/render/backend/webgpu/scatterPass.ts +2 -1
  257. package/src/render/backend/webgpu/sdfTextPass.ts +6 -1
  258. package/src/render/backend/webgpu/shaders/instanceCull.wgsl.ts +61 -0
  259. package/src/render/backend/webgpu/skinPaletteRing.ts +4 -0
  260. package/src/render/backend/webgpu/snapshotMips.ts +80 -0
  261. package/src/render/backend/webgpu/surfaceTexturePass.ts +91 -51
  262. package/src/render/driftLight/bake.ts +85 -62
  263. package/src/render/driftLight/denseField.ts +149 -0
  264. package/src/render/driftLight/lightField.ts +33 -18
  265. package/src/render/driftLight/presence.ts +69 -0
  266. package/src/render/driftLight/uniforms.ts +8 -9
  267. package/src/render/driftLight/worldLightField.ts +83 -0
  268. package/src/render/fixtureAtlas.ts +63 -0
  269. package/src/render/glass.ts +74 -0
  270. package/src/render/glassCasters.ts +164 -0
  271. package/src/render/glassShadow.ts +153 -0
  272. package/src/render/globalMediumPass.ts +4 -11
  273. package/src/render/gpudriven/lit.ts +31 -0
  274. package/src/render/gpudriven/materialTable.ts +28 -6
  275. package/src/render/gpudriven/pipeline.ts +4 -3
  276. package/src/render/gpudriven/sunGlass.ts +117 -0
  277. package/src/render/instanceCull.ts +205 -0
  278. package/src/render/instances.ts +16 -0
  279. package/src/render/lightBudget.ts +41 -38
  280. package/src/render/lightGrid.ts +130 -0
  281. package/src/render/mesh.ts +59 -72
  282. package/src/render/occlusion.ts +51 -7
  283. package/src/render/pointGlassTint.ts +250 -0
  284. package/src/render/pointLightSelection.ts +72 -4
  285. package/src/render/pointShadowArray.ts +188 -22
  286. package/src/render/pointShadowMap.ts +41 -0
  287. package/src/render/rainField.ts +39 -5
  288. package/src/render/recon/changeFrames.ts +24 -0
  289. package/src/render/recon/lateDepth.ts +83 -0
  290. package/src/render/recon/lateRouting.ts +29 -0
  291. package/src/render/recon/mover.ts +118 -0
  292. package/src/render/recon/resolve.ts +206 -20
  293. package/src/render/renderQuality.ts +47 -0
  294. package/src/render/rendererHarness.ts +16 -0
  295. package/src/render/sceneTarget.ts +62 -11
  296. package/src/render/shaders/depth.ts +118 -15
  297. package/src/render/shaders/flat/directionalShadow.ts +97 -6
  298. package/src/render/shaders/flat/driftLight.ts +32 -7
  299. package/src/render/shaders/flat/glassTint.ts +25 -0
  300. package/src/render/shaders/flat/index.ts +22 -3
  301. package/src/render/shaders/flat/lampGlass.ts +185 -0
  302. package/src/render/shaders/flat/main.ts +190 -38
  303. package/src/render/shaders/flat/pointShadow.ts +2 -0
  304. package/src/render/shaders/flat/preamble.ts +96 -26
  305. package/src/render/shaders/flat/surfaceEffects.ts +152 -0
  306. package/src/render/shaders/generated/depth.wgsl.ts +237 -7
  307. package/src/render/shaders/generated/flat.wgsl.ts +1685 -1621
  308. package/src/render/shaders/generated/globalMedium.wgsl.ts +3 -13
  309. package/src/render/shaders/generated/lightVolume.wgsl.ts +23 -17
  310. package/src/render/shaders/generated/octahedralResolve.wgsl.ts +30 -0
  311. package/src/render/shaders/generated/rush.wgsl.ts +21 -16
  312. package/src/render/shaders/generated/sdfText.wgsl.ts +2 -2
  313. package/src/render/shaders/generated/text.wgsl.ts +1 -1
  314. package/src/render/shaders/gi/driftLight.wgsl.ts +27 -4
  315. package/src/render/shaders/globalMedium.ts +6 -6
  316. package/src/render/shaders/gpudriven/blendRaster.wgsl.ts +88 -1
  317. package/src/render/shaders/gpudriven/cull.wgsl.ts +11 -2
  318. package/src/render/shaders/gpudriven/materialTable.wgsl.ts +3 -1
  319. package/src/render/shaders/gpudriven/shade.wgsl.ts +50 -1
  320. package/src/render/shaders/gpudriven/sunGlass.wgsl.ts +139 -0
  321. package/src/render/shaders/lightVolume.ts +6 -6
  322. package/src/render/shaders/octahedralResolve.ts +50 -17
  323. package/src/render/shaders/recon/lateDepth.wgsl.ts +72 -0
  324. package/src/render/shaders/recon/motion.wgsl.ts +84 -0
  325. package/src/render/shaders/recon/resolve.wgsl.ts +161 -27
  326. package/src/render/shaders/rush.ts +11 -1
  327. package/src/render/shaders/sdfText.ts +3 -2
  328. package/src/render/shaders/text.ts +18 -9
  329. package/src/render/shadowCasters.ts +28 -1
  330. package/src/render/shadowMap.ts +231 -37
  331. package/src/render/sunGlassTint.ts +86 -0
  332. package/src/render/surfaceEffects.ts +132 -0
  333. package/src/render/surfaceScene.ts +16 -0
  334. package/src/render/surfaceTexture.ts +110 -19
  335. package/src/render/temporalAa.ts +78 -14
  336. package/src/render/textureSource.ts +72 -0
  337. package/src/render/translucentQueue.ts +20 -1
  338. package/src/render/vertexDefaults.ts +2 -0
  339. package/src/world/hlod.ts +290 -0
@@ -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]],
@@ -0,0 +1,117 @@
1
+ /**
2
+ * The second pipeline's sunlight through glass, as a reference: `glassTintAt` over the glass depth
3
+ * and tint maps the GPU-driven pass draws from the sun, for `shade.wgsl.ts`'s lookup to be read
4
+ * against.
5
+ *
6
+ * **The forward path's rule, not a second one** (`glassShadow.ts`, and `sunGlassLookup` in
7
+ * `flat/directionalShadow.ts`): the outline from taps at the base filter radius that count a pane
8
+ * only where the receiver is behind it, and the colour from spread taps read coarse, unmixed from
9
+ * the ground around the pane by its clarity and weighed by its share. Frost widens only the
10
+ * colour's taps, so the light a frosted pane passes is the light a clear one does.
11
+ *
12
+ * **What differs is where two numbers come from.** A compute invocation has no derivatives, so the
13
+ * map's scale in metres is the caller's (`uvPerMetre`, which an orthographic light matrix states
14
+ * outright); and a coarse level here is a box average over the spread's footprint, which is what a
15
+ * mip level approximates on the device. `scripts/gpu-parity.mjs` holds the device's per-tap
16
+ * arithmetic to `glassShadow.ts`; the sampling is what the glass page measures.
17
+ */
18
+ import { frostRadius, spreadTint, unmixTint, type GlassTexel } from '../glassShadow.ts';
19
+ import { SHADOW_DEPTH_BIAS, SHADOW_PCF_OFFSETS, SHADOW_TEXEL_SCALE } from './shadow.ts';
20
+
21
+ /** The two maps the sun's glass pass draws, square, row-major from v = 0. */
22
+ export interface SunGlassMaps {
23
+ readonly size: number;
24
+ /** The nearest pane's depth a texel, 1 where no pane was drawn. */
25
+ readonly glassDepth: Float32Array;
26
+ /** What the panes on a texel's ray let through, and their clarity: four floats a texel. */
27
+ readonly tint: Float32Array;
28
+ }
29
+
30
+ export interface SunGlassSettings {
31
+ /** Metres the light's depth range spans, which turns a depth difference into a distance. */
32
+ readonly depthSpan: number;
33
+ /** Map units a metre across the light, which turns a spread in metres into one on the map. */
34
+ readonly uvPerMetre: number;
35
+ readonly taps: number;
36
+ }
37
+
38
+ const centre: GlassTexel = { r: 1, g: 1, b: 1, clarity: 1 };
39
+ const coarse: GlassTexel = { r: 1, g: 1, b: 1, clarity: 1 };
40
+ const pane: GlassTexel = { r: 1, g: 1, b: 1, clarity: 1 };
41
+ const found: GlassTexel = { r: 0, g: 0, b: 0, clarity: 0 };
42
+
43
+ function index(maps: SunGlassMaps, u: number, v: number): number {
44
+ const x = Math.min(maps.size - 1, Math.max(0, Math.floor(u * maps.size)));
45
+ const y = Math.min(maps.size - 1, Math.max(0, Math.floor(v * maps.size)));
46
+ return y * maps.size + x;
47
+ }
48
+
49
+ /** The average of every texel within `radius` of (u, v): a mip level's footprint, in the large. */
50
+ function coarseAt(maps: SunGlassMaps, u: number, v: number, radius: number, out: GlassTexel): void {
51
+ const reach = radius * maps.size;
52
+ const x0 = Math.max(0, Math.floor(u * maps.size - reach));
53
+ const x1 = Math.min(maps.size - 1, Math.floor(u * maps.size + reach));
54
+ const y0 = Math.max(0, Math.floor(v * maps.size - reach));
55
+ const y1 = Math.min(maps.size - 1, Math.floor(v * maps.size + reach));
56
+ out.r = 0;
57
+ out.g = 0;
58
+ out.b = 0;
59
+ out.clarity = 0;
60
+ let n = 0;
61
+ for (let y = y0; y <= y1; y++) {
62
+ for (let x = x0; x <= x1; x++) {
63
+ const at = (y * maps.size + x) * 4;
64
+ out.r += maps.tint[at] as number;
65
+ out.g += maps.tint[at + 1] as number;
66
+ out.b += maps.tint[at + 2] as number;
67
+ out.clarity += maps.tint[at + 3] as number;
68
+ n++;
69
+ }
70
+ }
71
+ out.r /= n;
72
+ out.g /= n;
73
+ out.b /= n;
74
+ out.clarity /= n;
75
+ }
76
+
77
+ /** What the sun keeps through glass at a receiver at map position (u, v) and light depth `depth`. */
78
+ export function glassTintAt(
79
+ u: number,
80
+ v: number,
81
+ depth: number,
82
+ maps: SunGlassMaps,
83
+ settings: SunGlassSettings,
84
+ out: GlassTexel,
85
+ ): GlassTexel {
86
+ out.r = 1;
87
+ out.g = 1;
88
+ out.b = 1;
89
+ out.clarity = 1;
90
+ if (u < 0 || u > 1 || v < 0 || v > 1) return out;
91
+ const texel = SHADOW_TEXEL_SCALE / maps.size;
92
+ const bias = SHADOW_DEPTH_BIAS / settings.depthSpan;
93
+ const taps = Math.min(settings.taps, SHADOW_PCF_OFFSETS.length);
94
+
95
+ const at = index(maps, u, v);
96
+ centre.clarity = maps.tint[at * 4 + 3] as number;
97
+ const behind = Math.max(depth - (maps.glassDepth[at] as number), 0) * settings.depthSpan;
98
+ const radius = frostRadius(centre.clarity, behind * settings.uvPerMetre, texel);
99
+
100
+ let cover = 0;
101
+ found.r = 0;
102
+ found.g = 0;
103
+ found.b = 0;
104
+ let shares = 0;
105
+ for (let i = 0; i < taps; i++) {
106
+ const offset = SHADOW_PCF_OFFSETS[i] as readonly [number, number];
107
+ const nearest = maps.glassDepth[index(maps, u + offset[0] * texel, v + offset[1] * texel)];
108
+ if (depth > (nearest as number) + bias) cover++;
109
+ coarseAt(maps, u + offset[0] * radius, v + offset[1] * radius, radius, coarse);
110
+ const share = unmixTint(coarse, centre.clarity, pane);
111
+ found.r += pane.r * share;
112
+ found.g += pane.g * share;
113
+ found.b += pane.b * share;
114
+ shares += share;
115
+ }
116
+ return spreadTint(cover / Math.max(taps, 1), found, shares, out);
117
+ }