@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,876 @@
1
+ // Portions derived from mjurczyk/openvdb (MIT), commit 75e7345 (v0.26.0):
2
+ // src/three-openvdb/frame/webgpu/frameShaderChunks.js L729–763 (createVolumeUniforms, the source
3
+ // half), L834–1028 (createVolumeNodes: getLod, sampleExtinction, sampleAlbedo, sampleEmission and
4
+ // the box helpers, through ./volume-source.ts).
5
+ // Copyright (c) mjurczyk/openvdb contributors. Licensed under the MIT License (see THIRD_PARTY_NOTICES.md).
6
+ // Changes: the density is read from a sparse NanoVDB grid in a VDBGridPool (vdbSample, trilinear)
7
+ // instead of an 8-bit 3D texture; three LOD strategies instead of his box mip atlas; an HDDA
8
+ // empty-space query for marches. Divergences: DIVERGES.md (K3.2 rows), V23–V25.
9
+ /**
10
+ * The sparse volume source (K3.2, R1 §4.2–4.3): a `VDBGridPool` grid behind the same seam as the
11
+ * dense `.wvdb` source, so `VDBFrameVolume` renders NanoVDB grids.
12
+ *
13
+ * const grids = new VDBGridNode(renderer, pool, { profile: "march", helpers: ["cellBoundTrilinear", "hddaCellEnd", "nodeAverage", "fastTrilinear"] });
14
+ * const source = vdbSparseSource(grids, { density: id }, { lod: "coarse-pyramid", images: { density: image } });
15
+ * const volume = new VDBFrameVolume(source, { extinction: 100 }); // box: the grid's world box
16
+ *
17
+ * Positions are his local unit box [-0.5, 0.5]³; uvw = local + 0.5 (+ offset, wrapped) maps onto the
18
+ * channel grid's index cell box `[min − ½, max + ½]` (voxel centres at integers), so uvw
19
+ * (i + 0.5) / N lands on voxel min + i as on a dense texel centre. The sample position is clamped to
20
+ * `[min, max]` (his clamp-to-edge). Level 0 is the pool's trilinear `vdbSample`; with the node helper
21
+ * `'fastTrilinear'` (recommended, K3.2d) it is `vdbSampleFast` through one accessor per shader invocation
22
+ * (`cacheAccessor`): the same values, 1.5–2× faster on the hardware GPU (docs/archive/handoffs/K3-2d.md).
23
+ *
24
+ * LOD (`lod`, his `sampleExtinction(localPos, lod)` at lod ≥ 1; R1 §4.3):
25
+ * - `'none'`: level 0 always (no prefilter; aliases when a froxel spans many voxels). lodLevels 0.
26
+ * - `'coarse-pyramid'` (default, the K3.2 A/B pick): a dense r16f pyramid from L₀ = max(1, ⌈log2(maxTexels / pyramidMax)⌉):
27
+ * the base is a box resample of the grid, the levels above it his box mip atlas. lod in (0, L₀)
28
+ * blends level 0 toward L₀; lod ≥ L₀ samples the pyramid as his mip atlas. GPU bytes: ~2 B × (base
29
+ * + atlas) texels. Built on the GPU from the pool grid by default (`pyramidBuild: 'gpu'`, K3.2a: the
30
+ * core's `vdbCreateBoxPyramid`, no CPU image needed) and rebuilt when the grid's image changes
31
+ * (`pool.replace`: a sequence frame), checked once per render that samples it; `'cpu'` keeps the
32
+ * K3.2 path (`@pmndrs/vdb/tools` `vdbBoxPyramid` from `images.density`, static). Both give the same
33
+ * texels to ≤ 1 f16 ulp (e2e/k3-pyramid.spec.ts).
34
+ * - `'node-average'`: no extra memory: trilinear over the node averages (`vdbNodeAverage`) at
35
+ * level 3 (leaves, the exact 8³ box mean for fog leaves) and level 7 (lower nodes, the mean of
36
+ * their active voxels); lod in (0, 3) blends level 0 → 3, (3, 7) blends 3 → 7. Needs stats.
37
+ *
38
+ * HDDA: `skipEmpty` lets a march jump samples that are provably 0 (`VDBFrameVolume` `hdda`, default
39
+ * off since D30 / D31: it slowed the shadow march on Apple GPUs; the image is the same either way).
40
+ *
41
+ * Following the image (`followImage`, G16a): the index boxes, world box and pyramid are fixed when the source is
42
+ * made unless it follows the grids' images. Then a `pool.replace` that changes a channel grid's box (a coarse-first
43
+ * swap: the preview, then the full grid in the same id) is re-read from `pool.info` once the new image is resident:
44
+ * new box uniforms, a new GPU pyramid of the new size (its textures swapped into the same texture nodes), the LOD
45
+ * range as a uniform, and a new `version`, which a `VDBFrameVolume` follows with its box. No shader is rebuilt, and the
46
+ * pyramid kernels of every type the `VDBGridNode` decodes are compiled up front.
47
+ *
48
+ * Mask, albedo and emission grids (optional, scalar; albedo may be three grids for RGB) are read at
49
+ * level 0 only. One VDB module per shader: every grid goes through the one `VDBGridNode`.
50
+ */
51
+ import * as THREE from "three";
52
+ import * as TSL from "three/tsl";
53
+ import { code, wgslFn } from "three/tsl";
54
+ import { VDBError } from "../core/errors.js";
55
+ import { vdbCreateBoxPyramid, warmBoxPyramidPipelines } from "../core/compute/pyramid.js";
56
+ import { vdbBoxPyramid } from "../tools/frame/sparse.js";
57
+ import { vdbBoxPyramidLayout } from "../tools/frame/pyramid-layout.js";
58
+ import { getGPUTexture, webgpuBackendOf } from "./device.js";
59
+ import { leafVoxels, treeNodes } from "../tools/cpu/march.js";
60
+ import { vdbSample } from "./grid-node.js";
61
+ import { vdbBoxNodes, vdbDensityScaleOf, vdbGridUv, vdbMaxComponent, vdbMipSampler, } from "./volume-source.js";
62
+ const { If, clamp, float, int, log2, max, min, mix, pow, select, texture3D, uint, uniform, vec2, vec3 } = TSL;
63
+ const bad = (message) => new VDBError("VDB_BAD_OPTION", `vdbSparseSource: ${message}`);
64
+ /** G16a: the pyramid texture nodes of a following source's uniforms (their textures change with the layout). */
65
+ const pyramidTextures = new WeakMap();
66
+ /** The non-zero density stats of an image (all leaf voxels, active or not, and active tiles). */
67
+ function imageStats(image, clamp01) {
68
+ let sum = 0;
69
+ let count = 0;
70
+ let maxValue = 0;
71
+ const add = (v, n) => {
72
+ const x = clamp01 ? Math.min(Math.max(v, 0), 1) : v;
73
+ if (x !== 0) {
74
+ sum += x * n;
75
+ count += n;
76
+ maxValue = Math.max(maxValue, x);
77
+ }
78
+ };
79
+ for (const node of treeNodes(image)) {
80
+ for (const t of node.activeTiles)
81
+ add(t.value, t.dim ** 3);
82
+ if (node.level !== 0)
83
+ continue;
84
+ const { values } = leafVoxels(image, node.address);
85
+ for (let n = 0; n < 512; n++)
86
+ add(values[n], 1);
87
+ }
88
+ return { meanNonZero: count > 0 ? sum / count : 0, max: maxValue, activeVoxels: count };
89
+ }
90
+ /** World AABB of an index cell box through a 3x4 row-major affine. */
91
+ function worldBox(box, m) {
92
+ const lo = [Infinity, Infinity, Infinity];
93
+ const hi = [-Infinity, -Infinity, -Infinity];
94
+ for (let c = 0; c < 8; c++) {
95
+ const p = [c & 1 ? box.max[0] + 0.5 : box.min[0] - 0.5, c & 2 ? box.max[1] + 0.5 : box.min[1] - 0.5, c & 4 ? box.max[2] + 0.5 : box.min[2] - 0.5];
96
+ for (let r = 0; r < 3; r++) {
97
+ const v = m[r * 4] * p[0] + m[r * 4 + 1] * p[1] + m[r * 4 + 2] * p[2] + m[r * 4 + 3];
98
+ lo[r] = Math.min(lo[r], v);
99
+ hi[r] = Math.max(hi[r], v);
100
+ }
101
+ }
102
+ return [lo, hi];
103
+ }
104
+ /** A half-float r16f Data3DTexture (linear, clamp-to-edge, no mipmaps); `data` null: GPU-written (no upload). */
105
+ function halfTexture(data, dims) {
106
+ let half = null;
107
+ if (data) {
108
+ half = new Uint16Array(data.length);
109
+ for (let i = 0; i < data.length; i++)
110
+ half[i] = THREE.DataUtils.toHalfFloat(data[i]);
111
+ }
112
+ const [w, h, d] = dims;
113
+ const texture = new THREE.Data3DTexture(half, w, h, d);
114
+ // A GPU-written texture: three allocates it (createTexture) but never uploads (no data to upload).
115
+ if (!half)
116
+ texture.source.dataReady = false;
117
+ texture.format = THREE.RedFormat;
118
+ texture.type = THREE.HalfFloatType;
119
+ texture.minFilter = THREE.LinearFilter;
120
+ texture.magFilter = THREE.LinearFilter;
121
+ texture.unpackAlignment = 1;
122
+ texture.generateMipmaps = false;
123
+ texture.needsUpdate = true;
124
+ return texture;
125
+ }
126
+ // WGSL helpers over the grid module (one module per shader: the VDBGridNode's).
127
+ const NODE_AVERAGE_WGSL = /* wgsl */ `fn vdbFrameNodeAverage(g: u32, p: vec3f, dim: u32) -> f32 {
128
+ var acc = vdbAccessorInit(g);
129
+ let fd = f32(dim);
130
+ let q = (p - vec3f(0.5 * fd - 0.5)) / fd;
131
+ let base = floor(q);
132
+ let w = q - base;
133
+ let c = vec3i(base) * i32(dim);
134
+ // One call site in a loop: SwiftShader compiles 8 inlined tree walks very slowly.
135
+ var v: array<f32, 8>;
136
+ for (var k = 0u; k < 8u; k++) {
137
+ v[k] = vdbNodeAverage(&acc, c + vec3i(vec3u(k & 1u, (k >> 1u) & 1u, (k >> 2u) & 1u)) * i32(dim), dim);
138
+ }
139
+ let x00 = v[0] + w.x * (v[1] - v[0]);
140
+ let x10 = v[2] + w.x * (v[3] - v[2]);
141
+ let x01 = v[4] + w.x * (v[5] - v[4]);
142
+ let x11 = v[6] + w.x * (v[7] - v[6]);
143
+ let y0 = x00 + w.y * (x10 - x00);
144
+ let y1 = x01 + w.y * (x11 - x01);
145
+ return y0 + w.z * (y1 - y0);
146
+ }`;
147
+ // The 'node-average' density at a lod (one call per sample: level 0 → 3 → 7 blends, two taps at most).
148
+ const nodeLodWgsl = (sample) => /* wgsl */ `fn vdbFrameNodeLod(g: u32, p: vec3f, lod: f32) -> f32 {
149
+ var acc = vdbAccessorInit(g);
150
+ if lod <= 0.0 {
151
+ return ${sample}(&acc, p);
152
+ }
153
+ let fine = lod < 3.0;
154
+ var a = 0.0;
155
+ if fine {
156
+ a = ${sample}(&acc, p);
157
+ } else {
158
+ a = vdbFrameNodeAverage(g, p, 8u);
159
+ }
160
+ if lod >= 7.0 {
161
+ return vdbFrameNodeAverage(g, p, 128u);
162
+ }
163
+ let b = vdbFrameNodeAverage(g, p, select(128u, 8u, fine));
164
+ let t = select((lod - 3.0) / 4.0, lod / 3.0, fine);
165
+ return mix(a, b, t);
166
+ }`;
167
+ // K3.2d: level-0 density through the source's own sampler: vdbSampleFast (helper 'fastTrilinear') and/or one
168
+ // accessor per invocation kept in a private variable (copied in and out: WGSL can't pass a ptr<private> where
169
+ // the API takes ptr<function>). Same values as vdbSample with a fresh accessor (the accessor cache only skips
170
+ // the descent).
171
+ const SAMPLE_CACHE_WGSL = /* wgsl */ `var<private> vdbFrameAcc: VdbAccessor;
172
+ var<private> vdbFrameAccGrid: u32 = 0xffffffffu;`;
173
+ const sampleWgsl = (fast, cached) => {
174
+ const call = fast ? "vdbSampleFast" : "vdbSample";
175
+ return cached
176
+ ? /* wgsl */ `fn vdbFrameSample(g: u32, xyz: vec3f) -> f32 {
177
+ if vdbFrameAccGrid != g {
178
+ vdbFrameAcc = vdbAccessorInit(g);
179
+ vdbFrameAccGrid = g;
180
+ }
181
+ var a = vdbFrameAcc;
182
+ let v = ${call}(&a, xyz);
183
+ vdbFrameAcc = a;
184
+ return v;
185
+ }`
186
+ : /* wgsl */ `fn vdbFrameSample(g: u32, xyz: vec3f) -> f32 {
187
+ var a = vdbAccessorInit(g);
188
+ return ${call}(&a, xyz);
189
+ }`;
190
+ };
191
+ // The HDDA empty-space query; CPU twin tools vdbFrameSkipEmpty (see there for the contract).
192
+ const SKIP_EMPTY_WGSL = /* wgsl */ `fn vdbFrameSkipEmpty(g: u32, o: vec3f, d: vec3f, t0: f32, t1: f32, cutoff: f32, dilate: u32) -> vec2f {
193
+ var acc = vdbAccessorInit(g);
194
+ var h: VdbHdda;
195
+ vdbHddaInit(&h, o, t0, d, t1, 8u);
196
+ let back = 1e-3 / max(length(d), 1e-6);
197
+ var ts = t0;
198
+ for (var n = 0u; n < 64u; n++) {
199
+ let te = vdbHddaCellEnd(&h);
200
+ var b = 0.0;
201
+ if dilate == 0u {
202
+ b = vdbCellBoundTrilinear(&acc, h.voxel, 8u);
203
+ } else {
204
+ b = -1e30;
205
+ for (var k = 0u; k < 27u; k++) {
206
+ let c = vec3i(i32(k % 3u) - 1, i32((k / 3u) % 3u) - 1, i32(k / 9u) - 1) * 8;
207
+ b = max(b, vdbNodeMax(&acc, h.voxel + c, 8u));
208
+ }
209
+ }
210
+ if b > cutoff {
211
+ return vec2f(max(t0, ts - back), te);
212
+ }
213
+ ts = te;
214
+ if !vdbHddaStep(&h) {
215
+ return vec2f(t1, t1);
216
+ }
217
+ }
218
+ let r = max(t0, ts - back);
219
+ return vec2f(r, r);
220
+ }`;
221
+ class SparseSource {
222
+ kind = "sparse";
223
+ grids;
224
+ ids;
225
+ lod;
226
+ normalize;
227
+ channels;
228
+ // G16a: the layout fields below are re-read after an image change when `followImage` is on.
229
+ indexBox;
230
+ auxBoxes;
231
+ texels;
232
+ lodLevels;
233
+ stats;
234
+ statsUpdates = 0;
235
+ boxToSource;
236
+ bbox;
237
+ pyramid;
238
+ gpuBytes;
239
+ fastTrilinear;
240
+ cacheAccessor;
241
+ followImage;
242
+ pyramidBuilds = 0;
243
+ layoutUpdates = 0;
244
+ /** G16a: the newest channel imageRevision the layout describes, the pool revision last checked, the uniforms to re-apply. */
245
+ layoutRevision = -1;
246
+ layoutPoolRevision = -1;
247
+ layoutListeners = new Set();
248
+ /** G16a: `pyramidMax` as given (the pyramid is re-laid out with it). */
249
+ pyramidMaxOption;
250
+ /** G16a: the atlas node's texture when the layout has no atlas (a 1³ zero texture of its own). */
251
+ emptyAtlas = null;
252
+ poolRevisionSeen = -1;
253
+ poolIds;
254
+ versionSeen = { poolRevision: -1, value: 0 };
255
+ nodeAverageCache = null;
256
+ nodeLodCache = null;
257
+ skipCache = null;
258
+ sampleCache = null;
259
+ /** K3.2e: the density imageRevision the stats describe, the pool revision last checked, and a readback in flight. */
260
+ statsRevision = -1;
261
+ statsPoolRevisionSeen = -1;
262
+ statsReadback = null;
263
+ /** K5.9a: each of this source's uniforms re-applies its density scale with the new mean when a readback lands. */
264
+ statsListeners = new Set();
265
+ constructor(grids, ids, o) {
266
+ const pool = grids.pool;
267
+ if (typeof pool.info !== "function")
268
+ throw bad("the VDBGridNode's pool has no info(); pass a VDBGridPool.");
269
+ const info = (id) => pool.info(id);
270
+ const lod = o.lod ?? "coarse-pyramid";
271
+ if (lod !== "none" && lod !== "coarse-pyramid" && lod !== "node-average")
272
+ throw bad(`unknown lod ${JSON.stringify(lod)}; use 'none', 'coarse-pyramid' or 'node-average'.`);
273
+ const normalize = o.normalize ?? "none";
274
+ if (normalize !== "none" && normalize !== "clamp01")
275
+ throw bad(`unknown normalize ${JSON.stringify(normalize)}; use 'none' or 'clamp01'.`);
276
+ const helpers = new Set(grids.options.helpers ?? []);
277
+ if (grids.options.profile !== "march" || !helpers.has("cellBoundTrilinear") || !helpers.has("hddaCellEnd") || (lod === "node-average" && !helpers.has("nodeAverage"))) {
278
+ throw bad(`make the VDBGridNode with profile 'march' and helpers ['cellBoundTrilinear', 'hddaCellEnd'${lod === "node-average" ? ", 'nodeAverage'" : ""}] ` +
279
+ `(got profile '${grids.options.profile}', helpers [${[...helpers].join(", ")}]).`);
280
+ }
281
+ const fastTrilinear = o.fastTrilinear ?? helpers.has("fastTrilinear");
282
+ if (fastTrilinear && !helpers.has("fastTrilinear"))
283
+ throw bad("fastTrilinear needs the VDBGridNode helper 'fastTrilinear' (add it to helpers), or pass fastTrilinear: false.");
284
+ if (fastTrilinear && grids.options.sampler === "nearest")
285
+ throw bad("fastTrilinear needs the VDBGridNode's sampler 'trilinear'.");
286
+ this.fastTrilinear = fastTrilinear;
287
+ this.cacheAccessor = o.cacheAccessor ?? true;
288
+ const followImage = o.followImage ?? false;
289
+ if (typeof followImage !== "boolean")
290
+ throw bad("followImage must be a boolean.");
291
+ if (followImage) {
292
+ const fixed = ["indexBox", "auxIndexBoxes", "bbox"].filter((k) => o[k] !== undefined);
293
+ if (fixed.length)
294
+ throw bad(`followImage re-reads every box from the pool; drop ${fixed.join(", ")} (fixed boxes).`);
295
+ if (lod === "coarse-pyramid" && o.pyramidBuild === "cpu")
296
+ throw bad("followImage rebuilds the pyramid from the pool on the GPU; use pyramidBuild 'gpu' (the default).");
297
+ }
298
+ this.followImage = followImage;
299
+ this.pyramidMaxOption = o.pyramidMax !== undefined ? { pyramidMax: o.pyramidMax } : {};
300
+ const density = info(ids.density);
301
+ if (lod === "node-average" && !density.hasStats)
302
+ throw bad("lod 'node-average' reads node statistics; this grid has none (rebuild it with stats).");
303
+ this.grids = grids;
304
+ const channelIds = [ids.density, ids.mask, ids.emission, ...(Array.isArray(ids.albedo) ? ids.albedo : [ids.albedo])].filter((v) => v !== undefined);
305
+ this.checkTypes(channelIds.map((id) => ({ id, type: info(id).type })), "a channel grid");
306
+ this.ids = Object.freeze({ ...ids, ...(Array.isArray(ids.albedo) ? { albedo: [...ids.albedo] } : {}) });
307
+ this.lod = lod;
308
+ this.normalize = normalize;
309
+ this.indexBox = o.indexBox ? { min: [...o.indexBox.min], max: [...o.indexBox.max] } : { min: [...density.indexBBox.min], max: [...density.indexBBox.max] };
310
+ const albedoId = Array.isArray(ids.albedo) ? ids.albedo[0] : ids.albedo;
311
+ const box = (id, given) => id === undefined ? null : given ? { min: [...given.min], max: [...given.max] } : { min: [...info(id).indexBBox.min], max: [...info(id).indexBBox.max] };
312
+ const aux = o.auxIndexBoxes ?? {};
313
+ this.auxBoxes = { mask: box(ids.mask, aux.mask), albedo: box(albedoId, aux.albedo), emission: box(ids.emission, aux.emission) };
314
+ this.channels = { mask: ids.mask !== undefined, albedo: ids.albedo !== undefined, emission: ids.emission !== undefined };
315
+ const b = this.indexBox;
316
+ this.texels = [b.max[0] - b.min[0] + 1, b.max[1] - b.min[1] + 1, b.max[2] - b.min[2] + 1];
317
+ if (this.texels.some((t) => !(t > 0)))
318
+ throw bad("the density index box is empty.");
319
+ const image = o.images?.density;
320
+ if (o.stats)
321
+ this.stats = { ...o.stats };
322
+ else if (image)
323
+ this.stats = imageStats(image, normalize === "clamp01");
324
+ else
325
+ throw bad("pass images.density (the CPU image, for the density stats) or stats.");
326
+ this.statsRevision = density.imageRevision ?? -1;
327
+ // Local unit box → sample index space: p = min − ½ + (local + ½)·size (column-major).
328
+ const [sx, sy, sz] = this.texels;
329
+ this.boxToSource = new Float32Array([sx, 0, 0, 0, 0, sy, 0, 0, 0, 0, sz, 0, b.min[0] - 0.5 + 0.5 * sx, b.min[1] - 0.5 + 0.5 * sy, b.min[2] - 0.5 + 0.5 * sz, 1]);
330
+ this.bbox = o.bbox ? [[...o.bbox[0]], [...o.bbox[1]]] : worldBox(b, density.indexToWorld);
331
+ let pyramid = null;
332
+ if (lod === "coarse-pyramid") {
333
+ const build = o.pyramidBuild ?? "gpu";
334
+ if (build !== "gpu" && build !== "cpu")
335
+ throw bad(`unknown pyramidBuild ${JSON.stringify(build)}; use 'gpu' or 'cpu'.`);
336
+ const pyramidMax = o.pyramidMax !== undefined ? { pyramidMax: o.pyramidMax } : {};
337
+ if (build === "cpu") {
338
+ if (!image)
339
+ throw bad("pyramidBuild 'cpu' builds the pyramid from images.density; pass it, or use pyramidBuild 'gpu' (the default).");
340
+ const p = vdbBoxPyramid(image, { indexBox: b, ...pyramidMax });
341
+ pyramid = {
342
+ build,
343
+ level: p.level,
344
+ dimensions: p.dimensions,
345
+ atlas: p.atlas,
346
+ lodLevels: p.lodLevels,
347
+ data: p.data,
348
+ map: halfTexture(p.data, p.dimensions),
349
+ mip: p.atlas ? halfTexture(p.atlas.data, p.atlas.dimensions) : null,
350
+ gpu: null,
351
+ };
352
+ }
353
+ else {
354
+ const renderer = grids.renderer;
355
+ let device;
356
+ try {
357
+ device = webgpuBackendOf(renderer, "vdbSparseSource").device;
358
+ }
359
+ catch {
360
+ throw bad("pyramidBuild 'gpu' (the default for lod 'coarse-pyramid') needs the VDBGridNode's initialized WebGPURenderer; use pyramidBuild 'cpu' with images.density otherwise.");
361
+ }
362
+ const initTexture = renderer.initTexture;
363
+ if (typeof initTexture !== "function")
364
+ throw bad("pyramidBuild 'gpu' needs renderer.initTexture (three r187 WebGPURenderer).");
365
+ // G16a: a following source compiles the pyramid kernels of every type the node decodes now, so a swap to
366
+ // another grid type creates no pipeline.
367
+ if (followImage)
368
+ warmBoxPyramidPipelines(device, grids.options.types);
369
+ pyramid = this.gpuPyramid(b, ids.density);
370
+ }
371
+ }
372
+ this.pyramid = pyramid;
373
+ this.lodLevels = lod === "none" ? 0 : lod === "node-average" ? 7 : pyramid.lodLevels;
374
+ this.poolIds = [...new Set(channelIds)];
375
+ this.gpuBytes = this.measure();
376
+ this.layoutRevision = this.poolIds.reduce((r, id) => Math.max(r, info(id).imageRevision ?? 0), 0);
377
+ this.refreshPyramid();
378
+ }
379
+ /** GPU bytes of the channel grids' pool ranges and the pyramid (see the interface). */
380
+ measure() {
381
+ const pool = this.grids.pool;
382
+ const p = this.pyramid;
383
+ const atlasTexels = p?.atlas ? p.atlas.dimensions[0] * p.atlas.dimensions[1] * p.atlas.dimensions[2] : 0;
384
+ const pyramidBytes = p ? 2 * (p.dimensions[0] * p.dimensions[1] * p.dimensions[2] + atlasTexels) : 0;
385
+ return { pool: this.poolIds.reduce((s, id) => s + pool.info(id).capacity, 0), pyramid: pyramidBytes, pyramidScratch: p?.gpu?.gpuBytes.scratch ?? 0 };
386
+ }
387
+ /** The GPU pyramid of `box` (K3.2a): three textures the core's `vdbCreateBoxPyramid` writes into. */
388
+ gpuPyramid(box, densityId) {
389
+ const renderer = this.grids.renderer;
390
+ const device = webgpuBackendOf(renderer, "vdbSparseSource").device;
391
+ const texels = [0, 1, 2].map((a) => box.max[a] - box.min[a] + 1);
392
+ const layout = vdbBoxPyramidLayout(texels, this.pyramidMaxOption);
393
+ const map = halfTexture(null, layout.dimensions);
394
+ const mip = layout.atlas ? halfTexture(null, layout.atlas.dimensions) : null;
395
+ map.name = "vdbSparseSource pyramid base";
396
+ if (mip)
397
+ mip.name = "vdbSparseSource pyramid atlas";
398
+ for (const t of [map, mip])
399
+ if (t)
400
+ renderer.initTexture(t);
401
+ const gpu = vdbCreateBoxPyramid(device, this.grids.pool, densityId, {
402
+ indexBox: box,
403
+ ...this.pyramidMaxOption,
404
+ targets: { base: getGPUTexture(renderer, map), atlas: mip ? getGPUTexture(renderer, mip) : null },
405
+ label: "vdbSparseSource pyramid",
406
+ });
407
+ return {
408
+ build: "gpu",
409
+ level: layout.level,
410
+ dimensions: [...layout.dimensions],
411
+ atlas: layout.atlas ? { dimensions: [...layout.atlas.dimensions], levels: layout.atlas.levels.length } : null,
412
+ lodLevels: layout.lodLevels,
413
+ map,
414
+ mip,
415
+ gpu,
416
+ };
417
+ }
418
+ /** G16a: the atlas texture a following source's shaders bind: the pyramid's, else a 1³ zero texture (never read). */
419
+ atlasTexture() {
420
+ if (this.pyramid?.mip)
421
+ return this.pyramid.mip;
422
+ if (!this.emptyAtlas) {
423
+ this.emptyAtlas = halfTexture(new Float32Array(1), [1, 1, 1]);
424
+ this.emptyAtlas.name = "vdbSparseSource empty atlas";
425
+ }
426
+ return this.emptyAtlas;
427
+ }
428
+ /**
429
+ * G16a (`followImage`): re-reads the layout when a channel grid's image changed and every channel grid is resident
430
+ * again (the update that writes the new image has run: the GPU must not read it through the old boxes, nor the
431
+ * old image through the new ones). Cheap when the pool's revision did not move; called from `version` (a
432
+ * `VDBFrameVolume` asks once per frame, before it places its box) and from the render updates.
433
+ */
434
+ syncLayout() {
435
+ if (!this.followImage)
436
+ return false;
437
+ const pool = this.grids.pool;
438
+ if (typeof pool.revision !== "number" || pool.revision === this.layoutPoolRevision)
439
+ return false;
440
+ let revision = 0;
441
+ let resident = true;
442
+ const infos = [];
443
+ for (const id of this.poolIds) {
444
+ let i;
445
+ try {
446
+ i = pool.info(id);
447
+ }
448
+ catch {
449
+ return false; // a removed grid: keep the last layout
450
+ }
451
+ revision = Math.max(revision, i.imageRevision ?? 0);
452
+ if (i.resident === false)
453
+ resident = false;
454
+ infos.push({ id, type: i.type });
455
+ }
456
+ if (revision === this.layoutRevision) {
457
+ this.layoutPoolRevision = pool.revision;
458
+ return false;
459
+ }
460
+ if (!resident)
461
+ return false; // look again once the update has written it
462
+ // A new image of a type the module does not decode would read 0 through the new boxes, silently: say so instead
463
+ // (every call until the source is remade over a node that compiles it).
464
+ this.checkTypes(infos, "a channel grid's new image");
465
+ this.relayout();
466
+ this.layoutRevision = revision;
467
+ this.layoutPoolRevision = pool.revision;
468
+ return true;
469
+ }
470
+ /**
471
+ * Throws `VDB_TYPE_NOT_COMPILED` when a channel grid's type is not among the `VDBGridNode`'s compiled `types` (those
472
+ * grids read 0). A node stand-in without `options.types` is not checked.
473
+ */
474
+ checkTypes(infos, what) {
475
+ const compiled = this.grids.options.types;
476
+ if (!compiled)
477
+ return;
478
+ const missing = infos.filter((i) => !compiled.includes(i.type));
479
+ if (missing.length === 0)
480
+ return;
481
+ throw new VDBError("VDB_TYPE_NOT_COMPILED", `vdbSparseSource: ${what} is ${missing.map((m) => `grid ${m.id}: ${m.type}`).join(", ")}, but the VDBGridNode compiles only ` +
482
+ `${compiled.join(", ")}; it would read 0. Make the VDBGridNode with types that include every type the images will have ` +
483
+ `(a coarse-first stream lists them in VDBCoarseFirstEvent.types: types: event.types), then make the source over it.`);
484
+ }
485
+ relayout() {
486
+ const pool = this.grids.pool;
487
+ const ids = this.ids;
488
+ const density = pool.info(ids.density);
489
+ const b = { min: [...density.indexBBox.min], max: [...density.indexBBox.max] };
490
+ const old = this.indexBox;
491
+ const moved = b.min.some((v, a) => v !== old.min[a]) || b.max.some((v, a) => v !== old.max[a]);
492
+ this.indexBox = b;
493
+ this.texels = [b.max[0] - b.min[0] + 1, b.max[1] - b.min[1] + 1, b.max[2] - b.min[2] + 1];
494
+ const [sx, sy, sz] = this.texels;
495
+ this.boxToSource = new Float32Array([sx, 0, 0, 0, 0, sy, 0, 0, 0, 0, sz, 0, b.min[0] - 0.5 + 0.5 * sx, b.min[1] - 0.5 + 0.5 * sy, b.min[2] - 0.5 + 0.5 * sz, 1]);
496
+ this.bbox = worldBox(b, density.indexToWorld);
497
+ const auxBox = (id) => (id === undefined ? null : { min: [...pool.info(id).indexBBox.min], max: [...pool.info(id).indexBBox.max] });
498
+ this.auxBoxes = { mask: auxBox(ids.mask), albedo: auxBox(Array.isArray(ids.albedo) ? ids.albedo[0] : ids.albedo), emission: auxBox(ids.emission) };
499
+ // A new box needs a pyramid of its size (and box min); the same box rebuilds in place (refreshPyramid).
500
+ const previous = this.pyramid;
501
+ if (moved && previous?.gpu) {
502
+ this.pyramid = this.gpuPyramid(b, ids.density);
503
+ this.lodLevels = this.pyramid.lodLevels;
504
+ this.poolRevisionSeen = -1;
505
+ }
506
+ this.gpuBytes = this.measure();
507
+ this.layoutUpdates++;
508
+ for (const apply of this.layoutListeners)
509
+ apply();
510
+ // After the uniforms point at the new textures, the old ones go, but not yet: three r187's texture.dispose()
511
+ // destroys the GPUTexture at once, and a pass this render already recorded (not submitted yet) may bind them.
512
+ // Renders submit in the task that records them, so wait past this task, then for the queue's submitted work.
513
+ if (previous !== this.pyramid && previous) {
514
+ const retire = () => {
515
+ previous.gpu?.destroy();
516
+ previous.map.dispose();
517
+ previous.mip?.dispose();
518
+ };
519
+ const device = this.grids.pool.device;
520
+ if (device?.queue)
521
+ setTimeout(() => void device.queue.onSubmittedWorkDone().then(retire, retire), 0);
522
+ else
523
+ retire();
524
+ }
525
+ }
526
+ /**
527
+ * Content version: the newest `imageRevision` of the channel grids (K3.2a), so a `pool.replace` (a
528
+ * sequence frame) changes it and the 'frame' lighting caster signature (V44) draws the shadow grid
529
+ * again. Read through the pool's `revision` first (cheap when nothing changed).
530
+ */
531
+ get version() {
532
+ // G16a: a following source's version is the image revision its layout describes (it moves once the new image is
533
+ // resident and the boxes were re-read), so a VDBFrameVolume re-reads its box and the uniforms then.
534
+ if (this.followImage) {
535
+ this.syncLayout();
536
+ return this.layoutRevision;
537
+ }
538
+ const pool = this.grids.pool;
539
+ if (typeof pool.revision !== "number")
540
+ return 0;
541
+ if (pool.revision !== this.versionSeen.poolRevision) {
542
+ let value = 0;
543
+ for (const id of this.poolIds) {
544
+ try {
545
+ value = Math.max(value, pool.info(id).imageRevision ?? 0);
546
+ }
547
+ catch {
548
+ // A removed grid: keep the others' revision.
549
+ }
550
+ }
551
+ this.versionSeen = { poolRevision: pool.revision, value };
552
+ }
553
+ return this.versionSeen.value;
554
+ }
555
+ refreshPyramid() {
556
+ this.syncLayout();
557
+ const gpu = this.pyramid?.gpu;
558
+ if (!gpu)
559
+ return false;
560
+ // Cheap test first: the pool's revision moves on any change; the grid's imageRevision tells whether this one did.
561
+ const pool = this.grids.pool;
562
+ if (pool.revision === this.poolRevisionSeen)
563
+ return false;
564
+ const device = pool.device;
565
+ const encoder = device.createCommandEncoder({ label: "vdbSparseSource pyramid" });
566
+ if (!gpu.update(encoder)) {
567
+ // Not resident yet (the update that writes it is still to come): look again next time.
568
+ if (!gpu.stale)
569
+ this.poolRevisionSeen = pool.revision;
570
+ return false;
571
+ }
572
+ device.queue.submit([encoder.finish()]);
573
+ this.poolRevisionSeen = pool.revision;
574
+ this.pyramidBuilds++;
575
+ return true;
576
+ }
577
+ refreshStats() {
578
+ if (this.statsReadback)
579
+ return this.statsReadback;
580
+ const pool = this.grids.pool;
581
+ // Cheap test first (once per render): the pool's revision moves on any change.
582
+ if (typeof pool.revision !== "number" || pool.revision === this.statsPoolRevisionSeen || !pool.device || !pool.buffer)
583
+ return Promise.resolve(false);
584
+ let info;
585
+ try {
586
+ info = pool.info(this.ids.density);
587
+ }
588
+ catch {
589
+ return Promise.resolve(false); // removed: keep the last stats
590
+ }
591
+ const revision = info.imageRevision ?? -1;
592
+ if (revision === this.statsRevision) {
593
+ this.statsPoolRevisionSeen = pool.revision;
594
+ return Promise.resolve(false);
595
+ }
596
+ // Not written yet (the update that writes it is still to come): look again next time.
597
+ if (!info.resident || info.imageOffset === null || info.imageOffset === undefined || !info.imageBytes)
598
+ return Promise.resolve(false);
599
+ const { device, buffer } = pool;
600
+ const bytes = info.imageBytes;
601
+ const staging = device.createBuffer({ label: "vdbSparseSource stats readback", size: bytes, usage: 0x1 | 0x8 }); // MAP_READ | COPY_DST (no WebGPU globals in Node tests)
602
+ const encoder = device.createCommandEncoder({ label: "vdbSparseSource stats readback" });
603
+ encoder.copyBufferToBuffer(buffer, info.imageOffset, staging, 0, bytes);
604
+ device.queue.submit([encoder.finish()]);
605
+ const seen = pool.revision;
606
+ this.statsReadback = staging
607
+ .mapAsync(0x1) // GPUMapMode.READ
608
+ .then(() => {
609
+ const image = new Uint32Array(staging.getMappedRange().slice(0));
610
+ this.stats = imageStats(image, this.normalize === "clamp01");
611
+ this.statsRevision = revision;
612
+ this.statsPoolRevisionSeen = seen;
613
+ this.statsUpdates++;
614
+ for (const apply of this.statsListeners)
615
+ apply();
616
+ return true;
617
+ })
618
+ .catch(() => false)
619
+ .finally(() => {
620
+ staging.destroy();
621
+ this.statsReadback = null;
622
+ });
623
+ return this.statsReadback;
624
+ }
625
+ nodeAverageFn() {
626
+ this.nodeAverageCache ??= wgslFn(NODE_AVERAGE_WGSL, [this.grids.module]);
627
+ return this.nodeAverageCache;
628
+ }
629
+ nodeLodFn() {
630
+ this.nodeLodCache ??= wgslFn(nodeLodWgsl(this.fastTrilinear ? "vdbSampleFast" : "vdbSample"), [this.grids.module, this.nodeAverageFn()]);
631
+ return this.nodeLodCache;
632
+ }
633
+ /** `vdbFrameSample(g, xyz) -> f32`: level 0 (index space) as configured (`fastTrilinear`, `cacheAccessor`). */
634
+ sampleFn() {
635
+ this.sampleCache ??= wgslFn(sampleWgsl(this.fastTrilinear, this.cacheAccessor), (this.cacheAccessor ? [this.grids.module, code(SAMPLE_CACHE_WGSL, [this.grids.module])] : [this.grids.module]));
636
+ return this.sampleCache;
637
+ }
638
+ skipEmptyFn() {
639
+ this.skipCache ??= wgslFn(SKIP_EMPTY_WGSL, [this.grids.module]);
640
+ return this.skipCache;
641
+ }
642
+ createUniforms(p) {
643
+ let densityScale = p.densityScale ?? 1.0;
644
+ let normalize = p.normalizeDensity ?? true;
645
+ const boxUniform = (bx) => bx ? { min: uniform(new THREE.Vector3(...bx.min)), size: uniform(new THREE.Vector3(bx.max[0] - bx.min[0] + 1, bx.max[1] - bx.min[1] + 1, bx.max[2] - bx.min[2] + 1)) } : null;
646
+ const levels = uniform(this.lodLevels);
647
+ // K3.2e: the mean follows the density image (refreshStats, checked once per render, reads a replaced image
648
+ // back). K5.9a: the new mean is applied once, when the readback lands; the render update never writes the
649
+ // value, so a value set per draw (VDBFrameVolume's densityScale, through setVolumeUniforms) stays as set.
650
+ const densityScaleU = uniform(vdbDensityScaleOf(this, densityScale, normalize)).onRenderUpdate(() => {
651
+ this.syncLayout();
652
+ void this.refreshStats();
653
+ return undefined;
654
+ });
655
+ const applyStats = () => {
656
+ densityScaleU.value = vdbDensityScaleOf(this, densityScale, normalize);
657
+ };
658
+ this.statsListeners.add(applyStats);
659
+ const pyr = this.pyramid;
660
+ // G16a: a following source samples its pyramid through texture nodes made here (one pair per uniforms), so a new
661
+ // layout swaps their textures; the atlas node always exists (a 1³ texture when the layout has none).
662
+ const textures = this.followImage && pyr ? { map: texture3D(pyr.map), mip: texture3D(this.atlasTexture()) } : null;
663
+ const u = {
664
+ worldScale: uniform(p.worldScale ?? 1.0),
665
+ gridTexels: uniform(new THREE.Vector3(...this.texels)),
666
+ mipAtlasSize: uniform(new THREE.Vector3(1, 1, 1)),
667
+ maps: { density: null, mask: null, albedo: null, emission: null },
668
+ mipMaps: { density: null, mask: null, albedo: null, emission: null },
669
+ // getSampledLod reads density (and mask) levels; aux channels are level 0 only but report the same.
670
+ mipLevels: { density: levels, mask: levels, albedo: levels, emission: levels },
671
+ baseColor: uniform(new THREE.Color(p.baseColor ?? 0xffffff)),
672
+ extinction: uniform(p.extinction ?? 100.0),
673
+ densityScale: densityScaleU,
674
+ densityCutoff: uniform(p.densityCutoff ?? 0.0),
675
+ emissiveIntensity: uniform(p.emissiveIntensity ?? 1.0),
676
+ temperatureRange: uniform(new THREE.Vector2(...(p.temperatureRange ?? [0.0, 6500.0]))),
677
+ offset3D: uniform(new THREE.Vector3().copy((p.offset3D ?? { x: 0, y: 0, z: 0 }))),
678
+ wrap3D: uniform(p.wrap3D ?? THREE.ClampToEdgeWrapping, "int"),
679
+ boxes: { density: boxUniform(this.indexBox), mask: boxUniform(this.auxBoxes.mask), albedo: boxUniform(this.auxBoxes.albedo), emission: boxUniform(this.auxBoxes.emission) },
680
+ // The GPU pyramid rebuilds (on an image change) from here: once per render that samples it.
681
+ pyramidLevel: pyr?.gpu ? uniform(pyr.level).onRenderUpdate(() => void this.refreshPyramid()) : uniform(pyr?.level ?? 0),
682
+ pyramidLevels: uniform(pyr?.atlas?.levels ?? 0),
683
+ pyramidTexels: uniform(new THREE.Vector3(...(pyr?.dimensions ?? [1, 1, 1]))),
684
+ pyramidAtlasSize: uniform(new THREE.Vector3(...(pyr?.atlas?.dimensions ?? [1, 1, 1]))),
685
+ update: (s, o = {}) => {
686
+ if (s !== this)
687
+ throw bad("these uniforms belong to another source.");
688
+ if (o.densityScale !== undefined)
689
+ densityScale = o.densityScale;
690
+ if (o.normalizeDensity !== undefined)
691
+ normalize = o.normalizeDensity;
692
+ densityScaleU.value = vdbDensityScaleOf(this, densityScale, normalize);
693
+ if (this.followImage)
694
+ applyLayout();
695
+ },
696
+ dispose: () => {
697
+ this.statsListeners.delete(applyStats);
698
+ this.layoutListeners.delete(applyLayout);
699
+ pyramidTextures.delete(u);
700
+ },
701
+ };
702
+ // G16a: the layout as uniform values (and texture swaps), applied again after every re-read.
703
+ const setBox = (b, bx) => {
704
+ if (!b || !bx)
705
+ return;
706
+ b.min.value.set(...bx.min);
707
+ b.size.value.set(bx.max[0] - bx.min[0] + 1, bx.max[1] - bx.min[1] + 1, bx.max[2] - bx.min[2] + 1);
708
+ };
709
+ const applyLayout = () => {
710
+ u.gridTexels.value.set(...this.texels);
711
+ setBox(u.boxes.density, this.indexBox);
712
+ setBox(u.boxes.mask, this.auxBoxes.mask);
713
+ setBox(u.boxes.albedo, this.auxBoxes.albedo);
714
+ setBox(u.boxes.emission, this.auxBoxes.emission);
715
+ levels.value = this.lodLevels;
716
+ const p = this.pyramid;
717
+ if (!p)
718
+ return;
719
+ u.pyramidLevel.value = p.level;
720
+ u.pyramidLevels.value = p.atlas?.levels ?? 0;
721
+ u.pyramidTexels.value.set(...p.dimensions);
722
+ u.pyramidAtlasSize.value.set(...(p.atlas?.dimensions ?? [1, 1, 1]));
723
+ if (textures) {
724
+ textures.map.value = p.map;
725
+ textures.mip.value = this.atlasTexture();
726
+ }
727
+ };
728
+ if (this.followImage) {
729
+ this.layoutListeners.add(applyLayout);
730
+ if (textures)
731
+ pyramidTextures.set(u, textures);
732
+ }
733
+ return u;
734
+ }
735
+ createNodes(u0, o) {
736
+ const u = u0;
737
+ if (!u.boxes)
738
+ throw bad("createNodes needs this source's uniforms (vdbVolumeUniforms(source)).");
739
+ const { grids, ids, lod } = this;
740
+ const useMipmaps = o.useMipmaps ?? this.lodLevels > 0;
741
+ const gridUv = vdbGridUv(u);
742
+ const clamp01 = this.normalize === "clamp01";
743
+ // G16a: a following source's LOD range is a uniform (it changes with the layout); otherwise a constant.
744
+ const lodMax = this.followImage ? u.mipLevels.density : float(this.lodLevels);
745
+ const followTextures = this.followImage ? pyramidTextures.get(u) : undefined;
746
+ // uvw → sample index space, clamped to the voxels (his clamp-to-edge).
747
+ const samplePos = (box, uvw) => clamp(box.min.add(uvw.mul(box.size)).sub(0.5), box.min, box.min.add(box.size).sub(1.0));
748
+ const level0 = this.fastTrilinear || this.cacheAccessor ? (id, p) => this.sampleFn()(uint(id), p) : (id, p) => vdbSample(grids, id, p, { space: "index" });
749
+ const densityAt = (uvw, lodIn) => {
750
+ const p = samplePos(u.boxes.density, uvw).toVar();
751
+ if (!useMipmaps || lod === "none")
752
+ return level0(ids.density, p);
753
+ const l = clamp(lodIn, 0.0, lodMax).toVar();
754
+ if (lod === "node-average")
755
+ return this.nodeLodFn()(uint(ids.density), p, l);
756
+ const r = float(0.0).toVar();
757
+ // coarse-pyramid
758
+ const pyr = this.pyramid;
759
+ if (this.followImage && !followTextures)
760
+ throw bad("createNodes needs this source's uniforms (vdbVolumeUniforms(source)).");
761
+ const pyrMap = followTextures ? followTextures.map : texture3D(pyr.map);
762
+ const pyrMip = followTextures ? followTextures.mip : pyr.mip ? texture3D(pyr.mip) : null;
763
+ const { sampleMipmapped } = vdbMipSampler(u.pyramidTexels, u.pyramidAtlasSize);
764
+ const coarse = (rel) => (pyrMip ? sampleMipmapped(pyrMap, pyrMip, u.pyramidLevels, uvw, rel).r : pyrMap.sample(uvw).level(float(0.0)).r);
765
+ // K3.2d: one level-0 call site (each inlines the whole tree walk), shared by the two branches below.
766
+ const fine = float(0.0).toVar();
767
+ If(l.lessThan(u.pyramidLevel), () => {
768
+ fine.assign(level0(ids.density, p));
769
+ });
770
+ If(l.lessThanEqual(0.0), () => {
771
+ r.assign(fine);
772
+ })
773
+ .ElseIf(l.lessThan(u.pyramidLevel), () => {
774
+ r.assign(mix(fine, coarse(float(0.0)), l.div(u.pyramidLevel)));
775
+ })
776
+ .Else(() => {
777
+ r.assign(coarse(l.sub(u.pyramidLevel)));
778
+ });
779
+ return r;
780
+ };
781
+ // L892
782
+ const getLod = (localSize) => max(0.0, log2(localSize.mul(vdbMaxComponent(u.gridTexels))));
783
+ // L914–936: extinction per world unit; the mask at local + 0.5 (no offset or wrap), as his.
784
+ const sampleExtinction = (localPosition, lodIn) => {
785
+ const mask = this.channels.mask ? level0(ids.mask, samplePos(u.boxes.mask, localPosition.add(0.5))) : float(1.0);
786
+ const raw = densityAt(gridUv(localPosition), lodIn);
787
+ const value = (clamp01 ? clamp(raw, 0.0, 1.0) : raw).mul(mask);
788
+ return select(value.greaterThan(u.densityCutoff), value, float(0.0)).mul(u.densityScale).mul(u.extinction).div(u.worldScale);
789
+ };
790
+ // L960–971 (level 0)
791
+ const sampleAlbedo = (localPosition) => {
792
+ if (!this.channels.albedo)
793
+ return u.baseColor.mul(vec3(1.0));
794
+ const p = samplePos(u.boxes.albedo, gridUv(localPosition)).toVar();
795
+ const a = ids.albedo;
796
+ const rgb = Array.isArray(a) ? vec3(level0(a[0], p), level0(a[1], p), level0(a[2], p)) : vec3(level0(a, p));
797
+ return u.baseColor.mul(clamp(rgb, 0.0, 1.0));
798
+ };
799
+ // L990–1017 (level 0), colour via the caller's hook (V24).
800
+ const emissionColor = o.emissionColor ?? (() => vec3(1.0));
801
+ const sampleEmission = (localPosition) => {
802
+ if (!this.channels.emission)
803
+ return vec3(0.0);
804
+ const range = u.temperatureRange;
805
+ const value = level0(ids.emission, samplePos(u.boxes.emission, gridUv(localPosition)));
806
+ const temperature = mix(range.x, range.y, value);
807
+ const power = pow(temperature.div(max(range.y, 1.0)), 4.0);
808
+ const color = select(temperature.greaterThan(0.0), emissionColor(temperature), vec3(0.0));
809
+ return color.mul(power).mul(u.emissiveIntensity);
810
+ };
811
+ // HDDA empty-space query (R1 §4.3 "HDDA gains"): exact only where the bound covers every voxel a
812
+ // sample reads: level 0 (the trilinear 2x2x2 cell block), or a coarse level whose taps reach at
813
+ // most 8 voxels past the cell (dilated 3x3x3 block): node averages up to level 3, pyramid bases
814
+ // up to level 2. Wrapping or an offset moves samples off the ray's cells: no skipping then.
815
+ const skipEmpty = (origin, direction, t0, t1, lodIn) => {
816
+ const box = u.boxes.density;
817
+ const l = useMipmaps && lod !== "none" ? clamp(lodIn, 0.0, lodMax) : float(0.0);
818
+ const dilate = int(0).toVar();
819
+ // Cost rule (K3.2 A/B): a walk visits 8³ cells at 8 node lookups each, so it only pays while samples are at
820
+ // most one cell apart (the march's lod ≤ 3, i.e. steps ≤ 8 voxels); sparser marches sample everything.
821
+ const enabled = u.wrap3D
822
+ .equal(int(THREE.ClampToEdgeWrapping))
823
+ .and(vdbMaxComponent(u.offset3D.abs()).equal(0.0))
824
+ .and(lodIn.lessThanEqual(3.0))
825
+ .toVar();
826
+ if (useMipmaps && lod === "node-average") {
827
+ If(l.greaterThan(0.0), () => {
828
+ dilate.assign(1);
829
+ If(l.greaterThan(3.0), () => {
830
+ enabled.assign(false);
831
+ });
832
+ });
833
+ }
834
+ else if (useMipmaps && lod === "coarse-pyramid") {
835
+ If(l.greaterThan(0.0), () => {
836
+ dilate.assign(1);
837
+ If(u.pyramidLevel.greaterThan(2.0).or(l.greaterThan(2.0)), () => {
838
+ enabled.assign(false);
839
+ });
840
+ });
841
+ }
842
+ const result = vec2(t0, 1e30).toVar();
843
+ If(enabled, () => {
844
+ const o2 = box.min.sub(0.5).add(origin.add(0.5).mul(box.size));
845
+ const d2 = direction.mul(box.size);
846
+ // The mask multiplies the density, so with a mask only a zero bound is a safe skip.
847
+ const cutoff = this.channels.mask ? float(0.0) : max(u.densityCutoff, 0.0);
848
+ result.assign(this.skipEmptyFn()(uint(ids.density), o2, d2, t0, t1, cutoff, dilate.toUint()));
849
+ });
850
+ return result;
851
+ };
852
+ return { getLod, sampleExtinction, sampleAlbedo, sampleEmission, ...vdbBoxNodes, skipEmpty };
853
+ }
854
+ dispose() {
855
+ this.pyramid?.gpu?.destroy();
856
+ this.pyramid?.map.dispose();
857
+ this.pyramid?.mip?.dispose();
858
+ this.emptyAtlas?.dispose();
859
+ this.layoutListeners.clear();
860
+ this.statsListeners.clear();
861
+ }
862
+ }
863
+ /**
864
+ * A sparse volume source over pool grids (see the file comment). `grids` must be a `VDBGridNode`
865
+ * made with profile `'march'` and helpers `['cellBoundTrilinear', 'hddaCellEnd']` (+ `'nodeAverage'`
866
+ * for that LOD). The source holds no grid buffer of its own (the pool's), only the pyramid textures
867
+ * (and, building on the GPU, the build's scratch buffers).
868
+ */
869
+ export function vdbSparseSource(grids, ids, options = {}) {
870
+ if (!grids || typeof grids !== "object" || !("module" in grids))
871
+ throw bad("pass a VDBGridNode.");
872
+ if (!ids || !Number.isInteger(ids.density))
873
+ throw bad("ids.density must be a pool grid id.");
874
+ return new SparseSource(grids, ids, options);
875
+ }
876
+ //# sourceMappingURL=sparse-source.js.map