@threenative/core 0.3.10 → 0.3.12

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 (158) hide show
  1. package/capabilities.json +1096 -26
  2. package/dist/camera-shake-CZKOfGn4.d.ts +46 -0
  3. package/dist/{canvas-layer-vCtanIeB.d.ts → canvas-layer-CWuFRT7G.d.ts} +1 -1
  4. package/dist/chunk-2Z44XSI2.js +35 -0
  5. package/dist/chunk-5O73CDMR.js +1849 -0
  6. package/dist/chunk-6KLNISRJ.js +153 -0
  7. package/dist/chunk-7UUT3TY5.js +133 -0
  8. package/dist/chunk-CFGTPFUH.js +231 -0
  9. package/dist/chunk-DBELTTTM.js +615 -0
  10. package/dist/chunk-GMHATRPW.js +7701 -0
  11. package/dist/chunk-GXY7B3JG.js +46 -0
  12. package/dist/chunk-KIRUKTBQ.js +187 -0
  13. package/dist/chunk-N5LRP4BR.js +220 -0
  14. package/dist/chunk-OKXUCASC.js +2238 -0
  15. package/dist/chunk-QWJEMUUT.js +94 -0
  16. package/dist/chunk-VAZM36TV.js +497 -0
  17. package/dist/{game-DzMcitDO.d.ts → game-D7pAzZUY.d.ts} +15 -3
  18. package/dist/hot.d.ts +3 -3
  19. package/dist/hot.js +2 -46
  20. package/dist/index.d.ts +329 -302
  21. package/dist/index.js +10565 -22533
  22. package/dist/{model-lod-CTpZ6imz.d.ts → model-lod-B6BMqS1r.d.ts} +33 -3
  23. package/dist/playtest.d.ts +3 -3
  24. package/dist/playtest.js +24 -75
  25. package/dist/react-css.js +1 -215
  26. package/dist/react.d.ts +2 -2
  27. package/dist/{renderer-rZm0hVqq.d.ts → renderer-DvFVoAgj.d.ts} +13 -4
  28. package/dist/spells/index.d.ts +643 -0
  29. package/dist/spells/index.js +1558 -0
  30. package/dist/spells/ultra/index.d.ts +1600 -0
  31. package/dist/spells/ultra/index.js +6804 -0
  32. package/dist/terrain-jobs-worker.js +1 -123
  33. package/dist/two-sided-foliage-W523J3UM.js +1 -0
  34. package/dist/ui-layer.js +5 -384
  35. package/dist/worker.js +4 -41
  36. package/dist/world.d.ts +725 -259
  37. package/dist/world.js +16714 -18284
  38. package/mcp/animation-server.mjs +1 -1
  39. package/mcp/animation.mjs +2 -3
  40. package/mcp/engine-server.mjs +142 -47
  41. package/mcp/servers.mjs +9 -2
  42. package/mcp/terrain.mjs +8 -0
  43. package/package.json +14 -6
  44. package/patches/three@0.187.0-prd269-upgrade.patch +2102 -0
  45. package/patches/three@0.187.0.patch +2004 -407
  46. package/src/assets.ts +9 -2
  47. package/src/card-lod.ts +152 -0
  48. package/src/compressed-texture-preparation.ts +1008 -0
  49. package/src/frame-budget.ts +31 -20
  50. package/src/game-dev-tools.ts +93 -0
  51. package/src/game-frame-loop.ts +995 -0
  52. package/src/game-renderer-metrics.ts +77 -0
  53. package/src/game-startup.ts +1001 -0
  54. package/src/game-validate.ts +70 -0
  55. package/src/game.ts +253 -1935
  56. package/src/index.ts +27 -2
  57. package/src/launch-diagnostics.ts +22 -12
  58. package/src/look/ao-admission.ts +24 -0
  59. package/src/look/filmic-tone-mapping.ts +15 -13
  60. package/src/look/look-diagnostics.ts +3 -3
  61. package/src/look/quality.ts +10 -5
  62. package/src/look/world-environment.ts +76 -9
  63. package/src/loop.ts +29 -0
  64. package/src/model-lod.ts +42 -3
  65. package/src/particle-kinds.ts +515 -0
  66. package/src/picking.ts +17 -3
  67. package/src/playtest.ts +27 -1
  68. package/src/projection-apply.ts +4 -1
  69. package/src/projection-plan.ts +9 -6
  70. package/src/render/mesh-pool.ts +1 -1
  71. package/src/render/resolution-subrect.ts +2 -0
  72. package/src/render/rough-diffuse.ts +46 -12
  73. package/src/render/soft-shadow.ts +27 -12
  74. package/src/render/virtual-shadow-adaptive.ts +55 -0
  75. package/src/render/virtual-shadow-casters.ts +175 -0
  76. package/src/render/virtual-shadow-layers.ts +76 -0
  77. package/src/render/virtual-shadow-level.ts +224 -0
  78. package/src/render/virtual-shadow-markers.ts +193 -0
  79. package/src/render/virtual-shadow-options.ts +370 -0
  80. package/src/render/virtual-shadow-pages.ts +5 -0
  81. package/src/render/virtual-shadow.ts +213 -992
  82. package/src/render/world-impostor-surface.ts +293 -34
  83. package/src/render-camera-cull.ts +32 -1
  84. package/src/render-pass-budget.ts +34 -7
  85. package/src/renderProjection.ts +22 -0
  86. package/src/renderer.ts +92 -37
  87. package/src/snow-field.ts +1 -1
  88. package/src/spells/cast-pool.ts +361 -0
  89. package/src/spells/index.ts +177 -0
  90. package/src/spells/mechanisms.ts +295 -0
  91. package/src/spells/reaction-field.ts +415 -0
  92. package/src/spells/spell-lights.ts +139 -0
  93. package/src/spells/spell-world.ts +187 -0
  94. package/src/spells/spell.ts +397 -0
  95. package/src/spells/spells.ts +283 -0
  96. package/src/spells/ultra/engine/camera.ts +184 -0
  97. package/src/spells/ultra/engine/quality.ts +96 -0
  98. package/src/spells/ultra/index.ts +52 -0
  99. package/src/spells/ultra/main.ts +305 -0
  100. package/src/spells/ultra/scene/arenaMaterial.ts +325 -0
  101. package/src/spells/ultra/spells/cast.ts +233 -0
  102. package/src/spells/ultra/spells/fire.ts +1267 -0
  103. package/src/spells/ultra/vfx/FluidVolume.ts +816 -0
  104. package/src/spells/ultra/vfx/FluidVolume.tsl.ts +855 -0
  105. package/src/spells/ultra/vfx/FxTarget.ts +61 -0
  106. package/src/spells/ultra/vfx/GroundField.ts +1399 -0
  107. package/src/spells/ultra/vfx/Particles.ts +1475 -0
  108. package/src/spells/ultra/vfx/Ribbons.ts +645 -0
  109. package/src/spells/ultra/vfx/Shockwaves.ts +286 -0
  110. package/src/spells/ultra/vfx/Volumes.ts +489 -0
  111. package/src/spells/ultra/vfx/common.ts +1195 -0
  112. package/src/spells/ultra/vfx/noise.ts +441 -0
  113. package/src/startup-readiness.ts +128 -34
  114. package/src/version.ts +1 -1
  115. package/src/warmup.ts +56 -8
  116. package/src/water-surface.ts +9 -3
  117. package/src/world-bundle-safety.ts +401 -0
  118. package/src/world-cell-batches.ts +1216 -0
  119. package/src/world-cell-chunks.ts +1132 -0
  120. package/src/world-cell-data.ts +995 -0
  121. package/src/world-cell-impostors.ts +1449 -0
  122. package/src/world-cell-lod-bias.ts +151 -0
  123. package/src/world-cell-models.ts +1074 -0
  124. package/src/world-cell-options.ts +189 -0
  125. package/src/world-cell-placements.ts +694 -0
  126. package/src/world-cell-prewarm.ts +557 -0
  127. package/src/world-cell-readiness.ts +357 -0
  128. package/src/world-cell-render-bundles.ts +459 -0
  129. package/src/world-cell-render-gpu-dress.ts +714 -0
  130. package/src/world-cell-render-main-cull.ts +484 -0
  131. package/src/world-cell-render-shadow-claims.ts +198 -0
  132. package/src/world-cell-render-util.ts +246 -0
  133. package/src/world-cell-render.ts +1493 -0
  134. package/src/world-cell-residency.ts +745 -0
  135. package/src/world-cells.ts +1318 -10418
  136. package/src/world-chunk-shadow.ts +308 -0
  137. package/src/world-collar.ts +315 -0
  138. package/src/world-gpu-flags.ts +163 -0
  139. package/src/world-gpu-readback.ts +881 -0
  140. package/src/world-gpu-reference.ts +510 -0
  141. package/src/world-gpu-scene.ts +214 -2264
  142. package/src/world-gpu-shadow.ts +190 -0
  143. package/src/world-gpu-types.ts +381 -0
  144. package/src/world-gpu-validation.ts +518 -0
  145. package/src/world-heightfield.ts +802 -0
  146. package/src/world-load-limiter.ts +106 -0
  147. package/src/world-noise.ts +241 -0
  148. package/src/world-package.ts +42 -0
  149. package/src/world-region.ts +1 -1
  150. package/src/world-terrain-splat.ts +365 -149
  151. package/src/world-tile-budget.ts +256 -0
  152. package/src/world-tile-geometry.ts +469 -0
  153. package/src/world-tile-lod.ts +408 -0
  154. package/src/world-tile-observation.ts +566 -0
  155. package/src/world-tile-stitch.ts +166 -0
  156. package/src/world-tiles.ts +170 -1745
  157. package/src/world-water-bodies.ts +359 -0
  158. package/src/world.ts +35 -741
@@ -0,0 +1,855 @@
1
+ import { Vector3 } from "three";
2
+ // Ported from Ultra Elements src/vfx/FluidVolume.glsl.js @698b720 (MIT, Copyright (c) 2026 Humpalumps). JavaScript copied verbatim; GLSL transliterated to TSL.
3
+ //
4
+ // Shaders for FluidVolume.ts (see its header for the design). Every sim pass is one fullscreen
5
+ // draw over a 2D atlas holding a 3D grid; all lookups are explicit LOD 0 (implicit-gradient
6
+ // samples inside data-dependent loops make ANGLE's FXC unroll them).
7
+ //
8
+ // TSL: the shader revisions FluidVolume.js patches over these base sources at load (REACT's turbulence,
9
+ // soot cooling and erosion; the whole RENDER_BODY) are written in place here, each marked "patch:".
10
+ // Grid is a plain object of nodes (a struct as a Fn argument fails on the engine), GLSL `out`
11
+ // parameters are var nodes the caller passes in, and each GLSL `uniform sampler2D` is one texture()
12
+ // node per pass whose `.value` the JS swaps (upstream: `u.uV.value = ...`).
13
+ // ADVECT, CORRECT, REACT and PROJECT write (oV, oS): each builder takes `out` ("V" or "S") and builds
14
+ // the part of the same maths that reaches that output; the pass hands both to the material's mrtNode.
15
+ import {
16
+ Break,
17
+ Discard,
18
+ Fn,
19
+ If,
20
+ Loop,
21
+ PI,
22
+ abs,
23
+ cameraPosition,
24
+ clamp,
25
+ cross,
26
+ dot,
27
+ exp,
28
+ float,
29
+ floor,
30
+ int,
31
+ ivec2,
32
+ length,
33
+ max,
34
+ min,
35
+ mix,
36
+ mod,
37
+ normalize,
38
+ positionWorld,
39
+ pow,
40
+ screenCoordinate,
41
+ select,
42
+ smoothstep,
43
+ type texture,
44
+ textureLoad,
45
+ uniform,
46
+ vec2,
47
+ vec3,
48
+ vec4,
49
+ } from "three/tsl";
50
+ import type { Node } from "three/webgpu";
51
+ import {
52
+ U,
53
+ blackbody,
54
+ curl3,
55
+ forceAccel,
56
+ fxOut,
57
+ hgPhase,
58
+ noise3,
59
+ noise3v,
60
+ sceneViewZ,
61
+ } from "./common.js";
62
+
63
+ type F = Node<"float">;
64
+ type V2 = Node<"vec2">;
65
+ type V3 = Node<"vec3">;
66
+ type V4 = Node<"vec4">;
67
+ type I = Node<"int">;
68
+ type Iv3 = V3; // a whole-number voxel index (see gridTexel)
69
+ type B = Node<"bool">;
70
+ export type TexNode = ReturnType<typeof texture>;
71
+ type Out = "V" | "S";
72
+
73
+ export const MAX_SRC = 8;
74
+
75
+ // engine: GLSL read the camera's forward axis from viewMatrix; FluidVolume.update sets it per frame
76
+ export const uCamFwd = uniform(new Vector3(0, 0, -1));
77
+
78
+ // 3D grid in a 2D atlas: slice k is tile (k % tiles.x, k / tiles.x), each tile dim.xy texels.
79
+ // Voxel centres sit at i + 0.5 in voxel units.
80
+ export interface IGrid {
81
+ dim: V3;
82
+ tiles: V2;
83
+ inv: V2;
84
+ }
85
+ // ENGINE-GAP: no ivec3 on the Wasm engine. A voxel index is a vec3 of whole numbers (exact in f32 at
86
+ // these sizes) and only the atlas texel becomes an ivec2; the tile maths is upstream's.
87
+ function gridTexel(g: IGrid, v0: Iv3) {
88
+ const v = clamp(v0, vec3(0.0), g.dim.sub(1.0)).toVar();
89
+ const tx = g.tiles.x;
90
+ const row = floor(v.z.add(0.5).div(tx)).toVar(); // v.z / tx
91
+ return ivec2(vec2(v.z.sub(row.mul(tx)).mul(g.dim.x).add(v.x), row.mul(g.dim.y).add(v.y))); // v.z % tx
92
+ }
93
+ const gridFetch = (t: TexNode, g: IGrid, v: Iv3): V4 =>
94
+ textureLoad(t as Parameters<typeof textureLoad>[0], gridTexel(g, v), int(0)) as unknown as V4;
95
+ function sliceOrigin(g: IGrid, k: F): V2 {
96
+ const row = floor(k.add(0.5).div(g.tiles.x));
97
+ return vec2(k.sub(row.mul(g.tiles.x)), row).mul(g.dim.xy);
98
+ }
99
+ // trilinear: hardware bilinear inside two slices, lerp between them (clamped so tiles never bleed)
100
+ function gridSample(t: TexNode, g: IGrid, p: V3): V4 {
101
+ const q = clamp(p, vec3(0.5), g.dim.sub(0.5)).toVar();
102
+ const z = q.z.sub(0.5).toVar();
103
+ const k0 = floor(z).toVar();
104
+ const f = z.sub(k0);
105
+ const k1 = min(k0.add(1.0), g.dim.z.sub(1.0));
106
+ const a = t.sample(sliceOrigin(g, k0).add(q.xy).mul(g.inv)).level(float(0)) as unknown as V4;
107
+ const b = t.sample(sliceOrigin(g, k1).add(q.xy).mul(g.inv)).level(float(0)) as unknown as V4;
108
+ return mix(a, b, f);
109
+ }
110
+ // out v; returns false past the last slice
111
+ function gridVoxel(g: IGrid, v: Iv3): B {
112
+ const p = floor(screenCoordinate.xy).toVar();
113
+ const d = g.dim.xy;
114
+ const tile = floor(p.div(d)).toVar();
115
+ v.assign(vec3(p.sub(tile.mul(d)), tile.y.mul(g.tiles.x).add(tile.x)));
116
+ return v.z.lessThan(g.dim.z);
117
+ }
118
+
119
+ // transfer function shared by the light pass and the raymarch
120
+ // temperature th: 0 ambient, 1 = uLook1.x kelvin (the spell's flame temperature)
121
+ export interface ILook {
122
+ uLook0: V4; // emission gain, soot extinction (1/m per unit), flame extinction (1/m), absorb (black flame)
123
+ uLook1: V4; // kelvin at th = 1, ambient kelvin, fuel extinction (1/m per unit), chroma (render only)
124
+ uTint: V3;
125
+ }
126
+ const kelvinOf = (L: ILook, th: F): F => L.uLook1.y.add(L.uLook1.x.sub(L.uLook1.y).mul(th));
127
+ const flameOf = (th: F): F => smoothstep(0.12, 0.5, th);
128
+ // Kirchhoff: hot soot and flame gas emit as much as they absorb, emission = sigma_abs * B(T). An
129
+ // opaque hot region therefore shows its own surface temperature (a blackbody), cooled soot is
130
+ // black smoke, and a thin flame sheet is a faint, see-through glow. B(T) in the visible band
131
+ // follows Planck's ~exp(-hc / (lambda k T)) at lambda ~ 600 nm (softened 2x for the display range):
132
+ // far steeper than T^4, so 1000 K soot is a dull red ember and 2400 K is ~4x the reference.
133
+ function glowOf(L: ILook, th: F): F {
134
+ const T = kelvinOf(L, th);
135
+ const k = exp(
136
+ float(12000.0)
137
+ .div(L.uLook1.x)
138
+ .sub(float(12000.0).div(max(T, 400.0))),
139
+ ).toVar();
140
+ return k.div(k.mul(0.08).add(1.0)).mul(smoothstep(0.08, 0.25, th));
141
+ }
142
+ const sigmaHot = (L: ILook, s: V4): F =>
143
+ L.uLook0.y.mul(max(s.b, 0.0)).add(L.uLook0.z.mul(flameOf(s.r)));
144
+ const sigmaOf = (L: ILook, s: V4): F =>
145
+ sigmaHot(L, s)
146
+ .mul(float(1.0).add(L.uLook0.w.mul(6.0).mul(flameOf(s.r))))
147
+ .add(L.uLook1.z.mul(max(s.g, 0.0)));
148
+ function emitOf(L: ILook, s: V4): V3 {
149
+ const th = max(s.r, 0.0).toVar();
150
+ const g = glowOf(L, th)
151
+ .mul(sigmaHot(L, s))
152
+ .mul(L.uLook0.x)
153
+ .mul(float(1.0).sub(L.uLook0.w))
154
+ .toVar();
155
+ return select(g.greaterThan(1e-4), blackbody(kelvinOf(L, th)).mul(g).mul(L.uTint), vec3(0.0));
156
+ }
157
+
158
+ // SIM_HEAD
159
+ export interface ISim {
160
+ uDim: V3;
161
+ uTiles: V2;
162
+ uInv: V2;
163
+ uOrigin: V3; // world position of the grid's min corner
164
+ uH: F; // voxel size (m)
165
+ uSimDt: F;
166
+ uShift: V3; // how far the domain moved this step (voxels): new origin - old origin
167
+ uObst: V4; // a solid sphere (an Aegis dome): centre in voxels minus half a voxel, radius in voxels (0: none)
168
+ }
169
+ const G = (su: ISim): IGrid => ({ dim: su.uDim, tiles: su.uTiles, inv: su.uInv });
170
+ // cells below the arena floor, and inside the sphere, are solid
171
+ function solid(su: ISim, v: Iv3): B {
172
+ const o = vec3(v).sub(su.uObst.xyz);
173
+ return v.y
174
+ .add(0.5)
175
+ .lessThan(su.uOrigin.y.negate().div(su.uH))
176
+ .or(su.uObst.w.greaterThan(0.0).and(dot(o, o).lessThan(su.uObst.w.mul(su.uObst.w)))); // (uniform: no sphere, no cost)
177
+ }
178
+ const outside = (su: ISim, v: Iv3): B =>
179
+ v.x
180
+ .lessThan(0)
181
+ .or(v.y.lessThan(0))
182
+ .or(v.z.lessThan(0))
183
+ .or(v.x.greaterThanEqual(su.uDim.x))
184
+ .or(v.y.greaterThanEqual(su.uDim.y))
185
+ .or(v.z.greaterThanEqual(su.uDim.z));
186
+ // open boundary: still air outside the box (one-voxel fade)
187
+ function sampleOpen(su: ISim, t: TexNode, p: V3): V4 {
188
+ const o = max(max(p.negate(), p.sub(su.uDim)), 0.0);
189
+ const w = clamp(float(1.0).sub(max(o.x, max(o.y, o.z))), 0.0, 1.0).toVar();
190
+ return select(w.greaterThan(0.0), gridSample(t, G(su), p).mul(w), vec4(0.0));
191
+ }
192
+
193
+ // 1. semi-Lagrangian advection of velocity and scalars (RK2 backtrace), in the moving frame
194
+ export function ADVECT(su: ISim, { uV, uS }: { uV: TexNode; uS: TexNode }, out: Out) {
195
+ return Fn(() => {
196
+ const o = vec4(0.0).toVar();
197
+ const v = vec3(0).toVar();
198
+ If(gridVoxel(G(su), v), () => {
199
+ const p = vec3(v).add(0.5).add(su.uShift).toVar(); // this cell's world point, in the previous grid
200
+ const k = su.uSimDt.div(su.uH).toVar();
201
+ const u1 = sampleOpen(su, uV, p).xyz.toVar();
202
+ const u2 = sampleOpen(su, uV, p.sub(u1.mul(k.mul(0.5)))).xyz.toVar();
203
+ const q = p.sub(u2.mul(k)).toVar();
204
+ o.assign(sampleOpen(su, out === "V" ? uV : uS, q));
205
+ });
206
+ return o;
207
+ })();
208
+ }
209
+
210
+ // 2. MacCormack correction (Selle et al. 2008), clamped to the old field's extrema around the
211
+ // departure point, so it never overshoots
212
+ export function CORRECT(
213
+ su: ISim,
214
+ own: { uV: TexNode; uS: TexNode; uVf: TexNode; uSf: TexNode; uMacCormack: F },
215
+ out: Out,
216
+ ) {
217
+ const X = out === "V" ? own.uV : own.uS;
218
+ const Xf = out === "V" ? own.uVf : own.uSf;
219
+ return Fn(() => {
220
+ const o = vec4(0.0).toVar();
221
+ const v = vec3(0).toVar();
222
+ If(gridVoxel(G(su), v), () => {
223
+ const vf = gridFetch(own.uVf, G(su), v).toVar();
224
+ const xf = out === "V" ? vf : gridFetch(Xf, G(su), v).toVar();
225
+ If(own.uMacCormack.lessThan(0.5), () => {
226
+ o.assign(xf);
227
+ }).Else(() => {
228
+ const k = su.uSimDt.div(su.uH).toVar();
229
+ const c = vec3(v).add(0.5).toVar();
230
+ const r = c.add(vf.xyz.mul(k));
231
+ const xb = gridSample(Xf, G(su), r);
232
+ const x0 = sampleOpen(su, X, c.add(su.uShift));
233
+ const xn = xf.add(x0.sub(xb).mul(0.5)).toVar();
234
+ const i0 = floor(c.add(su.uShift).sub(vf.xyz.mul(k)).sub(0.5)).toVar();
235
+ const lo = vec4(1e4).toVar();
236
+ const hi = vec4(-1e4).toVar();
237
+ for (let n = 0; n < 8; n++) {
238
+ const on = i0.add(vec3(n & 1, (n >> 1) & 1, n >> 2)).toVar();
239
+ const a = vec4(0.0).toVar();
240
+ If(outside(su, on).not(), () => {
241
+ a.assign(gridFetch(X, G(su), on));
242
+ });
243
+ lo.assign(min(lo, a));
244
+ hi.assign(max(hi, a));
245
+ }
246
+ o.assign(clamp(xn, lo, hi));
247
+ });
248
+ });
249
+ return o;
250
+ })();
251
+ }
252
+
253
+ // 3. vorticity w = curl u (rgb) and |w| (a)
254
+ export function CURL(su: ISim, { uV }: { uV: TexNode }) {
255
+ return Fn(() => {
256
+ const oC = vec4(0.0).toVar();
257
+ const v = vec3(0).toVar();
258
+ If(gridVoxel(G(su), v), () => {
259
+ const at = (o: Iv3) => gridFetch(uV, G(su), o).xyz.toVar();
260
+ const L = at(v.sub(vec3(1, 0, 0)));
261
+ const R = at(v.add(vec3(1, 0, 0)));
262
+ const D = at(v.sub(vec3(0, 1, 0)));
263
+ const Up = at(v.add(vec3(0, 1, 0)));
264
+ const Bk = at(v.sub(vec3(0, 0, 1)));
265
+ const Fr = at(v.add(vec3(0, 0, 1)));
266
+ const w = vec3(
267
+ Up.z.sub(D.z).sub(Fr.y.sub(Bk.y)),
268
+ Fr.x.sub(Bk.x).sub(R.z.sub(L.z)),
269
+ R.y.sub(L.y).sub(Up.x.sub(D.x)),
270
+ )
271
+ .mul(float(0.5).div(su.uH))
272
+ .toVar();
273
+ oC.assign(vec4(w, length(w)));
274
+ });
275
+ return oC;
276
+ })();
277
+ }
278
+
279
+ export interface IReactOwn {
280
+ uV: TexNode;
281
+ uS: TexNode;
282
+ uC: TexNode;
283
+ uSrcP: V4[]; // world centre, radius m
284
+ uSrcA: V4[]; // fuel, temperature, soot, noise amount 0..1
285
+ uSrcV: V4[]; // velocity m/s, radial speed m/s
286
+ uSrcB: V4[]; // mode (0 per second, 1 at least), noise frequency 1/m, seed, velocity coupling 1/s
287
+ uSrcN: I;
288
+ uBurn: V4; // ignition temperature, burn rate 1/s, heat per fuel, soot per fuel
289
+ uDecay: V4; // cooling 1/s, radiative cooling, soot decay 1/s, fuel decay 1/s
290
+ uForce: V4; // buoyancy m/s2 per th, soot weight m/s2 per unit, vorticity confinement, expansion per fuel
291
+ uFlow: V4; // external force coupling, damping 1/s, -, -
292
+ uTurb: V4; // patch: turbulence m/s2, turbulence frequency tiles/m, soot erosion units/s, soot radiative cooling
293
+ }
294
+
295
+ // 4. sources, combustion, cooling, buoyancy, vorticity confinement, external forces
296
+ export function REACT(su: ISim, u: IReactOwn, out: Out) {
297
+ const { uV, uS, uC, uBurn, uDecay, uForce, uFlow, uTurb } = u;
298
+ return Fn(() => {
299
+ const o = vec4(0.0).toVar();
300
+ const v = vec3(0).toVar();
301
+ If(gridVoxel(G(su), v).and(solid(su, v).not()), () => {
302
+ const vel = gridFetch(uV, G(su), v).xyz.toVar();
303
+ const s = gridFetch(uS, G(su), v).rgb.toVar(); // (s.a is rewritten below)
304
+ const pw = su.uOrigin.add(vec3(v).add(0.5).mul(su.uH)).toVar();
305
+ const dt = su.uSimDt;
306
+ for (let i = 0; i < MAX_SRC; i++) {
307
+ If(u.uSrcN.greaterThan(i), () => {
308
+ const P = u.uSrcP[i] as V4;
309
+ const d = pw.sub(P.xyz).toVar();
310
+ const r = length(d).toVar();
311
+ If(r.lessThan(P.w), () => {
312
+ const A = u.uSrcA[i] as V4;
313
+ const Bs = u.uSrcB[i] as V4;
314
+ const W = u.uSrcV[i] as V4;
315
+ const w = float(1.0)
316
+ .sub(smoothstep(P.w.mul(0.3), P.w, r))
317
+ .toVar();
318
+ If(A.w.greaterThan(0.0), () => {
319
+ // lumpy, flickering source: the noise scrolls up through it
320
+ const np = pw
321
+ .mul(Bs.y)
322
+ .add(vec3(Bs.z, Bs.z.mul(0.37).sub(U.uTime.mul(0.8).mul(Bs.y)), Bs.z.mul(0.71)));
323
+ w.mulAssign(mix(1.0, smoothstep(0.3, 0.7, noise3(np.mul(0.25))), A.w));
324
+ });
325
+ If(w.greaterThan(0.0), () => {
326
+ if (out === "V") {
327
+ const target = W.xyz.add(select(r.greaterThan(1e-4), d.div(r), vec3(0.0)).mul(W.w));
328
+ vel.assign(mix(vel, target, w.mul(float(1.0).sub(exp(Bs.w.negate().mul(dt))))));
329
+ }
330
+ If(Bs.x.lessThan(0.5), () => {
331
+ s.addAssign(vec3(A.y, A.x, A.z).mul(w.mul(dt)));
332
+ }).Else(() => {
333
+ s.assign(max(s, vec3(A.y, A.x, A.z).mul(w)));
334
+ });
335
+ });
336
+ });
337
+ });
338
+ }
339
+ // combustion: hot fuel burns into heat and soot, and the gas expands (a divergence source)
340
+ const th = max(s.x, 0.0).toVar();
341
+ const fuel = max(s.y, 0.0).toVar();
342
+ const soot = max(s.z, 0.0).toVar();
343
+ const burn = fuel
344
+ .mul(float(1.0).sub(exp(uBurn.y.negate().mul(dt))))
345
+ .mul(smoothstep(uBurn.x.mul(0.5), uBurn.x, th))
346
+ .toVar();
347
+ fuel.subAssign(burn);
348
+ th.addAssign(burn.mul(uBurn.z));
349
+ soot.addAssign(burn.mul(uBurn.w));
350
+ const expand = burn.div(max(dt, 1e-4)).mul(uForce.w).toVar();
351
+ // Newton cooling, plus radiative loss (exact step of dth/dt = -c th^4)
352
+ th.mulAssign(exp(uDecay.x.negate().mul(dt)));
353
+ // patch: soot radiates its heat away (dark lanes)
354
+ th.divAssign(
355
+ pow(
356
+ float(1.0).add(
357
+ uDecay.y
358
+ .mul(3.0)
359
+ .mul(dt)
360
+ .mul(th)
361
+ .mul(th)
362
+ .mul(th)
363
+ .mul(float(1.0).add(uTurb.w.mul(soot))),
364
+ ),
365
+ 1.0 / 3.0,
366
+ ),
367
+ );
368
+ // patch: soot thins by erosion instead of fading as a whole
369
+ soot.assign(max(soot.mul(exp(uDecay.z.negate().mul(dt))).sub(uTurb.z.mul(dt)), 0.0));
370
+ fuel.mulAssign(exp(uDecay.w.negate().mul(dt)));
371
+ if (out === "S") {
372
+ o.assign(vec4(min(th, 4.0), min(fuel, 4.0), min(soot, 8.0), expand));
373
+ return;
374
+ }
375
+ // hot gas rises, soot is heavy
376
+ const acc = vec3(0.0, uForce.x.mul(th).sub(uForce.y.mul(soot)), 0.0).toVar();
377
+ // vorticity confinement (Fedkiw et al. 2001): spin the small eddies back up
378
+ const magAt = (o2: Iv3) => gridFetch(uC, G(su), o2).a;
379
+ const c = gridFetch(uC, G(su), v).toVar();
380
+ const eta = vec3(
381
+ magAt(v.add(vec3(1, 0, 0))).sub(magAt(v.sub(vec3(1, 0, 0)))),
382
+ magAt(v.add(vec3(0, 1, 0))).sub(magAt(v.sub(vec3(0, 1, 0)))),
383
+ magAt(v.add(vec3(0, 0, 1))).sub(magAt(v.sub(vec3(0, 0, 1)))),
384
+ ).toVar();
385
+ const el = length(eta).toVar();
386
+ If(el.greaterThan(1e-4), () => {
387
+ acc.addAssign(cross(eta.div(el), c.xyz).mul(uForce.z.mul(su.uH)));
388
+ });
389
+ // patch: resolved turbulence (a curl-noise force on hot gas)
390
+ If(uTurb.x.greaterThan(0.0), () => {
391
+ acc.addAssign(
392
+ curl3(pw.mul(uTurb.y).add(vec3(0.0, U.uTime.mul(-0.6).mul(uTurb.y), 0.0))).mul(
393
+ uTurb.x.mul(min(th, 1.0)),
394
+ ),
395
+ );
396
+ });
397
+ // the shared force field (wind, vortices, shocks...) pushes the gas like it pushes particles
398
+ const kill = float(0.0).toVar();
399
+ acc.addAssign(forceAccel(pw, vel, kill).mul(uFlow.x));
400
+ vel.assign(vel.add(acc.mul(dt)).mul(exp(uFlow.y.negate().mul(dt))));
401
+ o.assign(vec4(clamp(vel, -60.0, 60.0), 0.0));
402
+ });
403
+ return o;
404
+ })();
405
+ }
406
+
407
+ // 5. divergence minus the combustion expansion (b = h^2 (div u - e)), plus a warm-start pressure
408
+ export function DIVERGE(
409
+ su: ISim,
410
+ { uV, uS, uP, uWarm }: { uV: TexNode; uS: TexNode; uP: TexNode; uWarm: F },
411
+ ) {
412
+ return Fn(() => {
413
+ const oP = vec4(0.0).toVar();
414
+ const v = vec3(0).toVar();
415
+ If(gridVoxel(G(su), v).and(solid(su, v).not()), () => {
416
+ const at = (o: Iv3) => select(solid(su, o), vec3(0.0), gridFetch(uV, G(su), o).xyz);
417
+ const div = at(v.add(vec3(1, 0, 0)))
418
+ .x.sub(at(v.sub(vec3(1, 0, 0))).x)
419
+ .add(at(v.add(vec3(0, 1, 0))).y)
420
+ .sub(at(v.sub(vec3(0, 1, 0))).y)
421
+ .add(at(v.add(vec3(0, 0, 1))).z)
422
+ .sub(at(v.sub(vec3(0, 0, 1))).z)
423
+ .mul(float(0.5).div(su.uH))
424
+ .toVar();
425
+ const e = gridFetch(uS, G(su), v).a;
426
+ const p0 = sampleOpen(su, uP, vec3(v).add(0.5).add(su.uShift)).r.mul(uWarm);
427
+ oP.assign(vec4(p0, su.uH.mul(su.uH).mul(div.sub(e)), 0.0, 0.0));
428
+ });
429
+ return oP;
430
+ })();
431
+ }
432
+
433
+ // pressure neighbours: open air (p = 0) outside the box, Neumann at the floor
434
+ const pN = (su: ISim, uP: TexNode, n: Iv3, pc: F): F =>
435
+ select(solid(su, n), pc, select(outside(su, n), float(0.0), gridFetch(uP, G(su), n).r));
436
+
437
+ // 6. Jacobi iteration
438
+ export function JACOBI(su: ISim, { uP }: { uP: TexNode }) {
439
+ return Fn(() => {
440
+ const oP = vec4(0.0).toVar();
441
+ const v = vec3(0).toVar();
442
+ If(gridVoxel(G(su), v), () => {
443
+ const c = gridFetch(uP, G(su), v).toVar();
444
+ If(solid(su, v), () => {
445
+ oP.assign(vec4(0.0, c.g, 0.0, 0.0));
446
+ }).Else(() => {
447
+ const s = pN(su, uP, v.add(vec3(1, 0, 0)), c.r)
448
+ .add(pN(su, uP, v.sub(vec3(1, 0, 0)), c.r))
449
+ .add(pN(su, uP, v.add(vec3(0, 1, 0)), c.r))
450
+ .add(pN(su, uP, v.sub(vec3(0, 1, 0)), c.r))
451
+ .add(pN(su, uP, v.add(vec3(0, 0, 1)), c.r))
452
+ .add(pN(su, uP, v.sub(vec3(0, 0, 1)), c.r));
453
+ oP.assign(vec4(s.sub(c.g).div(6.0), c.g, 0.0, 0.0));
454
+ });
455
+ });
456
+ return oP;
457
+ })();
458
+ }
459
+
460
+ // 7. projection: subtract the pressure gradient; no flow into the floor
461
+ export function PROJECT(
462
+ su: ISim,
463
+ { uV, uS, uP }: { uV: TexNode; uS: TexNode; uP: TexNode },
464
+ out: Out,
465
+ ) {
466
+ return Fn(() => {
467
+ const o = vec4(0.0).toVar();
468
+ const v = vec3(0).toVar();
469
+ If(gridVoxel(G(su), v).and(solid(su, v).not()), () => {
470
+ if (out === "S") {
471
+ o.assign(vec4(gridFetch(uS, G(su), v).rgb, 0.0));
472
+ return;
473
+ }
474
+ const pc = gridFetch(uP, G(su), v).r.toVar();
475
+ const g = vec3(
476
+ pN(su, uP, v.add(vec3(1, 0, 0)), pc).sub(pN(su, uP, v.sub(vec3(1, 0, 0)), pc)),
477
+ pN(su, uP, v.add(vec3(0, 1, 0)), pc).sub(pN(su, uP, v.sub(vec3(0, 1, 0)), pc)),
478
+ pN(su, uP, v.add(vec3(0, 0, 1)), pc).sub(pN(su, uP, v.sub(vec3(0, 0, 1)), pc)),
479
+ ).mul(float(0.5).div(su.uH));
480
+ const vel = gridFetch(uV, G(su), v).xyz.sub(g).toVar();
481
+ If(solid(su, v.sub(vec3(0, 1, 0))), () => {
482
+ vel.assign(vec3(vel.x, max(vel.y, 0.0), vel.z));
483
+ });
484
+ o.assign(vec4(vel, 0.0));
485
+ });
486
+ return o;
487
+ })();
488
+ }
489
+
490
+ // 8. lighting volume at half resolution, one RGBA texel per 2x2x2 block:
491
+ // r sun optical depth (march to the box exit) g sky optical depth (march straight up)
492
+ // b occupancy: 0 if nothing within ~3 voxels, else 1 + |vorticity| (drives the sub-grid detail)
493
+ // a fire light: blurred Kirchhoff emission around the cell (the self-illumination source)
494
+ export interface ILightOwn extends ILook {
495
+ uS: TexNode;
496
+ uC: TexNode;
497
+ uCDim: V3;
498
+ uCTiles: V2;
499
+ uCInv: V2;
500
+ uLightP: V4; // sun step (voxels), fire-light tap spacing (voxels), fire-light gain, empty below (1/m)
501
+ }
502
+ export function LIGHT(su: ISim, L: ILightOwn) {
503
+ const { uS, uC, uLightP } = L;
504
+ const inBox = (q: V3): B =>
505
+ q.x
506
+ .greaterThanEqual(0.0)
507
+ .and(q.y.greaterThanEqual(0.0))
508
+ .and(q.z.greaterThanEqual(0.0))
509
+ .and(q.x.lessThanEqual(su.uDim.x))
510
+ .and(q.y.lessThanEqual(su.uDim.y))
511
+ .and(q.z.lessThanEqual(su.uDim.z));
512
+ return Fn(() => {
513
+ const o0 = vec4(0.0).toVar();
514
+ const c = vec3(0).toVar();
515
+ If(gridVoxel({ dim: L.uCDim, tiles: L.uCTiles, inv: L.uCInv }, c), () => {
516
+ const p = vec3(c).add(0.5).mul(2.0).toVar(); // fine voxel units: the centre of this cell's 2x2x2 block
517
+ // 27 taps around the cell: occupancy (dilated) and the fire light
518
+ const pw = float(0.0).toVar();
519
+ const occ = float(0.0).toVar();
520
+ Loop({ start: 0, end: 27, type: "int", condition: "<" }, ({ i: k }: { i: I }) => {
521
+ const kf = float(k);
522
+ const o = vec3(mod(kf, 3.0), mod(floor(kf.div(3.0)), 3.0), floor(kf.div(9.0)))
523
+ .sub(1.0)
524
+ .toVar();
525
+ const st = gridSample(uS, G(su), p.add(o.mul(uLightP.y))).toVar();
526
+ const th = max(st.r, 0.0).toVar();
527
+ occ.assign(max(occ, sigmaOf(L, st).add(th).add(max(st.g, 0.0))));
528
+ pw.addAssign(
529
+ glowOf(L, th)
530
+ .mul(min(sigmaHot(L, st), 4.0))
531
+ .div(float(1.0).add(dot(o, o))),
532
+ );
533
+ });
534
+ const tau = float(0.0).toVar();
535
+ const tauU = float(0.0).toVar();
536
+ occ.assign(select(occ.greaterThan(max(uLightP.w, 1e-3)), occ, float(0.0)));
537
+ If(occ.greaterThan(0.0), () => {
538
+ const st = uLightP.x;
539
+ const q = p.add(U.uSunDir.mul(st.mul(0.5))).toVar();
540
+ Loop({ start: 0, end: 48, type: "int", condition: "<" }, () => {
541
+ If(inBox(q).not(), () => {
542
+ Break();
543
+ });
544
+ tau.addAssign(sigmaOf(L, gridSample(uS, G(su), q)));
545
+ q.addAssign(U.uSunDir.mul(st));
546
+ });
547
+ tau.mulAssign(st.mul(su.uH));
548
+ q.assign(p.add(vec3(0.0, 2.0, 0.0)));
549
+ Loop({ start: 0, end: 10, type: "int", condition: "<" }, () => {
550
+ If(q.y.greaterThan(su.uDim.y), () => {
551
+ Break();
552
+ });
553
+ tauU.addAssign(sigmaOf(L, gridSample(uS, G(su), q)).mul(su.uH.mul(4.0)));
554
+ q.addAssign(vec3(0.0, 4.0, 0.0));
555
+ });
556
+ });
557
+ const vort = gridSample(uC, G(su), p).a;
558
+ o0.assign(
559
+ vec4(
560
+ tau,
561
+ tauU,
562
+ select(occ.greaterThan(0.0), vort.add(1.0), float(0.0)),
563
+ pw.mul(L.uLook0.x.mul(float(1.0).sub(L.uLook0.w)).mul(uLightP.z).div(27.0)),
564
+ ),
565
+ );
566
+ });
567
+ return o0;
568
+ })();
569
+ }
570
+
571
+ // --- render: raymarch the domain box in the FX pass ----------------------------------------
572
+ // (RENDER_VERT: the material's own vertex stage; vWorld is positionWorld)
573
+ export interface IRenderOwn extends ILook {
574
+ uS: TexNode;
575
+ uL0: TexNode;
576
+ uDim: V3;
577
+ uTiles: V2;
578
+ uInv: V2;
579
+ uCDim: V3;
580
+ uCTiles: V2;
581
+ uCInv: V2;
582
+ uOrigin: V3;
583
+ uH: F;
584
+ uMarch: V4; // step (m), max steps, sub-grid amplitude (voxels), sub-grid frequency (tiles / m)
585
+ uMarch2: V4; // edge fade (voxels), m per FX pixel at 1 m, bottom face open (0/1), near fade 1/m
586
+ uLit: V4; // HG g, sun gain, sky gain, fire-light gain
587
+ uSootColor: V3;
588
+ uDetail: V4; // soot erosion (units), cell pattern frequency (tiles/m, 0 = off), heat chill 0..1, largest empty-space stride
589
+ uDetailC: V3; // pattern centre: it rides the gas
590
+ }
591
+ // the ray's globals (GLSL gSunPh, gMs, gFire, gKc, gRd)
592
+ interface IRay {
593
+ gSunPh: F;
594
+ gMs: F; // multiple-scattering weight from the albedo
595
+ gFire: V3; // fire-light colour (blackbody at a typical flame-edge temperature) x gain
596
+ gKc: V3; // per-channel extinction scale of the light on its way in (coloured gas)
597
+ gRd: V3;
598
+ }
599
+
600
+ // patch (RENDER_BODY): everything from the sampler down
601
+ // per metre: extinction, in-scattered radiance (soot only), emitted radiance
602
+ // returns false in empty space (nothing within ~3 voxels)
603
+ function sampleAt(R: IRenderOwn, g: IRay, pw: V3, sg: F, sc: V3, em: V3): F {
604
+ const Gr: IGrid = { dim: R.uDim, tiles: R.uTiles, inv: R.uInv };
605
+ const cg: IGrid = { dim: R.uCDim, tiles: R.uCTiles, inv: R.uCInv };
606
+ sg.assign(0.0);
607
+ sc.assign(vec3(0.0));
608
+ em.assign(vec3(0.0));
609
+ const occ = float(0.0).toVar(); // engine: no bool(); 0/1
610
+ const p = pw.sub(R.uOrigin).div(R.uH).toVar();
611
+ const l0 = gridSample(R.uL0, cg, p.mul(0.5)).toVar();
612
+ If(l0.b.greaterThan(0.0), () => {
613
+ occ.assign(1.0);
614
+ // sub-grid detail (in the spirit of wavelet turbulence): displace the lookup with band-limited
615
+ // curl noise drifting up with the gas, strongest where the resolved flow is turbulent
616
+ const n = curl3(pw.mul(R.uMarch.w).add(vec3(0.0, U.uTime.mul(-0.28), 0.0))).toVar();
617
+ const s = gridSample(
618
+ R.uS,
619
+ Gr,
620
+ p.add(n.mul(R.uMarch.z.mul(clamp(max(l0.b.sub(1.0), 0.0).mul(0.06).add(0.2), 0.2, 1.0)))),
621
+ ).toVar();
622
+ // fade out towards the open faces so the box never shows
623
+ const e = min(p, R.uDim.sub(p)).toVar();
624
+ const ed = min(
625
+ min(e.x, e.z),
626
+ select(R.uMarch2.z.greaterThan(0.5), min(e.y, R.uDim.y.sub(p.y)), R.uDim.y.sub(p.y)),
627
+ );
628
+ // (the fade distance is jittered by the same noise, so a cloud that reaches a face frays
629
+ // into wisps instead of showing a straight cut)
630
+ const fade = smoothstep(0.5, R.uMarch2.x, ed.add(n.x.add(n.z).mul(1.6))).toVar();
631
+ s.assign(vec4(s.rgb.mul(fade.mul(fade)), s.a));
632
+ If(R.uDetail.y.greaterThan(0.0), () => {
633
+ // Worley fbm from one fetch: f2 cells (big billows), f4 (the lobes on them), f8 (the
634
+ // cauliflower on those). Thin gas erodes harder than the core, so billows keep firm tops
635
+ // while their trailing edges tear into ragged wisps
636
+ const wv = noise3v(pw.sub(R.uDetailC).mul(R.uDetail.y).add(n.mul(0.05))).gba;
637
+ const crease = float(1.0)
638
+ .sub(dot(wv, vec3(0.45, 0.35, 0.2)))
639
+ .toVar();
640
+ const thin = exp(s.b.negate().div(max(R.uDetail.x, 1e-3)));
641
+ s.assign(
642
+ vec4(
643
+ s.r.mul(float(1.0).sub(R.uDetail.z.mul(crease))),
644
+ s.g,
645
+ max(s.b.sub(R.uDetail.x.mul(crease).mul(thin.add(1.0))), 0.0),
646
+ s.a,
647
+ ),
648
+ );
649
+ });
650
+ sg.assign(sigmaOf(R, s));
651
+ em.assign(emitOf(R, s));
652
+ const ss = R.uLook0.y.mul(max(s.b, 0.0)).toVar();
653
+ If(ss.greaterThanEqual(1e-4), () => {
654
+ // sun: single scattering through the precomputed optical depth, plus two wider, weaker octaves
655
+ // standing in for multiple scattering (Wrenninge 2015); sky through the optical depth above
656
+ const t = l0.r;
657
+ // multiple-scattering octaves scale with albedo: dark soot barely scatters twice, so it keeps
658
+ // its form shading (lit tops, near-black undersides) instead of glowing flat
659
+ const tc = g.gKc.mul(t).toVar();
660
+ const sun = U.uSunColor.mul(R.uLit.y).mul(
661
+ exp(tc.negate())
662
+ .mul(g.gSunPh)
663
+ .add(
664
+ exp(tc.mul(-0.5))
665
+ .mul(0.4)
666
+ .add(exp(tc.mul(-0.25)).mul(0.15))
667
+ .mul(g.gMs),
668
+ ),
669
+ );
670
+ const sky = U.uAmbient.mul(R.uLit.z).mul(
671
+ exp(l0.g.mul(-0.7).mul(g.gKc))
672
+ .mul(0.96)
673
+ .add(0.04), // deep cores go near black
674
+ );
675
+ sc.assign(R.uSootColor.mul(ss).mul(sun.add(sky).add(g.gFire.mul(l0.a))));
676
+ });
677
+ });
678
+ return occ;
679
+ }
680
+
681
+ // a sample on the ray, with the near fade applied (all three outputs are linear in density)
682
+ function tap(R: IRenderOwn, g: IRay, t: F, sg: F, sc: V3, em: V3): F {
683
+ const o = sampleAt(R, g, cameraPosition.add(g.gRd.mul(t)), sg, sc, em);
684
+ const k0 = clamp(t.mul(R.uMarch2.w), 0.0, 1.0).toVar();
685
+ const k = k0
686
+ .mul(k0)
687
+ .mul(float(3.0).sub(k0.mul(2.0)))
688
+ .toVar();
689
+ sg.mulAssign(k);
690
+ sc.mulAssign(k);
691
+ em.mulAssign(k);
692
+ return o;
693
+ }
694
+
695
+ // the integrator state, and one trapezoid slab (energy conserving) from ta over L
696
+ interface IIntegrator {
697
+ gT: F;
698
+ gZs: F;
699
+ gWs: F;
700
+ gScat: V3;
701
+ gEmit: V3;
702
+ }
703
+ function slab(S0: IIntegrator, ta: F, L: F, sa: F, ca: V3, ea: V3, sb: F, cb: V3, eb: V3): void {
704
+ const s = sa.add(sb).mul(0.5).toVar();
705
+ const Sc = ca.add(cb).mul(0.5).toVar();
706
+ const E = ea.add(eb).mul(0.5).toVar();
707
+ If(s.greaterThan(1e-5), () => {
708
+ const tr = exp(s.negate().mul(L)).toVar();
709
+ const a = S0.gT.mul(float(1.0).sub(tr)).toVar();
710
+ S0.gScat.addAssign(Sc.mul(a.div(s)));
711
+ S0.gEmit.addAssign(E.mul(a.div(s)));
712
+ S0.gZs.addAssign(a.mul(ta.add(L.mul(0.5))));
713
+ S0.gWs.addAssign(a);
714
+ S0.gT.mulAssign(tr);
715
+ }).Else(() => {
716
+ S0.gEmit.addAssign(E.mul(S0.gT).mul(L));
717
+ });
718
+ }
719
+
720
+ export function RENDER(R: IRenderOwn) {
721
+ return Fn(() => {
722
+ const out = vec4(0.0).toVar();
723
+ const rd = normalize(positionWorld.sub(cameraPosition)).toVar();
724
+ const dd = vec3(
725
+ select(abs(rd.x).lessThan(1e-6), float(1e-6), rd.x),
726
+ select(abs(rd.y).lessThan(1e-6), float(1e-6), rd.y),
727
+ select(abs(rd.z).lessThan(1e-6), float(1e-6), rd.z),
728
+ ).toVar();
729
+ const bmin = R.uOrigin;
730
+ const bmax = R.uOrigin.add(R.uDim.mul(R.uH));
731
+ const ta = bmin.sub(cameraPosition).div(dd).toVar();
732
+ const tb = bmax.sub(cameraPosition).div(dd).toVar();
733
+ const lo = min(ta, tb).toVar();
734
+ const hi = max(ta, tb).toVar();
735
+ const t0 = max(max(max(lo.x, lo.y), lo.z), 0.0).toVar();
736
+ const t1 = min(min(hi.x, hi.y), hi.z).toVar();
737
+ const cosV = dot(rd, uCamFwd).toVar();
738
+ t1.assign(min(t1, sceneViewZ().div(cosV)));
739
+ Discard(t1.lessThanEqual(t0));
740
+ const dt = R.uMarch.x;
741
+ const mu = dot(U.uSunDir, rd).toVar();
742
+ const g: IRay = {
743
+ // soot: forward lobe + a weak back lobe, normalised so isotropic = 1
744
+ gSunPh: mix(hgPhase(mu, R.uLit.x), hgPhase(mu, float(-0.25)), 0.25)
745
+ .mul(PI.mul(4.0))
746
+ .toVar(),
747
+ gFire: blackbody(kelvinOf(R, float(0.75)))
748
+ .mul(R.uTint)
749
+ .mul(R.uLit.w)
750
+ .toVar(),
751
+ gMs: clamp(dot(R.uSootColor, vec3(1.0)), 0.0, 1.0).toVar(),
752
+ // chroma > 0: a coloured gas (chlorine) takes its colour from depth, not from its albedo alone.
753
+ // The channels it absorbs (where the albedo is below its brightest) fade faster on the way in,
754
+ // so thin gas scatters near white and deep gas goes yellow-green; 0 = grey extinction
755
+ gKc: float(1.0)
756
+ .add(
757
+ R.uLook1.w.mul(
758
+ float(1.0).sub(
759
+ R.uSootColor.div(max(max(R.uSootColor.r, R.uSootColor.g), max(R.uSootColor.b, 1e-3))),
760
+ ),
761
+ ),
762
+ )
763
+ .toVar(),
764
+ gRd: rd,
765
+ };
766
+ const S0: IIntegrator = {
767
+ gT: float(1.0).toVar(),
768
+ gZs: float(0.0).toVar(),
769
+ gWs: float(0.0).toVar(),
770
+ gScat: vec3(0.0).toVar(),
771
+ gEmit: vec3(0.0).toVar(),
772
+ };
773
+ // the lattice: t0 + i dt, ending in a partial step at t1 (continuous in t0, t1 and dt)
774
+ const t = t0.toVar();
775
+ const sgP = float(0.0).toVar();
776
+ const scP = vec3(0.0).toVar();
777
+ const emP = vec3(0.0).toVar();
778
+ const occP = tap(R, g, t, sgP, scP, emP);
779
+ const empties = select(occP.greaterThan(0.5), float(0.0), float(1.0)).toVar();
780
+ const sg = float(0.0).toVar();
781
+ const sc = vec3(0.0).toVar();
782
+ const em = vec3(0.0).toVar();
783
+ const sgM = float(0.0).toVar();
784
+ const scM = vec3(0.0).toVar();
785
+ const emM = vec3(0.0).toVar();
786
+ const occ = float(0.0).toVar(); // engine: no bool(); 0/1
787
+ Loop({ start: 0, end: 256, type: "int", condition: "<" }, () => {
788
+ If(S0.gT.lessThan(0.03).or(t.greaterThanEqual(t1)), () => {
789
+ Break();
790
+ });
791
+ // stride over empty space (the occupancy is dilated ~3 voxels, so skipped points are empty)
792
+ const stride = clamp(empties.sub(1.0), 1.0, R.uDetail.w).toVar();
793
+ const tn = min(t.add(dt.mul(stride)), t1).toVar();
794
+ occ.assign(tap(R, g, tn, sg, sc, em));
795
+ If(occ.greaterThan(0.5).and(stride.greaterThan(1.0)), () => {
796
+ // landed in gas: back onto the next lattice point
797
+ tn.assign(min(t.add(dt), t1));
798
+ occ.assign(tap(R, g, tn, sg, sc, em));
799
+ empties.assign(0.0);
800
+ });
801
+ const L = tn.sub(t).toVar();
802
+ const w = smoothstep(0.8, 1.6, sg.add(sgP).mul(0.5).mul(L)).toVar();
803
+ If(w.greaterThan(0.0), () => {
804
+ // optically thick (a surface): also integrate it as two half slabs through the midpoint, and
805
+ // blend the two estimates by thickness, so the surface sits where it is, not on the lattice
806
+ const T0 = S0.gT.toVar();
807
+ const z0 = S0.gZs.toVar();
808
+ const w0 = S0.gWs.toVar();
809
+ const s0 = S0.gScat.toVar();
810
+ const e0 = S0.gEmit.toVar();
811
+ tap(R, g, t.add(L.mul(0.5)), sgM, scM, emM);
812
+ slab(S0, t, L.mul(0.5), sgP, scP, emP, sgM, scM, emM);
813
+ slab(S0, t.add(L.mul(0.5)), L.mul(0.5), sgM, scM, emM, sg, sc, em);
814
+ const T2 = S0.gT.toVar();
815
+ const z2 = S0.gZs.toVar();
816
+ const w2 = S0.gWs.toVar();
817
+ const s2 = S0.gScat.toVar();
818
+ const e2 = S0.gEmit.toVar();
819
+ S0.gT.assign(T0);
820
+ S0.gZs.assign(z0);
821
+ S0.gWs.assign(w0);
822
+ S0.gScat.assign(s0);
823
+ S0.gEmit.assign(e0);
824
+ If(w.lessThan(1.0), () => {
825
+ slab(S0, t, L, sgP, scP, emP, sg, sc, em);
826
+ });
827
+ S0.gT.assign(mix(S0.gT, T2, w));
828
+ S0.gZs.assign(mix(S0.gZs, z2, w));
829
+ S0.gWs.assign(mix(S0.gWs, w2, w));
830
+ S0.gScat.assign(mix(S0.gScat, s2, w));
831
+ S0.gEmit.assign(mix(S0.gEmit, e2, w));
832
+ }).Else(() => {
833
+ slab(S0, t, L, sgP, scP, emP, sg, sc, em);
834
+ });
835
+ sgP.assign(sg);
836
+ scP.assign(sc);
837
+ emP.assign(em);
838
+ t.assign(tn);
839
+ empties.assign(select(occ.greaterThan(0.5), float(0.0), empties.add(1.0)));
840
+ });
841
+ // stopped on opacity: the unmarched rest is more of the same gas, so renormalise to opaque
842
+ // (else the last 3% lets a bright HDR sky print its clouds through thick dark smoke)
843
+ If(S0.gT.lessThan(0.03), () => {
844
+ const k = float(1.0).div(float(1.0).sub(S0.gT)).toVar();
845
+ S0.gScat.mulAssign(k);
846
+ S0.gEmit.mulAssign(k);
847
+ S0.gT.assign(0.0);
848
+ });
849
+ const alpha = float(1.0).sub(S0.gT).toVar();
850
+ Discard(alpha.lessThan(0.002).and(dot(S0.gEmit, S0.gEmit).lessThan(1e-6)));
851
+ const z = select(S0.gWs.greaterThan(0.0), S0.gZs.div(S0.gWs), t0).mul(cosV);
852
+ out.assign(fxOut(S0.gScat.div(max(alpha, 1e-4)), alpha, S0.gEmit, vec2(0.0), z));
853
+ return out;
854
+ })();
855
+ }