@pmndrs/vdb 0.0.0-stage → 0.1.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 (390) hide show
  1. package/CHANGELOG.md +137 -0
  2. package/LICENSE +21 -0
  3. package/README.md +448 -2
  4. package/THIRD_PARTY_NOTICES.md +147 -0
  5. package/dist/core/compute/atlas.d.ts +94 -0
  6. package/dist/core/compute/atlas.js +98 -0
  7. package/dist/core/compute/atlas.js.map +1 -0
  8. package/dist/core/compute/index.d.ts +13 -0
  9. package/dist/core/compute/index.js +9 -0
  10. package/dist/core/compute/index.js.map +1 -0
  11. package/dist/core/compute/pyramid.d.ts +105 -0
  12. package/dist/core/compute/pyramid.js +455 -0
  13. package/dist/core/compute/pyramid.js.map +1 -0
  14. package/dist/core/compute/shared.d.ts +115 -0
  15. package/dist/core/compute/shared.js +211 -0
  16. package/dist/core/compute/shared.js.map +1 -0
  17. package/dist/core/compute/stats.d.ts +71 -0
  18. package/dist/core/compute/stats.js +188 -0
  19. package/dist/core/compute/stats.js.map +1 -0
  20. package/dist/core/compute/value-transform.d.ts +23 -0
  21. package/dist/core/compute/value-transform.js +58 -0
  22. package/dist/core/compute/value-transform.js.map +1 -0
  23. package/dist/core/errors.d.ts +11 -0
  24. package/dist/core/errors.js +9 -0
  25. package/dist/core/errors.js.map +1 -0
  26. package/dist/core/index.d.ts +30 -0
  27. package/dist/core/index.js +27 -0
  28. package/dist/core/index.js.map +1 -0
  29. package/dist/core/internal.d.ts +15 -0
  30. package/dist/core/internal.js +15 -0
  31. package/dist/core/internal.js.map +1 -0
  32. package/dist/core/pool/affine.d.ts +34 -0
  33. package/dist/core/pool/affine.js +100 -0
  34. package/dist/core/pool/affine.js.map +1 -0
  35. package/dist/core/pool/allocator.d.ts +32 -0
  36. package/dist/core/pool/allocator.js +81 -0
  37. package/dist/core/pool/allocator.js.map +1 -0
  38. package/dist/core/pool/image-header.d.ts +82 -0
  39. package/dist/core/pool/image-header.js +175 -0
  40. package/dist/core/pool/image-header.js.map +1 -0
  41. package/dist/core/pool/layout.d.ts +164 -0
  42. package/dist/core/pool/layout.js +267 -0
  43. package/dist/core/pool/layout.js.map +1 -0
  44. package/dist/core/pool/limits.d.ts +40 -0
  45. package/dist/core/pool/limits.js +79 -0
  46. package/dist/core/pool/limits.js.map +1 -0
  47. package/dist/core/pool/pool.d.ts +279 -0
  48. package/dist/core/pool/pool.js +953 -0
  49. package/dist/core/pool/pool.js.map +1 -0
  50. package/dist/core/source/dense.d.ts +19 -0
  51. package/dist/core/source/dense.js +287 -0
  52. package/dist/core/source/dense.js.map +1 -0
  53. package/dist/core/source/types.d.ts +63 -0
  54. package/dist/core/source/types.js +9 -0
  55. package/dist/core/source/types.js.map +1 -0
  56. package/dist/core/types.d.ts +28 -0
  57. package/dist/core/types.js +28 -0
  58. package/dist/core/types.js.map +1 -0
  59. package/dist/core/wgsl/api.d.ts +41 -0
  60. package/dist/core/wgsl/api.js +463 -0
  61. package/dist/core/wgsl/api.js.map +1 -0
  62. package/dist/core/wgsl/generate.d.ts +82 -0
  63. package/dist/core/wgsl/generate.js +505 -0
  64. package/dist/core/wgsl/generate.js.map +1 -0
  65. package/dist/core/wgsl/level-set.d.ts +54 -0
  66. package/dist/core/wgsl/level-set.js +194 -0
  67. package/dist/core/wgsl/level-set.js.map +1 -0
  68. package/dist/core/wgsl/pnanovdb.chunks.generated.d.ts +14 -0
  69. package/dist/core/wgsl/pnanovdb.chunks.generated.js +330 -0
  70. package/dist/core/wgsl/pnanovdb.chunks.generated.js.map +1 -0
  71. package/dist/core/wgsl/reserved.d.ts +9 -0
  72. package/dist/core/wgsl/reserved.js +26 -0
  73. package/dist/core/wgsl/reserved.js.map +1 -0
  74. package/dist/core/wgsl/types.d.ts +104 -0
  75. package/dist/core/wgsl/types.js +2 -0
  76. package/dist/core/wgsl/types.js.map +1 -0
  77. package/dist/three/coarse-first.d.ts +162 -0
  78. package/dist/three/coarse-first.js +187 -0
  79. package/dist/three/coarse-first.js.map +1 -0
  80. package/dist/three/compute.d.ts +63 -0
  81. package/dist/three/compute.js +56 -0
  82. package/dist/three/compute.js.map +1 -0
  83. package/dist/three/device.d.ts +59 -0
  84. package/dist/three/device.js +59 -0
  85. package/dist/three/device.js.map +1 -0
  86. package/dist/three/frame/blackbody.d.ts +42 -0
  87. package/dist/three/frame/blackbody.js +74 -0
  88. package/dist/three/frame/blackbody.js.map +1 -0
  89. package/dist/three/frame/chunks.d.ts +157 -0
  90. package/dist/three/frame/chunks.js +559 -0
  91. package/dist/three/frame/chunks.js.map +1 -0
  92. package/dist/three/frame/density-material.d.ts +53 -0
  93. package/dist/three/frame/density-material.js +353 -0
  94. package/dist/three/frame/density-material.js.map +1 -0
  95. package/dist/three/frame/emission-light.d.ts +167 -0
  96. package/dist/three/frame/emission-light.js +655 -0
  97. package/dist/three/frame/emission-light.js.map +1 -0
  98. package/dist/three/frame/environment-grid-material.d.ts +21 -0
  99. package/dist/three/frame/environment-grid-material.js +184 -0
  100. package/dist/three/frame/environment-grid-material.js.map +1 -0
  101. package/dist/three/frame/environment-material.d.ts +22 -0
  102. package/dist/three/frame/environment-material.js +87 -0
  103. package/dist/three/frame/environment-material.js.map +1 -0
  104. package/dist/three/frame/grid-mip-material.d.ts +12 -0
  105. package/dist/three/frame/grid-mip-material.js +61 -0
  106. package/dist/three/frame/grid-mip-material.js.map +1 -0
  107. package/dist/three/frame/index.d.ts +39 -0
  108. package/dist/three/frame/index.js +35 -0
  109. package/dist/three/frame/index.js.map +1 -0
  110. package/dist/three/frame/inject-material.d.ts +56 -0
  111. package/dist/three/frame/inject-material.js +424 -0
  112. package/dist/three/frame/inject-material.js.map +1 -0
  113. package/dist/three/frame/integrate-material.d.ts +26 -0
  114. package/dist/three/frame/integrate-material.js +198 -0
  115. package/dist/three/frame/integrate-material.js.map +1 -0
  116. package/dist/three/frame/layout.d.ts +34 -0
  117. package/dist/three/frame/layout.js +48 -0
  118. package/dist/three/frame/layout.js.map +1 -0
  119. package/dist/three/frame/lighting-material.d.ts +41 -0
  120. package/dist/three/frame/lighting-material.js +257 -0
  121. package/dist/three/frame/lighting-material.js.map +1 -0
  122. package/dist/three/frame/lights.d.ts +11 -0
  123. package/dist/three/frame/lights.js +16 -0
  124. package/dist/three/frame/lights.js.map +1 -0
  125. package/dist/three/frame/medium.d.ts +21 -0
  126. package/dist/three/frame/medium.js +29 -0
  127. package/dist/three/frame/medium.js.map +1 -0
  128. package/dist/three/frame/neural.d.ts +14 -0
  129. package/dist/three/frame/neural.js +105 -0
  130. package/dist/three/frame/neural.js.map +1 -0
  131. package/dist/three/frame/phase.d.ts +44 -0
  132. package/dist/three/frame/phase.js +80 -0
  133. package/dist/three/frame/phase.js.map +1 -0
  134. package/dist/three/frame/renderer.d.ts +493 -0
  135. package/dist/three/frame/renderer.js +1498 -0
  136. package/dist/three/frame/renderer.js.map +1 -0
  137. package/dist/three/frame/shadow-density-material.d.ts +30 -0
  138. package/dist/three/frame/shadow-density-material.js +123 -0
  139. package/dist/three/frame/shadow-density-material.js.map +1 -0
  140. package/dist/three/frame/spectrum.d.ts +18 -0
  141. package/dist/three/frame/spectrum.js +36 -0
  142. package/dist/three/frame/spectrum.js.map +1 -0
  143. package/dist/three/frame/temporal-material.d.ts +29 -0
  144. package/dist/three/frame/temporal-material.js +152 -0
  145. package/dist/three/frame/temporal-material.js.map +1 -0
  146. package/dist/three/frame/three-internals.d.ts +65 -0
  147. package/dist/three/frame/three-internals.js +150 -0
  148. package/dist/three/frame/three-internals.js.map +1 -0
  149. package/dist/three/frame/transmittance-material.d.ts +31 -0
  150. package/dist/three/frame/transmittance-material.js +315 -0
  151. package/dist/three/frame/transmittance-material.js.map +1 -0
  152. package/dist/three/frame/view.d.ts +103 -0
  153. package/dist/three/frame/view.js +296 -0
  154. package/dist/three/frame/view.js.map +1 -0
  155. package/dist/three/frame/volume-shadow-material.d.ts +36 -0
  156. package/dist/three/frame/volume-shadow-material.js +128 -0
  157. package/dist/three/frame/volume-shadow-material.js.map +1 -0
  158. package/dist/three/frame/volume-shadows.d.ts +153 -0
  159. package/dist/three/frame/volume-shadows.js +756 -0
  160. package/dist/three/frame/volume-shadows.js.map +1 -0
  161. package/dist/three/frame/volume.d.ts +172 -0
  162. package/dist/three/frame/volume.js +256 -0
  163. package/dist/three/frame/volume.js.map +1 -0
  164. package/dist/three/grid-node.d.ts +196 -0
  165. package/dist/three/grid-node.js +510 -0
  166. package/dist/three/grid-node.js.map +1 -0
  167. package/dist/three/index.d.ts +43 -0
  168. package/dist/three/index.js +49 -0
  169. package/dist/three/index.js.map +1 -0
  170. package/dist/three/internal.d.ts +14 -0
  171. package/dist/three/internal.js +14 -0
  172. package/dist/three/internal.js.map +1 -0
  173. package/dist/three/level-set-material.d.ts +112 -0
  174. package/dist/three/level-set-material.js +204 -0
  175. package/dist/three/level-set-material.js.map +1 -0
  176. package/dist/three/material.d.ts +194 -0
  177. package/dist/three/material.js +359 -0
  178. package/dist/three/material.js.map +1 -0
  179. package/dist/three/points.d.ts +84 -0
  180. package/dist/three/points.js +130 -0
  181. package/dist/three/points.js.map +1 -0
  182. package/dist/three/sequence.d.ts +317 -0
  183. package/dist/three/sequence.js +526 -0
  184. package/dist/three/sequence.js.map +1 -0
  185. package/dist/three/sparse-source.d.ts +233 -0
  186. package/dist/three/sparse-source.js +876 -0
  187. package/dist/three/sparse-source.js.map +1 -0
  188. package/dist/three/volume-source.d.ts +167 -0
  189. package/dist/three/volume-source.js +353 -0
  190. package/dist/three/volume-source.js.map +1 -0
  191. package/dist/tools/cpu/compute.d.ts +97 -0
  192. package/dist/tools/cpu/compute.js +218 -0
  193. package/dist/tools/cpu/compute.js.map +1 -0
  194. package/dist/tools/cpu/level-set.d.ts +72 -0
  195. package/dist/tools/cpu/level-set.js +261 -0
  196. package/dist/tools/cpu/level-set.js.map +1 -0
  197. package/dist/tools/cpu/march.d.ts +206 -0
  198. package/dist/tools/cpu/march.js +575 -0
  199. package/dist/tools/cpu/march.js.map +1 -0
  200. package/dist/tools/cpu/probe-coords.d.ts +70 -0
  201. package/dist/tools/cpu/probe-coords.js +116 -0
  202. package/dist/tools/cpu/probe-coords.js.map +1 -0
  203. package/dist/tools/cpu/read-value.d.ts +31 -0
  204. package/dist/tools/cpu/read-value.js +336 -0
  205. package/dist/tools/cpu/read-value.js.map +1 -0
  206. package/dist/tools/cpu/sample-trilinear.d.ts +30 -0
  207. package/dist/tools/cpu/sample-trilinear.js +156 -0
  208. package/dist/tools/cpu/sample-trilinear.js.map +1 -0
  209. package/dist/tools/cpu/stride-tables.d.ts +56 -0
  210. package/dist/tools/cpu/stride-tables.generated.d.ts +1058 -0
  211. package/dist/tools/cpu/stride-tables.generated.js +1266 -0
  212. package/dist/tools/cpu/stride-tables.generated.js.map +1 -0
  213. package/dist/tools/cpu/stride-tables.js +78 -0
  214. package/dist/tools/cpu/stride-tables.js.map +1 -0
  215. package/dist/tools/errors.d.ts +57 -0
  216. package/dist/tools/errors.js +58 -0
  217. package/dist/tools/errors.js.map +1 -0
  218. package/dist/tools/frame/blackbody.d.ts +42 -0
  219. package/dist/tools/frame/blackbody.js +116 -0
  220. package/dist/tools/frame/blackbody.js.map +1 -0
  221. package/dist/tools/frame/emission.d.ts +128 -0
  222. package/dist/tools/frame/emission.js +282 -0
  223. package/dist/tools/frame/emission.js.map +1 -0
  224. package/dist/tools/frame/environment.d.ts +105 -0
  225. package/dist/tools/frame/environment.js +268 -0
  226. package/dist/tools/frame/environment.js.map +1 -0
  227. package/dist/tools/frame/grid.d.ts +302 -0
  228. package/dist/tools/frame/grid.js +703 -0
  229. package/dist/tools/frame/grid.js.map +1 -0
  230. package/dist/tools/frame/index.d.ts +136 -0
  231. package/dist/tools/frame/index.js +348 -0
  232. package/dist/tools/frame/index.js.map +1 -0
  233. package/dist/tools/frame/medium.d.ts +61 -0
  234. package/dist/tools/frame/medium.js +99 -0
  235. package/dist/tools/frame/medium.js.map +1 -0
  236. package/dist/tools/frame/mie-table.d.ts +6 -0
  237. package/dist/tools/frame/mie-table.js +16 -0
  238. package/dist/tools/frame/mie-table.js.map +1 -0
  239. package/dist/tools/frame/mie.d.ts +54 -0
  240. package/dist/tools/frame/mie.js +176 -0
  241. package/dist/tools/frame/mie.js.map +1 -0
  242. package/dist/tools/frame/neural-weights.d.ts +9 -0
  243. package/dist/tools/frame/neural-weights.js +245 -0
  244. package/dist/tools/frame/neural-weights.js.map +1 -0
  245. package/dist/tools/frame/neural.d.ts +80 -0
  246. package/dist/tools/frame/neural.js +150 -0
  247. package/dist/tools/frame/neural.js.map +1 -0
  248. package/dist/tools/frame/pyramid-layout.d.ts +38 -0
  249. package/dist/tools/frame/pyramid-layout.js +53 -0
  250. package/dist/tools/frame/pyramid-layout.js.map +1 -0
  251. package/dist/tools/frame/shadows.d.ts +80 -0
  252. package/dist/tools/frame/shadows.js +182 -0
  253. package/dist/tools/frame/shadows.js.map +1 -0
  254. package/dist/tools/frame/sparse.d.ts +65 -0
  255. package/dist/tools/frame/sparse.js +191 -0
  256. package/dist/tools/frame/sparse.js.map +1 -0
  257. package/dist/tools/frame/spectral.d.ts +203 -0
  258. package/dist/tools/frame/spectral.js +550 -0
  259. package/dist/tools/frame/spectral.js.map +1 -0
  260. package/dist/tools/index.d.ts +106 -0
  261. package/dist/tools/index.js +88 -0
  262. package/dist/tools/index.js.map +1 -0
  263. package/dist/tools/internal.d.ts +46 -0
  264. package/dist/tools/internal.js +31 -0
  265. package/dist/tools/internal.js.map +1 -0
  266. package/dist/tools/nanovdb/build-from-vdb.d.ts +50 -0
  267. package/dist/tools/nanovdb/build-from-vdb.js +92 -0
  268. package/dist/tools/nanovdb/build-from-vdb.js.map +1 -0
  269. package/dist/tools/nanovdb/bytes.d.ts +220 -0
  270. package/dist/tools/nanovdb/bytes.js +345 -0
  271. package/dist/tools/nanovdb/bytes.js.map +1 -0
  272. package/dist/tools/nanovdb/coarse-first.d.ts +62 -0
  273. package/dist/tools/nanovdb/coarse-first.js +86 -0
  274. package/dist/tools/nanovdb/coarse-first.js.map +1 -0
  275. package/dist/tools/nanovdb/index.d.ts +32 -0
  276. package/dist/tools/nanovdb/index.js +24 -0
  277. package/dist/tools/nanovdb/index.js.map +1 -0
  278. package/dist/tools/nanovdb/inspect.d.ts +24 -0
  279. package/dist/tools/nanovdb/inspect.js +63 -0
  280. package/dist/tools/nanovdb/inspect.js.map +1 -0
  281. package/dist/tools/nanovdb/leaf-codec.d.ts +85 -0
  282. package/dist/tools/nanovdb/leaf-codec.js +320 -0
  283. package/dist/tools/nanovdb/leaf-codec.js.map +1 -0
  284. package/dist/tools/nanovdb/quantize.d.ts +43 -0
  285. package/dist/tools/nanovdb/quantize.js +277 -0
  286. package/dist/tools/nanovdb/quantize.js.map +1 -0
  287. package/dist/tools/nanovdb/resample.d.ts +160 -0
  288. package/dist/tools/nanovdb/resample.js +679 -0
  289. package/dist/tools/nanovdb/resample.js.map +1 -0
  290. package/dist/tools/nanovdb/serialize.d.ts +145 -0
  291. package/dist/tools/nanovdb/serialize.js +395 -0
  292. package/dist/tools/nanovdb/serialize.js.map +1 -0
  293. package/dist/tools/nanovdb/stats.d.ts +58 -0
  294. package/dist/tools/nanovdb/stats.js +149 -0
  295. package/dist/tools/nanovdb/stats.js.map +1 -0
  296. package/dist/tools/nanovdb/transform.d.ts +26 -0
  297. package/dist/tools/nanovdb/transform.js +91 -0
  298. package/dist/tools/nanovdb/transform.js.map +1 -0
  299. package/dist/tools/nanovdb/tree.d.ts +175 -0
  300. package/dist/tools/nanovdb/tree.js +518 -0
  301. package/dist/tools/nanovdb/tree.js.map +1 -0
  302. package/dist/tools/nanovdb/write-nvdb.d.ts +39 -0
  303. package/dist/tools/nanovdb/write-nvdb.js +210 -0
  304. package/dist/tools/nanovdb/write-nvdb.js.map +1 -0
  305. package/dist/tools/nvdb-file.d.ts +195 -0
  306. package/dist/tools/nvdb-file.js +606 -0
  307. package/dist/tools/nvdb-file.js.map +1 -0
  308. package/dist/tools/nvdb-stream.d.ts +138 -0
  309. package/dist/tools/nvdb-stream.js +365 -0
  310. package/dist/tools/nvdb-stream.js.map +1 -0
  311. package/dist/tools/vdb/bit-utils.d.ts +20 -0
  312. package/dist/tools/vdb/bit-utils.js +42 -0
  313. package/dist/tools/vdb/bit-utils.js.map +1 -0
  314. package/dist/tools/vdb/blosc.d.ts +56 -0
  315. package/dist/tools/vdb/blosc.js +231 -0
  316. package/dist/tools/vdb/blosc.js.map +1 -0
  317. package/dist/tools/vdb/byte-reader.d.ts +40 -0
  318. package/dist/tools/vdb/byte-reader.js +139 -0
  319. package/dist/tools/vdb/byte-reader.js.map +1 -0
  320. package/dist/tools/vdb/compression.d.ts +73 -0
  321. package/dist/tools/vdb/compression.js +249 -0
  322. package/dist/tools/vdb/compression.js.map +1 -0
  323. package/dist/tools/vdb/errors.d.ts +10 -0
  324. package/dist/tools/vdb/errors.js +11 -0
  325. package/dist/tools/vdb/errors.js.map +1 -0
  326. package/dist/tools/vdb/grid.d.ts +37 -0
  327. package/dist/tools/vdb/grid.js +147 -0
  328. package/dist/tools/vdb/grid.js.map +1 -0
  329. package/dist/tools/vdb/half-float.d.ts +9 -0
  330. package/dist/tools/vdb/half-float.js +24 -0
  331. package/dist/tools/vdb/half-float.js.map +1 -0
  332. package/dist/tools/vdb/index.d.ts +3 -0
  333. package/dist/tools/vdb/index.js +3 -0
  334. package/dist/tools/vdb/index.js.map +1 -0
  335. package/dist/tools/vdb/inflate.d.ts +24 -0
  336. package/dist/tools/vdb/inflate.js +334 -0
  337. package/dist/tools/vdb/inflate.js.map +1 -0
  338. package/dist/tools/vdb/metadata.d.ts +17 -0
  339. package/dist/tools/vdb/metadata.js +72 -0
  340. package/dist/tools/vdb/metadata.js.map +1 -0
  341. package/dist/tools/vdb/parse.d.ts +25 -0
  342. package/dist/tools/vdb/parse.js +171 -0
  343. package/dist/tools/vdb/parse.js.map +1 -0
  344. package/dist/tools/vdb/point-render-data.d.ts +31 -0
  345. package/dist/tools/vdb/point-render-data.js +57 -0
  346. package/dist/tools/vdb/point-render-data.js.map +1 -0
  347. package/dist/tools/vdb/points.d.ts +160 -0
  348. package/dist/tools/vdb/points.js +794 -0
  349. package/dist/tools/vdb/points.js.map +1 -0
  350. package/dist/tools/vdb/transform.d.ts +23 -0
  351. package/dist/tools/vdb/transform.js +54 -0
  352. package/dist/tools/vdb/transform.js.map +1 -0
  353. package/dist/tools/vdb/tree.d.ts +111 -0
  354. package/dist/tools/vdb/tree.js +228 -0
  355. package/dist/tools/vdb/tree.js.map +1 -0
  356. package/dist/tools/vdb/types.d.ts +138 -0
  357. package/dist/tools/vdb/types.js +3 -0
  358. package/dist/tools/vdb/types.js.map +1 -0
  359. package/dist/tools/wvdb/bake.d.ts +116 -0
  360. package/dist/tools/wvdb/bake.js +311 -0
  361. package/dist/tools/wvdb/bake.js.map +1 -0
  362. package/dist/tools/wvdb/dense.d.ts +112 -0
  363. package/dist/tools/wvdb/dense.js +309 -0
  364. package/dist/tools/wvdb/dense.js.map +1 -0
  365. package/dist/tools/wvdb/format.d.ts +161 -0
  366. package/dist/tools/wvdb/format.js +303 -0
  367. package/dist/tools/wvdb/format.js.map +1 -0
  368. package/dist/tools/wvdb/index.d.ts +13 -0
  369. package/dist/tools/wvdb/index.js +14 -0
  370. package/dist/tools/wvdb/index.js.map +1 -0
  371. package/dist/tools/wvdb/level-set.d.ts +48 -0
  372. package/dist/tools/wvdb/level-set.js +153 -0
  373. package/dist/tools/wvdb/level-set.js.map +1 -0
  374. package/dist/tools/wvdb/nanovdb.d.ts +54 -0
  375. package/dist/tools/wvdb/nanovdb.js +60 -0
  376. package/dist/tools/wvdb/nanovdb.js.map +1 -0
  377. package/dist/tools/wvdb/read.d.ts +260 -0
  378. package/dist/tools/wvdb/read.js +743 -0
  379. package/dist/tools/wvdb/read.js.map +1 -0
  380. package/dist/tools/wvdb/sha256.d.ts +7 -0
  381. package/dist/tools/wvdb/sha256.js +76 -0
  382. package/dist/tools/wvdb/sha256.js.map +1 -0
  383. package/dist/tools/wvdb/write.d.ts +127 -0
  384. package/dist/tools/wvdb/write.js +396 -0
  385. package/dist/tools/wvdb/write.js.map +1 -0
  386. package/package.json +82 -4
  387. package/vendor/LICENSE +201 -0
  388. package/vendor/NOTICE +19 -0
  389. package/vendor/VENDOR.md +85 -0
  390. package/vendor/pnanovdb.wgsl +1539 -0
@@ -0,0 +1,1498 @@
1
+ // Portions derived from mjurczyk/openvdb (MIT), commit 75e7345 (v0.26.0):
2
+ // src/three-openvdb/frame/webgpu/FrameRenderer.js L79–135 (caster signatures, fullscreen geometry,
3
+ // targets, scenes), L144–174 (getClipRect, getMaxTextureSize), L191–246 (settings, the ported
4
+ // subset), L249–430 (fields, constructor), L431–481 (render, createProxyGeometry), L493–570
5
+ // (isFrameLighting, renderEnvironmentGrid), L572–596 (renderEnvironmentMap), L598–645
6
+ // (collectVolumes, usesShadowGrid, isNeural), L715–895 (getSliceRange, updateTargets,
7
+ // updateGridTargets), L897–1005 (renderFrame, updateFroxelArea, renderTemporal), L1007–1061
8
+ // (copyEnvironment, prepareLightScene, renderInject), L1063–1308 (renderFrameLighting,
9
+ // transmittanceUnchanged, renderShadowDensity, updateGridVersion), L1347–1474 (pool setup,
10
+ // updatePool, getFroxelRect, getCascadeCount, updateGrid, getGridRange), L1476–1716 (light
11
+ // records, visitLight, updateLights), L1718–1784 (dispose);
12
+ // src/three-openvdb/frame/webgpu/frameShaderChunks.js L800–830 (setVolumeUniforms);
13
+ // K3.4: FrameRenderer.js L232–242 (multipleScattering 'neural' default, octave settings), L291–296 and
14
+ // L642–699 (isNeural, the grid mip targets, renderGridMips), L1052–1056 and L1086–1098 (octaves,
15
+ // two-sided and neural uniforms of the inject and transmittance passes), L1132 and L1201 (octaves of the
16
+ // density and lighting passes), L506–518 and L559 (octaves in the environment grid signature and pass),
17
+ // L1213–1226 (the neural, two-sided and octave fields of the transmittance signature);
18
+ // K3.5: L176–189 (createAtlasTarget, isSpectralTarget), L335–337 (the spectral transmittance targets),
19
+ // L514 and L556 (spectral in the environment grid signature and pass), L638–640 (isSpectral), L702–713
20
+ // (getSpectralTransmittanceTarget), L725–866 (the spectral targets of updateTargets / updateGridTargets),
21
+ // L908 (environment lights in both lighting modes), L973–1003 (the alpha targets of renderTemporal),
22
+ // L1030 (the inject pass reads the scene environment), L1079–1196 (the slot group loop and the spectral maps
23
+ // of renderFrameLighting), L1210–1231 (the spectral fields of the transmittance signature), L1303
24
+ // (spectral in the grid signature);
25
+ // K3.6: L218–230 (the castShadow* settings), L269 and L456 (volumeShadows, updated before each frame), L340–350
26
+ // (gridRenderId / gridCamera, castVersion), L634–636 (usesShadowGrid with castShadows), L1250–1290
27
+ // (renderShadowDensity onlyLast), L1310–1345 (prepareShadowGrid), L1719 (dispose of the volume shadows).
28
+ // Copyright (c) mjurczyk/openvdb contributors. Licensed under the MIT License (see THIRD_PARTY_NOTICES.md).
29
+ // Changes: TypeScript; one inject, density and shadow density material per volume source (the
30
+ // source is compiled into each; DIVERGES V30); mesh shadows (K3.6, volume-shadows.ts); the neural
31
+ // network is a settable value (`neuralNetwork`; null: diffusion, with a warning); per-pass timing
32
+ // hooks and a redraw-skip counter. Divergences: DIVERGES.md (K3.1, K3.3
33
+ // and K3.4 rows), V23–V25, V30–V34.
34
+ /**
35
+ * The froxel renderer (K3.1 `'volume'` lighting, K3.3 `'frame'` lighting; R1 §2). Each
36
+ * `VDBFrameVolume` proxy calls {@link VDBFrameRenderer.render} from `onBeforeRender`; the first
37
+ * proxy in a render draws the froxels into a slice atlas, and every proxy then draws the integrate
38
+ * material, which composites the atlas over the scene up to the scene depth.
39
+ *
40
+ * `'frame'` lighting: shadow density of every caster into a world grid (≤ 4 cascades, redrawn only
41
+ * when its signature changes), transmittance from each grid voxel to ≤ 6 light slots and the sky,
42
+ * an optional environment grid ('directional' / 'spherical-harmonics' environment lights from a
43
+ * PMREM readback), the density pass per volume, one lighting pass for all volumes, then temporal
44
+ * reprojection.
45
+ */
46
+ import * as THREE from "three";
47
+ import { NodeMaterial } from "three/webgpu";
48
+ import * as TSL from "three/tsl";
49
+ import { VDBFrameEnvironmentLights, frameEnvironmentMapHeight, frameEnvironmentMapWidth, frameEnvironmentModes } from "../../tools/frame/environment.js";
50
+ import { vdbFrameCascadeCasters, vdbFrameGridCascades, vdbFrameGridRange, vdbFrameGridSignatureState, vdbFrameUpdateGridSignatures, vdbSameValues, vdbSignaturePushElements, vdbSignaturePushVector, } from "../../tools/frame/grid.js";
51
+ import { createAreaUniforms, createEnvironmentGridUniforms, createFroxelUniforms, createGridUniforms, createLightUniforms, createTileUniforms, frameMaxHemisphereLights, frameMaxLights, frameMaxShadowCascades, frameTileVertex, } from "./chunks.js";
52
+ import { VDBFrameDensityMaterial, createDensityShadowUniforms, frameDensityEnvironment } from "./density-material.js";
53
+ import { VDBFrameEnvironmentMapMaterial } from "./environment-material.js";
54
+ import { VDBFrameEnvironmentGridMaterial } from "./environment-grid-material.js";
55
+ import { VDBFrameInjectMaterial } from "./inject-material.js";
56
+ import { VDBFrameIntegrateMaterial } from "./integrate-material.js";
57
+ import { getAtlasLayout, getFroxelSize } from "./layout.js";
58
+ import { VDBFrameLightingMaterial } from "./lighting-material.js";
59
+ import { getFramePhase, getFramePhaseSpectrum, getFrameMultipleScattering, getFrameScatteringOctaves, getFrameTwoSidedDiffusion, isFrameDiffusion } from "./phase.js";
60
+ import { VDBFrameGridMipMaterial } from "./grid-mip-material.js";
61
+ import { vdbFrameNeuralCheck } from "../../tools/frame/neural.js";
62
+ import { vdbFrameNeuralWeights } from "../../tools/frame/neural-weights.js";
63
+ import { VDBFrameShadowDensityMaterial } from "./shadow-density-material.js";
64
+ import { getFrameExtinctionSpectrum, getFrameSpectralGroups } from "./spectrum.js";
65
+ import { VDBFrameTemporalMaterial } from "./temporal-material.js";
66
+ import { VDBFrameTransmittanceMaterial, frameNeuralMipLevels, frameShadowLightSlots } from "./transmittance-material.js";
67
+ import { VDBFrameVolumeShadows } from "./volume-shadows.js";
68
+ import { FrameVolumeCollector, getFrameFadeRadius, getFroxelArea, getFroxelRect, getFroxelSliceRange } from "./view.js";
69
+ const { outputStruct, vec4 } = TSL;
70
+ const clearColor = new THREE.Color();
71
+ const vector3 = new THREE.Vector3();
72
+ const froxelSize = { width: 1, height: 1 };
73
+ const lightPosition = new THREE.Vector3();
74
+ const lightTarget = new THREE.Vector3();
75
+ const lightColor = new THREE.Color();
76
+ const cameraPosition = new THREE.Vector3();
77
+ const extinctionSpectrum = new THREE.Vector3();
78
+ // NOTE Instanced, then frame volume proxies skip the draw with instanceCount 0 (three.js does not draw 0 instances)
79
+ export const createFullscreenGeometry = () => {
80
+ const geometry = new THREE.InstancedBufferGeometry();
81
+ geometry.instanceCount = 1;
82
+ geometry.setAttribute("position", new THREE.Float32BufferAttribute([-1, -1, 0, 3, -1, 0, -1, 3, 0], 3));
83
+ return geometry;
84
+ };
85
+ const tileQuadPosition = new THREE.Float32BufferAttribute([-1, -1, 0, 1, -1, 0, 1, 1, 0, -1, -1, 0, 1, 1, 0, -1, 1, 0], 3);
86
+ const createRenderTarget = (width, height, options = {}) => new THREE.RenderTarget(width, height, {
87
+ type: THREE.HalfFloatType,
88
+ format: THREE.RGBAFormat,
89
+ minFilter: THREE.LinearFilter,
90
+ magFilter: THREE.LinearFilter,
91
+ generateMipmaps: false,
92
+ depthBuffer: false,
93
+ ...options,
94
+ });
95
+ // NOTE Froxel atlas. Spectral: a second target with the red and blue alpha (and the green alpha in w for blending).
96
+ const createAtlasTarget = (width, height, spectral) => {
97
+ if (!spectral) {
98
+ return createRenderTarget(width, height);
99
+ }
100
+ const target = createRenderTarget(width, height, { count: 2 });
101
+ target.textures[1].format = THREE.RGFormat;
102
+ return target;
103
+ };
104
+ const isSpectralTarget = (target) => target.textures.length > 1;
105
+ const createScene = () => {
106
+ const scene = new THREE.Scene();
107
+ scene.matrixWorldAutoUpdate = false;
108
+ return scene;
109
+ };
110
+ // NOTE Instanced tile quads: one instance for each slice
111
+ const createTileGeometry = () => {
112
+ const geometry = new THREE.InstancedBufferGeometry();
113
+ geometry.instanceCount = 0;
114
+ geometry.setAttribute("position", tileQuadPosition);
115
+ return geometry;
116
+ };
117
+ // NOTE Clip rectangle of a froxel cell rectangle, padding cells larger on each side. An empty rectangle stays empty.
118
+ const clipRect = { x0: 0, y0: 0, x1: 0, y1: 0 };
119
+ const getClipRect = (rect, width, height, padding) => {
120
+ if (rect.z <= rect.x || rect.w <= rect.y) {
121
+ clipRect.x0 = clipRect.y0 = clipRect.x1 = clipRect.y1 = 0.0;
122
+ return clipRect;
123
+ }
124
+ clipRect.x0 = (Math.max(0, rect.x - padding) / width) * 2.0 - 1.0;
125
+ clipRect.y0 = (Math.max(0, rect.y - padding) / height) * 2.0 - 1.0;
126
+ clipRect.x1 = (Math.min(width, rect.z + padding) / width) * 2.0 - 1.0;
127
+ clipRect.y1 = (Math.min(height, rect.w + padding) / height) * 2.0 - 1.0;
128
+ return clipRect;
129
+ };
130
+ const getMaxTextureSize = (renderer) => {
131
+ const backend = renderer.backend;
132
+ return backend.device ? backend.device.limits.maxTextureDimension2D : Infinity;
133
+ };
134
+ // NOTE What the shadow density pass reads of a caster's source: its identity, version and LOD mode
135
+ // (his pushCasterMaps pushes the texture ids and versions). A new source or a replaced grid draws again.
136
+ const sourceIds = new WeakMap();
137
+ let nextSourceId = 1;
138
+ const pushCasterSource = (values, frameVolume) => {
139
+ const source = frameVolume.source;
140
+ let id = sourceIds.get(source);
141
+ if (id === undefined) {
142
+ id = nextSourceId++;
143
+ sourceIds.set(source, id);
144
+ }
145
+ values.push(id, source.version ?? -1, source.lodLevels, source.lod === "node-average" ? 2 : source.lod === "none" ? 0 : 1);
146
+ if (source.textures) {
147
+ for (const name of ["density", "mask", "albedo"]) {
148
+ const t = source.textures[name];
149
+ values.push(t ? t.map.id : -1, t ? t.map.version : -1, t?.mip ? t.mip.id : -1, t?.mip ? t.mip.version : -1);
150
+ }
151
+ }
152
+ };
153
+ // NOTE An id for each network object (the transmittance signature: a swapped network draws again)
154
+ const networkIds = new WeakMap();
155
+ let nextNetworkId = 1;
156
+ const networkId = (network) => {
157
+ let id = networkIds.get(network);
158
+ if (id === undefined) {
159
+ id = nextNetworkId++;
160
+ networkIds.set(network, id);
161
+ }
162
+ return id;
163
+ };
164
+ /** His `FrameRenderer` (`'volume'` and `'frame'` lighting). One instance can serve many volumes and cameras. */
165
+ export class VDBFrameRenderer {
166
+ settings = {
167
+ width: 512,
168
+ height: 512,
169
+ pixelsPerFroxel: 0,
170
+ depth: 24,
171
+ subSteps: 32,
172
+ near: 0.1,
173
+ far: Infinity,
174
+ fadeRadius: Infinity,
175
+ jitter: false,
176
+ temporal: true,
177
+ temporalBlend: 0.2,
178
+ lighting: "frame",
179
+ shadowResolution: 64,
180
+ shadowCascades: 1,
181
+ shadowDistance: 512,
182
+ phase: "henyey-greenstein",
183
+ phaseG: 0.8,
184
+ phaseBackG: -0.3,
185
+ phaseBackWeight: 0.2,
186
+ phaseGExponent: 0.0,
187
+ srgbBlending: true,
188
+ environmentLighting: "directional",
189
+ environmentSamples: 8,
190
+ environmentShOrder: 1,
191
+ multipleScattering: "neural",
192
+ scatteringOctaves: 4,
193
+ scatteringOctaveExtinction: 0.5,
194
+ scatteringOctaveContribution: 0.5,
195
+ scatteringOctavePhase: 0.5,
196
+ spectral: false,
197
+ castShadows: false,
198
+ castShadowMapSize: 512,
199
+ castShadowCascades: 1,
200
+ castShadowDistance: 4000,
201
+ castShadowSteps: 32,
202
+ castShadowUpdatesPerFrame: 1,
203
+ castShadowCoarse: false,
204
+ };
205
+ /**
206
+ * The network of multipleScattering `'neural'` (default: his trained weights, `@pmndrs/vdb/tools`
207
+ * `vdbFrameNeuralWeights`; see THIRD_PARTY_NOTICES.md for their provenance). Set another network of the same
208
+ * shape to swap it, or null to run `'neural'` as 1D diffusion (warns once). Not his (his is a module import).
209
+ */
210
+ neuralNetwork = vdbFrameNeuralWeights;
211
+ /** Per-pass hooks (timing, debugging). */
212
+ passHook = null;
213
+ /**
214
+ * Timing/debug hook: proxies draw the integrate pass over the last froxel atlas without rendering
215
+ * the froxels again (the per-pass timing harness times integrate alone with it). Not his.
216
+ */
217
+ holdFroxels = false;
218
+ /** Redraws and skips of the signature-cached 'frame' passes (the K3.3 redraw-skip rate). Not his. */
219
+ redrawStats = {
220
+ shadowDensity: { drawn: 0, skipped: 0 },
221
+ transmittance: { drawn: 0, skipped: 0 },
222
+ environmentGrid: { drawn: 0, skipped: 0 },
223
+ };
224
+ // NOTE TSL uniform nodes. Passes share them, the CPU writes .value.
225
+ froxelUniforms = createFroxelUniforms();
226
+ // NOTE Canvas and drawing buffer sizes that settings.pixelsPerFroxel holds (layout.ts getFroxelSize)
227
+ froxelHeldSize = { canvasX: 0, canvasY: 0, bufferX: 0, bufferY: 0 };
228
+ froxelTileUniforms = createTileUniforms();
229
+ // NOTE Froxel area of all volumes. Passes over the froxel grid draw only this area.
230
+ froxelAreaUniforms = createAreaUniforms();
231
+ froxelArea = { rect: new THREE.Vector4(), first: 0, count: 0 };
232
+ areaGeometry = createTileGeometry();
233
+ gridUniforms = createGridUniforms(this.froxelUniforms.frameJitter);
234
+ gridTileUniforms = createTileUniforms();
235
+ densityShadowUniforms = createDensityShadowUniforms();
236
+ lightUniforms = createLightUniforms();
237
+ environmentGridUniforms = createEnvironmentGridUniforms();
238
+ // NOTE Octaves of the settings (getFrameScatteringOctaves), for the signatures
239
+ scatteringOctaves = new THREE.Vector4();
240
+ integrateMaterial;
241
+ lightingMaterial;
242
+ transmittanceMaterial;
243
+ temporalMaterial;
244
+ // NOTE The light scene draws the volume inject pass or the lighting pass. The inject mesh takes the inject material of each volume.
245
+ injectScene = createScene();
246
+ injectMesh = new THREE.Mesh(createTileGeometry(), new THREE.MeshBasicMaterial());
247
+ lightingMesh;
248
+ densityScene = createScene();
249
+ densityPool = [];
250
+ // NOTE Clears the density targets in the froxel area (one draw for all targets)
251
+ densityClearMaterial = new NodeMaterial();
252
+ densityClearScene = createScene();
253
+ densityClearMesh;
254
+ shadowScene = createScene();
255
+ shadowPool = [];
256
+ transmittanceScene = createScene();
257
+ transmittanceMesh;
258
+ temporalScene = createScene();
259
+ temporalMesh;
260
+ historyTargets = [null, null];
261
+ historyIndex = 0;
262
+ historyValid = false;
263
+ historyCamera = null;
264
+ previousViewMatrix = new THREE.Matrix4();
265
+ previousProjectionMatrix = new THREE.Matrix4();
266
+ previousRange = new THREE.Vector2(0.1, 1.0);
267
+ jitterIndex = 0;
268
+ // NOTE Environment lights from a small map of the scene environment (tools/frame/environment.ts)
269
+ environmentLights;
270
+ environmentMapMaterial = new VDBFrameEnvironmentMapMaterial();
271
+ environmentMapScene = createScene();
272
+ environmentMapMesh;
273
+ environmentMapCamera = new THREE.OrthographicCamera(-1.0, 1.0, 1.0, -1.0, 0.0, 1.0);
274
+ environmentMapTarget = null;
275
+ environment = null;
276
+ // NOTE Environment grid: environment light of the last cascade voxels, from the environment lights with no slot
277
+ environmentGridMaterial;
278
+ environmentGridScene = createScene();
279
+ environmentGridMesh;
280
+ environmentGridTarget = null;
281
+ environmentGridSignature = [];
282
+ nextEnvironmentGridSignature = [];
283
+ // NOTE multipleScattering 'neural': mips of the last grid cascade, levels 1 and up (VDBFrameGridMipMaterial)
284
+ gridMipMaterial = new VDBFrameGridMipMaterial();
285
+ gridMipScene = createScene();
286
+ gridMipMesh;
287
+ gridMipTargets = [];
288
+ gridMipSignature = [];
289
+ atlasTarget = null;
290
+ densityTarget = null;
291
+ shadowDensityTarget = null;
292
+ transmittanceTarget = null;
293
+ // NOTE Renderer settings.spectral: 3 transmittance targets for each slot group in use (spectrum.ts).
294
+ // Made when a group is first used. transmittanceTarget is null then.
295
+ spectralTransmittanceTargets = [];
296
+ lightLimitWarned = false;
297
+ warned = new Set();
298
+ // NOTE Shadow grid state: cascades of the last update, signatures (tools/frame/grid.ts)
299
+ gridState = vdbFrameGridSignatureState();
300
+ // NOTE World shadow grid of the current render. Volume shadows make it before the first lit mesh; 'frame' lighting
301
+ // then uses it again (gridFreshCamera). gridState.castVersion changes when the last cascade (all volumes) changes.
302
+ gridRenderId = -1;
303
+ gridCamera = null;
304
+ gridFreshCamera = null;
305
+ gridReady = false;
306
+ /** K3.6: volume shadows on meshes (settings.castShadows). */
307
+ volumeShadows;
308
+ // NOTE gridVersion and target of the last shadow density draw. The same version is not drawn again.
309
+ shadowDrawnVersion = -1;
310
+ shadowDrawnTarget = null;
311
+ transmittanceSignature = [];
312
+ nextTransmittanceSignature = [];
313
+ // NOTE collectVolumes result of the current render
314
+ collected = {
315
+ renderId: -1,
316
+ camera: null,
317
+ scene: null,
318
+ result: null,
319
+ };
320
+ volumeCollector = new FrameVolumeCollector();
321
+ sliceRange = { first: 0, count: 0 };
322
+ gridRange = { rect: new THREE.Vector4(), first: 0, count: 0 };
323
+ cascadeCasterPool = { items: [], entries: [] };
324
+ lightState = {
325
+ camera: null,
326
+ hemiCount: 0,
327
+ hasSky: false,
328
+ directions: { length: 0, entries: [] },
329
+ points: { length: 0, entries: [] },
330
+ spots: { length: 0, entries: [] },
331
+ };
332
+ lightResult = { shadowLightCount: 0, environmentLightCount: 0, environmentShadowSlot: 0, environmentGridLights: [] };
333
+ environmentGridLightEntries = [];
334
+ cachedFrame = -1;
335
+ cachedCamera = null;
336
+ rendererWarned = false;
337
+ constructor() {
338
+ this.integrateMaterial = new VDBFrameIntegrateMaterial({ froxelUniforms: this.froxelUniforms });
339
+ this.lightingMaterial = new VDBFrameLightingMaterial({
340
+ froxelUniforms: this.froxelUniforms,
341
+ froxelTileUniforms: this.froxelTileUniforms,
342
+ froxelAreaUniforms: this.froxelAreaUniforms,
343
+ lightUniforms: this.lightUniforms,
344
+ gridUniforms: this.gridUniforms,
345
+ densityShadowUniforms: this.densityShadowUniforms,
346
+ });
347
+ this.transmittanceMaterial = new VDBFrameTransmittanceMaterial({ gridUniforms: this.gridUniforms, lightUniforms: this.lightUniforms });
348
+ this.temporalMaterial = new VDBFrameTemporalMaterial({ froxelUniforms: this.froxelUniforms, froxelTileUniforms: this.froxelTileUniforms, froxelAreaUniforms: this.froxelAreaUniforms });
349
+ this.environmentGridMaterial = new VDBFrameEnvironmentGridMaterial({ gridUniforms: this.gridUniforms, environmentGridUniforms: this.environmentGridUniforms, lightUniforms: this.lightUniforms });
350
+ this.environmentLights = new VDBFrameEnvironmentLights((renderer, scene) => this.renderEnvironmentMap(renderer, scene));
351
+ this.volumeShadows = new VDBFrameVolumeShadows(this);
352
+ this.lightingMesh = new THREE.Mesh(this.areaGeometry, this.lightingMaterial);
353
+ this.densityClearMesh = new THREE.Mesh(this.areaGeometry, this.densityClearMaterial);
354
+ this.transmittanceMesh = new THREE.Mesh(createFullscreenGeometry(), this.transmittanceMaterial);
355
+ this.temporalMesh = new THREE.Mesh(this.areaGeometry, this.temporalMaterial);
356
+ this.environmentMapMesh = new THREE.Mesh(createFullscreenGeometry(), this.environmentMapMaterial);
357
+ this.environmentGridMesh = new THREE.Mesh(createFullscreenGeometry(), this.environmentGridMaterial);
358
+ this.gridMipMesh = new THREE.Mesh(createFullscreenGeometry(), this.gridMipMaterial);
359
+ for (const mesh of [this.injectMesh, this.lightingMesh, this.transmittanceMesh, this.temporalMesh, this.densityClearMesh, this.environmentMapMesh, this.environmentGridMesh, this.gridMipMesh]) {
360
+ mesh.frustumCulled = false;
361
+ }
362
+ this.temporalScene.add(this.temporalMesh);
363
+ this.densityClearMaterial.vertexNode = frameTileVertex(this.froxelTileUniforms, this.froxelAreaUniforms.frameAreaRect, this.froxelAreaUniforms.frameAreaSliceStart);
364
+ this.densityClearMaterial.fragmentNode = outputStruct(vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0));
365
+ this.densityClearMaterial.depthTest = false;
366
+ this.densityClearMaterial.depthWrite = false;
367
+ this.densityClearMaterial.blending = THREE.NoBlending;
368
+ this.densityClearScene.add(this.densityClearMesh);
369
+ this.injectScene.add(this.injectMesh, this.lightingMesh);
370
+ this.transmittanceScene.add(this.transmittanceMesh);
371
+ this.environmentMapScene.add(this.environmentMapMesh);
372
+ this.environmentGridScene.add(this.environmentGridMesh);
373
+ this.gridMipScene.add(this.gridMipMesh);
374
+ }
375
+ warnOnce(key, message) {
376
+ if (!this.warned.has(key)) {
377
+ this.warned.add(key);
378
+ console.warn(`VDBFrameRenderer: ${message}`);
379
+ }
380
+ }
381
+ // NOTE Each frame volume proxy calls this. The first proxy in a frame renders the frame. Other proxies draw nothing.
382
+ render(renderer, scene, camera, proxy) {
383
+ if (!renderer.isWebGPURenderer) {
384
+ if (!this.rendererWarned) {
385
+ console.error("VDBFrameVolume: needs WebGPURenderer (three/webgpu).");
386
+ this.rendererWarned = true;
387
+ }
388
+ return;
389
+ }
390
+ const frame = renderer.info.render.calls;
391
+ // NOTE Override passes (depth, normals) of other effects must not draw or start the frame
392
+ if (scene.overrideMaterial) {
393
+ proxy.geometry.instanceCount = 0;
394
+ return;
395
+ }
396
+ if (frame === this.cachedFrame && camera === this.cachedCamera) {
397
+ proxy.geometry.instanceCount = 0;
398
+ return;
399
+ }
400
+ // NOTE Puts the volume shadow filter on new shadow lights (and frees the maps of removed ones)
401
+ this.volumeShadows.update(renderer, scene);
402
+ if (this.holdFroxels && this.atlasTarget) {
403
+ this.cachedFrame = frame;
404
+ this.cachedCamera = camera;
405
+ proxy.geometry.instanceCount = 1;
406
+ return;
407
+ }
408
+ const { volumes, casters } = this.collectVolumes(renderer, scene, camera);
409
+ if (volumes.length > 0) {
410
+ this.renderFrame(renderer, scene, camera, volumes, casters);
411
+ }
412
+ // NOTE Nested renders increase info.render.calls. Keep the new value. Then other proxies in this frame skip.
413
+ this.cachedFrame = renderer.info.render.calls;
414
+ this.cachedCamera = camera;
415
+ proxy.geometry.instanceCount = volumes.length > 0 ? 1 : 0;
416
+ }
417
+ /**
418
+ * Draws only the froxel atlas for this scene and camera (no integrate): the per-pass timing
419
+ * harness renders this alone. Returns the number of volumes in view.
420
+ */
421
+ renderFroxels(renderer, scene, camera) {
422
+ this.collected.renderId = -1;
423
+ const { volumes, casters } = this.collectVolumes(renderer, scene, camera);
424
+ if (volumes.length > 0) {
425
+ this.renderFrame(renderer, scene, camera, volumes, casters);
426
+ }
427
+ return volumes.length;
428
+ }
429
+ // NOTE A quad. The integrate material puts it on the screen rectangle of the froxel area.
430
+ createProxyGeometry() {
431
+ const geometry = createTileGeometry();
432
+ geometry.instanceCount = 1;
433
+ return geometry;
434
+ }
435
+ isFrameLighting() {
436
+ return this.settings.lighting === "frame";
437
+ }
438
+ // NOTE The world shadow grid is used by 'frame' lighting and by volume shadows on meshes
439
+ usesShadowGrid() {
440
+ return this.isFrameLighting() || this.settings.castShadows;
441
+ }
442
+ // NOTE Visible volumes and shadow casters, one time in a render (the render id of three.js NodeFrame, internal)
443
+ collectVolumes(renderer, scene, camera) {
444
+ const renderId = renderer._nodes?.nodeFrame?.renderId ?? -1;
445
+ const { collected } = this;
446
+ if (renderId !== -1 && collected.renderId === renderId && collected.camera === camera && collected.scene === scene && collected.result) {
447
+ return collected.result;
448
+ }
449
+ collected.renderId = renderId;
450
+ collected.camera = camera;
451
+ collected.scene = scene;
452
+ collected.result = this.volumeCollector.collect(scene, camera, this.settings, this.usesShadowGrid(), null);
453
+ if (collected.result.volumes.length > 0) {
454
+ this.froxelUniforms.froxelRange.value.set(collected.result.near, collected.result.far);
455
+ }
456
+ return collected.result;
457
+ }
458
+ // NOTE Slices of a volume. Returns this.sliceRange ({ first, count }), used again in the next call.
459
+ getSliceRange(volume) {
460
+ const { froxelRange, froxelGrid } = this.froxelUniforms;
461
+ return getFroxelSliceRange(volume, froxelRange.value, froxelGrid.value.z, this.sliceRange);
462
+ }
463
+ checkSettings() {
464
+ const s = this.settings;
465
+ if (this.isFrameLighting() && s.multipleScattering === "neural" && !this.neuralNetwork) {
466
+ this.warnOnce("neural", "multipleScattering 'neural' has no network (neuralNetwork is null); the scene light slots use 1D diffusion (true).");
467
+ }
468
+ }
469
+ /** The multiple scattering mode (settings.multipleScattering; 'neural' is diffusion outside the network). */
470
+ multipleScatteringMode() {
471
+ return this.settings.multipleScattering;
472
+ }
473
+ isSpectral() {
474
+ return this.settings.spectral === true;
475
+ }
476
+ // NOTE multipleScattering 'neural' uses the network only with grey 'frame' lighting. Other cases use diffusion.
477
+ isNeural() {
478
+ if (this.settings.multipleScattering !== "neural" || this.settings.spectral || !this.neuralNetwork)
479
+ return false;
480
+ const problem = vdbFrameNeuralCheck(this.neuralNetwork);
481
+ if (problem) {
482
+ this.warnOnce("neural-shape", `neuralNetwork ${this.neuralNetwork.name ?? ""} does not fit the shader (${problem}); using 1D diffusion.`);
483
+ return false;
484
+ }
485
+ return true;
486
+ }
487
+ // NOTE Mips of the last grid cascade for the network. Drawn again only when the grid changes.
488
+ renderGridMips(renderer, camera) {
489
+ const { frameGridResolution, frameGridColumns, frameGridCount } = this.gridUniforms;
490
+ const resolution = frameGridResolution.value;
491
+ const source = this.shadowDensityTarget;
492
+ const signature = [this.gridState.gridVersion, source.texture.id, resolution, frameGridCount.value, frameGridColumns.value];
493
+ if (vdbSameValues(signature, this.gridMipSignature)) {
494
+ return;
495
+ }
496
+ this.gridMipSignature = signature;
497
+ this.gridMipMesh.layers.mask = camera.layers.mask;
498
+ const material = this.gridMipMaterial;
499
+ const { frameNeuralMipMaps, frameNeuralMipLayouts } = this.transmittanceMaterial;
500
+ // NOTE Source of the next level: the grid atlas first, then the level before
501
+ let previous = null;
502
+ for (let level = 1; level <= frameNeuralMipLevels; level++) {
503
+ const side = Math.max(1, resolution >> level);
504
+ const layout = getAtlasLayout(side, side, side);
505
+ let target = this.gridMipTargets[level - 1];
506
+ if (!target || target.width !== layout.width || target.height !== layout.height) {
507
+ target?.dispose();
508
+ target = createRenderTarget(layout.width, layout.height, { format: THREE.RGFormat });
509
+ this.gridMipTargets[level - 1] = target;
510
+ }
511
+ if (previous) {
512
+ material.frameMipSource.value = previous.target.texture;
513
+ material.frameMipSourceResolution.value = previous.side;
514
+ material.frameMipSourceColumns.value = previous.layout.columns;
515
+ material.frameMipSourceTile.value = 0;
516
+ material.frameMipSourceSize.value.set(previous.layout.width, previous.layout.height);
517
+ }
518
+ else {
519
+ material.frameMipSource.value = source.texture;
520
+ material.frameMipSourceResolution.value = resolution;
521
+ material.frameMipSourceColumns.value = frameGridColumns.value;
522
+ material.frameMipSourceTile.value = (frameGridCount.value - 1) * resolution;
523
+ material.frameMipSourceSize.value.set(source.width, source.height);
524
+ }
525
+ material.frameMipResolution.value = side;
526
+ material.frameMipColumns.value = layout.columns;
527
+ material.frameMipSize.value.set(layout.width, layout.height);
528
+ renderer.setRenderTarget(target);
529
+ renderer.render(this.gridMipScene, camera);
530
+ frameNeuralMipMaps[level - 1].value = target.texture;
531
+ frameNeuralMipLayouts[level - 1].value.set(layout.columns, layout.width, layout.height);
532
+ previous = { target, side, layout };
533
+ }
534
+ }
535
+ // NOTE The 3 transmittance targets of a spectral slot group, the same size as the shadow grid
536
+ getSpectralTransmittanceTarget(group) {
537
+ const targets = this.spectralTransmittanceTargets;
538
+ while (targets.length <= group) {
539
+ const { width, height } = this.shadowDensityTarget;
540
+ targets.push(createRenderTarget(width, height, { count: 3 }));
541
+ }
542
+ return targets[group];
543
+ }
544
+ // NOTE The lighting and density passes keep their group textures bound when they do not read them: they get their
545
+ // own placeholders (one texture each, see renderFrameLighting), not the disposed targets.
546
+ disposeSpectralTransmittanceTargets() {
547
+ this.spectralTransmittanceTargets.forEach((target) => target.dispose());
548
+ this.spectralTransmittanceTargets = [];
549
+ for (const map of [...this.lightingMaterial.frameSpectralTransmittanceMaps, ...this.densityShadowUniforms.frameSkyTransmittanceMaps]) {
550
+ map.value = new THREE.Texture();
551
+ }
552
+ }
553
+ updateTargets(renderer) {
554
+ const { depth, subSteps } = this.settings;
555
+ const { width, height } = getFroxelSize(this.settings, renderer, froxelSize, this.froxelHeldSize);
556
+ const spectral = this.isSpectral();
557
+ const froxelAtlas = getAtlasLayout(width, height, depth);
558
+ if (!this.atlasTarget || this.atlasTarget.width !== froxelAtlas.width || this.atlasTarget.height !== froxelAtlas.height || isSpectralTarget(this.atlasTarget) !== spectral) {
559
+ const maxTextureSize = getMaxTextureSize(renderer);
560
+ if (froxelAtlas.width > maxTextureSize || froxelAtlas.height > maxTextureSize) {
561
+ console.warn("VDBFrameRenderer: froxel atlas is larger than maxTextureSize. Decrease width, height or depth.");
562
+ }
563
+ this.atlasTarget?.dispose();
564
+ this.atlasTarget = createAtlasTarget(froxelAtlas.width, froxelAtlas.height, spectral);
565
+ this.historyValid = false;
566
+ }
567
+ // NOTE The per-volume inject materials are set in renderInject
568
+ this.lightingMaterial.setSpectral(spectral);
569
+ this.temporalMaterial.setSpectral(spectral);
570
+ this.integrateMaterial.setSpectral(spectral);
571
+ const { froxelGrid, froxelColumns } = this.froxelUniforms;
572
+ // NOTE A new grid layout makes the history unusable, also when the atlas keeps its size (600 x 400 to 400 x 600)
573
+ if (froxelGrid.value.x !== width || froxelGrid.value.y !== height || froxelGrid.value.z !== depth || froxelColumns.value !== froxelAtlas.columns) {
574
+ this.historyValid = false;
575
+ }
576
+ froxelGrid.value.set(width, height, depth);
577
+ froxelColumns.value = froxelAtlas.columns;
578
+ this.froxelUniforms.froxelSubSteps.value = Math.max(1, Math.round(subSteps));
579
+ this.froxelTileUniforms.frameTileSize.value.set(width, height);
580
+ this.froxelTileUniforms.frameTileColumns.value = froxelAtlas.columns;
581
+ this.froxelTileUniforms.frameTargetSize.value.set(froxelAtlas.width, froxelAtlas.height);
582
+ this.froxelUniforms.froxelAtlasSize.value.set(froxelAtlas.width, froxelAtlas.height);
583
+ this.integrateMaterial.froxelAtlas.value = this.atlasTarget.textures[0];
584
+ this.integrateMaterial.froxelAlphaAtlas.value = this.atlasTarget.textures[spectral ? 1 : 0];
585
+ if (this.settings.temporal) {
586
+ for (let index = 0; index < this.historyTargets.length; index++) {
587
+ const target = this.historyTargets[index];
588
+ if (!target || target.width !== froxelAtlas.width || target.height !== froxelAtlas.height || isSpectralTarget(target) !== spectral) {
589
+ target?.dispose();
590
+ this.historyTargets[index] = createAtlasTarget(froxelAtlas.width, froxelAtlas.height, spectral);
591
+ this.historyValid = false;
592
+ }
593
+ }
594
+ }
595
+ if (this.isFrameLighting()) {
596
+ // NOTE Spectral: 3 targets (albedo, emission, extinction of each channel), no light slot targets
597
+ const densityCount = spectral ? 3 : 4;
598
+ if (!this.densityTarget || this.densityTarget.width !== froxelAtlas.width || this.densityTarget.height !== froxelAtlas.height || this.densityTarget.textures.length !== densityCount) {
599
+ this.densityTarget?.dispose();
600
+ this.densityTarget = createRenderTarget(froxelAtlas.width, froxelAtlas.height, { count: densityCount, minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter });
601
+ this.densityClearMaterial.fragmentNode = outputStruct(...Array.from({ length: densityCount }, () => vec4(0.0)));
602
+ this.densityClearMaterial.needsUpdate = true;
603
+ }
604
+ this.updateGridTargets();
605
+ }
606
+ }
607
+ updateGridTargets() {
608
+ const resolution = Math.max(2, Math.round(this.settings.shadowResolution));
609
+ const cascades = this.getCascadeCount();
610
+ const gridAtlas = getAtlasLayout(resolution, resolution, resolution * cascades);
611
+ const spectral = this.isSpectral();
612
+ if (!this.shadowDensityTarget || this.shadowDensityTarget.width !== gridAtlas.width || this.shadowDensityTarget.height !== gridAtlas.height || isSpectralTarget(this.shadowDensityTarget) !== spectral) {
613
+ this.shadowDensityTarget?.dispose();
614
+ this.transmittanceTarget?.dispose();
615
+ this.disposeSpectralTransmittanceTargets();
616
+ // NOTE Extinction and absorption. Spectral: (extinction rgb, 0) and (absorption rgb, 0).
617
+ this.shadowDensityTarget = spectral ? createRenderTarget(gridAtlas.width, gridAtlas.height, { count: 2 }) : createRenderTarget(gridAtlas.width, gridAtlas.height, { format: THREE.RGFormat });
618
+ // NOTE Total transmittance (2 targets), then direct transmittance of the light slots (2 targets).
619
+ // Spectral: the slot group targets (getSpectralTransmittanceTarget).
620
+ this.transmittanceTarget = spectral ? null : createRenderTarget(gridAtlas.width, gridAtlas.height, { count: 4 });
621
+ // NOTE A new target is empty, so the last cascade and the environment grid are drawn again
622
+ this.gridState.castSignature = [];
623
+ this.environmentGridSignature = [];
624
+ }
625
+ // NOTE The per-volume shadow density materials are set in getShadowDensityMaterial
626
+ this.transmittanceMaterial.setSpectral(spectral);
627
+ this.environmentGridMaterial.setSpectral(spectral);
628
+ // NOTE The environment grid is one cascade. A new target is empty, so the signature is reset.
629
+ const environmentAtlas = getAtlasLayout(resolution, resolution, resolution);
630
+ if (!this.environmentGridTarget || this.environmentGridTarget.width !== environmentAtlas.width || this.environmentGridTarget.height !== environmentAtlas.height) {
631
+ this.environmentGridTarget?.dispose();
632
+ this.environmentGridTarget = createRenderTarget(environmentAtlas.width, environmentAtlas.height);
633
+ this.environmentGridSignature = [];
634
+ }
635
+ this.environmentGridUniforms.frameEnvironmentGridColumns.value = environmentAtlas.columns;
636
+ this.environmentGridUniforms.frameEnvironmentGridAtlasSize.value.set(environmentAtlas.width, environmentAtlas.height);
637
+ this.gridUniforms.frameGridResolution.value = resolution;
638
+ this.gridUniforms.frameGridColumns.value = gridAtlas.columns;
639
+ this.gridUniforms.frameGridAtlasSize.value.set(gridAtlas.width, gridAtlas.height);
640
+ this.gridTileUniforms.frameTileSize.value.set(resolution, resolution);
641
+ this.gridTileUniforms.frameTileColumns.value = gridAtlas.columns;
642
+ this.gridTileUniforms.frameTargetSize.value.set(gridAtlas.width, gridAtlas.height);
643
+ }
644
+ renderFrame(renderer, scene, camera, volumes, casters = []) {
645
+ this.checkSettings();
646
+ const currentRenderTarget = renderer.getRenderTarget();
647
+ const currentActiveCubeFace = renderer.getActiveCubeFace();
648
+ const currentActiveMipmapLevel = renderer.getActiveMipmapLevel();
649
+ const currentAutoClear = renderer.autoClear;
650
+ const currentClearAlpha = renderer.getClearAlpha();
651
+ const currentScissorTest = renderer.getScissorTest();
652
+ renderer.getClearColor(clearColor);
653
+ // NOTE null: the environment uses the 'outward' path. 'volume' lighting marches every environment light in each volume.
654
+ this.environment = this.environmentLights.update(renderer, scene, this.settings);
655
+ this.updateTargets(renderer);
656
+ this.updateFroxelArea(volumes, camera);
657
+ const cam = camera;
658
+ this.froxelUniforms.froxelProjectionInverse.value.copy(camera.projectionMatrixInverse);
659
+ this.froxelUniforms.frameFadeRadius.value = getFrameFadeRadius(this.settings);
660
+ this.integrateMaterial.frameCameraNear.value = cam.near;
661
+ this.integrateMaterial.frameCameraFar.value = cam.far;
662
+ this.integrateMaterial.frameCameraOrthographic.value = cam.isOrthographicCamera ? 1 : 0;
663
+ this.integrateMaterial.setSrgbBlending(this.settings.srgbBlending && renderer.outputColorSpace === THREE.SRGBColorSpace, renderer.toneMapping === THREE.NoToneMapping);
664
+ // NOTE Pass materials are unlit. Nested renders do not update shadow maps.
665
+ renderer.autoClear = false;
666
+ renderer.setScissorTest(false);
667
+ renderer.setClearColor(0x000000, 0.0);
668
+ // NOTE Golden ratio steps give a new, evenly spread jitter value for each frame
669
+ this.froxelUniforms.frameJitter.value = this.settings.jitter ? (this.jitterIndex * 0.618034) % 1.0 : -1.0;
670
+ this.jitterIndex = (this.jitterIndex + 1) % 1024;
671
+ this.passHook?.begin?.("froxels");
672
+ if (this.isFrameLighting()) {
673
+ this.renderFrameLighting(renderer, scene, camera, volumes, casters);
674
+ }
675
+ else {
676
+ this.renderInject(renderer, scene, camera, volumes);
677
+ }
678
+ if (this.settings.temporal) {
679
+ this.passHook?.begin?.("temporal");
680
+ this.renderTemporal(renderer, camera);
681
+ this.passHook?.end?.("temporal");
682
+ }
683
+ else {
684
+ this.historyValid = false;
685
+ }
686
+ this.passHook?.end?.("froxels");
687
+ this.gridFreshCamera = null;
688
+ renderer.setScissorTest(currentScissorTest);
689
+ renderer.autoClear = currentAutoClear;
690
+ renderer.setClearColor(clearColor, currentClearAlpha);
691
+ renderer.setRenderTarget(currentRenderTarget, currentActiveCubeFace, currentActiveMipmapLevel);
692
+ }
693
+ // NOTE Froxel area of the volumes, for the passes over the froxel grid and for the integrate pass
694
+ updateFroxelArea(volumes, camera) {
695
+ const { froxelGrid, froxelRange } = this.froxelUniforms;
696
+ const { x: width, y: height, z: depth } = froxelGrid.value;
697
+ const area = getFroxelArea(volumes, camera, width, height, froxelRange.value, depth, this.froxelArea);
698
+ const { frameAreaRect, frameAreaSliceStart } = this.froxelAreaUniforms;
699
+ frameAreaRect.value.copy(area.rect);
700
+ frameAreaSliceStart.value = area.first;
701
+ this.areaGeometry.instanceCount = area.count;
702
+ // NOTE One more cell on each side: the linear filter of the integrate pass reads one cell away
703
+ const { x0, y0, x1, y1 } = getClipRect(area.rect, width, height, area.count > 0 ? 1 : 0);
704
+ this.integrateMaterial.frameScreenRect.value.set(x0, y0, x1, y1);
705
+ this.integrateMaterial.frameSliceRange.value.set(area.first, area.first + area.count);
706
+ }
707
+ // NOTE Blends this frame into the history. The integrate pass then reads the blended froxels.
708
+ renderTemporal(renderer, camera) {
709
+ const material = this.temporalMaterial;
710
+ const readTarget = this.historyTargets[this.historyIndex];
711
+ const writeTarget = this.historyTargets[1 - this.historyIndex];
712
+ const cam = camera;
713
+ material.frameCurrentMap.value = this.atlasTarget.textures[0];
714
+ material.frameHistoryMap.value = readTarget.textures[0];
715
+ material.frameCurrentAlphaMap.value = this.atlasTarget.textures.at(-1);
716
+ material.frameHistoryAlphaMap.value = readTarget.textures.at(-1);
717
+ material.frameUseHistory.value = this.historyValid && this.historyCamera === camera ? 1 : 0;
718
+ material.frameTemporalBlend.value = Math.min(1.0, Math.max(0.01, this.settings.temporalBlend));
719
+ material.frameCameraMatrixWorld.value.copy(camera.matrixWorld);
720
+ material.framePreviousViewMatrix.value.copy(this.previousViewMatrix);
721
+ material.framePreviousProjectionMatrix.value.copy(this.previousProjectionMatrix);
722
+ material.framePreviousRange.value.copy(this.previousRange);
723
+ this.temporalMesh.layers.mask = camera.layers.mask;
724
+ // NOTE The pass draws only the froxel area. The clear empties the other froxels.
725
+ renderer.setRenderTarget(writeTarget);
726
+ renderer.clear(true, false, false);
727
+ renderer.render(this.temporalScene, camera);
728
+ this.integrateMaterial.froxelAtlas.value = writeTarget.textures[0];
729
+ this.integrateMaterial.froxelAlphaAtlas.value = writeTarget.textures.at(-1);
730
+ this.historyIndex = 1 - this.historyIndex;
731
+ this.historyValid = true;
732
+ this.historyCamera = camera;
733
+ this.previousViewMatrix.copy(camera.matrixWorldInverse);
734
+ this.previousProjectionMatrix.copy(cam.projectionMatrix);
735
+ this.previousRange.copy(this.froxelUniforms.froxelRange.value);
736
+ }
737
+ // NOTE PMREM rotation and intensity uniforms read the environment of the rendered pass scene
738
+ copyEnvironment(target, scene) {
739
+ target.environment = scene.environment ?? null;
740
+ target.environmentIntensity = scene.environmentIntensity ?? 1.0;
741
+ if (scene.environmentRotation)
742
+ target.environmentRotation.copy(scene.environmentRotation);
743
+ }
744
+ prepareLightScene(scene, camera, mesh) {
745
+ this.copyEnvironment(this.injectScene, scene);
746
+ this.injectMesh.visible = mesh === this.injectMesh;
747
+ this.lightingMesh.visible = mesh === this.lightingMesh;
748
+ mesh.layers.mask = camera.layers.mask;
749
+ return this.updateLights(scene, camera);
750
+ }
751
+ renderInject(renderer, scene, camera, volumes) {
752
+ const { atlasTarget, injectScene, injectMesh } = this;
753
+ this.prepareLightScene(scene, camera, this.injectMesh);
754
+ renderer.setRenderTarget(atlasTarget);
755
+ renderer.clear(true, false, false);
756
+ for (let i = 0; i < volumes.length; i++) {
757
+ const { frameVolume, volume } = volumes[i];
758
+ const injectMaterial = this.getInjectMaterial(frameVolume);
759
+ // NOTE Draw only the froxel rectangle and the slices of this volume. One instance for each slice.
760
+ if (!this.getFroxelRect(volume, camera, injectMaterial.frameRect.value)) {
761
+ continue;
762
+ }
763
+ const { first, count } = this.getSliceRange(volume);
764
+ injectMaterial.frameSliceStart.value = first;
765
+ injectMesh.geometry.instanceCount = count;
766
+ injectMesh.material = injectMaterial;
767
+ injectMaterial.frameCameraMatrixWorld.value.copy(camera.matrixWorld);
768
+ injectMaterial.setEnvironment(scene.environment);
769
+ setVolumeUniforms(injectMaterial, frameVolume, volume);
770
+ injectMaterial.frameLights.value = frameVolume.lights;
771
+ injectMaterial.frameShadowSteps.value = frameVolume.shadowSteps;
772
+ injectMaterial.frameMultipleScattering.value = getFrameMultipleScattering(this.multipleScatteringMode(), frameVolume.getPhase());
773
+ getFrameScatteringOctaves(this.settings, injectMaterial.frameScatteringOctaves.value);
774
+ injectMaterial.frameTwoSided.value = getFrameTwoSidedDiffusion(this.settings.multipleScattering);
775
+ this.passHook?.begin?.("inject", { volume: frameVolume });
776
+ renderer.setRenderTarget(atlasTarget);
777
+ renderer.render(injectScene, camera);
778
+ this.passHook?.end?.("inject", { volume: frameVolume });
779
+ }
780
+ }
781
+ /** The inject material of a volume: its source is compiled in (DIVERGES: one material per source). */
782
+ getInjectMaterial(frameVolume) {
783
+ const mipmaps = this.volumeMipmaps(frameVolume);
784
+ let material = frameVolume.injectMaterial;
785
+ if (!material || material.froxelUniforms !== this.froxelUniforms) {
786
+ material?.dispose();
787
+ material = new VDBFrameInjectMaterial({
788
+ froxelUniforms: this.froxelUniforms,
789
+ froxelTileUniforms: this.froxelTileUniforms,
790
+ lightUniforms: this.lightUniforms,
791
+ source: frameVolume.source,
792
+ sourceUniforms: frameVolume.sourceUniforms,
793
+ mipmaps,
794
+ hdda: frameVolume.hdda,
795
+ spectral: this.isSpectral(),
796
+ ...(frameVolume.emissionColor ? { emissionColor: frameVolume.emissionColor } : {}),
797
+ });
798
+ frameVolume.injectMaterial = material;
799
+ }
800
+ material.setMipmaps(mipmaps);
801
+ material.setHdda(frameVolume.hdda);
802
+ material.setSpectral(this.isSpectral());
803
+ // NOTE The hemisphere and environment-light loops are compiled in only when the scene has those lights (updateLights)
804
+ material.setLightTerms(this.lightUniforms.frameHemiCount.value > 0, this.lightUniforms.frameEnvironmentCount.value > 0);
805
+ return material;
806
+ }
807
+ volumeMipmaps(frameVolume) {
808
+ return frameVolume.mipmaps && frameVolume.source.lodLevels > 0;
809
+ }
810
+ /** The 'frame' density material of a volume (source compiled in, as the inject material). */
811
+ getDensityMaterial(frameVolume) {
812
+ const mipmaps = this.volumeMipmaps(frameVolume);
813
+ let material = frameVolume.densityMaterial;
814
+ if (!material || material.frameUniforms.froxelUniforms !== this.froxelUniforms) {
815
+ material?.dispose();
816
+ material = new VDBFrameDensityMaterial({
817
+ froxelUniforms: this.froxelUniforms,
818
+ froxelTileUniforms: this.froxelTileUniforms,
819
+ gridUniforms: this.gridUniforms,
820
+ environmentGridUniforms: this.environmentGridUniforms,
821
+ densityShadowUniforms: this.densityShadowUniforms,
822
+ lightUniforms: this.lightUniforms,
823
+ source: frameVolume.source,
824
+ sourceUniforms: frameVolume.sourceUniforms,
825
+ mipmaps,
826
+ ...(frameVolume.emissionColor ? { emissionColor: frameVolume.emissionColor } : {}),
827
+ });
828
+ frameVolume.densityMaterial = material;
829
+ }
830
+ material.setMipmaps(mipmaps);
831
+ material.setSpectral(this.isSpectral());
832
+ return material;
833
+ }
834
+ /** The shadow density material of a caster (source compiled in). */
835
+ getShadowDensityMaterial(frameVolume) {
836
+ const mipmaps = this.volumeMipmaps(frameVolume);
837
+ let material = frameVolume.shadowDensityMaterial;
838
+ if (!material || material.gridUniforms !== this.gridUniforms) {
839
+ material?.dispose();
840
+ material = new VDBFrameShadowDensityMaterial({
841
+ gridUniforms: this.gridUniforms,
842
+ gridTileUniforms: this.gridTileUniforms,
843
+ source: frameVolume.source,
844
+ sourceUniforms: frameVolume.sourceUniforms,
845
+ mipmaps,
846
+ });
847
+ frameVolume.shadowDensityMaterial = material;
848
+ }
849
+ material.setMipmaps(mipmaps);
850
+ material.setSpectral(this.isSpectral());
851
+ return material;
852
+ }
853
+ // NOTE Combined lighting: density of all volumes, one shadow grid, one lighting pass.
854
+ // Lighting and transmittance render calls do not grow with the number of volumes.
855
+ renderFrameLighting(renderer, scene, camera, volumes, casters) {
856
+ const lights = this.prepareLightScene(scene, camera, this.lightingMesh);
857
+ const ms = getFrameMultipleScattering(this.multipleScatteringMode(), this.settings);
858
+ const octaves = getFrameScatteringOctaves(this.settings, this.scatteringOctaves);
859
+ // NOTE Shadow pass. Extinction of all casters in one world grid, then transmittance to each light.
860
+ // It is before the density pass. Then the density pass can sample the shadows of each volume.
861
+ this.passHook?.begin?.("shadowDensity");
862
+ const useShadows = this.gridFreshCamera === camera ? this.gridReady : this.renderShadowDensity(renderer, camera, casters);
863
+ this.passHook?.end?.("shadowDensity");
864
+ this.gridFreshCamera = null;
865
+ const useEnvironmentGrid = useShadows && lights.environmentGridLights.length > 0;
866
+ const spectral = this.isSpectral();
867
+ const spectralGroups = getFrameSpectralGroups(lights.shadowLightCount);
868
+ if (useShadows) {
869
+ const material = this.transmittanceMaterial;
870
+ material.frameShadowDensityMap.value = this.shadowDensityTarget.textures[0];
871
+ material.frameShadowAbsorptionMap.value = this.shadowDensityTarget.textures.at(-1);
872
+ material.frameShadowLightCount.value = lights.shadowLightCount;
873
+ material.frameMultipleScattering.value = ms;
874
+ material.frameScatteringOctaves.value.copy(octaves);
875
+ material.frameTwoSided.value = getFrameTwoSidedDiffusion(this.settings.multipleScattering);
876
+ this.transmittanceMesh.layers.mask = camera.layers.mask;
877
+ material.setNeural(this.isNeural() ? this.neuralNetwork : null);
878
+ if (material.neural) {
879
+ this.passHook?.begin?.("gridMips");
880
+ this.renderGridMips(renderer, camera);
881
+ this.passHook?.end?.("gridMips");
882
+ }
883
+ if (this.transmittanceUnchanged(lights.shadowLightCount, spectral ? spectralGroups : 0)) {
884
+ this.redrawStats.transmittance.skipped++;
885
+ }
886
+ else if (spectral) {
887
+ this.redrawStats.transmittance.drawn++;
888
+ this.passHook?.begin?.("transmittance");
889
+ // NOTE One pass for each slot group in use, each into its own targets
890
+ for (let group = 0; group < spectralGroups; group++) {
891
+ material.frameSpectralGroup.value = group;
892
+ renderer.setRenderTarget(this.getSpectralTransmittanceTarget(group));
893
+ renderer.render(this.transmittanceScene, camera);
894
+ }
895
+ this.passHook?.end?.("transmittance");
896
+ }
897
+ else {
898
+ this.redrawStats.transmittance.drawn++;
899
+ this.passHook?.begin?.("transmittance");
900
+ renderer.setRenderTarget(this.transmittanceTarget);
901
+ renderer.render(this.transmittanceScene, camera);
902
+ this.passHook?.end?.("transmittance");
903
+ }
904
+ }
905
+ if (useEnvironmentGrid) {
906
+ this.passHook?.begin?.("environmentGrid");
907
+ this.renderEnvironmentGrid(renderer, camera, casters, lights.environmentGridLights, ms, octaves);
908
+ this.passHook?.end?.("environmentGrid");
909
+ }
910
+ const { densityShadowUniforms } = this;
911
+ densityShadowUniforms.frameUseShadows.value = useShadows ? 1 : 0;
912
+ if (!spectral) {
913
+ const transmittance = this.transmittanceTarget;
914
+ densityShadowUniforms.frameTransmittanceMap0.value = transmittance.textures[0];
915
+ densityShadowUniforms.frameTransmittanceMap1.value = transmittance.textures[1];
916
+ densityShadowUniforms.frameTransmittanceMap2.value = transmittance.textures[2];
917
+ densityShadowUniforms.frameTransmittanceMap3.value = transmittance.textures[3];
918
+ }
919
+ densityShadowUniforms.frameShadowSceneLightCount.value = lights.environmentShadowSlot;
920
+ getFramePhase(this.settings, densityShadowUniforms.framePhase.value);
921
+ getFramePhaseSpectrum(this.settings, densityShadowUniforms.framePhaseSpectrum.value);
922
+ densityShadowUniforms.frameScatteringOctaves.value.copy(octaves);
923
+ densityShadowUniforms.frameDiffusion.value = isFrameDiffusion(this.multipleScatteringMode()) ? 1 : 0;
924
+ densityShadowUniforms.frameEnvironmentGridMap.value = this.environmentGridTarget.texture;
925
+ densityShadowUniforms.frameEnvironmentMode.value = useEnvironmentGrid
926
+ ? frameDensityEnvironment.grid
927
+ : useShadows && lights.environmentLightCount > 0
928
+ ? frameDensityEnvironment.none
929
+ : frameDensityEnvironment.outward;
930
+ densityShadowUniforms.frameCameraMatrixWorld.value.copy(camera.matrixWorld);
931
+ // NOTE Spectral: sky up and sky down are in the first two targets of group 0 (bound also without shadows: not his)
932
+ if (spectral) {
933
+ const skyMaps = densityShadowUniforms.frameSkyTransmittanceMaps;
934
+ for (let index = 0; index < skyMaps.length; index++) {
935
+ skyMaps[index].value = this.getSpectralTransmittanceTarget(0).textures[index];
936
+ }
937
+ }
938
+ this.copyEnvironment(this.densityScene, scene);
939
+ // NOTE Density pass. One instanced draw for each volume, one instance for each slice.
940
+ this.updatePool(this.densityPool, this.densityScene, volumes, camera, this.setupDensityEntry);
941
+ this.densityClearMesh.layers.mask = camera.layers.mask;
942
+ this.passHook?.begin?.("density");
943
+ renderer.setRenderTarget(this.densityTarget);
944
+ renderer.render(this.densityClearScene, camera);
945
+ renderer.render(this.densityScene, camera);
946
+ this.passHook?.end?.("density");
947
+ // NOTE Lighting pass. Each froxel is lit one time from the combined data.
948
+ const { lightingMaterial } = this;
949
+ const density = this.densityTarget;
950
+ lightingMaterial.frameDensityAlbedoMap.value = density.textures[0];
951
+ lightingMaterial.frameDensityEmissionMap.value = density.textures[1];
952
+ if (spectral) {
953
+ lightingMaterial.frameDensityExtinctionMap.value = density.textures[2];
954
+ lightingMaterial.frameSpectralGroups.value = useShadows ? spectralGroups : 0;
955
+ // NOTE Groups not in use keep the textures they have (their own targets or the initial placeholders); the lighting
956
+ // pass does not read them. Not his (he gives them group 0's targets): three's TextureNode uniform hash is its
957
+ // texture's uuid, so nodes that hold one texture when the shader is built share one binding, and a group that
958
+ // came into use later (an environment light taking a slot after its readback) kept reading group 0.
959
+ if (useShadows) {
960
+ const maps = lightingMaterial.frameSpectralTransmittanceMaps;
961
+ for (let index = 0; index < spectralGroups * 3; index++) {
962
+ maps[index].value = this.getSpectralTransmittanceTarget(Math.floor(index / 3)).textures[index % 3];
963
+ }
964
+ }
965
+ }
966
+ else {
967
+ lightingMaterial.frameDensityShadowMap0.value = density.textures[2];
968
+ lightingMaterial.frameDensityShadowMap1.value = density.textures[3];
969
+ }
970
+ lightingMaterial.frameShadowLightCount.value = lights.shadowLightCount;
971
+ lightingMaterial.frameEnvironmentLightCount.value = useShadows ? lights.environmentLightCount : 0;
972
+ lightingMaterial.frameEnvironmentShadowSlot.value = lights.environmentShadowSlot;
973
+ lightingMaterial.frameCameraMatrixWorld.value.copy(camera.matrixWorld);
974
+ getFramePhase(this.settings, lightingMaterial.framePhase.value);
975
+ getFramePhaseSpectrum(this.settings, lightingMaterial.framePhaseSpectrum.value);
976
+ lightingMaterial.frameScatteringOctaves.value.copy(octaves);
977
+ this.passHook?.begin?.("lighting");
978
+ renderer.setRenderTarget(this.atlasTarget);
979
+ renderer.clear(true, false, false);
980
+ renderer.render(this.injectScene, camera);
981
+ this.passHook?.end?.("lighting");
982
+ }
983
+ // NOTE Whether the transmittance target already holds what this render would draw: the same grid content, target,
984
+ // shadow lights and settings. A jittered grid changes in each frame, so it is always drawn.
985
+ transmittanceUnchanged(shadowLightCount, spectralGroups = 0) {
986
+ const material = this.transmittanceMaterial;
987
+ const values = this.nextTransmittanceSignature;
988
+ values.length = 0;
989
+ // NOTE Spectral rendering has no single transmittance target, only the targets of its groups below
990
+ values.push(this.gridState.gridVersion, this.shadowDensityTarget.texture.id, this.transmittanceTarget ? this.transmittanceTarget.texture.id : -1, spectralGroups, shadowLightCount, material.frameMultipleScattering.value);
991
+ // NOTE The network: on/off and which weights (a swapped network draws again)
992
+ values.push(material.neural ? networkId(material.neural) : 0, material.frameTwoSided.value);
993
+ vdbSignaturePushVector(values, material.frameScatteringOctaves.value);
994
+ vdbSignaturePushVector(values, this.lightUniforms.frameSkyDirection.value);
995
+ for (let group = 0; group < spectralGroups; group++) {
996
+ values.push(this.getSpectralTransmittanceTarget(group).texture.id);
997
+ }
998
+ const shadowLights = material.frameShadowLights.array;
999
+ for (let i = 0; i < shadowLightCount; i++) {
1000
+ vdbSignaturePushVector(values, shadowLights[i]);
1001
+ }
1002
+ const jittered = this.froxelUniforms.frameJitter.value >= 0.0;
1003
+ const same = !jittered && vdbSameValues(values, this.transmittanceSignature);
1004
+ // NOTE A jittered draw matches no signature, so the first frame after jitter is turned off draws again
1005
+ if (jittered) {
1006
+ values.length = 0;
1007
+ }
1008
+ this.nextTransmittanceSignature = this.transmittanceSignature;
1009
+ this.transmittanceSignature = values;
1010
+ return same;
1011
+ }
1012
+ // NOTE Extinction of all casters in the world shadow grid cascades. Returns false with no casters.
1013
+ // NOTE onlyLast: only the last cascade (all volumes), drawn only when castVersion changed. Volume shadows on meshes
1014
+ // read only the last cascade; 'frame' lighting needs all cascades.
1015
+ renderShadowDensity(renderer, camera, casters, onlyLast = false) {
1016
+ const ready = casters.length > 0 && this.updateGrid(casters, camera);
1017
+ const castChanged = this.updateGridVersion(ready ? casters : []);
1018
+ if (!ready) {
1019
+ return false;
1020
+ }
1021
+ if (onlyLast && !castChanged) {
1022
+ this.redrawStats.shadowDensity.skipped++;
1023
+ return true;
1024
+ }
1025
+ if (!onlyLast && this.shadowDrawnVersion === this.gridState.gridVersion && this.shadowDrawnTarget === this.shadowDensityTarget) {
1026
+ this.redrawStats.shadowDensity.skipped++;
1027
+ return true;
1028
+ }
1029
+ this.redrawStats.shadowDensity.drawn++;
1030
+ // NOTE The clear below empties every cascade, so a draw of the last one alone leaves no full grid to keep
1031
+ this.shadowDrawnVersion = onlyLast ? -1 : this.gridState.gridVersion;
1032
+ this.shadowDrawnTarget = this.shadowDensityTarget;
1033
+ const count = this.gridUniforms.frameGridCount.value;
1034
+ const cascadeCasters = vdbFrameCascadeCasters(casters, onlyLast ? count - 1 : 0, count, this.cascadeCasterPool);
1035
+ this.updatePool(this.shadowPool, this.shadowScene, cascadeCasters, camera, this.setupShadowEntry);
1036
+ renderer.setRenderTarget(this.shadowDensityTarget);
1037
+ renderer.clear(true, false, false);
1038
+ renderer.render(this.shadowScene, camera);
1039
+ return true;
1040
+ }
1041
+ /**
1042
+ * Makes the world shadow grid one time in a render, before the first lit mesh (the volume shadow maps need it).
1043
+ * 'frame' lighting uses it again in the same render. Returns false with no casters. `renderId`: three's node frame
1044
+ * render id (three-internals.ts).
1045
+ */
1046
+ prepareShadowGrid(renderer, scene, camera, renderId) {
1047
+ if (this.gridRenderId === renderId && this.gridCamera === camera) {
1048
+ return this.gridReady;
1049
+ }
1050
+ this.gridRenderId = renderId;
1051
+ this.gridCamera = camera;
1052
+ const { casters } = this.collectVolumes(renderer, scene, camera);
1053
+ const currentRenderTarget = renderer.getRenderTarget();
1054
+ const currentActiveCubeFace = renderer.getActiveCubeFace();
1055
+ const currentActiveMipmapLevel = renderer.getActiveMipmapLevel();
1056
+ const currentAutoClear = renderer.autoClear;
1057
+ const currentClearAlpha = renderer.getClearAlpha();
1058
+ const currentScissorTest = renderer.getScissorTest();
1059
+ renderer.getClearColor(clearColor);
1060
+ renderer.autoClear = false;
1061
+ renderer.setScissorTest(false);
1062
+ renderer.setClearColor(0x000000, 0.0);
1063
+ this.updateGridTargets();
1064
+ this.passHook?.begin?.("shadowDensity");
1065
+ // NOTE 'volume' lighting: only the last cascade, for the volume shadow maps
1066
+ this.gridReady = this.renderShadowDensity(renderer, camera, casters, !this.isFrameLighting());
1067
+ this.passHook?.end?.("shadowDensity");
1068
+ this.gridFreshCamera = camera;
1069
+ renderer.setScissorTest(currentScissorTest);
1070
+ renderer.autoClear = currentAutoClear;
1071
+ renderer.setClearColor(clearColor, currentClearAlpha);
1072
+ renderer.setRenderTarget(currentRenderTarget, currentActiveCubeFace, currentActiveMipmapLevel);
1073
+ return this.gridReady;
1074
+ }
1075
+ // NOTE gridVersion changes when the cascades, a caster transform, a caster's data or a caster's shadow props change.
1076
+ // castVersion changes only for the last cascade. Returns true when castVersion changed.
1077
+ updateGridVersion(casters) {
1078
+ const { frameGridMin, frameGridSize, frameGridCount, frameGridResolution } = this.gridUniforms;
1079
+ return vdbFrameUpdateGridSignatures(this.gridState, casters, frameGridCount.value, frameGridResolution.value, this.isSpectral(), frameGridMin.array, frameGridSize.array, (values, { frameVolume, volume }) => {
1080
+ vdbSignaturePushElements(values, volume.matrixWorld.elements);
1081
+ pushCasterSource(values, frameVolume);
1082
+ values.push(frameVolume.baseColor.r, frameVolume.baseColor.g, frameVolume.baseColor.b);
1083
+ values.push(frameVolume.getDensityScale(), frameVolume.densityCutoff, frameVolume.extinction);
1084
+ vdbSignaturePushVector(values, getFrameExtinctionSpectrum(frameVolume, extinctionSpectrum));
1085
+ values.push(frameVolume.absorbance, frameVolume.mipmaps ? 1 : 0, frameVolume.wrap3D);
1086
+ vdbSignaturePushVector(values, frameVolume.offset3D);
1087
+ });
1088
+ }
1089
+ // NOTE Environment grid of the last cascade. It is drawn again only when its signature changes.
1090
+ // NOTE Signature: the grid lights, the last cascade, and the caster data that the shadow density pass reads.
1091
+ renderEnvironmentGrid(renderer, camera, casters, gridLights, ms, octaves) {
1092
+ const { frameGridMin, frameGridSize, frameGridCount, frameGridResolution } = this.gridUniforms;
1093
+ const cascade = frameGridCount.value - 1;
1094
+ const previous = this.environmentGridSignature;
1095
+ const signature = this.nextEnvironmentGridSignature;
1096
+ this.environmentGridUniforms.frameEnvironmentGridCascade.value = cascade;
1097
+ signature.length = 0;
1098
+ signature.push(frameGridResolution.value, cascade, this.isSpectral() ? 1 : 0, ms);
1099
+ vdbSignaturePushVector(signature, octaves);
1100
+ vdbSignaturePushVector(signature, frameGridMin.array[cascade]);
1101
+ vdbSignaturePushVector(signature, frameGridSize.array[cascade]);
1102
+ for (const light of gridLights) {
1103
+ vdbSignaturePushElements(signature, light.direction);
1104
+ vdbSignaturePushElements(signature, light.color);
1105
+ }
1106
+ // NOTE The caster data the shadow density pass reads: the grid version covers it (transforms, sources, props)
1107
+ signature.push(this.gridState.castVersion, casters.length);
1108
+ if (vdbSameValues(signature, previous)) {
1109
+ this.redrawStats.environmentGrid.skipped++;
1110
+ return;
1111
+ }
1112
+ this.redrawStats.environmentGrid.drawn++;
1113
+ this.environmentGridSignature = signature;
1114
+ this.nextEnvironmentGridSignature = previous;
1115
+ const material = this.environmentGridMaterial;
1116
+ material.frameShadowDensityMap.value = this.shadowDensityTarget.textures[0];
1117
+ material.frameShadowAbsorptionMap.value = this.shadowDensityTarget.textures.at(-1);
1118
+ material.frameLightCount.value = gridLights.length;
1119
+ material.frameMultipleScattering.value = ms;
1120
+ material.frameScatteringOctaves.value.copy(octaves);
1121
+ const directions = material.frameLightDirections.array;
1122
+ const colors = material.frameLightColors.array;
1123
+ for (let i = 0; i < gridLights.length; i++) {
1124
+ directions[i].fromArray(gridLights[i].direction);
1125
+ colors[i].fromArray(gridLights[i].color);
1126
+ }
1127
+ this.environmentGridMesh.layers.mask = camera.layers.mask;
1128
+ renderer.setRenderTarget(this.environmentGridTarget);
1129
+ renderer.render(this.environmentGridScene, camera);
1130
+ }
1131
+ // NOTE Draws the lat-long environment map with the rotation of the scene and intensity 1.0, then reads it.
1132
+ renderEnvironmentMap(renderer, scene) {
1133
+ if (!this.environmentMapTarget) {
1134
+ this.environmentMapTarget = createRenderTarget(frameEnvironmentMapWidth, frameEnvironmentMapHeight, {
1135
+ type: THREE.FloatType,
1136
+ minFilter: THREE.NearestFilter,
1137
+ magFilter: THREE.NearestFilter,
1138
+ });
1139
+ }
1140
+ this.copyEnvironment(this.environmentMapScene, scene);
1141
+ this.environmentMapScene.environmentIntensity = 1.0;
1142
+ this.environmentMapMaterial.setEnvironment(scene.environment);
1143
+ const currentRenderTarget = renderer.getRenderTarget();
1144
+ renderer.setRenderTarget(this.environmentMapTarget);
1145
+ renderer.render(this.environmentMapScene, this.environmentMapCamera);
1146
+ renderer.setRenderTarget(currentRenderTarget);
1147
+ return renderer.readRenderTargetPixelsAsync(this.environmentMapTarget, 0, 0, frameEnvironmentMapWidth, frameEnvironmentMapHeight);
1148
+ }
1149
+ // NOTE updatePool setup of the density pass: one instance for each slice of the volume
1150
+ setupDensityEntry = (entry, { frameVolume, volume }, camera) => {
1151
+ if (!this.getFroxelRect(volume, camera, entry.rect)) {
1152
+ return false;
1153
+ }
1154
+ const { first, count } = this.getSliceRange(volume);
1155
+ entry.mesh.material = this.getDensityMaterial(frameVolume);
1156
+ entry.mesh.material.setEnvironment(this.densityScene.environment);
1157
+ entry.frameVolume = frameVolume;
1158
+ entry.volume = volume;
1159
+ entry.sliceStart = first;
1160
+ entry.geometry.instanceCount = count;
1161
+ return true;
1162
+ };
1163
+ // NOTE updatePool setup of the shadow density pass: the voxels of the volume in one cascade
1164
+ setupShadowEntry = (entry, { caster, cascade }) => {
1165
+ const { frameVolume, volume } = caster;
1166
+ const range = this.getGridRange(volume, cascade);
1167
+ if (!range) {
1168
+ return false;
1169
+ }
1170
+ entry.mesh.material = this.getShadowDensityMaterial(frameVolume);
1171
+ entry.frameVolume = frameVolume;
1172
+ entry.volume = volume;
1173
+ entry.rect.copy(range.rect);
1174
+ entry.sliceStart = range.first;
1175
+ entry.geometry.instanceCount = range.count;
1176
+ return true;
1177
+ };
1178
+ // NOTE Pool entries are reused between frames. Unused entries are hidden.
1179
+ // Each entry takes the material of its volume and sets the volume uniforms before its draw.
1180
+ updatePool(pool, scene, items, camera, setup) {
1181
+ let used = 0;
1182
+ for (let i = 0; i < items.length; i++) {
1183
+ if (!pool[used]) {
1184
+ const geometry = new THREE.InstancedBufferGeometry();
1185
+ geometry.setAttribute("position", tileQuadPosition);
1186
+ const mesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial());
1187
+ mesh.frustumCulled = false;
1188
+ mesh.matrixAutoUpdate = false;
1189
+ const entry = { mesh, geometry, frameVolume: null, volume: null, rect: new THREE.Vector4(), sliceStart: 0 };
1190
+ mesh.onBeforeRender = () => {
1191
+ const material = mesh.material;
1192
+ setVolumeUniforms(material, entry.frameVolume, entry.volume);
1193
+ material.frameRect.value.copy(entry.rect);
1194
+ material.frameSliceStart.value = entry.sliceStart;
1195
+ };
1196
+ scene.add(mesh);
1197
+ pool[used] = entry;
1198
+ }
1199
+ const entry = pool[used];
1200
+ if (setup(entry, items[i], camera)) {
1201
+ entry.mesh.visible = true;
1202
+ entry.mesh.layers.mask = camera.layers.mask;
1203
+ used++;
1204
+ }
1205
+ }
1206
+ for (let i = used; i < pool.length; i++) {
1207
+ pool[i].mesh.visible = false;
1208
+ }
1209
+ }
1210
+ // NOTE Screen rectangle of the volume in froxel cells. Writes target. Returns false when it covers no cell.
1211
+ getFroxelRect(volume, camera, target) {
1212
+ const { x: width, y: height } = this.froxelUniforms.froxelGrid.value;
1213
+ return getFroxelRect(volume, camera, width, height, target);
1214
+ }
1215
+ getCascadeCount() {
1216
+ return Math.min(frameMaxShadowCascades, Math.max(1, Math.round(this.settings.shadowCascades)));
1217
+ }
1218
+ // NOTE Shadow grid cascades of the casters (tools/frame/grid.ts). Returns false with no casters.
1219
+ updateGrid(casters, camera) {
1220
+ const { frameGridMin, frameGridSize, frameGridVoxel, frameGridCount, frameGridResolution } = this.gridUniforms;
1221
+ cameraPosition.setFromMatrixPosition(camera.matrixWorld);
1222
+ const count = vdbFrameGridCascades(casters.map((c) => c.volume.worldBox), cameraPosition, this.settings.shadowDistance, frameGridResolution.value, this.getCascadeCount(), frameGridMin.array, frameGridSize.array, frameGridVoxel.array);
1223
+ if (count > 0) {
1224
+ frameGridCount.value = count;
1225
+ }
1226
+ return count > 0;
1227
+ }
1228
+ // NOTE Voxel rectangle and atlas slices of the volume in one shadow grid cascade
1229
+ getGridRange(volume, cascade) {
1230
+ const { frameGridMin, frameGridVoxel, frameGridResolution } = this.gridUniforms;
1231
+ const found = vdbFrameGridRange(volume.worldBox, cascade, frameGridResolution.value, frameGridMin.array, frameGridVoxel.array, this.gridRange);
1232
+ return found ? this.gridRange : null;
1233
+ }
1234
+ // NOTE A pooled light record of updateLights: (position, w type), (color, w spot cos), (spot direction, cos), falloff
1235
+ getLightRecord(list) {
1236
+ if (list.length === list.entries.length) {
1237
+ list.entries.push({ position: new THREE.Vector4(), color: new THREE.Vector4(), spot: new THREE.Vector4(), falloff: new THREE.Vector2() });
1238
+ }
1239
+ const record = list.entries[list.length++];
1240
+ record.spot.set(0.0, 0.0, 0.0, 0.0);
1241
+ record.falloff.set(0.0, 0.0);
1242
+ return record;
1243
+ }
1244
+ // NOTE Light i of updateLights in slot order: directional, point, spot
1245
+ getSortedLight(index) {
1246
+ const { directions, points, spots } = this.lightState;
1247
+ if (index < directions.length) {
1248
+ return directions.entries[index];
1249
+ }
1250
+ if (index < directions.length + points.length) {
1251
+ return points.entries[index - directions.length];
1252
+ }
1253
+ return spots.entries[index - directions.length - points.length];
1254
+ }
1255
+ // NOTE traverseVisible callback of updateLights. this.lightState holds the state of the current call.
1256
+ visitLight = (object) => {
1257
+ const state = this.lightState;
1258
+ const light = object;
1259
+ if (!light.isLight || !light.layers.test(state.camera.layers)) {
1260
+ return;
1261
+ }
1262
+ const { frameAmbientColor, frameLightProbe, frameHemiSkyColor, frameHemiGroundColor, frameSkyDirection, frameHemiSkyColors, frameHemiGroundColors, frameHemiDirections } = this.lightUniforms;
1263
+ lightColor.copy(light.color).multiplyScalar(light.intensity);
1264
+ lightPosition.setFromMatrixPosition(light.matrixWorld);
1265
+ if (light.isAmbientLight) {
1266
+ frameAmbientColor.value.add(lightColor);
1267
+ }
1268
+ else if (light.isHemisphereLight) {
1269
+ const hemiCount = state.hemiCount;
1270
+ if (hemiCount < frameMaxHemisphereLights) {
1271
+ frameHemiSkyColors.array[hemiCount].set(lightColor.r, lightColor.g, lightColor.b);
1272
+ frameHemiDirections.array[hemiCount].copy(lightPosition).normalize();
1273
+ }
1274
+ frameHemiSkyColor.value.add(lightColor);
1275
+ lightColor.copy(light.groundColor).multiplyScalar(light.intensity);
1276
+ frameHemiGroundColor.value.add(lightColor);
1277
+ if (hemiCount < frameMaxHemisphereLights) {
1278
+ frameHemiGroundColors.array[hemiCount].set(lightColor.r, lightColor.g, lightColor.b);
1279
+ }
1280
+ state.hemiCount++;
1281
+ if (!state.hasSky && lightPosition.lengthSq() > 0.0) {
1282
+ frameSkyDirection.value.copy(lightPosition).normalize();
1283
+ state.hasSky = true;
1284
+ }
1285
+ }
1286
+ else if (light.isLightProbe) {
1287
+ const { coefficients } = light.sh;
1288
+ for (let i = 0; i < coefficients.length; i++) {
1289
+ frameLightProbe.array[i].addScaledVector(coefficients[i], light.intensity);
1290
+ }
1291
+ }
1292
+ else if (light.isDirectionalLight) {
1293
+ const record = this.getLightRecord(state.directions);
1294
+ lightTarget.setFromMatrixPosition(light.target.matrixWorld);
1295
+ lightPosition.sub(lightTarget).normalize();
1296
+ record.position.set(lightPosition.x, lightPosition.y, lightPosition.z, 1.0);
1297
+ record.color.set(lightColor.r, lightColor.g, lightColor.b, 0.0);
1298
+ }
1299
+ else if (light.isSpotLight) {
1300
+ const record = this.getLightRecord(state.spots);
1301
+ lightTarget.setFromMatrixPosition(light.target.matrixWorld);
1302
+ record.position.set(lightPosition.x, lightPosition.y, lightPosition.z, 3.0);
1303
+ record.color.set(lightColor.r, lightColor.g, lightColor.b, Math.cos(light.angle * (1.0 - light.penumbra)));
1304
+ lightTarget.subVectors(lightPosition, lightTarget).normalize();
1305
+ record.spot.set(lightTarget.x, lightTarget.y, lightTarget.z, Math.cos(light.angle));
1306
+ record.falloff.set(light.distance, light.decay);
1307
+ }
1308
+ else if (light.isPointLight) {
1309
+ const record = this.getLightRecord(state.points);
1310
+ record.position.set(lightPosition.x, lightPosition.y, lightPosition.z, 2.0);
1311
+ record.color.set(lightColor.r, lightColor.g, lightColor.b, 0.0);
1312
+ record.falloff.set(light.distance, light.decay);
1313
+ }
1314
+ };
1315
+ // NOTE Scene lights into the light uniforms. Node materials in custom passes have no three.js light uniforms.
1316
+ // Shadow slots follow the light order: directional, point, spot.
1317
+ // NOTE Returns this.lightResult, used again in the next call.
1318
+ updateLights(scene, camera) {
1319
+ const u = this.lightUniforms;
1320
+ const state = this.lightState;
1321
+ const { directions, points, spots } = state;
1322
+ state.camera = camera;
1323
+ state.hemiCount = 0;
1324
+ state.hasSky = false;
1325
+ directions.length = 0;
1326
+ points.length = 0;
1327
+ spots.length = 0;
1328
+ u.frameAmbientColor.value.setRGB(0.0, 0.0, 0.0);
1329
+ u.frameHemiSkyColor.value.setRGB(0.0, 0.0, 0.0);
1330
+ u.frameHemiGroundColor.value.setRGB(0.0, 0.0, 0.0);
1331
+ u.frameSkyDirection.value.set(0.0, 1.0, 0.0);
1332
+ for (const v of u.frameLightProbe.array) {
1333
+ v.set(0.0, 0.0, 0.0);
1334
+ }
1335
+ scene.traverseVisible(this.visitLight);
1336
+ state.camera = null;
1337
+ const { hemiCount } = state;
1338
+ const lightTotal = directions.length + points.length + spots.length;
1339
+ if ((lightTotal > frameMaxLights || hemiCount > frameMaxHemisphereLights) && !this.lightLimitWarned) {
1340
+ console.warn(`VDBFrameRenderer: too many lights. Only ${frameMaxLights} lights and ${frameMaxHemisphereLights} hemisphere lights are used.`);
1341
+ this.lightLimitWarned = true;
1342
+ }
1343
+ u.frameHemiCount.value = Math.min(hemiCount, frameMaxHemisphereLights);
1344
+ const count = Math.min(lightTotal, frameMaxLights);
1345
+ const positions = u.frameLightPositions.array;
1346
+ const colors = u.frameLightColors.array;
1347
+ const spotArray = u.frameLightSpots.array;
1348
+ const falloffs = u.frameLightFalloffs.array;
1349
+ for (let i = 0; i < count; i++) {
1350
+ const light = this.getSortedLight(i);
1351
+ positions[i].copy(light.position);
1352
+ colors[i].copy(light.color);
1353
+ spotArray[i].copy(light.spot);
1354
+ falloffs[i].copy(light.falloff);
1355
+ }
1356
+ u.frameLightCount.value = count;
1357
+ // NOTE Environment lights. 'volume' lighting marches all of them in each volume.
1358
+ const { environment } = this;
1359
+ const environmentCount = environment ? environment.count : 0;
1360
+ const environmentDirections = u.frameEnvironmentDirections.array;
1361
+ const environmentColors = u.frameEnvironmentColors.array;
1362
+ u.frameEnvironmentCount.value = environmentCount;
1363
+ for (let i = 0; i < environmentCount; i++) {
1364
+ environmentDirections[i].fromArray(environment.directions[i]);
1365
+ environmentColors[i].fromArray(environment.colors[i]);
1366
+ }
1367
+ // NOTE 'frame' lighting. Scene lights take the first shadow slots.
1368
+ // NOTE The dominant 'directional' environment light (light 0) takes the next free slot. Other environment lights go to the environment grid.
1369
+ const sceneShadowCount = Math.min(count, frameShadowLightSlots);
1370
+ const shadowLights = this.transmittanceMaterial.frameShadowLights.array;
1371
+ const frameLighting = this.isFrameLighting() && environmentCount > 0;
1372
+ const environmentLightCount = frameLighting &&
1373
+ environment.mode === frameEnvironmentModes.directional &&
1374
+ sceneShadowCount < frameShadowLightSlots &&
1375
+ // NOTE Diffusion: all environment lights use the environment grid. Then both lights of an opposite pair get the same march.
1376
+ !isFrameDiffusion(this.multipleScatteringMode())
1377
+ ? 1
1378
+ : 0;
1379
+ const result = this.lightResult;
1380
+ const environmentGridLights = result.environmentGridLights;
1381
+ environmentGridLights.length = 0;
1382
+ for (let i = 0; i < sceneShadowCount; i++) {
1383
+ shadowLights[i].copy(this.getSortedLight(i).position);
1384
+ }
1385
+ if (environmentLightCount > 0) {
1386
+ shadowLights[sceneShadowCount].fromArray(environment.directions[0]).setW(1.0);
1387
+ }
1388
+ u.frameEnvironmentUnshadowed.value.set(0.0, 0.0, 0.0);
1389
+ if (frameLighting) {
1390
+ for (let i = environmentLightCount; i < environmentCount; i++) {
1391
+ const entries = this.environmentGridLightEntries;
1392
+ const index = environmentGridLights.length;
1393
+ if (index === entries.length) {
1394
+ entries.push({ direction: [], color: [] });
1395
+ }
1396
+ entries[index].direction = environment.directions[i];
1397
+ entries[index].color = environment.colors[i];
1398
+ environmentGridLights.push(entries[index]);
1399
+ u.frameEnvironmentUnshadowed.value.add(environmentColors[i]);
1400
+ }
1401
+ }
1402
+ u.frameEnvironmentUnshadowed.value.max(vector3.set(0.0, 0.0, 0.0));
1403
+ result.shadowLightCount = sceneShadowCount + environmentLightCount;
1404
+ result.environmentLightCount = environmentLightCount;
1405
+ result.environmentShadowSlot = sceneShadowCount;
1406
+ return result;
1407
+ }
1408
+ dispose() {
1409
+ // NOTE Puts back the shadow filter of each light; the new instance installs again on the next render
1410
+ this.volumeShadows.dispose();
1411
+ this.volumeShadows = new VDBFrameVolumeShadows(this);
1412
+ this.gridRenderId = -1;
1413
+ this.gridCamera = null;
1414
+ this.historyTargets.forEach((target) => target?.dispose());
1415
+ this.historyTargets = [null, null];
1416
+ this.historyValid = false;
1417
+ this.disposeSpectralTransmittanceTargets();
1418
+ for (const key of ["atlasTarget", "densityTarget", "shadowDensityTarget", "transmittanceTarget", "environmentMapTarget", "environmentGridTarget"]) {
1419
+ this[key]?.dispose();
1420
+ this[key] = null;
1421
+ }
1422
+ this.environmentLights = new VDBFrameEnvironmentLights((renderer, scene) => this.renderEnvironmentMap(renderer, scene));
1423
+ this.environment = null;
1424
+ this.environmentGridSignature = [];
1425
+ this.gridMipTargets.forEach((target) => target.dispose());
1426
+ this.gridMipTargets = [];
1427
+ this.gridMipSignature = [];
1428
+ for (const { mesh, geometry } of [...this.densityPool, ...this.shadowPool]) {
1429
+ mesh.parent?.remove(mesh);
1430
+ geometry.dispose();
1431
+ }
1432
+ this.densityPool = [];
1433
+ this.shadowPool = [];
1434
+ this.shadowDrawnVersion = -1;
1435
+ this.shadowDrawnTarget = null;
1436
+ for (const material of [this.integrateMaterial, this.lightingMaterial, this.transmittanceMaterial, this.temporalMaterial, this.densityClearMaterial, this.environmentMapMaterial, this.environmentGridMaterial, this.gridMipMaterial]) {
1437
+ material.dispose();
1438
+ }
1439
+ this.injectMesh.geometry.dispose();
1440
+ this.injectMesh.material.dispose();
1441
+ for (const mesh of [this.transmittanceMesh, this.environmentMapMesh, this.environmentGridMesh, this.gridMipMesh]) {
1442
+ mesh.geometry.dispose();
1443
+ }
1444
+ this.areaGeometry.dispose();
1445
+ this.cachedFrame = -1;
1446
+ this.cachedCamera = null;
1447
+ }
1448
+ }
1449
+ /** frameShaderChunks.js L800–830 `setVolumeUniforms`: the values of one volume before its draw (inject, density or shadow density). */
1450
+ export function setVolumeUniforms(material, frameVolume, volume) {
1451
+ const u = material.volumeUniforms;
1452
+ const s = material.sourceUniforms;
1453
+ // K5.9: a source.replace() (a sequence frame, a resize) reaches the uniforms without volume.updateSource()
1454
+ frameVolume.syncSource();
1455
+ u.frameViewToLocal.value.copy(volume.viewToLocal);
1456
+ u.frameLocalToView.value.copy(volume.localToView);
1457
+ u.frameWorldToLocal.value.copy(volume.worldToLocal);
1458
+ s.worldScale.value = volume.matrixWorld.getMaxScaleOnAxis();
1459
+ s.baseColor.value.copy(frameVolume.baseColor);
1460
+ u.frameScatterColor.value.copy(frameVolume.scatterColor);
1461
+ s.extinction.value = frameVolume.extinction;
1462
+ getFrameExtinctionSpectrum(frameVolume, u.frameExtinctionSpectrum.value);
1463
+ s.densityScale.value = frameVolume.getDensityScale();
1464
+ s.densityCutoff.value = frameVolume.densityCutoff;
1465
+ u.frameAbsorbance.value = frameVolume.absorbance;
1466
+ u.frameOpacity.value = frameVolume.opacity;
1467
+ s.emissiveIntensity.value = frameVolume.emissiveIntensity;
1468
+ s.temperatureRange.value.copy(frameVolume.temperatureRange);
1469
+ s.offset3D.value.copy(frameVolume.offset3D);
1470
+ s.wrap3D.value = frameVolume.wrap3D;
1471
+ // K5.9: the volume's phase overrides, else the renderer's settings (the 'volume' lighting phase; 'frame' lighting
1472
+ // draws its one phase from the settings in the lighting pass)
1473
+ const phase = frameVolume.getPhase();
1474
+ getFramePhase(phase, u.framePhase.value);
1475
+ getFramePhaseSpectrum(phase, u.framePhaseSpectrum.value);
1476
+ }
1477
+ /** Per three renderer shared instances (`vdbFrameRenderer(renderer)`). */
1478
+ const sharedByRenderer = new WeakMap();
1479
+ let shared = null;
1480
+ /**
1481
+ * The shared {@link VDBFrameRenderer} that a `VDBFrameVolume` uses when you pass no `frameRenderer`.
1482
+ * Its `settings` are global to every volume that uses it (the froxel grid, shadows and scattering are
1483
+ * built once for all of them). Pass a three renderer to get the instance shared by that renderer only
1484
+ * (a WeakMap, so two three renderers never share targets or settings); with no argument you get the
1485
+ * one process-wide default. For volumes that need different settings, make your own
1486
+ * `new VDBFrameRenderer()` and pass it as `VDBFrameVolume({ frameRenderer })`.
1487
+ */
1488
+ export function vdbFrameRenderer(renderer) {
1489
+ if (renderer) {
1490
+ let r = sharedByRenderer.get(renderer);
1491
+ if (!r)
1492
+ sharedByRenderer.set(renderer, (r = new VDBFrameRenderer()));
1493
+ return r;
1494
+ }
1495
+ shared ??= new VDBFrameRenderer();
1496
+ return shared;
1497
+ }
1498
+ //# sourceMappingURL=renderer.js.map