@threenative/core 0.3.11 → 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.
- package/capabilities.json +1096 -26
- package/dist/camera-shake-CZKOfGn4.d.ts +46 -0
- package/dist/{canvas-layer-vCtanIeB.d.ts → canvas-layer-CWuFRT7G.d.ts} +1 -1
- package/dist/chunk-2Z44XSI2.js +35 -0
- package/dist/chunk-5O73CDMR.js +1849 -0
- package/dist/chunk-6KLNISRJ.js +153 -0
- package/dist/chunk-7UUT3TY5.js +133 -0
- package/dist/chunk-CFGTPFUH.js +231 -0
- package/dist/chunk-DBELTTTM.js +615 -0
- package/dist/chunk-GMHATRPW.js +7701 -0
- package/dist/chunk-GXY7B3JG.js +46 -0
- package/dist/chunk-KIRUKTBQ.js +187 -0
- package/dist/chunk-N5LRP4BR.js +220 -0
- package/dist/chunk-OKXUCASC.js +2238 -0
- package/dist/chunk-QWJEMUUT.js +94 -0
- package/dist/chunk-VAZM36TV.js +497 -0
- package/dist/{game-DzMcitDO.d.ts → game-D7pAzZUY.d.ts} +15 -3
- package/dist/hot.d.ts +3 -3
- package/dist/hot.js +2 -46
- package/dist/index.d.ts +329 -302
- package/dist/index.js +10566 -22552
- package/dist/{model-lod-CTpZ6imz.d.ts → model-lod-B6BMqS1r.d.ts} +33 -3
- package/dist/playtest.d.ts +3 -3
- package/dist/playtest.js +24 -75
- package/dist/react-css.js +1 -215
- package/dist/react.d.ts +2 -2
- package/dist/{renderer-rZm0hVqq.d.ts → renderer-DvFVoAgj.d.ts} +13 -4
- package/dist/spells/index.d.ts +643 -0
- package/dist/spells/index.js +1558 -0
- package/dist/spells/ultra/index.d.ts +1600 -0
- package/dist/spells/ultra/index.js +6804 -0
- package/dist/terrain-jobs-worker.js +1 -123
- package/dist/two-sided-foliage-W523J3UM.js +1 -0
- package/dist/ui-layer.js +5 -384
- package/dist/worker.js +4 -41
- package/dist/world.d.ts +725 -259
- package/dist/world.js +16714 -18284
- package/mcp/animation-server.mjs +1 -1
- package/mcp/animation.mjs +2 -3
- package/mcp/engine-server.mjs +142 -47
- package/mcp/servers.mjs +9 -2
- package/mcp/terrain.mjs +8 -0
- package/package.json +14 -6
- package/patches/three@0.187.0-prd269-upgrade.patch +2102 -0
- package/patches/three@0.187.0.patch +2004 -407
- package/src/assets.ts +9 -2
- package/src/card-lod.ts +152 -0
- package/src/compressed-texture-preparation.ts +1008 -0
- package/src/frame-budget.ts +31 -20
- package/src/game-dev-tools.ts +93 -0
- package/src/game-frame-loop.ts +995 -0
- package/src/game-renderer-metrics.ts +77 -0
- package/src/game-startup.ts +1001 -0
- package/src/game-validate.ts +70 -0
- package/src/game.ts +253 -1935
- package/src/index.ts +27 -2
- package/src/launch-diagnostics.ts +22 -12
- package/src/look/ao-admission.ts +24 -0
- package/src/look/filmic-tone-mapping.ts +15 -13
- package/src/look/look-diagnostics.ts +3 -3
- package/src/look/quality.ts +10 -5
- package/src/look/world-environment.ts +76 -9
- package/src/loop.ts +29 -0
- package/src/model-lod.ts +42 -3
- package/src/particle-kinds.ts +515 -0
- package/src/picking.ts +17 -3
- package/src/playtest.ts +27 -1
- package/src/projection-apply.ts +4 -1
- package/src/projection-plan.ts +9 -6
- package/src/render/mesh-pool.ts +1 -1
- package/src/render/resolution-subrect.ts +2 -0
- package/src/render/rough-diffuse.ts +46 -12
- package/src/render/soft-shadow.ts +27 -12
- package/src/render/virtual-shadow-adaptive.ts +55 -0
- package/src/render/virtual-shadow-casters.ts +175 -0
- package/src/render/virtual-shadow-layers.ts +76 -0
- package/src/render/virtual-shadow-level.ts +224 -0
- package/src/render/virtual-shadow-markers.ts +193 -0
- package/src/render/virtual-shadow-options.ts +370 -0
- package/src/render/virtual-shadow-pages.ts +5 -0
- package/src/render/virtual-shadow.ts +213 -992
- package/src/render/world-impostor-surface.ts +293 -34
- package/src/render-pass-budget.ts +34 -7
- package/src/renderProjection.ts +22 -0
- package/src/renderer.ts +92 -37
- package/src/snow-field.ts +1 -1
- package/src/spells/cast-pool.ts +361 -0
- package/src/spells/index.ts +177 -0
- package/src/spells/mechanisms.ts +295 -0
- package/src/spells/reaction-field.ts +415 -0
- package/src/spells/spell-lights.ts +139 -0
- package/src/spells/spell-world.ts +187 -0
- package/src/spells/spell.ts +397 -0
- package/src/spells/spells.ts +283 -0
- package/src/spells/ultra/engine/camera.ts +184 -0
- package/src/spells/ultra/engine/quality.ts +96 -0
- package/src/spells/ultra/index.ts +52 -0
- package/src/spells/ultra/main.ts +305 -0
- package/src/spells/ultra/scene/arenaMaterial.ts +325 -0
- package/src/spells/ultra/spells/cast.ts +233 -0
- package/src/spells/ultra/spells/fire.ts +1267 -0
- package/src/spells/ultra/vfx/FluidVolume.ts +816 -0
- package/src/spells/ultra/vfx/FluidVolume.tsl.ts +855 -0
- package/src/spells/ultra/vfx/FxTarget.ts +61 -0
- package/src/spells/ultra/vfx/GroundField.ts +1399 -0
- package/src/spells/ultra/vfx/Particles.ts +1475 -0
- package/src/spells/ultra/vfx/Ribbons.ts +645 -0
- package/src/spells/ultra/vfx/Shockwaves.ts +286 -0
- package/src/spells/ultra/vfx/Volumes.ts +489 -0
- package/src/spells/ultra/vfx/common.ts +1195 -0
- package/src/spells/ultra/vfx/noise.ts +441 -0
- package/src/startup-readiness.ts +128 -34
- package/src/version.ts +1 -1
- package/src/warmup.ts +56 -8
- package/src/water-surface.ts +9 -3
- package/src/world-bundle-safety.ts +401 -0
- package/src/world-cell-batches.ts +1216 -0
- package/src/world-cell-chunks.ts +1132 -0
- package/src/world-cell-data.ts +995 -0
- package/src/world-cell-impostors.ts +1449 -0
- package/src/world-cell-lod-bias.ts +151 -0
- package/src/world-cell-models.ts +1074 -0
- package/src/world-cell-options.ts +189 -0
- package/src/world-cell-placements.ts +694 -0
- package/src/world-cell-prewarm.ts +557 -0
- package/src/world-cell-readiness.ts +357 -0
- package/src/world-cell-render-bundles.ts +459 -0
- package/src/world-cell-render-gpu-dress.ts +714 -0
- package/src/world-cell-render-main-cull.ts +484 -0
- package/src/world-cell-render-shadow-claims.ts +198 -0
- package/src/world-cell-render-util.ts +246 -0
- package/src/world-cell-render.ts +1493 -0
- package/src/world-cell-residency.ts +745 -0
- package/src/world-cells.ts +1318 -10418
- package/src/world-chunk-shadow.ts +308 -0
- package/src/world-collar.ts +315 -0
- package/src/world-gpu-flags.ts +163 -0
- package/src/world-gpu-readback.ts +881 -0
- package/src/world-gpu-reference.ts +510 -0
- package/src/world-gpu-scene.ts +214 -2264
- package/src/world-gpu-shadow.ts +190 -0
- package/src/world-gpu-types.ts +381 -0
- package/src/world-gpu-validation.ts +518 -0
- package/src/world-heightfield.ts +802 -0
- package/src/world-load-limiter.ts +106 -0
- package/src/world-noise.ts +241 -0
- package/src/world-package.ts +42 -0
- package/src/world-region.ts +1 -1
- package/src/world-terrain-splat.ts +365 -149
- package/src/world-tile-budget.ts +256 -0
- package/src/world-tile-geometry.ts +469 -0
- package/src/world-tile-lod.ts +408 -0
- package/src/world-tile-observation.ts +566 -0
- package/src/world-tile-stitch.ts +166 -0
- package/src/world-tiles.ts +170 -1745
- package/src/world-water-bodies.ts +359 -0
- package/src/world.ts +35 -741
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
// Ported from Ultra Elements src/vfx/noise.js @698b720 (MIT, Copyright (c) 2026 Humpalumps). JavaScript copied verbatim; GLSL transliterated to TSL.
|
|
2
|
+
//
|
|
3
|
+
// One change the engine forces: upstream bakes the two 3D textures on the GPU, one fullscreen draw
|
|
4
|
+
// per z slice into a WebGL3DRenderTarget. The ThreeNative engine has Data3DTexture/texture3D but no
|
|
5
|
+
// 3D render target, so the same HASH / NOISE_FRAG / CURL_FRAG maths runs here on the CPU, texel for
|
|
6
|
+
// texel (GLSL -> JS: uint maths through Math.imul and >>> 0), into the same RGBA8 layout. The lattice
|
|
7
|
+
// hashes are memoised per octave (a pure function of the wrapped cell), so each texel pays the
|
|
8
|
+
// interpolation, not the hash. bakeNoise() logs the wall time.
|
|
9
|
+
import {
|
|
10
|
+
Color,
|
|
11
|
+
Data3DTexture,
|
|
12
|
+
DataTexture,
|
|
13
|
+
DataUtils,
|
|
14
|
+
HalfFloatType,
|
|
15
|
+
LinearFilter,
|
|
16
|
+
LinearSRGBColorSpace,
|
|
17
|
+
RGBAFormat,
|
|
18
|
+
RepeatWrapping,
|
|
19
|
+
} from "three";
|
|
20
|
+
import { U } from "./common.js";
|
|
21
|
+
|
|
22
|
+
// Procedural noise, baked at load (no assets):
|
|
23
|
+
// noise3D N^3 RGBA8, tileable on all axes. r = Perlin-Worley fbm (Schneider 2015: Perlin fbm
|
|
24
|
+
// dilated by Worley fbm), g/b/a = inverted Worley F1 fbm at base frequencies 2/4/8.
|
|
25
|
+
// curl3D 64^3 RGBA8, tileable. rgb = curl * 0.5 + 0.5 of a 3-field gradient-noise vector
|
|
26
|
+
// potential (Bridson et al. 2007), divergence-free by construction.
|
|
27
|
+
// blackbody 256x1 RGBA16F, CPU: Planck x Wyman-Sloan-Shirley 2013 CIE fit -> linear sRGB, Y = 1.
|
|
28
|
+
// Sampling contract: NOISE_GLSL in common.js (noise3v, noise3, worley3, curl3, fbm3, blackbody in common.ts).
|
|
29
|
+
|
|
30
|
+
// --- HASH (GLSL -> JS) -------------------------------------------------------------------
|
|
31
|
+
// pcg3d (Jarzynski & Olano 2020): cheap and free of lattice artefacts
|
|
32
|
+
const _v = new Uint32Array(3);
|
|
33
|
+
function pcg3d(x: number, y: number, z: number): Uint32Array {
|
|
34
|
+
const v = _v;
|
|
35
|
+
v[0] = Math.imul(x, 1664525) + 1013904223;
|
|
36
|
+
v[1] = Math.imul(y, 1664525) + 1013904223;
|
|
37
|
+
v[2] = Math.imul(z, 1664525) + 1013904223;
|
|
38
|
+
v[0] = (v[0] as number) + Math.imul(v[1] as number, v[2] as number);
|
|
39
|
+
v[1] = (v[1] as number) + Math.imul(v[2] as number, v[0] as number);
|
|
40
|
+
v[2] = (v[2] as number) + Math.imul(v[0] as number, v[1] as number);
|
|
41
|
+
v[0] = (v[0] as number) ^ ((v[0] as number) >>> 16);
|
|
42
|
+
v[1] = (v[1] as number) ^ ((v[1] as number) >>> 16);
|
|
43
|
+
v[2] = (v[2] as number) ^ ((v[2] as number) >>> 16);
|
|
44
|
+
v[0] = (v[0] as number) + Math.imul(v[1] as number, v[2] as number);
|
|
45
|
+
v[1] = (v[1] as number) + Math.imul(v[2] as number, v[0] as number);
|
|
46
|
+
v[2] = (v[2] as number) + Math.imul(v[0] as number, v[1] as number);
|
|
47
|
+
return v;
|
|
48
|
+
}
|
|
49
|
+
// lattice point -> hash, wrapped at period P so every octave tiles the unit cube. c is always
|
|
50
|
+
// within one period of [0, P), so a select replaces two integer modulos per component.
|
|
51
|
+
const wrapP = (c: number, P: number): number => c + P * ((c < 0 ? 1 : 0) - (c >= P ? 1 : 0));
|
|
52
|
+
function hashL(cx: number, cy: number, cz: number, P: number, seed: number): Uint32Array {
|
|
53
|
+
return pcg3d(
|
|
54
|
+
(wrapP(cx, P) + Math.imul(seed, 0x9e3779b9)) >>> 0,
|
|
55
|
+
(wrapP(cy, P) + Math.imul(seed, 0x85ebca6b)) >>> 0,
|
|
56
|
+
(wrapP(cz, P) + Math.imul(seed, 0xc2b2ae35)) >>> 0,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
// hash3(c, P, seed) = vec3(hashL(c, P, seed) >> 8u) * (1.0 / 16777216.0), memoised per (P, seed):
|
|
60
|
+
// table[(z * P + y) * P + x] over the wrapped lattice
|
|
61
|
+
function hash3Table(P: number, seed: number): Float32Array {
|
|
62
|
+
const t = new Float32Array(P * P * P * 3);
|
|
63
|
+
for (let z = 0; z < P; z++)
|
|
64
|
+
for (let y = 0; y < P; y++)
|
|
65
|
+
for (let x = 0; x < P; x++) {
|
|
66
|
+
const h = hashL(x, y, z, P, seed);
|
|
67
|
+
const o = ((z * P + y) * P + x) * 3;
|
|
68
|
+
t[o] = ((h[0] as number) >>> 8) * (1.0 / 16777216.0);
|
|
69
|
+
t[o + 1] = ((h[1] as number) >>> 8) * (1.0 / 16777216.0);
|
|
70
|
+
t[o + 2] = ((h[2] as number) >>> 8) * (1.0 / 16777216.0);
|
|
71
|
+
}
|
|
72
|
+
return t;
|
|
73
|
+
}
|
|
74
|
+
// gnoiseGrad3's three gradients per corner, memoised the same way: 9 floats per lattice point
|
|
75
|
+
function grad3Table(P: number, seed: number): Float32Array {
|
|
76
|
+
const t = new Float32Array(P * P * P * 9);
|
|
77
|
+
for (let z = 0; z < P; z++)
|
|
78
|
+
for (let y = 0; y < P; y++)
|
|
79
|
+
for (let x = 0; x < P; x++) {
|
|
80
|
+
const h = hashL(x, y, z, P, seed);
|
|
81
|
+
const o = ((z * P + y) * P + x) * 9;
|
|
82
|
+
for (let c = 0; c < 3; c++) {
|
|
83
|
+
const hc = h[c] as number;
|
|
84
|
+
t[o + c] = ((hc >>> 22) & 1023) / 511.5 - 1.0;
|
|
85
|
+
t[o + 3 + c] = ((hc >>> 12) & 1023) / 511.5 - 1.0;
|
|
86
|
+
t[o + 6 + c] = ((hc >>> 2) & 1023) / 511.5 - 1.0;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return t;
|
|
90
|
+
}
|
|
91
|
+
const cellIndex = (x: number, y: number, z: number, P: number): number =>
|
|
92
|
+
(wrapP(z, P) * P + wrapP(y, P)) * P + wrapP(x, P);
|
|
93
|
+
|
|
94
|
+
// inverted Worley F1: 1 at feature points, ~0 on cell borders. x in cells (= p * P)
|
|
95
|
+
function worley(px: number, py: number, pz: number, P: number, table: Float32Array): number {
|
|
96
|
+
const ix = Math.floor(px);
|
|
97
|
+
const iy = Math.floor(py);
|
|
98
|
+
const iz = Math.floor(pz);
|
|
99
|
+
const fx = px - ix;
|
|
100
|
+
const fy = py - iy;
|
|
101
|
+
const fz = pz - iz;
|
|
102
|
+
let d = 8.0;
|
|
103
|
+
for (let z = -1; z <= 1; z++)
|
|
104
|
+
for (let y = -1; y <= 1; y++)
|
|
105
|
+
for (let k = -1; k <= 1; k++) {
|
|
106
|
+
const h = cellIndex(ix + k, iy + y, iz + z, P) * 3;
|
|
107
|
+
const rx = k + (table[h] as number) - fx;
|
|
108
|
+
const ry = y + (table[h + 1] as number) - fy;
|
|
109
|
+
const rz = z + (table[h + 2] as number) - fz;
|
|
110
|
+
d = Math.min(d, rx * rx + ry * ry + rz * rz);
|
|
111
|
+
}
|
|
112
|
+
return 1.0 - Math.min(Math.max(Math.sqrt(d), 0.0), 1.0);
|
|
113
|
+
}
|
|
114
|
+
// quintic gradient noise, ~[-1, 1]; corners in a loop (one inlined hash instead of eight)
|
|
115
|
+
function gnoise(px: number, py: number, pz: number, P: number, table: Float32Array): number {
|
|
116
|
+
const ix = Math.floor(px);
|
|
117
|
+
const iy = Math.floor(py);
|
|
118
|
+
const iz = Math.floor(pz);
|
|
119
|
+
const fx = px - ix;
|
|
120
|
+
const fy = py - iy;
|
|
121
|
+
const fz = pz - iz;
|
|
122
|
+
const ux = fx * fx * fx * (fx * (fx * 6.0 - 15.0) + 10.0);
|
|
123
|
+
const uy = fy * fy * fy * (fy * (fy * 6.0 - 15.0) + 10.0);
|
|
124
|
+
const uz = fz * fz * fz * (fz * (fz * 6.0 - 15.0) + 10.0);
|
|
125
|
+
let v = 0.0;
|
|
126
|
+
for (let c = 0; c < 8; c++) {
|
|
127
|
+
const ox = c & 1;
|
|
128
|
+
const oy = (c >> 1) & 1;
|
|
129
|
+
const oz = c >> 2;
|
|
130
|
+
const sx = ox ? ux : 1.0 - ux;
|
|
131
|
+
const sy = oy ? uy : 1.0 - uy;
|
|
132
|
+
const sz = oz ? uz : 1.0 - uz;
|
|
133
|
+
const h = cellIndex(ix + ox, iy + oy, iz + oz, P) * 3;
|
|
134
|
+
v +=
|
|
135
|
+
sx *
|
|
136
|
+
sy *
|
|
137
|
+
sz *
|
|
138
|
+
(((table[h] as number) * 2.0 - 1.0) * (fx - ox) +
|
|
139
|
+
((table[h + 1] as number) * 2.0 - 1.0) * (fy - oy) +
|
|
140
|
+
((table[h + 2] as number) * 2.0 - 1.0) * (fz - oz));
|
|
141
|
+
}
|
|
142
|
+
return v;
|
|
143
|
+
}
|
|
144
|
+
// gradients (d/dx) of three independent gradient-noise fields at once, as mat3 columns. One
|
|
145
|
+
// hash per corner carries all three gradients (10 bits per component). J: 9 floats, column k at 3k.
|
|
146
|
+
function gnoiseGrad3(
|
|
147
|
+
px: number,
|
|
148
|
+
py: number,
|
|
149
|
+
pz: number,
|
|
150
|
+
P: number,
|
|
151
|
+
table: Float32Array,
|
|
152
|
+
J: Float64Array,
|
|
153
|
+
amp: number,
|
|
154
|
+
): void {
|
|
155
|
+
const ix = Math.floor(px);
|
|
156
|
+
const iy = Math.floor(py);
|
|
157
|
+
const iz = Math.floor(pz);
|
|
158
|
+
const fx = px - ix;
|
|
159
|
+
const fy = py - iy;
|
|
160
|
+
const fz = pz - iz;
|
|
161
|
+
const ux = fx * fx * fx * (fx * (fx * 6.0 - 15.0) + 10.0);
|
|
162
|
+
const dux = 30.0 * fx * fx * (fx * (fx - 2.0) + 1.0);
|
|
163
|
+
const uy = fy * fy * fy * (fy * (fy * 6.0 - 15.0) + 10.0);
|
|
164
|
+
const duy = 30.0 * fy * fy * (fy * (fy - 2.0) + 1.0);
|
|
165
|
+
const uz = fz * fz * fz * (fz * (fz * 6.0 - 15.0) + 10.0);
|
|
166
|
+
const duz = 30.0 * fz * fz * (fz * (fz - 2.0) + 1.0);
|
|
167
|
+
for (let c = 0; c < 8; c++) {
|
|
168
|
+
const ox = c & 1;
|
|
169
|
+
const oy = (c >> 1) & 1;
|
|
170
|
+
const oz = c >> 2;
|
|
171
|
+
const fox = fx - ox;
|
|
172
|
+
const foy = fy - oy;
|
|
173
|
+
const foz = fz - oz;
|
|
174
|
+
const sx = ox ? ux : 1.0 - ux;
|
|
175
|
+
const sy = oy ? uy : 1.0 - uy;
|
|
176
|
+
const sz = oz ? uz : 1.0 - uz;
|
|
177
|
+
const dsx = ox ? dux : -dux;
|
|
178
|
+
const dsy = oy ? duy : -duy;
|
|
179
|
+
const dsz = oz ? duz : -duz;
|
|
180
|
+
const w = sx * sy * sz;
|
|
181
|
+
const dwx = dsx * sy * sz;
|
|
182
|
+
const dwy = sx * dsy * sz;
|
|
183
|
+
const dwz = sx * sy * dsz;
|
|
184
|
+
const h = cellIndex(ix + ox, iy + oy, iz + oz, P) * 9;
|
|
185
|
+
for (let k = 0; k < 3; k++) {
|
|
186
|
+
const gx = table[h + 3 * k] as number;
|
|
187
|
+
const gy = table[h + 3 * k + 1] as number;
|
|
188
|
+
const gz = table[h + 3 * k + 2] as number;
|
|
189
|
+
const gf = gx * fox + gy * foy + gz * foz;
|
|
190
|
+
J[3 * k] = (J[3 * k] as number) + (w * gx + gf * dwx) * amp;
|
|
191
|
+
J[3 * k + 1] = (J[3 * k + 1] as number) + (w * gy + gf * dwy) * amp;
|
|
192
|
+
J[3 * k + 2] = (J[3 * k + 2] as number) + (w * gz + gf * dwz) * amp;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// calibration knobs, from dev/fields.js "noise stats" (1st / 99th percentiles of the raw sums;
|
|
198
|
+
// curl scale puts the mean |curl| at 0.5)
|
|
199
|
+
// biome-ignore lint/style/useNamingConvention: upstream Ultra Elements code, copied verbatim
|
|
200
|
+
export const NOISE_TUNING = { PW_LO: 0.53, PW_HI: 0.9, W_LO: 0.21, W_HI: 0.75, CURL_SCALE: 0.37 };
|
|
201
|
+
|
|
202
|
+
// Ranges measured from a readback histogram of the raw fbm (dev/fields.js "noise stats"):
|
|
203
|
+
// stretch the 1st..99th percentile to 0..1 so volume thresholds have the whole byte range.
|
|
204
|
+
const stretch = (x: number, lo: number, hi: number): number =>
|
|
205
|
+
Math.min(Math.max((x - lo) / (hi - lo), 0.0), 1.0);
|
|
206
|
+
// what the GPU stores for a float written to an RGBA8 target
|
|
207
|
+
const unorm8 = (x: number): number => Math.round(Math.min(Math.max(x, 0), 1) * 255);
|
|
208
|
+
|
|
209
|
+
// NOISE_FRAG for every texel of an N^3 RGBA8 volume (vUv, uZ at texel centres)
|
|
210
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: upstream Ultra Elements code, copied verbatim
|
|
211
|
+
function bakeNoiseData(size: number): Uint8Array {
|
|
212
|
+
const { PW_LO, PW_HI, W_LO, W_HI } = NOISE_TUNING;
|
|
213
|
+
const g = Array.from({ length: 5 }, (_, o) => hash3Table(4 << o, 7));
|
|
214
|
+
const w = Array.from({ length: 5 }, (_, o) => hash3Table(2 << o, o + 1));
|
|
215
|
+
const out = new Uint8Array(size * size * size * 4);
|
|
216
|
+
const gba = [0, 0, 0];
|
|
217
|
+
for (let z = 0; z < size; z++) {
|
|
218
|
+
const pz = (z + 0.5) / size;
|
|
219
|
+
for (let y = 0; y < size; y++) {
|
|
220
|
+
const py = (y + 0.5) / size;
|
|
221
|
+
for (let x = 0; x < size; x++) {
|
|
222
|
+
const px = (x + 0.5) / size;
|
|
223
|
+
let pf = 0.0;
|
|
224
|
+
let amp = 1.0;
|
|
225
|
+
for (let o = 0; o < 5; o++) {
|
|
226
|
+
const P = 4 << o;
|
|
227
|
+
pf += gnoise(px * P, py * P, pz * P, P, g[o] as Float32Array) * amp;
|
|
228
|
+
amp *= 0.5;
|
|
229
|
+
}
|
|
230
|
+
const perlin = Math.min(Math.max(pf * 0.5 + 0.5, 0.0), 1.0);
|
|
231
|
+
// Worley octaves at 2, 4, 8, 16, 32 cells; channel c is the fbm starting at octave c with
|
|
232
|
+
// weights 0.625 / 0.25 / 0.125 (Schneider 2015), so g/b/a share octaves like his detail noise
|
|
233
|
+
gba[0] = gba[1] = gba[2] = 0;
|
|
234
|
+
for (let o = 0; o < 5; o++) {
|
|
235
|
+
const P = 2 << o;
|
|
236
|
+
const wo = worley(px * P, py * P, pz * P, P, w[o] as Float32Array);
|
|
237
|
+
for (let c = 0; c < 3; c++) {
|
|
238
|
+
const k = o - c;
|
|
239
|
+
gba[c] =
|
|
240
|
+
(gba[c] as number) + (k === 0 ? 0.625 : k === 1 ? 0.25 : k === 2 ? 0.125 : 0) * wo;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
// dilate Perlin by the low Worley fbm: remap(perlin, 0, 1, worley, 1) -> billowy, round cells
|
|
244
|
+
const pw = (gba[1] as number) + perlin * (1.0 - (gba[1] as number));
|
|
245
|
+
const i = ((z * size + y) * size + x) * 4;
|
|
246
|
+
out[i] = unorm8(stretch(pw, PW_LO, PW_HI));
|
|
247
|
+
out[i + 1] = unorm8(stretch(gba[0] as number, W_LO, W_HI));
|
|
248
|
+
out[i + 2] = unorm8(stretch(gba[1] as number, W_LO, W_HI));
|
|
249
|
+
out[i + 3] = unorm8(stretch(gba[2] as number, W_LO, W_HI));
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return out;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// vector potential = three gradient-noise fbms; octave amplitudes fall faster than 1/f so the
|
|
257
|
+
// big swirls dominate and the fine octaves only roughen them
|
|
258
|
+
function bakeCurlData(size: number): Uint8Array {
|
|
259
|
+
const { CURL_SCALE } = NOISE_TUNING;
|
|
260
|
+
const tables = Array.from({ length: 3 }, (_, o) => grad3Table(4 << o, 11));
|
|
261
|
+
const out = new Uint8Array(size * size * size * 4);
|
|
262
|
+
const J = new Float64Array(9); // column k = grad of potential k
|
|
263
|
+
for (let z = 0; z < size; z++)
|
|
264
|
+
for (let y = 0; y < size; y++)
|
|
265
|
+
for (let x = 0; x < size; x++) {
|
|
266
|
+
const px = (x + 0.5) / size;
|
|
267
|
+
const py = (y + 0.5) / size;
|
|
268
|
+
const pz = (z + 0.5) / size;
|
|
269
|
+
J.fill(0);
|
|
270
|
+
let amp = 1.0;
|
|
271
|
+
for (let o = 0; o < 3; o++) {
|
|
272
|
+
const P = 4 << o;
|
|
273
|
+
gnoiseGrad3(px * P, py * P, pz * P, P, tables[o] as Float32Array, J, amp); // d/dp = d/dx * P; amplitude 1/P -> amp
|
|
274
|
+
amp *= 0.6;
|
|
275
|
+
}
|
|
276
|
+
const cx = ((J[7] as number) - (J[5] as number)) * CURL_SCALE; // J[2].y - J[1].z
|
|
277
|
+
const cy = ((J[2] as number) - (J[6] as number)) * CURL_SCALE; // J[0].z - J[2].x
|
|
278
|
+
const cz = ((J[3] as number) - (J[1] as number)) * CURL_SCALE; // J[1].x - J[0].y
|
|
279
|
+
const i = ((z * size + y) * size + x) * 4;
|
|
280
|
+
out[i] = unorm8(cx * 0.5 + 0.5);
|
|
281
|
+
out[i + 1] = unorm8(cy * 0.5 + 0.5);
|
|
282
|
+
out[i + 2] = unorm8(cz * 0.5 + 0.5);
|
|
283
|
+
out[i + 3] = 255;
|
|
284
|
+
}
|
|
285
|
+
return out;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function bake3D(data: Uint8Array, size: number): Data3DTexture {
|
|
289
|
+
const tex = new Data3DTexture(data, size, size, size);
|
|
290
|
+
tex.format = RGBAFormat;
|
|
291
|
+
tex.minFilter = tex.magFilter = LinearFilter;
|
|
292
|
+
tex.wrapS = tex.wrapT = tex.wrapR = RepeatWrapping;
|
|
293
|
+
tex.generateMipmaps = false;
|
|
294
|
+
tex.unpackAlignment = 1;
|
|
295
|
+
tex.needsUpdate = true;
|
|
296
|
+
return tex;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// --- blackbody (CPU) -------------------------------------------------------------------
|
|
300
|
+
const LAMBDA0 = 360;
|
|
301
|
+
const LAMBDA_STEP = 5;
|
|
302
|
+
const LAMBDA_N = 95; // 360..830 nm
|
|
303
|
+
const cmf = new Float64Array(LAMBDA_N * 3);
|
|
304
|
+
{
|
|
305
|
+
// Wyman, Sloan & Shirley 2013, multi-lobe piecewise-Gaussian fit to CIE 1931 2-degree
|
|
306
|
+
const g = (x: number, mu: number, s1: number, s2: number) => {
|
|
307
|
+
const t = (x - mu) / (x < mu ? s1 : s2);
|
|
308
|
+
return Math.exp(-0.5 * t * t);
|
|
309
|
+
};
|
|
310
|
+
for (let i = 0; i < LAMBDA_N; i++) {
|
|
311
|
+
const l = LAMBDA0 + i * LAMBDA_STEP;
|
|
312
|
+
cmf[i * 3] =
|
|
313
|
+
1.056 * g(l, 599.8, 37.9, 31.0) +
|
|
314
|
+
0.362 * g(l, 442.0, 16.0, 26.7) -
|
|
315
|
+
0.065 * g(l, 501.1, 20.4, 26.2);
|
|
316
|
+
cmf[i * 3 + 1] = 0.821 * g(l, 568.8, 46.9, 40.5) + 0.286 * g(l, 530.9, 16.3, 31.1);
|
|
317
|
+
cmf[i * 3 + 2] = 1.217 * g(l, 437.0, 11.8, 36.0) + 0.681 * g(l, 459.0, 26.0, 13.8);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Planck spectrum against the CMFs -> XYZ / Y -> linear sRGB (D65), negatives clipped,
|
|
322
|
+
// then rescaled so Rec.709 luminance is exactly 1 again
|
|
323
|
+
function planckRGB(kelvin: number, out: Float32Array, o: number): void {
|
|
324
|
+
const c2 = 1.4387769e7; // hc / k in nm K
|
|
325
|
+
let X = 0;
|
|
326
|
+
let Y = 0;
|
|
327
|
+
let Z = 0;
|
|
328
|
+
for (let i = 0; i < LAMBDA_N; i++) {
|
|
329
|
+
const l = LAMBDA0 + i * LAMBDA_STEP;
|
|
330
|
+
const b = 1 / (l ** 5 * (Math.exp(c2 / (l * kelvin)) - 1));
|
|
331
|
+
X += b * (cmf[i * 3] as number);
|
|
332
|
+
Y += b * (cmf[i * 3 + 1] as number);
|
|
333
|
+
Z += b * (cmf[i * 3 + 2] as number);
|
|
334
|
+
}
|
|
335
|
+
X /= Y;
|
|
336
|
+
Z /= Y;
|
|
337
|
+
Y = 1;
|
|
338
|
+
const r = Math.max(0, 3.2404542 * X - 1.5371385 * Y - 0.4985314 * Z);
|
|
339
|
+
const g = Math.max(0, -0.969266 * X + 1.8760108 * Y + 0.041556 * Z);
|
|
340
|
+
const b = Math.max(0, 0.0556434 * X - 0.2040259 * Y + 1.0572252 * Z);
|
|
341
|
+
const lum = 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
342
|
+
out[o] = r / lum;
|
|
343
|
+
out[o + 1] = g / lum;
|
|
344
|
+
out[o + 2] = b / lum;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const BB_N = 256;
|
|
348
|
+
const bbLUT = new Float32Array(BB_N * 3); // CPU copy, texel centres at u = (i + 0.5) / 256
|
|
349
|
+
const kelvinAt = (u: number) => 500 * 40 ** u;
|
|
350
|
+
for (let i = 0; i < BB_N; i++) planckRGB(kelvinAt((i + 0.5) / BB_N), bbLUT, i * 3);
|
|
351
|
+
|
|
352
|
+
const lut = (i: number): number => bbLUT[i] as number;
|
|
353
|
+
// same mapping and linear filtering as blackbody() in NOISE_GLSL (minus fp16 rounding)
|
|
354
|
+
export function blackbodyRGB(kelvin: number, outColor = new Color()): Color {
|
|
355
|
+
const u = Math.min(1, Math.max(0, Math.log(kelvin / 500) / Math.log(40)));
|
|
356
|
+
const x = Math.min(BB_N - 1, Math.max(0, u * BB_N - 0.5));
|
|
357
|
+
const i = Math.min(BB_N - 2, Math.floor(x));
|
|
358
|
+
const t = x - i;
|
|
359
|
+
const a = i * 3;
|
|
360
|
+
const b = a + 3;
|
|
361
|
+
return outColor.setRGB(
|
|
362
|
+
lut(a) + (lut(b) - lut(a)) * t,
|
|
363
|
+
lut(a + 1) + (lut(b + 1) - lut(a + 1)) * t,
|
|
364
|
+
lut(a + 2) + (lut(b + 2) - lut(a + 2)) * t,
|
|
365
|
+
LinearSRGBColorSpace,
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function makeBlackbodyTexture(): DataTexture {
|
|
370
|
+
const data = new Uint16Array(BB_N * 4);
|
|
371
|
+
for (let i = 0; i < BB_N; i++) {
|
|
372
|
+
for (let c = 0; c < 3; c++) data[i * 4 + c] = DataUtils.toHalfFloat(lut(i * 3 + c));
|
|
373
|
+
data[i * 4 + 3] = DataUtils.toHalfFloat(1);
|
|
374
|
+
}
|
|
375
|
+
const tex = new DataTexture(data, BB_N, 1, RGBAFormat, HalfFloatType);
|
|
376
|
+
tex.minFilter = tex.magFilter = LinearFilter;
|
|
377
|
+
tex.needsUpdate = true;
|
|
378
|
+
return tex;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// throws on failure; returns a one-line summary
|
|
382
|
+
export function blackbodySelfCheck(): string {
|
|
383
|
+
const assert = (ok: boolean, msg: string) => {
|
|
384
|
+
if (!ok) throw new Error(`blackbody self-check: ${msg}`);
|
|
385
|
+
};
|
|
386
|
+
const c = new Color();
|
|
387
|
+
const w = blackbodyRGB(6500, c).toArray();
|
|
388
|
+
assert(
|
|
389
|
+
Math.max(...w) / Math.min(...w) < 1.15,
|
|
390
|
+
`6500 K not near white (${w.map((x) => x.toFixed(3))})`,
|
|
391
|
+
);
|
|
392
|
+
const o = blackbodyRGB(1900, new Color()).toArray();
|
|
393
|
+
assert(
|
|
394
|
+
(o[0] as number) > (o[1] as number) && (o[1] as number) > (o[2] as number),
|
|
395
|
+
`1900 K not orange (${o.map((x) => x.toFixed(3))})`,
|
|
396
|
+
);
|
|
397
|
+
let prev = -1;
|
|
398
|
+
for (let i = 0; i < BB_N; i++) {
|
|
399
|
+
const r = lut(i * 3);
|
|
400
|
+
const g = lut(i * 3 + 1);
|
|
401
|
+
const b = lut(i * 3 + 2);
|
|
402
|
+
assert(
|
|
403
|
+
Math.abs(0.2126 * r + 0.7152 * g + 0.0722 * b - 1) < 1e-6,
|
|
404
|
+
`luminance != 1 at texel ${i}`,
|
|
405
|
+
);
|
|
406
|
+
const hue = (b + 1e-6) / (r + 1e-6); // blue/red rises monotonically with temperature
|
|
407
|
+
assert(hue >= prev, `hue not monotone at texel ${i}`);
|
|
408
|
+
prev = hue;
|
|
409
|
+
}
|
|
410
|
+
return `blackbody ok: 6500K (${w.map((x) => x.toFixed(3))}), 1900K (${o.map((x) => x.toFixed(3))})`;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// --- bake -------------------------------------------------------------------------------
|
|
414
|
+
export interface IBakedNoise {
|
|
415
|
+
noise3D: Data3DTexture;
|
|
416
|
+
curl3D: Data3DTexture;
|
|
417
|
+
blackbody: DataTexture;
|
|
418
|
+
ms: number;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// Call once, before any material that samples noise is built: TSL reads U.uNoise3D/uCurl3D/uBlackbody
|
|
422
|
+
// .value when the material's graph is made (upstream's GLSL bound them as uniforms at draw).
|
|
423
|
+
export function bakeNoise({ noiseSize = 128 }: { noiseSize?: number } = {}): IBakedNoise {
|
|
424
|
+
const now = () => globalThis.performance?.now() ?? Date.now();
|
|
425
|
+
const t0 = now();
|
|
426
|
+
const noise = bake3D(bakeNoiseData(noiseSize), noiseSize);
|
|
427
|
+
const tNoise = now() - t0;
|
|
428
|
+
const curl = bake3D(bakeCurlData(64), 64);
|
|
429
|
+
const blackbody = makeBlackbodyTexture();
|
|
430
|
+
const ms = now() - t0;
|
|
431
|
+
U.uNoise3D.value = noise;
|
|
432
|
+
U.uCurl3D.value = curl;
|
|
433
|
+
U.uBlackbody.value = blackbody;
|
|
434
|
+
console.info(
|
|
435
|
+
`noise bake (CPU): ${noiseSize}^3 noise + 64^3 curl + blackbody in ${ms.toFixed(1)} ms (noise ${tNoise.toFixed(1)}, curl ${(ms - tNoise).toFixed(1)})`,
|
|
436
|
+
);
|
|
437
|
+
return { noise3D: noise, curl3D: curl, blackbody, ms };
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// exposed for the bake test (same maths as the GPU programs upstream)
|
|
441
|
+
export const __bake = { bakeNoiseData, bakeCurlData };
|