@pmndrs/vdb 0.1.0 → 0.2.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 (576) hide show
  1. package/CHANGELOG.md +592 -2
  2. package/README.md +85 -37
  3. package/THIRD_PARTY_NOTICES.md +197 -29
  4. package/dist/core/compute/atlas.d.ts +10 -2
  5. package/dist/core/compute/atlas.js +6 -1
  6. package/dist/core/compute/atlas.js.map +1 -1
  7. package/dist/core/compute/index.d.ts +3 -3
  8. package/dist/core/compute/index.js +2 -2
  9. package/dist/core/compute/index.js.map +1 -1
  10. package/dist/core/compute/majorant-build.d.ts +45 -0
  11. package/dist/core/compute/majorant-build.js +193 -0
  12. package/dist/core/compute/majorant-build.js.map +1 -0
  13. package/dist/core/compute/multi-scale.d.ts +155 -0
  14. package/dist/core/compute/multi-scale.js +684 -0
  15. package/dist/core/compute/multi-scale.js.map +1 -0
  16. package/dist/core/compute/pyramid.d.ts +22 -7
  17. package/dist/core/compute/pyramid.js +22 -9
  18. package/dist/core/compute/pyramid.js.map +1 -1
  19. package/dist/core/compute/shared.d.ts +1 -0
  20. package/dist/core/compute/shared.js.map +1 -1
  21. package/dist/core/compute/stats.d.ts +10 -3
  22. package/dist/core/compute/stats.js +6 -1
  23. package/dist/core/compute/stats.js.map +1 -1
  24. package/dist/core/compute/value-transform.d.ts +8 -1
  25. package/dist/core/compute/value-transform.js +9 -3
  26. package/dist/core/compute/value-transform.js.map +1 -1
  27. package/dist/core/errors.d.ts +10 -0
  28. package/dist/core/errors.js +8 -0
  29. package/dist/core/errors.js.map +1 -1
  30. package/dist/core/frame/emission.d.ts +85 -0
  31. package/dist/core/frame/emission.js +422 -0
  32. package/dist/core/frame/emission.js.map +1 -0
  33. package/dist/core/frame/environment.d.ts +47 -0
  34. package/dist/core/frame/environment.js +178 -0
  35. package/dist/core/frame/environment.js.map +1 -0
  36. package/dist/core/frame/index.d.ts +39 -0
  37. package/dist/core/frame/index.js +27 -0
  38. package/dist/core/frame/index.js.map +1 -0
  39. package/dist/core/frame/kernels.d.ts +115 -0
  40. package/dist/core/frame/kernels.js +496 -0
  41. package/dist/core/frame/kernels.js.map +1 -0
  42. package/dist/core/frame/limits.d.ts +67 -0
  43. package/dist/core/frame/limits.js +109 -0
  44. package/dist/core/frame/limits.js.map +1 -0
  45. package/dist/core/frame/phase.d.ts +17 -0
  46. package/dist/core/frame/phase.js +53 -0
  47. package/dist/core/frame/phase.js.map +1 -0
  48. package/dist/core/frame/pipelines.d.ts +49 -0
  49. package/dist/core/frame/pipelines.js +147 -0
  50. package/dist/core/frame/pipelines.js.map +1 -0
  51. package/dist/core/frame/renderer.d.ts +122 -0
  52. package/dist/core/frame/renderer.js +1712 -0
  53. package/dist/core/frame/renderer.js.map +1 -0
  54. package/dist/core/frame/resources.d.ts +71 -0
  55. package/dist/core/frame/resources.js +100 -0
  56. package/dist/core/frame/resources.js.map +1 -0
  57. package/dist/core/frame/scattering.d.ts +97 -0
  58. package/dist/core/frame/scattering.js +332 -0
  59. package/dist/core/frame/scattering.js.map +1 -0
  60. package/dist/core/frame/settings.d.ts +243 -0
  61. package/dist/core/frame/settings.js +67 -0
  62. package/dist/core/frame/settings.js.map +1 -0
  63. package/dist/core/frame/shadows.d.ts +55 -0
  64. package/dist/core/frame/shadows.js +460 -0
  65. package/dist/core/frame/shadows.js.map +1 -0
  66. package/dist/core/frame/types.d.ts +415 -0
  67. package/dist/core/frame/types.js +49 -0
  68. package/dist/core/frame/types.js.map +1 -0
  69. package/dist/core/frame/uniforms.d.ts +105 -0
  70. package/dist/core/frame/uniforms.js +276 -0
  71. package/dist/core/frame/uniforms.js.map +1 -0
  72. package/dist/core/frame/wgsl/chunks.d.ts +4 -0
  73. package/dist/core/frame/wgsl/chunks.js +728 -0
  74. package/dist/core/frame/wgsl/chunks.js.map +1 -0
  75. package/dist/core/frame/wgsl/emission.d.ts +82 -0
  76. package/dist/core/frame/wgsl/emission.js +353 -0
  77. package/dist/core/frame/wgsl/emission.js.map +1 -0
  78. package/dist/core/frame/wgsl/environment.d.ts +42 -0
  79. package/dist/core/frame/wgsl/environment.js +711 -0
  80. package/dist/core/frame/wgsl/environment.js.map +1 -0
  81. package/dist/core/frame/wgsl/gather.d.ts +55 -0
  82. package/dist/core/frame/wgsl/gather.js +850 -0
  83. package/dist/core/frame/wgsl/gather.js.map +1 -0
  84. package/dist/core/frame/wgsl/integrate.d.ts +78 -0
  85. package/dist/core/frame/wgsl/integrate.js +213 -0
  86. package/dist/core/frame/wgsl/integrate.js.map +1 -0
  87. package/dist/core/frame/wgsl/lighting.d.ts +103 -0
  88. package/dist/core/frame/wgsl/lighting.js +1636 -0
  89. package/dist/core/frame/wgsl/lighting.js.map +1 -0
  90. package/dist/core/frame/wgsl/occluders.d.ts +20 -0
  91. package/dist/core/frame/wgsl/occluders.js +95 -0
  92. package/dist/core/frame/wgsl/occluders.js.map +1 -0
  93. package/dist/core/frame/wgsl/resolve.d.ts +19 -0
  94. package/dist/core/frame/wgsl/resolve.js +205 -0
  95. package/dist/core/frame/wgsl/resolve.js.map +1 -0
  96. package/dist/core/frame/wgsl/shadows.d.ts +35 -0
  97. package/dist/core/frame/wgsl/shadows.js +328 -0
  98. package/dist/core/frame/wgsl/shadows.js.map +1 -0
  99. package/dist/core/half.d.ts +11 -0
  100. package/dist/core/half.js +22 -0
  101. package/dist/core/half.js.map +1 -0
  102. package/dist/core/index.d.ts +25 -3
  103. package/dist/core/index.js +23 -4
  104. package/dist/core/index.js.map +1 -1
  105. package/dist/core/internal.d.ts +17 -1
  106. package/dist/core/internal.js +18 -1
  107. package/dist/core/internal.js.map +1 -1
  108. package/dist/core/pool/image-header.d.ts +13 -2
  109. package/dist/core/pool/image-header.js +24 -5
  110. package/dist/core/pool/image-header.js.map +1 -1
  111. package/dist/core/pool/layout.d.ts +35 -1
  112. package/dist/core/pool/layout.js +51 -2
  113. package/dist/core/pool/layout.js.map +1 -1
  114. package/dist/core/pool/limits.d.ts +1 -0
  115. package/dist/core/pool/limits.js +1 -0
  116. package/dist/core/pool/limits.js.map +1 -1
  117. package/dist/core/pool/pool.d.ts +120 -15
  118. package/dist/core/pool/pool.js +255 -24
  119. package/dist/core/pool/pool.js.map +1 -1
  120. package/dist/core/reference/index.d.ts +6 -0
  121. package/dist/core/reference/index.js +7 -0
  122. package/dist/core/reference/index.js.map +1 -0
  123. package/dist/core/reference/renderer.d.ts +105 -0
  124. package/dist/core/reference/renderer.js +474 -0
  125. package/dist/core/reference/renderer.js.map +1 -0
  126. package/dist/core/reference/wgsl.d.ts +65 -0
  127. package/dist/core/reference/wgsl.js +803 -0
  128. package/dist/core/reference/wgsl.js.map +1 -0
  129. package/dist/{three → core}/sequence.d.ts +32 -2
  130. package/dist/{three → core}/sequence.js +24 -2
  131. package/dist/core/sequence.js.map +1 -0
  132. package/dist/core/source/brick-cache.d.ts +165 -0
  133. package/dist/core/source/brick-cache.js +627 -0
  134. package/dist/core/source/brick-cache.js.map +1 -0
  135. package/dist/core/source/dense-fill.d.ts +53 -0
  136. package/dist/core/source/dense-fill.js +377 -0
  137. package/dist/core/source/dense-fill.js.map +1 -0
  138. package/dist/core/source/dense.d.ts +33 -2
  139. package/dist/core/source/dense.js +121 -24
  140. package/dist/core/source/dense.js.map +1 -1
  141. package/dist/core/source/sparse-wgsl.d.ts +92 -0
  142. package/dist/core/source/sparse-wgsl.js +481 -0
  143. package/dist/core/source/sparse-wgsl.js.map +1 -0
  144. package/dist/core/source/sparse.d.ts +192 -0
  145. package/dist/core/source/sparse.js +762 -0
  146. package/dist/core/source/sparse.js.map +1 -0
  147. package/dist/core/source/types.d.ts +59 -2
  148. package/dist/core/source/types.js.map +1 -1
  149. package/dist/core/surface/index.d.ts +10 -0
  150. package/dist/core/surface/index.js +9 -0
  151. package/dist/core/surface/index.js.map +1 -0
  152. package/dist/core/surface/level-set-pass.d.ts +140 -0
  153. package/dist/core/surface/level-set-pass.js +291 -0
  154. package/dist/core/surface/level-set-pass.js.map +1 -0
  155. package/dist/core/surface/level-set-wgsl.d.ts +46 -0
  156. package/dist/core/surface/level-set-wgsl.js +99 -0
  157. package/dist/core/surface/level-set-wgsl.js.map +1 -0
  158. package/dist/core/surface/points.d.ts +109 -0
  159. package/dist/core/surface/points.js +226 -0
  160. package/dist/core/surface/points.js.map +1 -0
  161. package/dist/core/types.d.ts +12 -3
  162. package/dist/core/types.js +6 -2
  163. package/dist/core/types.js.map +1 -1
  164. package/dist/core/wgsl/api.d.ts +29 -5
  165. package/dist/core/wgsl/api.js +110 -27
  166. package/dist/core/wgsl/api.js.map +1 -1
  167. package/dist/core/wgsl/generate.d.ts +8 -0
  168. package/dist/core/wgsl/generate.js +195 -22
  169. package/dist/core/wgsl/generate.js.map +1 -1
  170. package/dist/core/wgsl/lean.d.ts +50 -0
  171. package/dist/core/wgsl/lean.js +327 -0
  172. package/dist/core/wgsl/lean.js.map +1 -0
  173. package/dist/core/wgsl/level-set.d.ts +5 -1
  174. package/dist/core/wgsl/level-set.js +11 -5
  175. package/dist/core/wgsl/level-set.js.map +1 -1
  176. package/dist/core/wgsl/majorant.d.ts +62 -0
  177. package/dist/core/wgsl/majorant.js +381 -0
  178. package/dist/core/wgsl/majorant.js.map +1 -0
  179. package/dist/core/wgsl/pnanovdb.chunks.generated.d.ts +2 -2
  180. package/dist/core/wgsl/pnanovdb.chunks.generated.js +15 -12
  181. package/dist/core/wgsl/pnanovdb.chunks.generated.js.map +1 -1
  182. package/dist/core/wgsl/types.d.ts +97 -3
  183. package/dist/core/wgsl/types.js.map +1 -1
  184. package/dist/three/coarse-first.d.ts +53 -8
  185. package/dist/three/coarse-first.js +24 -6
  186. package/dist/three/coarse-first.js.map +1 -1
  187. package/dist/three/compute.d.ts +12 -2
  188. package/dist/three/compute.js +16 -6
  189. package/dist/three/compute.js.map +1 -1
  190. package/dist/three/device.d.ts +7 -0
  191. package/dist/three/device.js +3 -0
  192. package/dist/three/device.js.map +1 -1
  193. package/dist/three/frame/core-environment.d.ts +38 -0
  194. package/dist/three/frame/core-environment.js +154 -0
  195. package/dist/three/frame/core-environment.js.map +1 -0
  196. package/dist/three/frame/core-integrate-material.d.ts +34 -0
  197. package/dist/three/frame/core-integrate-material.js +162 -0
  198. package/dist/three/frame/core-integrate-material.js.map +1 -0
  199. package/dist/three/frame/core-occluders.d.ts +53 -0
  200. package/dist/three/frame/core-occluders.js +193 -0
  201. package/dist/three/frame/core-occluders.js.map +1 -0
  202. package/dist/three/frame/core-structs.d.ts +34 -0
  203. package/dist/three/frame/core-structs.js +61 -0
  204. package/dist/three/frame/core-structs.js.map +1 -0
  205. package/dist/three/frame/core-textures.d.ts +41 -0
  206. package/dist/three/frame/core-textures.js +114 -0
  207. package/dist/three/frame/core-textures.js.map +1 -0
  208. package/dist/three/frame/emission-light.d.ts +64 -114
  209. package/dist/three/frame/emission-light.js +188 -567
  210. package/dist/three/frame/emission-light.js.map +1 -1
  211. package/dist/three/frame/index.d.ts +11 -31
  212. package/dist/three/frame/index.js +9 -29
  213. package/dist/three/frame/index.js.map +1 -1
  214. package/dist/three/frame/medium.d.ts +12 -0
  215. package/dist/three/frame/medium.js.map +1 -1
  216. package/dist/three/frame/renderer.d.ts +58 -454
  217. package/dist/three/frame/renderer.js +409 -1427
  218. package/dist/three/frame/renderer.js.map +1 -1
  219. package/dist/three/frame/spectrum.d.ts +0 -14
  220. package/dist/three/frame/spectrum.js +2 -23
  221. package/dist/three/frame/spectrum.js.map +1 -1
  222. package/dist/three/frame/three-internals.d.ts +3 -8
  223. package/dist/three/frame/three-internals.js +13 -11
  224. package/dist/three/frame/three-internals.js.map +1 -1
  225. package/dist/three/frame/volume-shadows.d.ts +93 -107
  226. package/dist/three/frame/volume-shadows.js +278 -631
  227. package/dist/three/frame/volume-shadows.js.map +1 -1
  228. package/dist/three/frame/volume.d.ts +91 -33
  229. package/dist/three/frame/volume.js +63 -51
  230. package/dist/three/frame/volume.js.map +1 -1
  231. package/dist/three/grid-node.d.ts +34 -1
  232. package/dist/three/grid-node.js +36 -1
  233. package/dist/three/grid-node.js.map +1 -1
  234. package/dist/three/index.d.ts +20 -11
  235. package/dist/three/index.js +21 -12
  236. package/dist/three/index.js.map +1 -1
  237. package/dist/three/internal.d.ts +14 -8
  238. package/dist/three/internal.js +11 -7
  239. package/dist/three/internal.js.map +1 -1
  240. package/dist/three/level-set-material.d.ts +81 -8
  241. package/dist/three/level-set-material.js +162 -125
  242. package/dist/three/level-set-material.js.map +1 -1
  243. package/dist/three/level-set-physical-material.d.ts +154 -0
  244. package/dist/three/level-set-physical-material.js +253 -0
  245. package/dist/three/level-set-physical-material.js.map +1 -0
  246. package/dist/three/level-set-velocity.d.ts +94 -0
  247. package/dist/three/level-set-velocity.js +208 -0
  248. package/dist/three/level-set-velocity.js.map +1 -0
  249. package/dist/three/material.d.ts +141 -11
  250. package/dist/three/material.js +161 -18
  251. package/dist/three/material.js.map +1 -1
  252. package/dist/three/points.d.ts +60 -4
  253. package/dist/three/points.js +140 -9
  254. package/dist/three/points.js.map +1 -1
  255. package/dist/three/sparse-source.d.ts +49 -117
  256. package/dist/three/sparse-source.js +109 -580
  257. package/dist/three/sparse-source.js.map +1 -1
  258. package/dist/three/volume-source.d.ts +11 -156
  259. package/dist/three/volume-source.js +29 -272
  260. package/dist/three/volume-source.js.map +1 -1
  261. package/dist/tools/cpu/brick-atlas.d.ts +36 -0
  262. package/dist/tools/cpu/brick-atlas.js +129 -0
  263. package/dist/tools/cpu/brick-atlas.js.map +1 -0
  264. package/dist/tools/cpu/compute.d.ts +23 -4
  265. package/dist/tools/cpu/compute.js +35 -14
  266. package/dist/tools/cpu/compute.js.map +1 -1
  267. package/dist/tools/cpu/lean-reads.d.ts +81 -0
  268. package/dist/tools/cpu/lean-reads.js +262 -0
  269. package/dist/tools/cpu/lean-reads.js.map +1 -0
  270. package/dist/tools/cpu/level-set.d.ts +42 -3
  271. package/dist/tools/cpu/level-set.js +59 -7
  272. package/dist/tools/cpu/level-set.js.map +1 -1
  273. package/dist/tools/cpu/majorant.d.ts +265 -0
  274. package/dist/tools/cpu/majorant.js +541 -0
  275. package/dist/tools/cpu/majorant.js.map +1 -0
  276. package/dist/tools/cpu/march.d.ts +93 -2
  277. package/dist/tools/cpu/march.js +165 -14
  278. package/dist/tools/cpu/march.js.map +1 -1
  279. package/dist/tools/cpu/mesh-decimate.d.ts +124 -0
  280. package/dist/tools/cpu/mesh-decimate.js +1225 -0
  281. package/dist/tools/cpu/mesh-decimate.js.map +1 -0
  282. package/dist/tools/cpu/points.d.ts +29 -0
  283. package/dist/tools/cpu/points.js +38 -0
  284. package/dist/tools/cpu/points.js.map +1 -0
  285. package/dist/tools/cpu/read-value.d.ts +19 -6
  286. package/dist/tools/cpu/read-value.js +32 -17
  287. package/dist/tools/cpu/read-value.js.map +1 -1
  288. package/dist/tools/cpu/sample-trilinear.d.ts +17 -4
  289. package/dist/tools/cpu/sample-trilinear.js +19 -7
  290. package/dist/tools/cpu/sample-trilinear.js.map +1 -1
  291. package/dist/tools/cpu/stride-tables.d.ts +2 -2
  292. package/dist/tools/cpu/stride-tables.generated.d.ts +6 -5
  293. package/dist/tools/cpu/stride-tables.generated.js +7 -6
  294. package/dist/tools/cpu/stride-tables.generated.js.map +1 -1
  295. package/dist/tools/cpu/stride-tables.js +1 -1
  296. package/dist/tools/cpu/stride-tables.js.map +1 -1
  297. package/dist/tools/cpu/surface-nets.d.ts +90 -0
  298. package/dist/tools/cpu/surface-nets.js +597 -0
  299. package/dist/tools/cpu/surface-nets.js.map +1 -0
  300. package/dist/tools/errors.d.ts +9 -0
  301. package/dist/tools/errors.js +9 -0
  302. package/dist/tools/errors.js.map +1 -1
  303. package/dist/tools/frame/ambient.d.ts +98 -0
  304. package/dist/tools/frame/ambient.js +274 -0
  305. package/dist/tools/frame/ambient.js.map +1 -0
  306. package/dist/tools/frame/blackbody.d.ts +15 -0
  307. package/dist/tools/frame/blackbody.js +35 -0
  308. package/dist/tools/frame/blackbody.js.map +1 -1
  309. package/dist/tools/frame/brick-cache.d.ts +165 -0
  310. package/dist/tools/frame/brick-cache.js +326 -0
  311. package/dist/tools/frame/brick-cache.js.map +1 -0
  312. package/dist/tools/frame/emission.d.ts +9 -0
  313. package/dist/tools/frame/emission.js +9 -1
  314. package/dist/tools/frame/emission.js.map +1 -1
  315. package/dist/tools/frame/environment-gpu.d.ts +111 -0
  316. package/dist/tools/frame/environment-gpu.js +280 -0
  317. package/dist/tools/frame/environment-gpu.js.map +1 -0
  318. package/dist/tools/frame/environment.d.ts +3 -2
  319. package/dist/tools/frame/environment.js +8 -4
  320. package/dist/tools/frame/environment.js.map +1 -1
  321. package/dist/tools/frame/froxel.d.ts +58 -0
  322. package/dist/tools/frame/froxel.js +115 -0
  323. package/dist/tools/frame/froxel.js.map +1 -0
  324. package/dist/tools/frame/grid.d.ts +90 -0
  325. package/dist/tools/frame/grid.js +112 -13
  326. package/dist/tools/frame/grid.js.map +1 -1
  327. package/dist/tools/frame/index.d.ts +14 -0
  328. package/dist/tools/frame/index.js +45 -4
  329. package/dist/tools/frame/index.js.map +1 -1
  330. package/dist/tools/frame/medium.d.ts +16 -0
  331. package/dist/tools/frame/medium.js +3 -1
  332. package/dist/tools/frame/medium.js.map +1 -1
  333. package/dist/tools/frame/mie.d.ts +9 -0
  334. package/dist/tools/frame/mie.js +18 -0
  335. package/dist/tools/frame/mie.js.map +1 -1
  336. package/dist/tools/frame/mlp-nn.d.ts +36 -0
  337. package/dist/tools/frame/mlp-nn.js +159 -0
  338. package/dist/tools/frame/mlp-nn.js.map +1 -0
  339. package/dist/tools/frame/mlp-tza.d.ts +29 -0
  340. package/dist/tools/frame/mlp-tza.js +186 -0
  341. package/dist/tools/frame/mlp-tza.js.map +1 -0
  342. package/dist/tools/frame/mlp.d.ts +183 -0
  343. package/dist/tools/frame/mlp.js +434 -0
  344. package/dist/tools/frame/mlp.js.map +1 -0
  345. package/dist/tools/frame/ms-exit.d.ts +42 -0
  346. package/dist/tools/frame/ms-exit.js +90 -0
  347. package/dist/tools/frame/ms-exit.js.map +1 -0
  348. package/dist/tools/frame/multi-scale.d.ts +152 -0
  349. package/dist/tools/frame/multi-scale.js +302 -0
  350. package/dist/tools/frame/multi-scale.js.map +1 -0
  351. package/dist/tools/frame/neural-weights.d.ts +6 -5
  352. package/dist/tools/frame/neural-weights.js +234 -235
  353. package/dist/tools/frame/neural-weights.js.map +1 -1
  354. package/dist/tools/frame/neural.js +1 -1
  355. package/dist/tools/frame/neural.js.map +1 -1
  356. package/dist/tools/frame/occluders.d.ts +51 -0
  357. package/dist/tools/frame/occluders.js +72 -0
  358. package/dist/tools/frame/occluders.js.map +1 -0
  359. package/dist/tools/frame/pyramid-layout.d.ts +17 -0
  360. package/dist/tools/frame/pyramid-layout.js +47 -0
  361. package/dist/tools/frame/pyramid-layout.js.map +1 -1
  362. package/dist/tools/frame/shadows.d.ts +110 -0
  363. package/dist/tools/frame/shadows.js +286 -0
  364. package/dist/tools/frame/shadows.js.map +1 -1
  365. package/dist/tools/frame/sparse.d.ts +82 -0
  366. package/dist/tools/frame/sparse.js +242 -1
  367. package/dist/tools/frame/sparse.js.map +1 -1
  368. package/dist/tools/frame/spectral-stack.d.ts +159 -0
  369. package/dist/tools/frame/spectral-stack.js +391 -0
  370. package/dist/tools/frame/spectral-stack.js.map +1 -0
  371. package/dist/tools/frame/spectral.d.ts +21 -2
  372. package/dist/tools/frame/spectral.js +30 -8
  373. package/dist/tools/frame/spectral.js.map +1 -1
  374. package/dist/tools/frame/stack.d.ts +115 -0
  375. package/dist/tools/frame/stack.js +290 -0
  376. package/dist/tools/frame/stack.js.map +1 -0
  377. package/dist/tools/frame/view.d.ts +131 -0
  378. package/dist/tools/frame/view.js +337 -0
  379. package/dist/tools/frame/view.js.map +1 -0
  380. package/dist/tools/index.d.ts +66 -15
  381. package/dist/tools/index.js +55 -11
  382. package/dist/tools/index.js.map +1 -1
  383. package/dist/tools/internal.d.ts +58 -17
  384. package/dist/tools/internal.js +65 -11
  385. package/dist/tools/internal.js.map +1 -1
  386. package/dist/tools/nanovdb/build-from-vdb.d.ts +11 -2
  387. package/dist/tools/nanovdb/build-from-vdb.js +10 -2
  388. package/dist/tools/nanovdb/build-from-vdb.js.map +1 -1
  389. package/dist/tools/nanovdb/bytes.d.ts +17 -4
  390. package/dist/tools/nanovdb/bytes.js +46 -3
  391. package/dist/tools/nanovdb/bytes.js.map +1 -1
  392. package/dist/tools/nanovdb/coarse-first.d.ts +19 -3
  393. package/dist/tools/nanovdb/coarse-first.js +7 -1
  394. package/dist/tools/nanovdb/coarse-first.js.map +1 -1
  395. package/dist/tools/nanovdb/half.d.ts +22 -0
  396. package/dist/tools/nanovdb/half.js +73 -0
  397. package/dist/tools/nanovdb/half.js.map +1 -0
  398. package/dist/tools/nanovdb/index.d.ts +3 -1
  399. package/dist/tools/nanovdb/index.js +2 -1
  400. package/dist/tools/nanovdb/index.js.map +1 -1
  401. package/dist/tools/nanovdb/inspect.d.ts +10 -1
  402. package/dist/tools/nanovdb/inspect.js +8 -1
  403. package/dist/tools/nanovdb/inspect.js.map +1 -1
  404. package/dist/tools/nanovdb/leaf-codec.d.ts +35 -1
  405. package/dist/tools/nanovdb/leaf-codec.js +40 -4
  406. package/dist/tools/nanovdb/leaf-codec.js.map +1 -1
  407. package/dist/tools/nanovdb/quantize.d.ts +39 -10
  408. package/dist/tools/nanovdb/quantize.js +96 -44
  409. package/dist/tools/nanovdb/quantize.js.map +1 -1
  410. package/dist/tools/nanovdb/resample.d.ts +20 -3
  411. package/dist/tools/nanovdb/resample.js +20 -7
  412. package/dist/tools/nanovdb/resample.js.map +1 -1
  413. package/dist/tools/nanovdb/serialize.d.ts +34 -3
  414. package/dist/tools/nanovdb/serialize.js +51 -7
  415. package/dist/tools/nanovdb/serialize.js.map +1 -1
  416. package/dist/tools/nanovdb/stats.d.ts +34 -5
  417. package/dist/tools/nanovdb/stats.js +87 -11
  418. package/dist/tools/nanovdb/stats.js.map +1 -1
  419. package/dist/tools/nanovdb/transform.d.ts +4 -0
  420. package/dist/tools/nanovdb/transform.js +3 -0
  421. package/dist/tools/nanovdb/transform.js.map +1 -1
  422. package/dist/tools/nanovdb/tree.d.ts +14 -3
  423. package/dist/tools/nanovdb/tree.js +25 -13
  424. package/dist/tools/nanovdb/tree.js.map +1 -1
  425. package/dist/tools/nanovdb/write-nvdb.d.ts +47 -12
  426. package/dist/tools/nanovdb/write-nvdb.js +108 -28
  427. package/dist/tools/nanovdb/write-nvdb.js.map +1 -1
  428. package/dist/tools/nvdb-file.d.ts +77 -7
  429. package/dist/tools/nvdb-file.js +177 -20
  430. package/dist/tools/nvdb-file.js.map +1 -1
  431. package/dist/tools/nvdb-stream.d.ts +23 -7
  432. package/dist/tools/nvdb-stream.js +78 -34
  433. package/dist/tools/nvdb-stream.js.map +1 -1
  434. package/dist/tools/reference/analytic.d.ts +41 -0
  435. package/dist/tools/reference/analytic.js +95 -0
  436. package/dist/tools/reference/analytic.js.map +1 -0
  437. package/dist/tools/reference/environment.d.ts +62 -0
  438. package/dist/tools/reference/environment.js +154 -0
  439. package/dist/tools/reference/environment.js.map +1 -0
  440. package/dist/tools/reference/index.d.ts +12 -0
  441. package/dist/tools/reference/index.js +13 -0
  442. package/dist/tools/reference/index.js.map +1 -0
  443. package/dist/tools/reference/media.d.ts +14 -0
  444. package/dist/tools/reference/media.js +30 -0
  445. package/dist/tools/reference/media.js.map +1 -0
  446. package/dist/tools/reference/rng.d.ts +20 -0
  447. package/dist/tools/reference/rng.js +30 -0
  448. package/dist/tools/reference/rng.js.map +1 -0
  449. package/dist/tools/reference/scene.d.ts +312 -0
  450. package/dist/tools/reference/scene.js +169 -0
  451. package/dist/tools/reference/scene.js.map +1 -0
  452. package/dist/tools/reference/tables.d.ts +98 -0
  453. package/dist/tools/reference/tables.js +264 -0
  454. package/dist/tools/reference/tables.js.map +1 -0
  455. package/dist/tools/reference/trace.d.ts +160 -0
  456. package/dist/tools/reference/trace.js +497 -0
  457. package/dist/tools/reference/trace.js.map +1 -0
  458. package/dist/tools/vdb/blosc-write.d.ts +43 -0
  459. package/dist/tools/vdb/blosc-write.js +197 -0
  460. package/dist/tools/vdb/blosc-write.js.map +1 -0
  461. package/dist/tools/vdb/blosc.d.ts +35 -5
  462. package/dist/tools/vdb/blosc.js +82 -26
  463. package/dist/tools/vdb/blosc.js.map +1 -1
  464. package/dist/tools/vdb/blosclz.d.ts +33 -0
  465. package/dist/tools/vdb/blosclz.js +112 -0
  466. package/dist/tools/vdb/blosclz.js.map +1 -0
  467. package/dist/tools/vdb/byte-reader.d.ts +13 -3
  468. package/dist/tools/vdb/byte-reader.js +26 -4
  469. package/dist/tools/vdb/byte-reader.js.map +1 -1
  470. package/dist/tools/vdb/compression.d.ts +13 -5
  471. package/dist/tools/vdb/compression.js +13 -2
  472. package/dist/tools/vdb/compression.js.map +1 -1
  473. package/dist/tools/vdb/grid.js +8 -2
  474. package/dist/tools/vdb/grid.js.map +1 -1
  475. package/dist/tools/vdb/index.d.ts +2 -1
  476. package/dist/tools/vdb/index.js.map +1 -1
  477. package/dist/tools/vdb/lz4.d.ts +30 -0
  478. package/dist/tools/vdb/lz4.js +228 -0
  479. package/dist/tools/vdb/lz4.js.map +1 -0
  480. package/dist/tools/vdb/metadata.js +1 -1
  481. package/dist/tools/vdb/metadata.js.map +1 -1
  482. package/dist/tools/vdb/parse.d.ts +24 -2
  483. package/dist/tools/vdb/parse.js +102 -32
  484. package/dist/tools/vdb/parse.js.map +1 -1
  485. package/dist/tools/vdb/point-render-data.d.ts +49 -1
  486. package/dist/tools/vdb/point-render-data.js +91 -11
  487. package/dist/tools/vdb/point-render-data.js.map +1 -1
  488. package/dist/tools/vdb/points.d.ts +26 -0
  489. package/dist/tools/vdb/points.js +46 -5
  490. package/dist/tools/vdb/points.js.map +1 -1
  491. package/dist/tools/vdb/types.d.ts +59 -2
  492. package/dist/tools/vdb/types.js.map +1 -1
  493. package/dist/tools/wvdb/bake.d.ts +38 -9
  494. package/dist/tools/wvdb/bake.js +63 -13
  495. package/dist/tools/wvdb/bake.js.map +1 -1
  496. package/dist/tools/wvdb/dense.d.ts +80 -1
  497. package/dist/tools/wvdb/dense.js +67 -8
  498. package/dist/tools/wvdb/dense.js.map +1 -1
  499. package/dist/tools/wvdb/format.d.ts +59 -0
  500. package/dist/tools/wvdb/format.js +3 -0
  501. package/dist/tools/wvdb/format.js.map +1 -1
  502. package/dist/tools/wvdb/index.d.ts +1 -0
  503. package/dist/tools/wvdb/index.js +1 -0
  504. package/dist/tools/wvdb/index.js.map +1 -1
  505. package/dist/tools/wvdb/level-set.d.ts +10 -2
  506. package/dist/tools/wvdb/level-set.js.map +1 -1
  507. package/dist/tools/wvdb/nanovdb.d.ts +6 -1
  508. package/dist/tools/wvdb/nanovdb.js +5 -1
  509. package/dist/tools/wvdb/nanovdb.js.map +1 -1
  510. package/dist/tools/wvdb/partial.d.ts +74 -0
  511. package/dist/tools/wvdb/partial.js +233 -0
  512. package/dist/tools/wvdb/partial.js.map +1 -0
  513. package/dist/tools/wvdb/read.d.ts +77 -1
  514. package/dist/tools/wvdb/read.js +30 -7
  515. package/dist/tools/wvdb/read.js.map +1 -1
  516. package/dist/tools/wvdb/write.d.ts +23 -1
  517. package/dist/tools/wvdb/write.js +8 -1
  518. package/dist/tools/wvdb/write.js.map +1 -1
  519. package/package.json +2 -1
  520. package/vendor/VENDOR.md +21 -6
  521. package/vendor/pnanovdb.wgsl +52 -14
  522. package/dist/three/frame/blackbody.d.ts +0 -42
  523. package/dist/three/frame/blackbody.js +0 -74
  524. package/dist/three/frame/blackbody.js.map +0 -1
  525. package/dist/three/frame/chunks.d.ts +0 -157
  526. package/dist/three/frame/chunks.js +0 -559
  527. package/dist/three/frame/chunks.js.map +0 -1
  528. package/dist/three/frame/density-material.d.ts +0 -53
  529. package/dist/three/frame/density-material.js +0 -353
  530. package/dist/three/frame/density-material.js.map +0 -1
  531. package/dist/three/frame/environment-grid-material.d.ts +0 -21
  532. package/dist/three/frame/environment-grid-material.js +0 -184
  533. package/dist/three/frame/environment-grid-material.js.map +0 -1
  534. package/dist/three/frame/environment-material.d.ts +0 -22
  535. package/dist/three/frame/environment-material.js +0 -87
  536. package/dist/three/frame/environment-material.js.map +0 -1
  537. package/dist/three/frame/grid-mip-material.d.ts +0 -12
  538. package/dist/three/frame/grid-mip-material.js +0 -61
  539. package/dist/three/frame/grid-mip-material.js.map +0 -1
  540. package/dist/three/frame/inject-material.d.ts +0 -56
  541. package/dist/three/frame/inject-material.js +0 -424
  542. package/dist/three/frame/inject-material.js.map +0 -1
  543. package/dist/three/frame/integrate-material.d.ts +0 -26
  544. package/dist/three/frame/integrate-material.js +0 -198
  545. package/dist/three/frame/integrate-material.js.map +0 -1
  546. package/dist/three/frame/layout.d.ts +0 -34
  547. package/dist/three/frame/layout.js +0 -48
  548. package/dist/three/frame/layout.js.map +0 -1
  549. package/dist/three/frame/lighting-material.d.ts +0 -41
  550. package/dist/three/frame/lighting-material.js +0 -257
  551. package/dist/three/frame/lighting-material.js.map +0 -1
  552. package/dist/three/frame/lights.d.ts +0 -11
  553. package/dist/three/frame/lights.js +0 -16
  554. package/dist/three/frame/lights.js.map +0 -1
  555. package/dist/three/frame/neural.d.ts +0 -14
  556. package/dist/three/frame/neural.js +0 -105
  557. package/dist/three/frame/neural.js.map +0 -1
  558. package/dist/three/frame/phase.d.ts +0 -44
  559. package/dist/three/frame/phase.js +0 -80
  560. package/dist/three/frame/phase.js.map +0 -1
  561. package/dist/three/frame/shadow-density-material.d.ts +0 -30
  562. package/dist/three/frame/shadow-density-material.js +0 -123
  563. package/dist/three/frame/shadow-density-material.js.map +0 -1
  564. package/dist/three/frame/temporal-material.d.ts +0 -29
  565. package/dist/three/frame/temporal-material.js +0 -152
  566. package/dist/three/frame/temporal-material.js.map +0 -1
  567. package/dist/three/frame/transmittance-material.d.ts +0 -31
  568. package/dist/three/frame/transmittance-material.js +0 -315
  569. package/dist/three/frame/transmittance-material.js.map +0 -1
  570. package/dist/three/frame/view.d.ts +0 -103
  571. package/dist/three/frame/view.js +0 -296
  572. package/dist/three/frame/view.js.map +0 -1
  573. package/dist/three/frame/volume-shadow-material.d.ts +0 -36
  574. package/dist/three/frame/volume-shadow-material.js +0 -128
  575. package/dist/three/frame/volume-shadow-material.js.map +0 -1
  576. package/dist/three/sequence.js.map +0 -1
@@ -8,6 +8,10 @@
8
8
  * scene.add(new Mesh(vdbProxyGeometry(pool.info(id), { pad: 1 }), material));
9
9
  * // per frame: pool.update(encoder) (or pool.flush()), then grids.update(), then render.
10
10
  *
11
+ * The trace, normal and pixel ray are the core's `vdbLevelSetSurface` / `vdbLevelSetViewRay` WGSL
12
+ * (`core/surface/level-set-wgsl.ts`, K6.6), the same functions the raw pass `vdbCreateLevelSetPass`
13
+ * calls; this material adds three's lighting, shadows and depth conventions around them.
14
+ *
11
15
  * Portions derived from mjurczyk/openvdb (MIT), commit 75e7345
12
16
  * (`src/three-openvdb/levelset/webgpu/LevelSetSurfaceMaterial.js`,
13
17
  * `LevelSetSurfaceCore.js`): the back-face proxy box (the camera can be
@@ -40,7 +44,31 @@ import type { VDBUintUniform } from "./material.js";
40
44
  /** Default compiled cap on the samples one fragment's trace takes (the live `maxSamples` clamps under it). */
41
45
  export declare const VDB_LEVEL_SET_SAMPLES_CAP = 1024;
42
46
  /** Parameters of {@link VDBLevelSetMaterial}: three's standard material parameters plus the trace's. */
43
- export interface VDBLevelSetMaterialParameters extends THREE.MeshStandardMaterialParameters {
47
+ export interface VDBLevelSetMaterialParameters extends THREE.MeshStandardMaterialParameters, VDBLevelSetTraceParameters {
48
+ }
49
+ type ScalarUniform = ReturnType<typeof TSL.uniform> & {
50
+ value: number;
51
+ };
52
+ /** The trace a level-set material shades: its uniforms and TSL nodes (shared by the standard and physical materials). */
53
+ export interface VDBLevelSetTrace<TGridId extends number | VDBTslNode = number> {
54
+ readonly grids: VDBGridNode;
55
+ readonly gridId: TGridId extends number ? VDBUintUniform : VDBTslNode;
56
+ readonly sdfToIndex: ScalarUniform;
57
+ readonly maxSamples: ScalarUniform;
58
+ readonly normalStep: ScalarUniform;
59
+ readonly traceSource: string;
60
+ readonly surfaceNode: VDBTslNode;
61
+ readonly traceNode: VDBTslNode;
62
+ readonly hitWorldNode: VDBTslNode;
63
+ readonly hitViewNode: VDBTslNode;
64
+ readonly hitMaskNode: VDBTslNode;
65
+ readonly normalWorldNode: VDBTslNode;
66
+ readonly normalViewNode: VDBTslNode;
67
+ /** G13c: whether the roughness is widened by the traced normal's screen-space variation (compile time). */
68
+ readonly specularAntialiasing: boolean;
69
+ }
70
+ /** The trace parameters of {@link VDBLevelSetMaterialParameters} (also taken by `VDBLevelSetPhysicalMaterial`). */
71
+ export interface VDBLevelSetTraceParameters {
44
72
  /**
45
73
  * Index voxels per SDF unit. Default: `1 / voxelSize.x` from `grids.pool.info(id)` when the pool
46
74
  * has `info` and the id is a number, else 1. A level set stores distances in its own world units,
@@ -51,12 +79,50 @@ export interface VDBLevelSetMaterialParameters extends THREE.MeshStandardMateria
51
79
  maxSamples?: number;
52
80
  /** Compiled cap on {@link maxSamples}. Default {@link VDB_LEVEL_SET_SAMPLES_CAP}. */
53
81
  maxSamplesCap?: number;
54
- /** Central-difference step of the normal, in index voxels (live). Default 1. */
82
+ /**
83
+ * Central-difference step of the normal, in index voxels (live). Default 1. `0` (G13c-a): the exact gradient of the
84
+ * trilinear cell the trace hits (the trace's own surface: it never faces away from the ray; discontinuous across cell
85
+ * faces).
86
+ */
55
87
  normalStep?: number;
88
+ /**
89
+ * G13c: geometric specular antialiasing on the traced normal (Tokuyoshi and Kaplanyan 2019): the roughness grows
90
+ * with the normal's change across the pixel, so a low-roughness highlight on a sharp crease is spread over the pixel
91
+ * instead of hitting a few pixel centres with extreme values (on the teapot lid at clearcoat roughness 0.04: linear
92
+ * RMS error against a 4x4 supersampled frame 0.157 vs 0.541, largest per-pixel flicker 1.7 vs 143). three's own term
93
+ * (`getGeometryRoughness`) reads the proxy box's flat normal and adds nothing here. Sets `roughnessNode` (and
94
+ * `clearcoatRoughnessNode` on the physical material); compile time. Default false: in a tone-mapped image lit by
95
+ * an environment it measured worse (G13c-a: ACES RMS on the lid 0.0143 vs 0.0085 in demo 13's studio lighting).
96
+ */
97
+ specularAntialiasing?: boolean;
56
98
  }
57
- type ScalarUniform = ReturnType<typeof TSL.uniform> & {
58
- value: number;
59
- };
99
+ /** Screen-space variance of the pixel filter (Tokuyoshi and Kaplanyan 2019: 0.25, a box of one pixel). */
100
+ export declare const VDB_SPECULAR_AA_VARIANCE = 0.25;
101
+ /** Clamp on the added GGX alpha squared (Tokuyoshi and Kaplanyan's kappa, 0.18): bounds the term at silhouettes. */
102
+ export declare const VDB_SPECULAR_AA_CLAMP = 0.18;
103
+ /**
104
+ * CPU twin of {@link vdbLevelSetSpecularAa}: the perceptual roughness after adding the kernel's variance to GGX
105
+ * alpha squared, from the normal's screen-space differences (`dndx`, `dndy`; view space, per pixel).
106
+ */
107
+ export declare function vdbLevelSetSpecularAaCpu(roughness: number, dndx: readonly number[], dndy: readonly number[]): number;
108
+ /**
109
+ * G13c: `roughness` widened by the traced view normal's screen-space derivatives (see
110
+ * {@link VDBLevelSetTraceParameters.specularAntialiasing}). A miss pixel's normal is replaced by +z so its derivative
111
+ * stays finite; the clamp bounds what a silhouette quad adds.
112
+ */
113
+ export declare function vdbLevelSetSpecularAa(roughness: VDBTslNode, trace: Pick<VDBLevelSetTrace<number | VDBTslNode>, "normalViewNode" | "hitMaskNode">): VDBTslNode;
114
+ /**
115
+ * Validates the inputs and builds the trace's uniforms and nodes (G13a: one setup for both level-set materials).
116
+ * `owner` names the class in error messages.
117
+ */
118
+ export declare function vdbLevelSetTrace<TGridId extends number | VDBTslNode>(owner: string, renderer: VDBThreeRendererLike, grids: VDBGridNode, gridId: TGridId, params: VDBLevelSetTraceParameters): VDBLevelSetTrace<TGridId>;
119
+ /**
120
+ * Puts a trace on a node material: the back-face proxy, the hit mask, the vertex node (so `positionView` is a
121
+ * variable the lighting moves to the hit), the hit's depth, the received-shadow position and the normal.
122
+ */
123
+ export declare function vdbApplyLevelSetTrace(material: THREE.Material, trace: VDBLevelSetTrace<number | VDBTslNode>): void;
124
+ /** Lights read `positionView`: the shading point is the hit, not the box face (both materials' `setupLighting`). */
125
+ export declare function vdbLevelSetLightingPosition(trace: VDBLevelSetTrace<number | VDBTslNode>): void;
60
126
  /**
61
127
  * A level set's surface on a world-space proxy box (see the file comment).
62
128
  * Shades as a `MeshStandardNodeMaterial`: `color`, `roughness`, `metalness`,
@@ -67,11 +133,14 @@ type ScalarUniform = ReturnType<typeof TSL.uniform> & {
67
133
  * {@link normalWorldNode}, {@link normalViewNode} (e.g. `material.colorNode =
68
134
  * material.normalViewNode.mul(0.5).add(0.5)` with no lights shows the normals).
69
135
  * - Perspective and orthographic cameras (directional-light shadow maps).
136
+ * - Temporal antialiasing and upscalers: three's `velocity` is the proxy box's. Put a
137
+ * `VDBLevelSetVelocityNode` in the pass's MRT instead: it writes the hit's velocity for this material.
70
138
  *
71
139
  * @typeParam TGridId What the constructor got as the id: a number (default)
72
140
  * makes {@link gridId} a `uint` uniform; a node is kept as given.
73
141
  */
74
142
  export declare class VDBLevelSetMaterial<TGridId extends number | VDBTslNode = number> extends MeshStandardNodeMaterial {
143
+ /** Type flag: true. */
75
144
  readonly isVDBLevelSetMaterial = true;
76
145
  /** The pool node this material reads. */
77
146
  readonly grids: VDBGridNode;
@@ -81,13 +150,15 @@ export declare class VDBLevelSetMaterial<TGridId extends number | VDBTslNode = n
81
150
  readonly sdfToIndex: ScalarUniform;
82
151
  /** Trilinear samples per trace (live, clamped to the compiled cap). */
83
152
  readonly maxSamples: ScalarUniform;
84
- /** Central-difference step of the normal in index voxels (live). */
153
+ /** Central-difference step of the normal in index voxels (live); 0: the trilinear cell's exact gradient. */
85
154
  readonly normalStep: ScalarUniform;
86
- /** The trace entry's WGSL (for inspection and tests). */
155
+ /** The entry's WGSL (for inspection and tests): calls the core's `vdbLevelSetViewRay` and `vdbLevelSetSurface`. */
87
156
  readonly traceSource: string;
157
+ /** The packed surface (`mat4`, fragment stage): see {@link traceNode}, {@link normalWorldNode}; column 2 `.x` is the NDC depth. */
158
+ readonly surfaceNode: VDBTslNode;
88
159
  /** `vec4(hit world position, 1)` on a hit, `vec4(0)` on a miss (fragment stage). */
89
160
  readonly traceNode: VDBTslNode;
90
- /** The hit in world space. */
161
+ /** The hit in world space (what `VDBLevelSetVelocityNode` reprojects for the MRT `velocity` output). */
91
162
  readonly hitWorldNode: VDBTslNode;
92
163
  /** The hit in view space. */
93
164
  readonly hitViewNode: VDBTslNode;
@@ -97,6 +168,8 @@ export declare class VDBLevelSetMaterial<TGridId extends number | VDBTslNode = n
97
168
  readonly normalWorldNode: VDBTslNode;
98
169
  /** View-space unit normal at the hit (the material's `normalNode`). */
99
170
  readonly normalViewNode: VDBTslNode;
171
+ /** Whether `roughnessNode` carries the geometric specular antialiasing (G13c; compile time). */
172
+ readonly specularAntialiasing: boolean;
100
173
  /**
101
174
  * @param renderer The initialized `WebGPURenderer` the pool and `grids` live on.
102
175
  * @param grids A `VDBGridNode` on this renderer with profile `'march'` and helpers including `'levelSet'`.
@@ -8,6 +8,10 @@
8
8
  * scene.add(new Mesh(vdbProxyGeometry(pool.info(id), { pad: 1 }), material));
9
9
  * // per frame: pool.update(encoder) (or pool.flush()), then grids.update(), then render.
10
10
  *
11
+ * The trace, normal and pixel ray are the core's `vdbLevelSetSurface` / `vdbLevelSetViewRay` WGSL
12
+ * (`core/surface/level-set-wgsl.ts`, K6.6), the same functions the raw pass `vdbCreateLevelSetPass`
13
+ * calls; this material adds three's lighting, shadows and depth conventions around them.
14
+ *
11
15
  * Portions derived from mjurczyk/openvdb (MIT), commit 75e7345
12
16
  * (`src/three-openvdb/levelset/webgpu/LevelSetSurfaceMaterial.js`,
13
17
  * `LevelSetSurfaceCore.js`): the back-face proxy box (the camera can be
@@ -35,32 +39,164 @@ import * as THREE from "three";
35
39
  import { MeshStandardNodeMaterial } from "three/webgpu";
36
40
  import * as TSL from "three/tsl";
37
41
  import { VDBError } from "../core/errors.js";
42
+ import { vdbLevelSetSurfaceWgsl } from "../core/surface/level-set-wgsl.js";
38
43
  import { getDevice } from "./device.js";
39
44
  import { VDBGridNode } from "./grid-node.js";
40
- const { Fn, float, uniform, vec2, vec3, vec4, wgslFn, positionLocal, positionView, viewportUV } = TSL;
41
- const { cameraFar, cameraNear, cameraPosition, cameraProjectionMatrix, cameraProjectionMatrixInverse, cameraViewMatrix, cameraWorldMatrix, modelViewMatrix } = TSL;
45
+ const { Fn, code, float, select, uint, uniform, vec3, vec4, wgslFn, positionLocal, positionView, viewportUV } = TSL;
46
+ const { materialRoughness } = TSL;
47
+ const { cameraFar, cameraNear, cameraProjectionMatrix, cameraProjectionMatrixInverse, cameraViewMatrix, cameraWorldMatrix, modelViewMatrix } = TSL;
42
48
  const { viewZToOrthographicDepth, viewZToPerspectiveDepth } = TSL;
43
49
  /** Default compiled cap on the samples one fragment's trace takes (the live `maxSamples` clamps under it). */
44
50
  export const VDB_LEVEL_SET_SAMPLES_CAP = 1024;
45
- const TRACE = "vdbThreeLevelSetTrace";
46
- const NORMAL = "vdbThreeLevelSetNormal";
47
- function traceWgsl(cap) {
48
- return /* wgsl */ `fn ${TRACE}(grid_id: u32, origin_w: vec3f, dir_w: vec3f, t_range: vec2f, sdf_to_index: f32, max_samples: f32) -> vec4f {
49
- let o = vdbWorldToIndex(grid_id, origin_w);
50
- let d = vdbWorldToIndexDir(grid_id, dir_w);
51
- var span = t_range;
52
- let n = u32(clamp(max_samples, 1.0, ${cap}.0));
53
- if !vdbLevelSetHit(grid_id, o, d, &span, sdf_to_index, n) {
54
- return vec4f(0.0);
55
- }
56
- return vec4f(origin_w + dir_w * span.x, 1.0);
51
+ const SURFACE = "vdbThreeLevelSetSurface";
52
+ /**
53
+ * The material's entry: the core's pixel ray and surface (one call site: compile time, K3.7), packed into a
54
+ * `mat4x4f` for TSL (three's `wgslFn` takes built-in types only): column 0 `(world hit, hit ? 1 : 0)`,
55
+ * column 1 `(world normal, t)`, column 2 `(ndc depth, 0, 0, 0)`.
56
+ */
57
+ function surfaceWgsl(cap) {
58
+ return /* wgsl */ `fn ${SURFACE}(grid_id: u32, uv: vec2f, projection_inverse: mat4x4f, camera_world: mat4x4f, view_projection: mat4x4f, orthographic: u32, sdf_to_index: f32, max_samples: f32, normal_step: f32) -> mat4x4f {
59
+ let ray = vdbLevelSetViewRay(uv, projection_inverse, camera_world, orthographic != 0u);
60
+ let s = vdbLevelSetSurface(grid_id, ray, view_projection, sdf_to_index, u32(clamp(max_samples, 1.0, ${cap}.0)), normal_step);
61
+ return mat4x4f(vec4f(s.world, select(0.0, 1.0, s.hit)), vec4f(s.normal, s.t), vec4f(s.ndcDepth, 0.0, 0.0, 0.0), vec4f(0.0));
57
62
  }`;
58
63
  }
59
- const NORMAL_WGSL = /* wgsl */ `fn ${NORMAL}(grid_id: u32, p_w: vec3f, h: f32) -> vec3f {
60
- return vdbLevelSetNormal(grid_id, vdbWorldToIndex(grid_id, p_w), h);
61
- }`;
62
- function bad(message) {
63
- return new VDBError("VDB_BAD_OPTION", `VDBLevelSetMaterial: ${message}`);
64
+ /** One surface `CodeNode` per `VDBGridNode` (included after its module; deduplicated within a shader). */
65
+ const surfaceCode = new WeakMap();
66
+ function surfaceCodeOf(grids) {
67
+ let c = surfaceCode.get(grids);
68
+ if (!c)
69
+ surfaceCode.set(grids, (c = code(vdbLevelSetSurfaceWgsl(), [grids.module])));
70
+ return c;
71
+ }
72
+ function bad(owner, message) {
73
+ return new VDBError("VDB_BAD_OPTION", `${owner}: ${message}`);
74
+ }
75
+ /** Screen-space variance of the pixel filter (Tokuyoshi and Kaplanyan 2019: 0.25, a box of one pixel). */
76
+ export const VDB_SPECULAR_AA_VARIANCE = 0.25;
77
+ /** Clamp on the added GGX alpha squared (Tokuyoshi and Kaplanyan's kappa, 0.18): bounds the term at silhouettes. */
78
+ export const VDB_SPECULAR_AA_CLAMP = 0.18;
79
+ /**
80
+ * CPU twin of {@link vdbLevelSetSpecularAa}: the perceptual roughness after adding the kernel's variance to GGX
81
+ * alpha squared, from the normal's screen-space differences (`dndx`, `dndy`; view space, per pixel).
82
+ */
83
+ export function vdbLevelSetSpecularAaCpu(roughness, dndx, dndy) {
84
+ const dot = (v) => v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
85
+ const kernel = Math.min(2 * VDB_SPECULAR_AA_VARIANCE * (dot(dndx) + dot(dndy)), VDB_SPECULAR_AA_CLAMP);
86
+ const a2 = Math.min(1, Math.max(0, roughness ** 4 + kernel));
87
+ return Math.sqrt(Math.sqrt(a2));
88
+ }
89
+ /**
90
+ * G13c: `roughness` widened by the traced view normal's screen-space derivatives (see
91
+ * {@link VDBLevelSetTraceParameters.specularAntialiasing}). A miss pixel's normal is replaced by +z so its derivative
92
+ * stays finite; the clamp bounds what a silhouette quad adds.
93
+ */
94
+ export function vdbLevelSetSpecularAa(roughness, trace) {
95
+ const n = select(trace.hitMaskNode, trace.normalViewNode, vec3(0.0, 0.0, 1.0));
96
+ const dx = n.dFdx();
97
+ const dy = n.dFdy();
98
+ const kernel = dx.dot(dx).add(dy.dot(dy)).mul(2 * VDB_SPECULAR_AA_VARIANCE).min(VDB_SPECULAR_AA_CLAMP);
99
+ const r = float(roughness);
100
+ return r.mul(r).mul(r.mul(r)).add(kernel).clamp(0.0, 1.0).sqrt().sqrt();
101
+ }
102
+ /**
103
+ * Validates the inputs and builds the trace's uniforms and nodes (G13a: one setup for both level-set materials).
104
+ * `owner` names the class in error messages.
105
+ */
106
+ export function vdbLevelSetTrace(owner, renderer, grids, gridId, params) {
107
+ const { sdfToIndex, maxSamples, maxSamplesCap, normalStep, specularAntialiasing } = params;
108
+ if (!(grids instanceof VDBGridNode))
109
+ throw bad(owner, "grids must be a VDBGridNode.");
110
+ getDevice(renderer);
111
+ if (grids.renderer !== renderer) {
112
+ throw bad(owner, "grids was made for another renderer: create one VDBGridNode per renderer.");
113
+ }
114
+ if (grids.options.profile !== "march" || !grids.options.helpers?.includes("levelSet")) {
115
+ throw bad(owner, "grids needs profile 'march' and the 'levelSet' helper: new VDBGridNode(renderer, pool, { profile: 'march', helpers: ['levelSet'] }).");
116
+ }
117
+ if (typeof gridId === "number") {
118
+ if (!Number.isInteger(gridId) || gridId < 0)
119
+ throw bad(owner, `gridId must be a non-negative integer, got ${gridId}.`);
120
+ }
121
+ else if (gridId === null || typeof gridId !== "object") {
122
+ throw bad(owner, "gridId must be a number or a uint TSL node.");
123
+ }
124
+ const cap = maxSamplesCap ?? VDB_LEVEL_SET_SAMPLES_CAP;
125
+ if (!Number.isInteger(cap) || cap < 1)
126
+ throw bad(owner, `maxSamplesCap must be an integer >= 1, got ${JSON.stringify(maxSamplesCap)}.`);
127
+ let toIndex = sdfToIndex;
128
+ if (toIndex === undefined) {
129
+ const info = typeof gridId === "number" ? grids.pool.info?.(gridId) : undefined;
130
+ toIndex = info ? 1 / info.voxelSize[0] : 1;
131
+ }
132
+ if (!(Number.isFinite(toIndex) && toIndex > 0))
133
+ throw bad(owner, `sdfToIndex must be a positive finite number, got ${toIndex}.`);
134
+ const id = (typeof gridId === "number" ? uniform(gridId, "uint") : gridId);
135
+ const toIndexUniform = uniform(toIndex);
136
+ const samples = uniform(maxSamples ?? 512);
137
+ const step = uniform(normalStep ?? 1);
138
+ const traceSource = surfaceWgsl(cap);
139
+ const surfaceFn = wgslFn(traceSource, [surfaceCodeOf(grids)]);
140
+ // The pixel's view ray (core vdbLevelSetViewRay): from the fragment's viewport uv through the inverse
141
+ // projection, not from interpolated box positions, whose varying precision moves the ray by ~0.02 px on
142
+ // large faces (V57). Perspective from the camera; orthographic (directional-light shadow maps) from the
143
+ // pixel on the camera plane along the view axis.
144
+ const surfaceNode = Fn((builder) => surfaceFn({
145
+ grid_id: id,
146
+ uv: viewportUV,
147
+ projection_inverse: cameraProjectionMatrixInverse,
148
+ camera_world: cameraWorldMatrix,
149
+ view_projection: cameraProjectionMatrix.mul(cameraViewMatrix),
150
+ orthographic: uint(builder.camera?.isOrthographicCamera === true ? 1 : 0),
151
+ sdf_to_index: toIndexUniform,
152
+ max_samples: samples,
153
+ normal_step: step,
154
+ }))
155
+ .once()()
156
+ .toVar("vdbThreeLevelSetSurfaceOut");
157
+ const traceNode = surfaceNode.element(0);
158
+ const hitWorldNode = traceNode.xyz;
159
+ const normalWorldNode = surfaceNode.element(1).xyz;
160
+ return {
161
+ grids,
162
+ gridId: id,
163
+ sdfToIndex: toIndexUniform,
164
+ maxSamples: samples,
165
+ normalStep: step,
166
+ traceSource,
167
+ surfaceNode,
168
+ traceNode,
169
+ hitWorldNode,
170
+ hitMaskNode: traceNode.w.greaterThan(0.5),
171
+ hitViewNode: cameraViewMatrix.mul(vec4(hitWorldNode, 1.0)).xyz.toVar("vdbLevelSetHitView"),
172
+ normalWorldNode,
173
+ normalViewNode: cameraViewMatrix.mul(vec4(normalWorldNode, 0.0)).xyz.normalize(),
174
+ specularAntialiasing: specularAntialiasing ?? false,
175
+ };
176
+ }
177
+ /**
178
+ * Puts a trace on a node material: the back-face proxy, the hit mask, the vertex node (so `positionView` is a
179
+ * variable the lighting moves to the hit), the hit's depth, the received-shadow position and the normal.
180
+ */
181
+ export function vdbApplyLevelSetTrace(material, trace) {
182
+ const m = material;
183
+ m.side = THREE.BackSide;
184
+ m.shadowSide = THREE.BackSide;
185
+ m.maskNode = trace.hitMaskNode;
186
+ // A vertexNode makes the fragment's positionView a variable, which setupLighting moves to the hit.
187
+ m.vertexNode = cameraProjectionMatrix.mul(modelViewMatrix.mul(vec4(positionLocal, 1.0)));
188
+ const hitView = trace.hitViewNode;
189
+ m.depthNode = Fn((builder) => builder.camera?.isOrthographicCamera
190
+ ? viewZToOrthographicDepth(hitView.z, cameraNear, cameraFar)
191
+ : viewZToPerspectiveDepth(hitView.z, cameraNear, cameraFar))();
192
+ m.receivedShadowPositionNode = trace.hitWorldNode;
193
+ m.normalNode = trace.normalViewNode;
194
+ if (trace.specularAntialiasing)
195
+ m.roughnessNode = vdbLevelSetSpecularAa(materialRoughness, trace);
196
+ }
197
+ /** Lights read `positionView`: the shading point is the hit, not the box face (both materials' `setupLighting`). */
198
+ export function vdbLevelSetLightingPosition(trace) {
199
+ positionView.assign(trace.hitViewNode).toStack();
64
200
  }
65
201
  /**
66
202
  * A level set's surface on a world-space proxy box (see the file comment).
@@ -72,36 +208,15 @@ function bad(message) {
72
208
  * {@link normalWorldNode}, {@link normalViewNode} (e.g. `material.colorNode =
73
209
  * material.normalViewNode.mul(0.5).add(0.5)` with no lights shows the normals).
74
210
  * - Perspective and orthographic cameras (directional-light shadow maps).
211
+ * - Temporal antialiasing and upscalers: three's `velocity` is the proxy box's. Put a
212
+ * `VDBLevelSetVelocityNode` in the pass's MRT instead: it writes the hit's velocity for this material.
75
213
  *
76
214
  * @typeParam TGridId What the constructor got as the id: a number (default)
77
215
  * makes {@link gridId} a `uint` uniform; a node is kept as given.
78
216
  */
79
217
  export class VDBLevelSetMaterial extends MeshStandardNodeMaterial {
218
+ /** Type flag: true. */
80
219
  isVDBLevelSetMaterial = true;
81
- /** The pool node this material reads. */
82
- grids;
83
- /** The grid id: a `uint` uniform for a numeric id (`material.gridId.value = otherId`), else the caller's node. */
84
- gridId;
85
- /** Index voxels per SDF unit (live). */
86
- sdfToIndex;
87
- /** Trilinear samples per trace (live, clamped to the compiled cap). */
88
- maxSamples;
89
- /** Central-difference step of the normal in index voxels (live). */
90
- normalStep;
91
- /** The trace entry's WGSL (for inspection and tests). */
92
- traceSource;
93
- /** `vec4(hit world position, 1)` on a hit, `vec4(0)` on a miss (fragment stage). */
94
- traceNode;
95
- /** The hit in world space. */
96
- hitWorldNode;
97
- /** The hit in view space. */
98
- hitViewNode;
99
- /** True where the ray hit the surface (the material's `maskNode`). */
100
- hitMaskNode;
101
- /** World-space unit normal at the hit. */
102
- normalWorldNode;
103
- /** View-space unit normal at the hit (the material's `normalNode`). */
104
- normalViewNode;
105
220
  /**
106
221
  * @param renderer The initialized `WebGPURenderer` the pool and `grids` live on.
107
222
  * @param grids A `VDBGridNode` on this renderer with profile `'march'` and helpers including `'levelSet'`.
@@ -111,93 +226,15 @@ export class VDBLevelSetMaterial extends MeshStandardNodeMaterial {
111
226
  * `VDB_UNSUPPORTED_DEVICE` for an uninitialized renderer.
112
227
  */
113
228
  constructor(renderer, grids, gridId, params = {}) {
114
- const { sdfToIndex, maxSamples, maxSamplesCap, normalStep, ...standard } = params;
229
+ const { sdfToIndex: _s, maxSamples: _m, maxSamplesCap: _c, normalStep: _n, specularAntialiasing: _a, ...standard } = params;
115
230
  super(standard);
116
231
  this.name = "VDBLevelSetMaterial";
117
- if (!(grids instanceof VDBGridNode))
118
- throw bad("grids must be a VDBGridNode.");
119
- getDevice(renderer);
120
- if (grids.renderer !== renderer) {
121
- throw bad("grids was made for another renderer: create one VDBGridNode per renderer.");
122
- }
123
- if (grids.options.profile !== "march" || !grids.options.helpers?.includes("levelSet")) {
124
- throw bad("grids needs profile 'march' and the 'levelSet' helper: new VDBGridNode(renderer, pool, { profile: 'march', helpers: ['levelSet'] }).");
125
- }
126
- if (typeof gridId === "number") {
127
- if (!Number.isInteger(gridId) || gridId < 0)
128
- throw bad(`gridId must be a non-negative integer, got ${gridId}.`);
129
- }
130
- else if (gridId === null || typeof gridId !== "object") {
131
- throw bad("gridId must be a number or a uint TSL node.");
132
- }
133
- const cap = maxSamplesCap ?? VDB_LEVEL_SET_SAMPLES_CAP;
134
- if (!Number.isInteger(cap) || cap < 1)
135
- throw bad(`maxSamplesCap must be an integer >= 1, got ${JSON.stringify(maxSamplesCap)}.`);
136
- let toIndex = sdfToIndex;
137
- if (toIndex === undefined) {
138
- const info = typeof gridId === "number" ? grids.pool.info?.(gridId) : undefined;
139
- toIndex = info ? 1 / info.voxelSize[0] : 1;
140
- }
141
- if (!(Number.isFinite(toIndex) && toIndex > 0))
142
- throw bad(`sdfToIndex must be a positive finite number, got ${toIndex}.`);
143
- this.grids = grids;
144
- this.gridId = (typeof gridId === "number" ? uniform(gridId, "uint") : gridId);
145
- this.sdfToIndex = uniform(toIndex);
146
- this.maxSamples = uniform(maxSamples ?? 512);
147
- this.normalStep = uniform(normalStep ?? 1);
148
- this.traceSource = traceWgsl(cap);
149
- const traceFn = wgslFn(this.traceSource, [grids.module]);
150
- const normalFn = wgslFn(NORMAL_WGSL, [grids.module]);
151
- // The pixel's view ray, from the fragment's viewport position through the inverse projection (not from
152
- // interpolated box positions, whose varying precision moves the ray by ~0.02 px on large faces). Perspective:
153
- // from the camera, t >= 0. Orthographic (directional-light shadow maps): from the pixel on the camera plane
154
- // along the view axis, t in [-1e5, 1e5].
155
- const ray = (builder) => {
156
- const uv = viewportUV;
157
- const ndc = vec2(uv.x.mul(2.0).sub(1.0), float(1.0).sub(uv.y.mul(2.0)));
158
- const clip = cameraProjectionMatrixInverse.mul(vec4(ndc, 0.5, 1.0));
159
- const view = clip.xyz.div(clip.w);
160
- const ortho = builder.camera?.isOrthographicCamera === true;
161
- const dirView = ortho ? vec3(0.0, 0.0, -1.0) : view.normalize();
162
- const origin = ortho ? cameraWorldMatrix.mul(vec4(view.x, view.y, 0.0, 1.0)).xyz : cameraPosition;
163
- const dir = cameraWorldMatrix.mul(vec4(dirView, 0.0)).xyz.normalize();
164
- return { origin, dir, range: ortho ? vec2(-1e5, 1e5) : vec2(0.0, 1e30) };
165
- };
166
- this.traceNode = Fn((builder) => {
167
- const r = ray(builder);
168
- return traceFn({
169
- grid_id: this.gridId,
170
- origin_w: r.origin,
171
- dir_w: r.dir,
172
- t_range: r.range,
173
- sdf_to_index: this.sdfToIndex,
174
- max_samples: this.maxSamples,
175
- });
176
- })
177
- .once()()
178
- .toVar("vdbLevelSetTrace");
179
- this.hitWorldNode = this.traceNode.xyz;
180
- this.hitMaskNode = this.traceNode.w.greaterThan(0.5);
181
- this.hitViewNode = cameraViewMatrix.mul(vec4(this.hitWorldNode, 1.0)).xyz.toVar("vdbLevelSetHitView");
182
- this.normalWorldNode = Fn(() => normalFn({ grid_id: this.gridId, p_w: this.hitWorldNode, h: this.normalStep }))
183
- .once()()
184
- .toVar("vdbLevelSetNormalWorld");
185
- this.normalViewNode = cameraViewMatrix.mul(vec4(this.normalWorldNode, 0.0)).xyz.normalize();
186
- this.side = THREE.BackSide;
187
- this.shadowSide = THREE.BackSide;
188
- this.maskNode = this.hitMaskNode;
189
- // A vertexNode makes the fragment's positionView a variable, which setupLighting moves to the hit.
190
- this.vertexNode = cameraProjectionMatrix.mul(modelViewMatrix.mul(vec4(positionLocal, 1.0)));
191
- const hitView = this.hitViewNode;
192
- this.depthNode = Fn((builder) => builder.camera?.isOrthographicCamera
193
- ? viewZToOrthographicDepth(hitView.z, cameraNear, cameraFar)
194
- : viewZToPerspectiveDepth(hitView.z, cameraNear, cameraFar))();
195
- this.receivedShadowPositionNode = this.hitWorldNode;
196
- this.normalNode = this.normalViewNode;
232
+ Object.assign(this, vdbLevelSetTrace("VDBLevelSetMaterial", renderer, grids, gridId, params));
233
+ vdbApplyLevelSetTrace(this, this);
197
234
  }
198
235
  /** Lights read `positionView`: the shading point is the hit, not the box face. */
199
236
  setupLighting(builder) {
200
- positionView.assign(this.hitViewNode).toStack();
237
+ vdbLevelSetLightingPosition(this);
201
238
  return super.setupLighting(builder);
202
239
  }
203
240
  }
@@ -1 +1 @@
1
- {"version":3,"file":"level-set-material.js","sourceRoot":"","sources":["../../src/three/level-set-material.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAEH,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,KAAK,GAAG,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAA6B,MAAM,aAAa,CAAC;AACnE,OAAO,EAAE,WAAW,EAAmB,MAAM,gBAAgB,CAAC;AAG9D,MAAM,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,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E,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC;QAE5F,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,QAAQ,CAAC;QAC3B,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC,QAAQ,CAAC;QACjC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,WAAW,CAAC;QACjC,mGAAmG;QACnG,IAAI,CAAC,UAAU,GAAG,sBAAsB,CAAC,GAAG,CAAC,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,aAAa,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;QAC5F,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC;QACjC,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC,CAAC,OAAwD,EAAE,EAAE,CAC/E,OAAO,CAAC,MAAM,EAAE,oBAAoB;YAClC,CAAC,CAAC,wBAAwB,CAAC,OAAO,CAAC,CAAC,EAAE,UAAU,EAAE,SAAS,CAAC;YAC5D,CAAC,CAAC,uBAAuB,CAAC,OAAO,CAAC,CAAC,EAAE,UAAU,EAAE,SAAS,CAAC,CAC9D,EAAE,CAAC;QACJ,IAAI,CAAC,0BAA0B,GAAG,IAAI,CAAC,YAAY,CAAC;QACpD,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,cAAc,CAAC;IACxC,CAAC;IAED,kFAAkF;IACzE,aAAa,CAAC,OAAiE;QACtF,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,OAAO,EAAE,CAAC;QAChD,OAAO,KAAK,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IACtC,CAAC;CACF","sourcesContent":["/**\n * `VDBLevelSetMaterial`: a NanoVDB level set's zero surface, traced on the GPU\n * on the sparse pool (K3.7, R1 §6 Step 7), as a three r187\n * `MeshStandardNodeMaterial` (lights, shadows, depth, fog: three's own).\n *\n * const grids = new VDBGridNode(renderer, pool, { profile: 'march', helpers: ['levelSet'] });\n * const material = new VDBLevelSetMaterial(renderer, grids, id, { color: 0xffffff, roughness: 0.5 });\n * scene.add(new Mesh(vdbProxyGeometry(pool.info(id), { pad: 1 }), material));\n * // per frame: pool.update(encoder) (or pool.flush()), then grids.update(), then render.\n *\n * Portions derived from mjurczyk/openvdb (MIT), commit 75e7345\n * (`src/three-openvdb/levelset/webgpu/LevelSetSurfaceMaterial.js`,\n * `LevelSetSurfaceCore.js`): the back-face proxy box (the camera can be\n * inside), perspective and orthographic rays (so shadow passes work), the mask,\n * depth, received-shadow position and the `setupLighting` override that moves\n * `positionView` to the hit. Copyright (c) mjurczyk/openvdb contributors, MIT\n * (see THIRD_PARTY_NOTICES.md). Changes: the trace is the core's `vdbLevelSetHit` on\n * the sparse tree (HDDA skip, full-resolution trilinear SDF) instead of a\n * sphere trace of a dense 8-bit texture; the ray comes from the pixel through\n * the inverse projection, not from interpolated box positions; the normal is\n * `vdbLevelSetNormal` (central differences at `normalStep` voxels through the\n * grid affine); the proxy is in world space and the grid is placed with\n * `pool.setTransform` (DIVERGES.md).\n *\n * The trace runs in the grid's index space: `o = worldToIndex(ray origin)`,\n * `d = worldToIndexDir(unit world direction)`, so `t` is in world units. The\n * ray is the pixel's (`viewportUV` through `cameraProjectionMatrixInverse`):\n * perspective from `cameraPosition` over `t >= 0`; orthographic from the pixel\n * on the camera plane along the view axis over `t` in `[-1e5, 1e5]`. The mesh\n * must have an identity transform (the pool record holds the grid's\n * placement); give it `vdbProxyGeometry(pool.info(id), { pad: 1 })`, which\n * covers the trilinear support the trace clips to.\n */\n\nimport * as THREE from \"three\";\nimport { MeshStandardNodeMaterial } from \"three/webgpu\";\nimport * as TSL from \"three/tsl\";\nimport { VDBError } from \"../core/errors.js\";\nimport { getDevice, type VDBThreeRendererLike } from \"./device.js\";\nimport { VDBGridNode, type VDBTslNode } from \"./grid-node.js\";\nimport type { VDBUintUniform } from \"./material.js\";\n\nconst { Fn, float, uniform, vec2, vec3, vec4, wgslFn, positionLocal, positionView, viewportUV } = TSL as unknown as Record<string, VDBTslNode>;\nconst { cameraFar, cameraNear, cameraPosition, cameraProjectionMatrix, cameraProjectionMatrixInverse, cameraViewMatrix, cameraWorldMatrix, modelViewMatrix } =\n TSL as unknown as Record<string, VDBTslNode>;\nconst { viewZToOrthographicDepth, viewZToPerspectiveDepth } = TSL as unknown as Record<string, VDBTslNode>;\n\n/** Default compiled cap on the samples one fragment's trace takes (the live `maxSamples` clamps under it). */\nexport const VDB_LEVEL_SET_SAMPLES_CAP = 1024;\n\n/** Parameters of {@link VDBLevelSetMaterial}: three's standard material parameters plus the trace's. */\nexport interface VDBLevelSetMaterialParameters extends THREE.MeshStandardMaterialParameters {\n /**\n * Index voxels per SDF unit. Default: `1 / voxelSize.x` from `grids.pool.info(id)` when the pool\n * has `info` and the id is a number, else 1. A level set stores distances in its own world units,\n * so this is 1 / its NanoVDB voxel size.\n */\n sdfToIndex?: number;\n /** Trilinear samples per fragment trace (live), clamped to `[1, maxSamplesCap]`. Default 512. */\n maxSamples?: number;\n /** Compiled cap on {@link maxSamples}. Default {@link VDB_LEVEL_SET_SAMPLES_CAP}. */\n maxSamplesCap?: number;\n /** Central-difference step of the normal, in index voxels (live). Default 1. */\n normalStep?: number;\n}\n\ntype ScalarUniform = ReturnType<typeof TSL.uniform> & { value: number };\n\nconst TRACE = \"vdbThreeLevelSetTrace\";\nconst NORMAL = \"vdbThreeLevelSetNormal\";\n\nfunction traceWgsl(cap: number): string {\n return /* wgsl */ `fn ${TRACE}(grid_id: u32, origin_w: vec3f, dir_w: vec3f, t_range: vec2f, sdf_to_index: f32, max_samples: f32) -> vec4f {\n let o = vdbWorldToIndex(grid_id, origin_w);\n let d = vdbWorldToIndexDir(grid_id, dir_w);\n var span = t_range;\n let n = u32(clamp(max_samples, 1.0, ${cap}.0));\n if !vdbLevelSetHit(grid_id, o, d, &span, sdf_to_index, n) {\n return vec4f(0.0);\n }\n return vec4f(origin_w + dir_w * span.x, 1.0);\n}`;\n}\n\nconst NORMAL_WGSL = /* wgsl */ `fn ${NORMAL}(grid_id: u32, p_w: vec3f, h: f32) -> vec3f {\n return vdbLevelSetNormal(grid_id, vdbWorldToIndex(grid_id, p_w), h);\n}`;\n\nfunction bad(message: string): VDBError {\n return new VDBError(\"VDB_BAD_OPTION\", `VDBLevelSetMaterial: ${message}`);\n}\n\n/**\n * A level set's surface on a world-space proxy box (see the file comment).\n * Shades as a `MeshStandardNodeMaterial`: `color`, `roughness`, `metalness`,\n * maps-free; lights and shadows read the hit point.\n *\n * - Exposes the trace as TSL nodes for custom shading or debugging:\n * {@link hitWorldNode}, {@link hitViewNode}, {@link hitMaskNode},\n * {@link normalWorldNode}, {@link normalViewNode} (e.g. `material.colorNode =\n * material.normalViewNode.mul(0.5).add(0.5)` with no lights shows the normals).\n * - Perspective and orthographic cameras (directional-light shadow maps).\n *\n * @typeParam TGridId What the constructor got as the id: a number (default)\n * makes {@link gridId} a `uint` uniform; a node is kept as given.\n */\nexport class VDBLevelSetMaterial<TGridId extends number | VDBTslNode = number> extends MeshStandardNodeMaterial {\n readonly isVDBLevelSetMaterial = true;\n /** The pool node this material reads. */\n readonly grids: VDBGridNode;\n /** The grid id: a `uint` uniform for a numeric id (`material.gridId.value = otherId`), else the caller's node. */\n readonly gridId: TGridId extends number ? VDBUintUniform : VDBTslNode;\n /** Index voxels per SDF unit (live). */\n readonly sdfToIndex: ScalarUniform;\n /** Trilinear samples per trace (live, clamped to the compiled cap). */\n readonly maxSamples: ScalarUniform;\n /** Central-difference step of the normal in index voxels (live). */\n readonly normalStep: ScalarUniform;\n /** The trace entry's WGSL (for inspection and tests). */\n readonly traceSource: string;\n /** `vec4(hit world position, 1)` on a hit, `vec4(0)` on a miss (fragment stage). */\n readonly traceNode: VDBTslNode;\n /** The hit in world space. */\n readonly hitWorldNode: VDBTslNode;\n /** The hit in view space. */\n readonly hitViewNode: VDBTslNode;\n /** True where the ray hit the surface (the material's `maskNode`). */\n readonly hitMaskNode: VDBTslNode;\n /** World-space unit normal at the hit. */\n readonly normalWorldNode: VDBTslNode;\n /** View-space unit normal at the hit (the material's `normalNode`). */\n readonly normalViewNode: VDBTslNode;\n\n /**\n * @param renderer The initialized `WebGPURenderer` the pool and `grids` live on.\n * @param grids A `VDBGridNode` on this renderer with profile `'march'` and helpers including `'levelSet'`.\n * @param gridId The level set's pool id (a number, made a `uint` uniform) or a `uint` node.\n * @param params three's `MeshStandardMaterialParameters` plus the trace's ({@link VDBLevelSetMaterialParameters}).\n * @throws VDBError `VDB_BAD_OPTION` for a `grids` this material can't use, a bad id or cap;\n * `VDB_UNSUPPORTED_DEVICE` for an uninitialized renderer.\n */\n constructor(renderer: VDBThreeRendererLike, grids: VDBGridNode, gridId: TGridId, params: VDBLevelSetMaterialParameters = {}) {\n const { sdfToIndex, maxSamples, maxSamplesCap, normalStep, ...standard } = params;\n super(standard);\n this.name = \"VDBLevelSetMaterial\";\n if (!(grids instanceof VDBGridNode)) throw bad(\"grids must be a VDBGridNode.\");\n getDevice(renderer);\n if (grids.renderer !== renderer) {\n throw bad(\"grids was made for another renderer: create one VDBGridNode per renderer.\");\n }\n if (grids.options.profile !== \"march\" || !grids.options.helpers?.includes(\"levelSet\")) {\n throw bad(\"grids needs profile 'march' and the 'levelSet' helper: new VDBGridNode(renderer, pool, { profile: 'march', helpers: ['levelSet'] }).\");\n }\n if (typeof gridId === \"number\") {\n if (!Number.isInteger(gridId) || gridId < 0) throw bad(`gridId must be a non-negative integer, got ${gridId}.`);\n } else if (gridId === null || typeof gridId !== \"object\") {\n throw bad(\"gridId must be a number or a uint TSL node.\");\n }\n const cap = maxSamplesCap ?? VDB_LEVEL_SET_SAMPLES_CAP;\n if (!Number.isInteger(cap) || cap < 1) throw bad(`maxSamplesCap must be an integer >= 1, got ${JSON.stringify(maxSamplesCap)}.`);\n let toIndex = sdfToIndex;\n if (toIndex === undefined) {\n const info = typeof gridId === \"number\" ? (grids.pool as { info?: (id: number) => { voxelSize: readonly number[] } }).info?.(gridId) : undefined;\n toIndex = info ? 1 / info.voxelSize[0]! : 1;\n }\n if (!(Number.isFinite(toIndex) && toIndex > 0)) throw bad(`sdfToIndex must be a positive finite number, got ${toIndex}.`);\n\n this.grids = grids;\n this.gridId = (typeof gridId === \"number\" ? uniform(gridId, \"uint\") : gridId) as this[\"gridId\"];\n this.sdfToIndex = uniform(toIndex) as ScalarUniform;\n this.maxSamples = uniform(maxSamples ?? 512) as ScalarUniform;\n this.normalStep = uniform(normalStep ?? 1) as ScalarUniform;\n this.traceSource = traceWgsl(cap);\n\n const traceFn = wgslFn(this.traceSource, [grids.module] as never);\n const normalFn = wgslFn(NORMAL_WGSL, [grids.module] as never);\n\n // The pixel's view ray, from the fragment's viewport position through the inverse projection (not from\n // interpolated box positions, whose varying precision moves the ray by ~0.02 px on large faces). Perspective:\n // from the camera, t >= 0. Orthographic (directional-light shadow maps): from the pixel on the camera plane\n // along the view axis, t in [-1e5, 1e5].\n const ray = (builder: { camera?: { isOrthographicCamera?: boolean } }) => {\n const uv = viewportUV;\n const ndc = vec2(uv.x.mul(2.0).sub(1.0), float(1.0).sub(uv.y.mul(2.0)));\n const clip = cameraProjectionMatrixInverse.mul(vec4(ndc, 0.5, 1.0));\n const view = clip.xyz.div(clip.w);\n const ortho = builder.camera?.isOrthographicCamera === true;\n const dirView = ortho ? vec3(0.0, 0.0, -1.0) : view.normalize();\n const origin = ortho ? cameraWorldMatrix.mul(vec4(view.x, view.y, 0.0, 1.0)).xyz : cameraPosition;\n const dir = cameraWorldMatrix.mul(vec4(dirView, 0.0)).xyz.normalize();\n return { origin, dir, range: ortho ? vec2(-1e5, 1e5) : vec2(0.0, 1e30) };\n };\n this.traceNode = Fn((builder: { camera?: { isOrthographicCamera?: boolean } }) => {\n const r = ray(builder);\n return traceFn({\n grid_id: this.gridId,\n origin_w: r.origin,\n dir_w: r.dir,\n t_range: r.range,\n sdf_to_index: this.sdfToIndex,\n max_samples: this.maxSamples,\n });\n })\n .once()()\n .toVar(\"vdbLevelSetTrace\");\n this.hitWorldNode = this.traceNode.xyz;\n this.hitMaskNode = this.traceNode.w.greaterThan(0.5);\n this.hitViewNode = cameraViewMatrix.mul(vec4(this.hitWorldNode, 1.0)).xyz.toVar(\"vdbLevelSetHitView\");\n this.normalWorldNode = Fn(() => normalFn({ grid_id: this.gridId, p_w: this.hitWorldNode, h: this.normalStep }))\n .once()()\n .toVar(\"vdbLevelSetNormalWorld\");\n this.normalViewNode = cameraViewMatrix.mul(vec4(this.normalWorldNode, 0.0)).xyz.normalize();\n\n this.side = THREE.BackSide;\n this.shadowSide = THREE.BackSide;\n this.maskNode = this.hitMaskNode;\n // A vertexNode makes the fragment's positionView a variable, which setupLighting moves to the hit.\n this.vertexNode = cameraProjectionMatrix.mul(modelViewMatrix.mul(vec4(positionLocal, 1.0)));\n const hitView = this.hitViewNode;\n this.depthNode = Fn((builder: { camera?: { isOrthographicCamera?: boolean } }) =>\n builder.camera?.isOrthographicCamera\n ? viewZToOrthographicDepth(hitView.z, cameraNear, cameraFar)\n : viewZToPerspectiveDepth(hitView.z, cameraNear, cameraFar),\n )();\n this.receivedShadowPositionNode = this.hitWorldNode;\n this.normalNode = this.normalViewNode;\n }\n\n /** Lights read `positionView`: the shading point is the hit, not the box face. */\n override setupLighting(builder: Parameters<MeshStandardNodeMaterial[\"setupLighting\"]>[0]): ReturnType<MeshStandardNodeMaterial[\"setupLighting\"]> {\n positionView.assign(this.hitViewNode).toStack();\n return super.setupLighting(builder);\n }\n}\n"]}
1
+ {"version":3,"file":"level-set-material.js","sourceRoot":"","sources":["../../src/three/level-set-material.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAEH,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,KAAK,GAAG,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO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as a three r187\n * `MeshStandardNodeMaterial` (lights, shadows, depth, fog: three's own).\n *\n * const grids = new VDBGridNode(renderer, pool, { profile: 'march', helpers: ['levelSet'] });\n * const material = new VDBLevelSetMaterial(renderer, grids, id, { color: 0xffffff, roughness: 0.5 });\n * scene.add(new Mesh(vdbProxyGeometry(pool.info(id), { pad: 1 }), material));\n * // per frame: pool.update(encoder) (or pool.flush()), then grids.update(), then render.\n *\n * The trace, normal and pixel ray are the core's `vdbLevelSetSurface` / `vdbLevelSetViewRay` WGSL\n * (`core/surface/level-set-wgsl.ts`, K6.6), the same functions the raw pass `vdbCreateLevelSetPass`\n * calls; this material adds three's lighting, shadows and depth conventions around them.\n *\n * Portions derived from mjurczyk/openvdb (MIT), commit 75e7345\n * (`src/three-openvdb/levelset/webgpu/LevelSetSurfaceMaterial.js`,\n * `LevelSetSurfaceCore.js`): the back-face proxy box (the camera can be\n * inside), perspective and orthographic rays (so shadow passes work), the mask,\n * depth, received-shadow position and the `setupLighting` override that moves\n * `positionView` to the hit. Copyright (c) mjurczyk/openvdb contributors, MIT\n * (see THIRD_PARTY_NOTICES.md). Changes: the trace is the core's `vdbLevelSetHit` on\n * the sparse tree (HDDA skip, full-resolution trilinear SDF) instead of a\n * sphere trace of a dense 8-bit texture; the ray comes from the pixel through\n * the inverse projection, not from interpolated box positions; the normal is\n * `vdbLevelSetNormal` (central differences at `normalStep` voxels through the\n * grid affine); the proxy is in world space and the grid is placed with\n * `pool.setTransform` (DIVERGES.md).\n *\n * The trace runs in the grid's index space: `o = worldToIndex(ray origin)`,\n * `d = worldToIndexDir(unit world direction)`, so `t` is in world units. The\n * ray is the pixel's (`viewportUV` through `cameraProjectionMatrixInverse`):\n * perspective from `cameraPosition` over `t >= 0`; orthographic from the pixel\n * on the camera plane along the view axis over `t` in `[-1e5, 1e5]`. The mesh\n * must have an identity transform (the pool record holds the grid's\n * placement); give it `vdbProxyGeometry(pool.info(id), { pad: 1 })`, which\n * covers the trilinear support the trace clips to.\n */\n\nimport * as THREE from \"three\";\nimport { MeshStandardNodeMaterial } from \"three/webgpu\";\nimport * as TSL from \"three/tsl\";\nimport { VDBError } from \"../core/errors.js\";\nimport { vdbLevelSetSurfaceWgsl } from \"../core/surface/level-set-wgsl.js\";\nimport { getDevice, type VDBThreeRendererLike } from \"./device.js\";\nimport { VDBGridNode, type VDBTslNode } from \"./grid-node.js\";\nimport type { VDBUintUniform } from \"./material.js\";\n\nconst { Fn, code, float, select, uint, uniform, vec3, vec4, wgslFn, positionLocal, positionView, viewportUV } = TSL as unknown as Record<string, VDBTslNode>;\nconst { materialRoughness } = TSL as unknown as Record<string, VDBTslNode>;\nconst { cameraFar, cameraNear, cameraProjectionMatrix, cameraProjectionMatrixInverse, cameraViewMatrix, cameraWorldMatrix, modelViewMatrix } =\n TSL as unknown as Record<string, VDBTslNode>;\nconst { viewZToOrthographicDepth, viewZToPerspectiveDepth } = TSL as unknown as Record<string, VDBTslNode>;\n\n/** Default compiled cap on the samples one fragment's trace takes (the live `maxSamples` clamps under it). */\nexport const VDB_LEVEL_SET_SAMPLES_CAP = 1024;\n\n/** Parameters of {@link VDBLevelSetMaterial}: three's standard material parameters plus the trace's. */\nexport interface VDBLevelSetMaterialParameters extends THREE.MeshStandardMaterialParameters, VDBLevelSetTraceParameters {}\n\ntype ScalarUniform = ReturnType<typeof TSL.uniform> & { value: number };\n\nconst SURFACE = \"vdbThreeLevelSetSurface\";\n\n/**\n * The material's entry: the core's pixel ray and surface (one call site: compile time, K3.7), packed into a\n * `mat4x4f` for TSL (three's `wgslFn` takes built-in types only): column 0 `(world hit, hit ? 1 : 0)`,\n * column 1 `(world normal, t)`, column 2 `(ndc depth, 0, 0, 0)`.\n */\nfunction surfaceWgsl(cap: number): string {\n return /* wgsl */ `fn ${SURFACE}(grid_id: u32, uv: vec2f, projection_inverse: mat4x4f, camera_world: mat4x4f, view_projection: mat4x4f, orthographic: u32, sdf_to_index: f32, max_samples: f32, normal_step: f32) -> mat4x4f {\n let ray = vdbLevelSetViewRay(uv, projection_inverse, camera_world, orthographic != 0u);\n let s = vdbLevelSetSurface(grid_id, ray, view_projection, sdf_to_index, u32(clamp(max_samples, 1.0, ${cap}.0)), normal_step);\n return mat4x4f(vec4f(s.world, select(0.0, 1.0, s.hit)), vec4f(s.normal, s.t), vec4f(s.ndcDepth, 0.0, 0.0, 0.0), vec4f(0.0));\n}`;\n}\n\n/** One surface `CodeNode` per `VDBGridNode` (included after its module; deduplicated within a shader). */\nconst surfaceCode = new WeakMap<VDBGridNode, VDBTslNode>();\nfunction surfaceCodeOf(grids: VDBGridNode): VDBTslNode {\n let c = surfaceCode.get(grids);\n if (!c) surfaceCode.set(grids, (c = code(vdbLevelSetSurfaceWgsl(), [grids.module])));\n return c;\n}\n\nfunction bad(owner: string, message: string): VDBError {\n return new VDBError(\"VDB_BAD_OPTION\", `${owner}: ${message}`);\n}\n\n/** The trace a level-set material shades: its uniforms and TSL nodes (shared by the standard and physical materials). */\nexport interface VDBLevelSetTrace<TGridId extends number | VDBTslNode = number> {\n readonly grids: VDBGridNode;\n readonly gridId: TGridId extends number ? VDBUintUniform : VDBTslNode;\n readonly sdfToIndex: ScalarUniform;\n readonly maxSamples: ScalarUniform;\n readonly normalStep: ScalarUniform;\n readonly traceSource: string;\n readonly surfaceNode: VDBTslNode;\n readonly traceNode: VDBTslNode;\n readonly hitWorldNode: VDBTslNode;\n readonly hitViewNode: VDBTslNode;\n readonly hitMaskNode: VDBTslNode;\n readonly normalWorldNode: VDBTslNode;\n readonly normalViewNode: VDBTslNode;\n /** G13c: whether the roughness is widened by the traced normal's screen-space variation (compile time). */\n readonly specularAntialiasing: boolean;\n}\n\n/** The trace parameters of {@link VDBLevelSetMaterialParameters} (also taken by `VDBLevelSetPhysicalMaterial`). */\nexport interface VDBLevelSetTraceParameters {\n /**\n * Index voxels per SDF unit. Default: `1 / voxelSize.x` from `grids.pool.info(id)` when the pool\n * has `info` and the id is a number, else 1. A level set stores distances in its own world units,\n * so this is 1 / its NanoVDB voxel size.\n */\n sdfToIndex?: number;\n /** Trilinear samples per fragment trace (live), clamped to `[1, maxSamplesCap]`. Default 512. */\n maxSamples?: number;\n /** Compiled cap on {@link maxSamples}. Default {@link VDB_LEVEL_SET_SAMPLES_CAP}. */\n maxSamplesCap?: number;\n /**\n * Central-difference step of the normal, in index voxels (live). Default 1. `0` (G13c-a): the exact gradient of the\n * trilinear cell the trace hits (the trace's own surface: it never faces away from the ray; discontinuous across cell\n * faces).\n */\n normalStep?: number;\n /**\n * G13c: geometric specular antialiasing on the traced normal (Tokuyoshi and Kaplanyan 2019): the roughness grows\n * with the normal's change across the pixel, so a low-roughness highlight on a sharp crease is spread over the pixel\n * instead of hitting a few pixel centres with extreme values (on the teapot lid at clearcoat roughness 0.04: linear\n * RMS error against a 4x4 supersampled frame 0.157 vs 0.541, largest per-pixel flicker 1.7 vs 143). three's own term\n * (`getGeometryRoughness`) reads the proxy box's flat normal and adds nothing here. Sets `roughnessNode` (and\n * `clearcoatRoughnessNode` on the physical material); compile time. Default false: in a tone-mapped image lit by\n * an environment it measured worse (G13c-a: ACES RMS on the lid 0.0143 vs 0.0085 in demo 13's studio lighting).\n */\n specularAntialiasing?: boolean;\n}\n\n/** Screen-space variance of the pixel filter (Tokuyoshi and Kaplanyan 2019: 0.25, a box of one pixel). */\nexport const VDB_SPECULAR_AA_VARIANCE = 0.25;\n/** Clamp on the added GGX alpha squared (Tokuyoshi and Kaplanyan's kappa, 0.18): bounds the term at silhouettes. */\nexport const VDB_SPECULAR_AA_CLAMP = 0.18;\n\n/**\n * CPU twin of {@link vdbLevelSetSpecularAa}: the perceptual roughness after adding the kernel's variance to GGX\n * alpha squared, from the normal's screen-space differences (`dndx`, `dndy`; view space, per pixel).\n */\nexport function vdbLevelSetSpecularAaCpu(roughness: number, dndx: readonly number[], dndy: readonly number[]): number {\n const dot = (v: readonly number[]) => v[0]! * v[0]! + v[1]! * v[1]! + v[2]! * v[2]!;\n const kernel = Math.min(2 * VDB_SPECULAR_AA_VARIANCE * (dot(dndx) + dot(dndy)), VDB_SPECULAR_AA_CLAMP);\n const a2 = Math.min(1, Math.max(0, roughness ** 4 + kernel));\n return Math.sqrt(Math.sqrt(a2));\n}\n\n/**\n * G13c: `roughness` widened by the traced view normal's screen-space derivatives (see\n * {@link VDBLevelSetTraceParameters.specularAntialiasing}). A miss pixel's normal is replaced by +z so its derivative\n * stays finite; the clamp bounds what a silhouette quad adds.\n */\nexport function vdbLevelSetSpecularAa(roughness: VDBTslNode, trace: Pick<VDBLevelSetTrace<number | VDBTslNode>, \"normalViewNode\" | \"hitMaskNode\">): VDBTslNode {\n const n = select(trace.hitMaskNode, trace.normalViewNode, vec3(0.0, 0.0, 1.0));\n const dx = n.dFdx();\n const dy = n.dFdy();\n const kernel = dx.dot(dx).add(dy.dot(dy)).mul(2 * VDB_SPECULAR_AA_VARIANCE).min(VDB_SPECULAR_AA_CLAMP);\n const r = float(roughness);\n return r.mul(r).mul(r.mul(r)).add(kernel).clamp(0.0, 1.0).sqrt().sqrt();\n}\n\n/**\n * Validates the inputs and builds the trace's uniforms and nodes (G13a: one setup for both level-set materials).\n * `owner` names the class in error messages.\n */\nexport function vdbLevelSetTrace<TGridId extends number | VDBTslNode>(\n owner: string,\n renderer: VDBThreeRendererLike,\n grids: VDBGridNode,\n gridId: TGridId,\n params: VDBLevelSetTraceParameters,\n): VDBLevelSetTrace<TGridId> {\n const { sdfToIndex, maxSamples, maxSamplesCap, normalStep, specularAntialiasing } = params;\n if (!(grids instanceof VDBGridNode)) throw bad(owner, \"grids must be a VDBGridNode.\");\n getDevice(renderer);\n if (grids.renderer !== renderer) {\n throw bad(owner, \"grids was made for another renderer: create one VDBGridNode per renderer.\");\n }\n if (grids.options.profile !== \"march\" || !grids.options.helpers?.includes(\"levelSet\")) {\n throw bad(owner, \"grids needs profile 'march' and the 'levelSet' helper: new VDBGridNode(renderer, pool, { profile: 'march', helpers: ['levelSet'] }).\");\n }\n if (typeof gridId === \"number\") {\n if (!Number.isInteger(gridId) || gridId < 0) throw bad(owner, `gridId must be a non-negative integer, got ${gridId}.`);\n } else if (gridId === null || typeof gridId !== \"object\") {\n throw bad(owner, \"gridId must be a number or a uint TSL node.\");\n }\n const cap = maxSamplesCap ?? VDB_LEVEL_SET_SAMPLES_CAP;\n if (!Number.isInteger(cap) || cap < 1) throw bad(owner, `maxSamplesCap must be an integer >= 1, got ${JSON.stringify(maxSamplesCap)}.`);\n let toIndex = sdfToIndex;\n if (toIndex === undefined) {\n const info = typeof gridId === \"number\" ? (grids.pool as { info?: (id: number) => { voxelSize: readonly number[] } }).info?.(gridId) : undefined;\n toIndex = info ? 1 / info.voxelSize[0]! : 1;\n }\n if (!(Number.isFinite(toIndex) && toIndex > 0)) throw bad(owner, `sdfToIndex must be a positive finite number, got ${toIndex}.`);\n\n const id = (typeof gridId === \"number\" ? uniform(gridId, \"uint\") : gridId) as VDBLevelSetTrace<TGridId>[\"gridId\"];\n const toIndexUniform = uniform(toIndex) as ScalarUniform;\n const samples = uniform(maxSamples ?? 512) as ScalarUniform;\n const step = uniform(normalStep ?? 1) as ScalarUniform;\n const traceSource = surfaceWgsl(cap);\n const surfaceFn = wgslFn(traceSource, [surfaceCodeOf(grids)] as never);\n\n // The pixel's view ray (core vdbLevelSetViewRay): from the fragment's viewport uv through the inverse\n // projection, not from interpolated box positions, whose varying precision moves the ray by ~0.02 px on\n // large faces (V57). Perspective from the camera; orthographic (directional-light shadow maps) from the\n // pixel on the camera plane along the view axis.\n const surfaceNode = Fn((builder: { camera?: { isOrthographicCamera?: boolean } }) =>\n surfaceFn({\n grid_id: id,\n uv: viewportUV,\n projection_inverse: cameraProjectionMatrixInverse,\n camera_world: cameraWorldMatrix,\n view_projection: cameraProjectionMatrix.mul(cameraViewMatrix),\n orthographic: uint(builder.camera?.isOrthographicCamera === true ? 1 : 0),\n sdf_to_index: toIndexUniform,\n max_samples: samples,\n normal_step: step,\n }),\n )\n .once()()\n .toVar(\"vdbThreeLevelSetSurfaceOut\");\n const traceNode = surfaceNode.element(0);\n const hitWorldNode = traceNode.xyz;\n const normalWorldNode = surfaceNode.element(1).xyz;\n return {\n grids,\n gridId: id,\n sdfToIndex: toIndexUniform,\n maxSamples: samples,\n normalStep: step,\n traceSource,\n surfaceNode,\n traceNode,\n hitWorldNode,\n hitMaskNode: traceNode.w.greaterThan(0.5),\n hitViewNode: cameraViewMatrix.mul(vec4(hitWorldNode, 1.0)).xyz.toVar(\"vdbLevelSetHitView\"),\n normalWorldNode,\n normalViewNode: cameraViewMatrix.mul(vec4(normalWorldNode, 0.0)).xyz.normalize(),\n specularAntialiasing: specularAntialiasing ?? false,\n };\n}\n\n/**\n * Puts a trace on a node material: the back-face proxy, the hit mask, the vertex node (so `positionView` is a\n * variable the lighting moves to the hit), the hit's depth, the received-shadow position and the normal.\n */\nexport function vdbApplyLevelSetTrace(material: THREE.Material, trace: VDBLevelSetTrace<number | VDBTslNode>): void {\n const m = material as THREE.Material & Record<string, unknown>;\n m.side = THREE.BackSide;\n m.shadowSide = THREE.BackSide;\n m.maskNode = trace.hitMaskNode;\n // A vertexNode makes the fragment's positionView a variable, which setupLighting moves to the hit.\n m.vertexNode = cameraProjectionMatrix.mul(modelViewMatrix.mul(vec4(positionLocal, 1.0)));\n const hitView = trace.hitViewNode;\n m.depthNode = Fn((builder: { camera?: { isOrthographicCamera?: boolean } }) =>\n builder.camera?.isOrthographicCamera\n ? viewZToOrthographicDepth(hitView.z, cameraNear, cameraFar)\n : viewZToPerspectiveDepth(hitView.z, cameraNear, cameraFar),\n )();\n m.receivedShadowPositionNode = trace.hitWorldNode;\n m.normalNode = trace.normalViewNode;\n if (trace.specularAntialiasing) m.roughnessNode = vdbLevelSetSpecularAa(materialRoughness, trace);\n}\n\n/** Lights read `positionView`: the shading point is the hit, not the box face (both materials' `setupLighting`). */\nexport function vdbLevelSetLightingPosition(trace: VDBLevelSetTrace<number | VDBTslNode>): void {\n positionView.assign(trace.hitViewNode).toStack();\n}\n\n/**\n * A level set's surface on a world-space proxy box (see the file comment).\n * Shades as a `MeshStandardNodeMaterial`: `color`, `roughness`, `metalness`,\n * maps-free; lights and shadows read the hit point.\n *\n * - Exposes the trace as TSL nodes for custom shading or debugging:\n * {@link hitWorldNode}, {@link hitViewNode}, {@link hitMaskNode},\n * {@link normalWorldNode}, {@link normalViewNode} (e.g. `material.colorNode =\n * material.normalViewNode.mul(0.5).add(0.5)` with no lights shows the normals).\n * - Perspective and orthographic cameras (directional-light shadow maps).\n * - Temporal antialiasing and upscalers: three's `velocity` is the proxy box's. Put a\n * `VDBLevelSetVelocityNode` in the pass's MRT instead: it writes the hit's velocity for this material.\n *\n * @typeParam TGridId What the constructor got as the id: a number (default)\n * makes {@link gridId} a `uint` uniform; a node is kept as given.\n */\nexport class VDBLevelSetMaterial<TGridId extends number | VDBTslNode = number> extends MeshStandardNodeMaterial {\n /** Type flag: true. */\n readonly isVDBLevelSetMaterial = true;\n /** The pool node this material reads. */\n declare readonly grids: VDBGridNode;\n /** The grid id: a `uint` uniform for a numeric id (`material.gridId.value = otherId`), else the caller's node. */\n declare readonly gridId: TGridId extends number ? VDBUintUniform : VDBTslNode;\n /** Index voxels per SDF unit (live). */\n declare readonly sdfToIndex: ScalarUniform;\n /** Trilinear samples per trace (live, clamped to the compiled cap). */\n declare readonly maxSamples: ScalarUniform;\n /** Central-difference step of the normal in index voxels (live); 0: the trilinear cell's exact gradient. */\n declare readonly normalStep: ScalarUniform;\n /** The entry's WGSL (for inspection and tests): calls the core's `vdbLevelSetViewRay` and `vdbLevelSetSurface`. */\n declare readonly traceSource: string;\n /** The packed surface (`mat4`, fragment stage): see {@link traceNode}, {@link normalWorldNode}; column 2 `.x` is the NDC depth. */\n declare readonly surfaceNode: VDBTslNode;\n /** `vec4(hit world position, 1)` on a hit, `vec4(0)` on a miss (fragment stage). */\n declare readonly traceNode: VDBTslNode;\n /** The hit in world space (what `VDBLevelSetVelocityNode` reprojects for the MRT `velocity` output). */\n declare readonly hitWorldNode: VDBTslNode;\n /** The hit in view space. */\n declare readonly hitViewNode: VDBTslNode;\n /** True where the ray hit the surface (the material's `maskNode`). */\n declare readonly hitMaskNode: VDBTslNode;\n /** World-space unit normal at the hit. */\n declare readonly normalWorldNode: VDBTslNode;\n /** View-space unit normal at the hit (the material's `normalNode`). */\n declare readonly normalViewNode: VDBTslNode;\n /** Whether `roughnessNode` carries the geometric specular antialiasing (G13c; compile time). */\n declare readonly specularAntialiasing: boolean;\n\n /**\n * @param renderer The initialized `WebGPURenderer` the pool and `grids` live on.\n * @param grids A `VDBGridNode` on this renderer with profile `'march'` and helpers including `'levelSet'`.\n * @param gridId The level set's pool id (a number, made a `uint` uniform) or a `uint` node.\n * @param params three's `MeshStandardMaterialParameters` plus the trace's ({@link VDBLevelSetMaterialParameters}).\n * @throws VDBError `VDB_BAD_OPTION` for a `grids` this material can't use, a bad id or cap;\n * `VDB_UNSUPPORTED_DEVICE` for an uninitialized renderer.\n */\n constructor(renderer: VDBThreeRendererLike, grids: VDBGridNode, gridId: TGridId, params: VDBLevelSetMaterialParameters = {}) {\n const { sdfToIndex: _s, maxSamples: _m, maxSamplesCap: _c, normalStep: _n, specularAntialiasing: _a, ...standard } = params;\n super(standard);\n this.name = \"VDBLevelSetMaterial\";\n Object.assign(this, vdbLevelSetTrace(\"VDBLevelSetMaterial\", renderer, grids, gridId, params));\n vdbApplyLevelSetTrace(this, this);\n }\n\n /** Lights read `positionView`: the shading point is the hit, not the box face. */\n override setupLighting(builder: Parameters<MeshStandardNodeMaterial[\"setupLighting\"]>[0]): ReturnType<MeshStandardNodeMaterial[\"setupLighting\"]> {\n vdbLevelSetLightingPosition(this);\n return super.setupLighting(builder);\n }\n}\n"]}