@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.
- 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 +10565 -22533
- 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-camera-cull.ts +32 -1
- 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,1195 @@
|
|
|
1
|
+
// Ported from Ultra Elements src/vfx/common.js @698b720 (MIT, Copyright (c) 2026 Humpalumps). JavaScript copied verbatim; GLSL transliterated to TSL.
|
|
2
|
+
//
|
|
3
|
+
// How the GLSL chunks map to TSL, for every module ported after this one:
|
|
4
|
+
// - each GLSL chunk (COMMON_GLSL, NOISE_GLSL, ...) is a set of exported TSL functions with the GLSL
|
|
5
|
+
// function's own name (hash11, noise3, blackbody, spellLightScatter, fxOut, ...). TSL Fns inline at
|
|
6
|
+
// the call site, so a GLSL `out`/`inout` parameter is a var node the caller passes in
|
|
7
|
+
// (`const s = float(0).toVar(); segSegDist(p1, q1, p2, q2, s)`), and a GLSL struct is a plain
|
|
8
|
+
// object of nodes (spellLight(i)).
|
|
9
|
+
// - U keeps upstream's shape: every scalar/vector entry is a TSL uniform node, whose `.value` is the
|
|
10
|
+
// same object upstream's `{ value }` held, so `U.uTime.value = t` is unchanged JS. Texture entries
|
|
11
|
+
// stay `{ value: Texture }` holders read when a material's graph is built (bake noise first).
|
|
12
|
+
// - GLSL globals a shader sets (gLightNoShadow, gVolDt) become build-time arguments or vars.
|
|
13
|
+
// What the engine forces (reported, not hidden):
|
|
14
|
+
// - no matrix uniforms: a mat3 uniform is three vec3 row uniforms (Volumes' uVolRot).
|
|
15
|
+
// - no CustomBlending and no multi-target render target: see FX_BLENDING and fxOut below.
|
|
16
|
+
// - uSceneDepth is the engine's viewportDepthTexture() (the frame's depth as drawn before the FX).
|
|
17
|
+
import {
|
|
18
|
+
AddEquation,
|
|
19
|
+
AdditiveBlending,
|
|
20
|
+
Color,
|
|
21
|
+
CustomBlending,
|
|
22
|
+
Data3DTexture,
|
|
23
|
+
DataTexture,
|
|
24
|
+
FloatType,
|
|
25
|
+
type Matrix3,
|
|
26
|
+
NearestFilter,
|
|
27
|
+
NormalBlending,
|
|
28
|
+
OneFactor,
|
|
29
|
+
OneMinusSrcAlphaFactor,
|
|
30
|
+
RGBAFormat,
|
|
31
|
+
Vector2,
|
|
32
|
+
Vector3,
|
|
33
|
+
Vector4,
|
|
34
|
+
ZeroFactor,
|
|
35
|
+
} from "three";
|
|
36
|
+
import {
|
|
37
|
+
Break,
|
|
38
|
+
Continue,
|
|
39
|
+
Fn,
|
|
40
|
+
If,
|
|
41
|
+
Loop,
|
|
42
|
+
PI,
|
|
43
|
+
abs,
|
|
44
|
+
atan,
|
|
45
|
+
clamp,
|
|
46
|
+
cos,
|
|
47
|
+
cross,
|
|
48
|
+
distance,
|
|
49
|
+
dot,
|
|
50
|
+
exp,
|
|
51
|
+
float,
|
|
52
|
+
fract,
|
|
53
|
+
int,
|
|
54
|
+
ivec2,
|
|
55
|
+
length,
|
|
56
|
+
log,
|
|
57
|
+
mat3,
|
|
58
|
+
max,
|
|
59
|
+
min,
|
|
60
|
+
mix,
|
|
61
|
+
mod,
|
|
62
|
+
normalize,
|
|
63
|
+
pow,
|
|
64
|
+
reflect,
|
|
65
|
+
screenUV,
|
|
66
|
+
select,
|
|
67
|
+
sin,
|
|
68
|
+
smoothstep,
|
|
69
|
+
sqrt,
|
|
70
|
+
step,
|
|
71
|
+
texture,
|
|
72
|
+
texture3D,
|
|
73
|
+
textureLoad,
|
|
74
|
+
uniform,
|
|
75
|
+
vec2,
|
|
76
|
+
vec3,
|
|
77
|
+
vec4,
|
|
78
|
+
viewportDepthTexture,
|
|
79
|
+
viewportSharedTexture,
|
|
80
|
+
} from "three/tsl";
|
|
81
|
+
import type { Node } from "three/webgpu";
|
|
82
|
+
|
|
83
|
+
type F = Node<"float">;
|
|
84
|
+
type V2 = Node<"vec2">;
|
|
85
|
+
type V3 = Node<"vec3">;
|
|
86
|
+
type V4 = Node<"vec4">;
|
|
87
|
+
type I = Node<"int">;
|
|
88
|
+
|
|
89
|
+
// The shared world every spell, sim and material sees. One file so the GLSL and its JS mirror
|
|
90
|
+
// stay side by side:
|
|
91
|
+
// - ARENA: the fixed layout (floor, rim wall, pillars, dummies, pool, caster focus)
|
|
92
|
+
// - U: uniforms shared by reference across every material ({ ...U, own uniforms })
|
|
93
|
+
// - the world texture: lights, forces, impacts and capsule occluders packed into one 64x4
|
|
94
|
+
// RGBA32F DataTexture, uploaded once per frame and read with texelFetch (no uniform-array
|
|
95
|
+
// limits, no per-material uploads)
|
|
96
|
+
// - GLSL chunks to read all of the above, and arenaSDF()/arenaRaycast() mirrored in JS
|
|
97
|
+
//
|
|
98
|
+
// Conventions: metres, y up, floor at y = 0, linear HDR. Spells never touch the texture;
|
|
99
|
+
// they hold handles from lights/forces/impacts/occluders and set plain fields.
|
|
100
|
+
|
|
101
|
+
// --- arena layout -------------------------------------------------------------------
|
|
102
|
+
const pillarAngles = Array.from({ length: 8 }, (_, i) => ((i + 0.5) * Math.PI) / 4);
|
|
103
|
+
export const ARENA = {
|
|
104
|
+
radius: 18, // inner face of the rim wall
|
|
105
|
+
wallThickness: 0.6,
|
|
106
|
+
wallHeight: 1.2,
|
|
107
|
+
pillarRing: 14,
|
|
108
|
+
pillarRadius: 0.6,
|
|
109
|
+
pillarHeight: 6,
|
|
110
|
+
pillars: pillarAngles.map((a) => new Vector3(Math.cos(a) * 14, 0, Math.sin(a) * 14)),
|
|
111
|
+
focus: new Vector3(0, 1.6, 12), // caster's focus orb, on a pedestal
|
|
112
|
+
pedestal: { pos: new Vector3(0, 0, 12), radius: 0.35, height: 1.15 },
|
|
113
|
+
dummies: [new Vector3(0, 0, 2), new Vector3(-3.5, 0, -2), new Vector3(3.5, 0, -6)],
|
|
114
|
+
dummyRadius: 0.35, // capsule (x, 0.35, z) -> (x, 1.55, z)
|
|
115
|
+
dummyHeight: 1.9,
|
|
116
|
+
pool: { center: new Vector2(9.75, 0), half: new Vector2(2.25, 4.5), depth: 0.15, water: -0.03 },
|
|
117
|
+
ground: { min: new Vector2(-20, -20), size: 40 }, // ground field covers this square
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// --- shared uniforms ----------------------------------------------------------------
|
|
121
|
+
export const WORLD_W = 64;
|
|
122
|
+
export const MAX_LIGHTS = 16,
|
|
123
|
+
MAX_FORCES = 16,
|
|
124
|
+
MAX_IMPACTS = 16,
|
|
125
|
+
MAX_OCCLUDERS = 32;
|
|
126
|
+
const worldData = new Float32Array(WORLD_W * 4 * 4);
|
|
127
|
+
const worldTex = new DataTexture(worldData, WORLD_W, 4, RGBAFormat, FloatType);
|
|
128
|
+
worldTex.magFilter = worldTex.minFilter = NearestFilter;
|
|
129
|
+
worldTex.needsUpdate = true;
|
|
130
|
+
|
|
131
|
+
const placeholder3D = new Data3DTexture(new Uint8Array([128, 128, 128, 128]), 1, 1, 1);
|
|
132
|
+
placeholder3D.needsUpdate = true;
|
|
133
|
+
const placeholder2D = new DataTexture(new Uint8Array([0, 0, 0, 0]), 1, 1);
|
|
134
|
+
placeholder2D.needsUpdate = true;
|
|
135
|
+
|
|
136
|
+
export const U = {
|
|
137
|
+
uTime: uniform(0), // seconds, scaled by time scale, wraps at 3600
|
|
138
|
+
uDt: uniform(1 / 60),
|
|
139
|
+
uFrame: uniform(0),
|
|
140
|
+
uWorld: { value: worldTex },
|
|
141
|
+
uWorldCounts: uniform(new Vector4()), // lights, forces, impacts, occluders
|
|
142
|
+
uNoise3D: { value: placeholder3D }, // noise.js: 128^3 RGBA8 tileable (r perlin-worley fbm, gba worley f2/f4/f8)
|
|
143
|
+
uCurl3D: { value: placeholder3D }, // noise.js: 64^3 RGBA8 tileable curl, rgb = curl * 0.5 + 0.5
|
|
144
|
+
uBlackbody: { value: placeholder2D }, // noise.js: 256x1 RGBA16F, see blackbody() below for the mapping
|
|
145
|
+
// its own placeholder: GroundField moves a holder's shared texture node, so A, B and uBlackbody must not share one
|
|
146
|
+
uGroundA: { value: placeholder2D.clone() }, // GroundField: heat, char, wet, frost
|
|
147
|
+
uGroundB: { value: placeholder2D.clone() }, // GroundField: crack, acid, corruption, glow
|
|
148
|
+
uGroundRect: uniform(
|
|
149
|
+
new Vector4(ARENA.ground.min.x, ARENA.ground.min.y, ARENA.ground.size, 1 / ARENA.ground.size),
|
|
150
|
+
),
|
|
151
|
+
uSceneDepth: { value: null }, // engine: unused, sceneViewZ() reads viewportDepthTexture()
|
|
152
|
+
uSceneColor: { value: null }, // engine: unused, a refraction material reads viewportSharedTexture()
|
|
153
|
+
uResolution: uniform(new Vector2(1, 1)),
|
|
154
|
+
uFxResolution: uniform(new Vector2(1, 1)),
|
|
155
|
+
uNearFar: uniform(new Vector2(0.1, 400)),
|
|
156
|
+
uSunDir: uniform(new Vector3(0, 1, 0)),
|
|
157
|
+
uSunColor: uniform(new Vector3(1, 1, 1)), // radiance scale, as UW's sky.sunColor
|
|
158
|
+
uAmbient: uniform(new Vector3(0.1, 0.1, 0.12)), // average sky irradiance / pi, for particles
|
|
159
|
+
uSkyCube: { value: null },
|
|
160
|
+
uDebug: uniform(0), // debug view id (0 = off), read by FX materials for overdraw
|
|
161
|
+
// FxTarget.params as fxOut reads them: smoke occludes glow, distortion scale, CA, - (engine: the
|
|
162
|
+
// composite runs inside fxOut, see FX_BLENDING)
|
|
163
|
+
uFxParams: uniform(new Vector4(0.5, 1, 0.35, 0)),
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
// --- GLSL -> TSL ---------------------------------------------------------------------
|
|
167
|
+
// COMMON_GLSL
|
|
168
|
+
// Hash without Sine (Dave Hoskins, MIT)
|
|
169
|
+
export const hash11 = Fn(([p0]: [F]) => {
|
|
170
|
+
const p = fract(p0.mul(0.1031)).toVar();
|
|
171
|
+
p.mulAssign(p.add(33.33));
|
|
172
|
+
p.mulAssign(p.add(p));
|
|
173
|
+
return fract(p);
|
|
174
|
+
});
|
|
175
|
+
export const hash12 = Fn(([p]: [V2]) => {
|
|
176
|
+
const q = fract(vec3(p.x, p.y, p.x).mul(0.1031));
|
|
177
|
+
const p3 = q.add(dot(q, q.yzx.add(33.33)));
|
|
178
|
+
return fract(p3.x.add(p3.y).mul(p3.z));
|
|
179
|
+
});
|
|
180
|
+
export const hash21 = Fn(([p]: [F]) => {
|
|
181
|
+
const q = fract(vec3(p).mul(vec3(0.1031, 0.103, 0.0973)));
|
|
182
|
+
const p3 = q.add(dot(q, q.yzx.add(33.33)));
|
|
183
|
+
return fract(p3.xx.add(p3.yz).mul(p3.zy));
|
|
184
|
+
});
|
|
185
|
+
export const hash31 = Fn(([p]: [F]) => {
|
|
186
|
+
const q = fract(vec3(p).mul(vec3(0.1031, 0.103, 0.0973)));
|
|
187
|
+
const p3 = q.add(dot(q, q.yzx.add(33.33)));
|
|
188
|
+
return fract(p3.xxy.add(p3.yzz).mul(p3.zyx));
|
|
189
|
+
});
|
|
190
|
+
export const hash33 = Fn(([p]: [V3]) => {
|
|
191
|
+
const q = fract(p.mul(vec3(0.1031, 0.103, 0.0973)));
|
|
192
|
+
const p3 = q.add(dot(q, q.yxz.add(33.33)));
|
|
193
|
+
return fract(p3.xxy.add(p3.yxx).mul(p3.zyx));
|
|
194
|
+
});
|
|
195
|
+
export const hash41 = Fn(([p]: [F]) => {
|
|
196
|
+
const q = fract(vec4(p).mul(vec4(0.1031, 0.103, 0.0973, 0.1099)));
|
|
197
|
+
const p4 = q.add(dot(q, q.wzxy.add(33.33)));
|
|
198
|
+
return fract(p4.xxyz.add(p4.yzzw).mul(p4.zywx));
|
|
199
|
+
});
|
|
200
|
+
// interleaved gradient noise (Jimenez 2014), rotated per frame
|
|
201
|
+
export const ign = Fn(([px]: [V2]) => {
|
|
202
|
+
const q = px.add(mod(U.uFrame, 64.0).mul(5.588238));
|
|
203
|
+
return fract(fract(dot(q, vec2(0.06711056, 0.00583715))).mul(52.9829189));
|
|
204
|
+
});
|
|
205
|
+
export const linearDepth = Fn(([d]: [F]) =>
|
|
206
|
+
U.uNearFar.x.mul(U.uNearFar.y).div(U.uNearFar.y.sub(d.mul(U.uNearFar.y.sub(U.uNearFar.x)))),
|
|
207
|
+
);
|
|
208
|
+
export const randomInSphere = Fn(([r]: [V3]) => {
|
|
209
|
+
const z = r.x.mul(2.0).sub(1.0);
|
|
210
|
+
const a = r.y.mul(2.0 * Math.PI);
|
|
211
|
+
const s = sqrt(float(1.0).sub(z.mul(z)));
|
|
212
|
+
return vec3(s.mul(cos(a)), z, s.mul(sin(a))).mul(pow(r.z, 1.0 / 3.0));
|
|
213
|
+
});
|
|
214
|
+
export const randomOnSphere = Fn(([r]: [V2]) => {
|
|
215
|
+
const z = r.x.mul(2.0).sub(1.0);
|
|
216
|
+
const a = r.y.mul(2.0 * Math.PI);
|
|
217
|
+
const s = sqrt(float(1.0).sub(z.mul(z)));
|
|
218
|
+
return vec3(s.mul(cos(a)), z, s.mul(sin(a)));
|
|
219
|
+
});
|
|
220
|
+
export const basisFrom = Fn(([n]: [V3]) => {
|
|
221
|
+
const t = select(abs(n.y).lessThan(0.99), normalize(cross(n, vec3(0, 1, 0))), vec3(1, 0, 0));
|
|
222
|
+
return mat3(t, cross(t, n), n);
|
|
223
|
+
});
|
|
224
|
+
export const hgPhase = Fn(([mu, g]: [F, F]) => {
|
|
225
|
+
const g2 = g.mul(g);
|
|
226
|
+
return float(1.0)
|
|
227
|
+
.sub(g2)
|
|
228
|
+
.div(pow(float(1.0).add(g2).sub(g.mul(2.0).mul(mu)), 1.5).mul(4.0 * Math.PI));
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// NOISE_GLSL. Every lookup is LOD 0 (the noise, curl and blackbody textures have no mips), which is
|
|
232
|
+
// also what WGSL needs to sample inside the data-dependent loops of a march.
|
|
233
|
+
// Engine: each texture()/texture3D() call is its own binding (a fragment stage holds 16), so every
|
|
234
|
+
// texture is one shared node, built on first use, and each lookup is `.sample(uv)` of it (GLSL's
|
|
235
|
+
// one `uniform sampler`).
|
|
236
|
+
const shared = new Map<object, Node>();
|
|
237
|
+
function textureNode(tex: object, make: () => Node): Node {
|
|
238
|
+
let node = shared.get(tex);
|
|
239
|
+
if (node === undefined) {
|
|
240
|
+
node = make();
|
|
241
|
+
shared.set(tex, node);
|
|
242
|
+
}
|
|
243
|
+
return node;
|
|
244
|
+
}
|
|
245
|
+
const sample3 = (tex: Data3DTexture, p: V3): V4 =>
|
|
246
|
+
(textureNode(tex, () => texture3D(tex)) as ReturnType<typeof texture3D>)
|
|
247
|
+
.sample(p)
|
|
248
|
+
.level(float(0)) as unknown as V4;
|
|
249
|
+
const sample2 = (tex: DataTexture, uv: V2, lod = true): V4 => {
|
|
250
|
+
const node = (textureNode(tex, () => texture(tex)) as ReturnType<typeof texture>).sample(uv);
|
|
251
|
+
return (lod ? node.level(float(0)) : node) as unknown as V4;
|
|
252
|
+
};
|
|
253
|
+
// p in tile units (one tile = one repeat of the texture)
|
|
254
|
+
export const noise3v = Fn(([p]: [V3]) => sample3(U.uNoise3D.value, p));
|
|
255
|
+
export const noise3 = Fn(([p]: [V3]) => sample3(U.uNoise3D.value, p).r);
|
|
256
|
+
export const worley3 = Fn(([p]: [V3]) => sample3(U.uNoise3D.value, p).g);
|
|
257
|
+
export const curl3 = Fn(([p]: [V3]) => sample3(U.uCurl3D.value, p).rgb.mul(2.0).sub(1.0));
|
|
258
|
+
// two-octave fbm from the baked fbm channel (already 4 octaves deep)
|
|
259
|
+
export const fbm3 = Fn(([p]: [V3]) =>
|
|
260
|
+
sample3(U.uNoise3D.value, p)
|
|
261
|
+
.r.mul(0.7)
|
|
262
|
+
.add(sample3(U.uNoise3D.value, p.mul(2.63).add(0.37)).r.mul(0.3)),
|
|
263
|
+
);
|
|
264
|
+
// Blackbody chromaticity in linear sRGB, normalised to luminance 1. u = log(K / 500) / log(40),
|
|
265
|
+
// i.e. 500 K .. 20000 K logarithmically. Intensity is a separate artist curve.
|
|
266
|
+
export const blackbody = Fn(
|
|
267
|
+
([kelvin]: [F]) =>
|
|
268
|
+
sample2(
|
|
269
|
+
U.uBlackbody.value,
|
|
270
|
+
vec2(clamp(log(kelvin.div(500.0)).div(Math.log(40.0)), 0.0, 1.0), 0.5),
|
|
271
|
+
).rgb,
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
// WORLD_GLSL
|
|
275
|
+
export const worldTexel = Fn(([row, i]: [I, I]) =>
|
|
276
|
+
textureLoad(
|
|
277
|
+
textureNode(U.uWorld.value, () => texture(U.uWorld.value)) as Parameters<typeof textureLoad>[0],
|
|
278
|
+
ivec2(i, row),
|
|
279
|
+
int(0),
|
|
280
|
+
),
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
// ARENA_GLSL: floor (pool recess), rim wall ring, then every capsule in the occluder list
|
|
284
|
+
export const sdCapsule = Fn(([p, a, b, r]: [V3, V3, V3, F]) => {
|
|
285
|
+
const pa = p.sub(a);
|
|
286
|
+
const ba = b.sub(a);
|
|
287
|
+
const h = clamp(dot(pa, ba).div(max(dot(ba, ba), 1e-6)), 0.0, 1.0);
|
|
288
|
+
return length(pa.sub(ba.mul(h))).sub(r);
|
|
289
|
+
});
|
|
290
|
+
export const arenaStatic = Fn(([p]: [V3]) => {
|
|
291
|
+
const pq = abs(p.xz.sub(vec2(ARENA.pool.center.x, ARENA.pool.center.y)));
|
|
292
|
+
const floorY = select(
|
|
293
|
+
pq.x.lessThan(ARENA.pool.half.x).and(pq.y.lessThan(ARENA.pool.half.y)),
|
|
294
|
+
float(ARENA.pool.water),
|
|
295
|
+
float(0.0),
|
|
296
|
+
);
|
|
297
|
+
const d = p.y.sub(floorY);
|
|
298
|
+
const q = vec2(
|
|
299
|
+
abs(length(p.xz).sub(ARENA.radius + ARENA.wallThickness / 2)).sub(ARENA.wallThickness / 2),
|
|
300
|
+
abs(p.y.sub(ARENA.wallHeight / 2)).sub(ARENA.wallHeight / 2),
|
|
301
|
+
);
|
|
302
|
+
return min(d, length(max(q, 0.0)).add(min(max(q.x, q.y), 0.0)));
|
|
303
|
+
});
|
|
304
|
+
export const arenaSDF = Fn(([p]: [V3]) => {
|
|
305
|
+
const d = arenaStatic(p).toVar();
|
|
306
|
+
const n = int(U.uWorldCounts.w);
|
|
307
|
+
Loop({ start: 0, end: MAX_OCCLUDERS, type: "int", condition: "<" }, ({ i }: { i: I }) => {
|
|
308
|
+
If(i.greaterThanEqual(n), () => {
|
|
309
|
+
Break();
|
|
310
|
+
});
|
|
311
|
+
const a = worldTexel(int(3), i.mul(2));
|
|
312
|
+
const b = worldTexel(int(3), i.mul(2).add(1));
|
|
313
|
+
d.assign(min(d, sdCapsule(p, a.xyz, b.xyz, a.w)));
|
|
314
|
+
});
|
|
315
|
+
return d;
|
|
316
|
+
});
|
|
317
|
+
export const arenaNormal = Fn(([p]: [V3]) => {
|
|
318
|
+
const e = 0.01;
|
|
319
|
+
return normalize(
|
|
320
|
+
vec3(
|
|
321
|
+
arenaSDF(p.add(vec3(e, 0, 0))).sub(arenaSDF(p.sub(vec3(e, 0, 0)))),
|
|
322
|
+
arenaSDF(p.add(vec3(0, e, 0))).sub(arenaSDF(p.sub(vec3(0, e, 0)))),
|
|
323
|
+
arenaSDF(p.add(vec3(0, 0, e))).sub(arenaSDF(p.sub(vec3(0, 0, e)))),
|
|
324
|
+
),
|
|
325
|
+
);
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
// LIGHTS_GLSL
|
|
329
|
+
// Spell lights: point (type 0), line (1, segment pos -> end), cookie (2, point light with a
|
|
330
|
+
// projected pattern: cookie.x = 1 caustic, 2 rune ring; cookie.y scale, cookie.z rotation).
|
|
331
|
+
// Karis 2013 windowed inverse-square falloff; line lights use the representative point. The
|
|
332
|
+
// first lights flagged shadow (the CPU keeps at most Q.shadowLights of them) get analytic
|
|
333
|
+
// capsule shadows from the occluder list (UE4 capsule shadows), penumbra growing with distance.
|
|
334
|
+
// gLightNoShadow (set true in volume/media shaders: capsule shadows per step cost too much) is the
|
|
335
|
+
// build-time `noShadow` argument here.
|
|
336
|
+
export interface ISpellLight {
|
|
337
|
+
pos: V3;
|
|
338
|
+
range: F;
|
|
339
|
+
color: V3;
|
|
340
|
+
type: I;
|
|
341
|
+
end: V3;
|
|
342
|
+
shadow: F;
|
|
343
|
+
cookie: V4;
|
|
344
|
+
}
|
|
345
|
+
export function spellLight(i: I): ISpellLight {
|
|
346
|
+
const a = worldTexel(int(0), i.mul(4));
|
|
347
|
+
const b = worldTexel(int(0), i.mul(4).add(1));
|
|
348
|
+
const c = worldTexel(int(0), i.mul(4).add(2));
|
|
349
|
+
const d = worldTexel(int(0), i.mul(4).add(3));
|
|
350
|
+
return {
|
|
351
|
+
pos: a.xyz,
|
|
352
|
+
range: a.w,
|
|
353
|
+
color: b.rgb,
|
|
354
|
+
type: int(b.a),
|
|
355
|
+
end: c.xyz,
|
|
356
|
+
shadow: c.w,
|
|
357
|
+
cookie: d,
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
export const lightFalloff = Fn(([d, range]: [F, F]) => {
|
|
361
|
+
const x = d.div(range).toVar();
|
|
362
|
+
x.assign(x.mul(x));
|
|
363
|
+
x.assign(x.mul(x));
|
|
364
|
+
const w = clamp(float(1.0).sub(x), 0.0, 1.0);
|
|
365
|
+
return w.mul(w).div(d.mul(d).add(1.0));
|
|
366
|
+
});
|
|
367
|
+
export const closestOnSegment = Fn(([a, b, p]: [V3, V3, V3]) => {
|
|
368
|
+
const ab = b.sub(a);
|
|
369
|
+
return a.add(ab.mul(clamp(dot(p.sub(a), ab).div(max(dot(ab, ab), 1e-6)), 0.0, 1.0)));
|
|
370
|
+
});
|
|
371
|
+
// closest point on segment a-b to the ray o + t*r (t >= 0)
|
|
372
|
+
export const closestOnSegmentToRay = Fn(([a, b, o, r]: [V3, V3, V3, V3]) => {
|
|
373
|
+
const ab = b.sub(a);
|
|
374
|
+
const ao = a.sub(o);
|
|
375
|
+
const abr = dot(ab, r);
|
|
376
|
+
const abab = dot(ab, ab);
|
|
377
|
+
const t = dot(ao, r)
|
|
378
|
+
.mul(abr)
|
|
379
|
+
.sub(dot(ao, ab))
|
|
380
|
+
.div(max(abab.sub(abr.mul(abr)), 1e-5));
|
|
381
|
+
return a.add(ab.mul(clamp(t, 0.0, 1.0)));
|
|
382
|
+
});
|
|
383
|
+
// distance between segments p1-q1 and p2-q2; s = parameter on the first (Ericson 5.1.9). s: out var
|
|
384
|
+
export const segSegDist = Fn(([p1, q1, p2, q2, s]: [V3, V3, V3, V3, F]) => {
|
|
385
|
+
const d1 = q1.sub(p1);
|
|
386
|
+
const d2 = q2.sub(p2);
|
|
387
|
+
const r = p1.sub(p2);
|
|
388
|
+
const a = dot(d1, d1);
|
|
389
|
+
const e = dot(d2, d2);
|
|
390
|
+
const f = dot(d2, r);
|
|
391
|
+
const c = dot(d1, r);
|
|
392
|
+
const b = dot(d1, d2);
|
|
393
|
+
const denom = a.mul(e).sub(b.mul(b));
|
|
394
|
+
s.assign(
|
|
395
|
+
select(denom.greaterThan(1e-6), clamp(b.mul(f).sub(c.mul(e)).div(denom), 0.0, 1.0), float(0.0)),
|
|
396
|
+
);
|
|
397
|
+
const t = b.mul(s).add(f).div(max(e, 1e-6)).toVar();
|
|
398
|
+
If(t.lessThan(0.0), () => {
|
|
399
|
+
t.assign(0.0);
|
|
400
|
+
s.assign(clamp(c.negate().div(max(a, 1e-6)), 0.0, 1.0));
|
|
401
|
+
}).ElseIf(t.greaterThan(1.0), () => {
|
|
402
|
+
t.assign(1.0);
|
|
403
|
+
s.assign(clamp(b.sub(c).div(max(a, 1e-6)), 0.0, 1.0));
|
|
404
|
+
});
|
|
405
|
+
return length(p1.add(d1.mul(s)).sub(p2.add(d2.mul(t))));
|
|
406
|
+
});
|
|
407
|
+
export const capsuleShadow = Fn(([p0, lp]: [V3, V3]) => {
|
|
408
|
+
const dir = lp.sub(p0);
|
|
409
|
+
const len = length(dir);
|
|
410
|
+
const p = p0.add(dir.mul(min(float(0.15).div(max(len, 1e-3)), 0.5))); // start off the surface (own capsule)
|
|
411
|
+
const vis = float(1.0).toVar();
|
|
412
|
+
const n = int(U.uWorldCounts.w);
|
|
413
|
+
Loop({ start: 0, end: MAX_OCCLUDERS, type: "int", condition: "<" }, ({ i }: { i: I }) => {
|
|
414
|
+
If(i.greaterThanEqual(n), () => {
|
|
415
|
+
Break();
|
|
416
|
+
});
|
|
417
|
+
const a = worldTexel(int(3), i.mul(2));
|
|
418
|
+
const b = worldTexel(int(3), i.mul(2).add(1));
|
|
419
|
+
If(abs(b.w.sub(3.0)).lessThan(0.5), () => {
|
|
420
|
+
Continue();
|
|
421
|
+
}); // shields are glass: they collide but cast no shadow
|
|
422
|
+
// bounding-sphere reject first: most capsules are nowhere near this shadow ray
|
|
423
|
+
const m = a.xyz.add(b.xyz).mul(0.5);
|
|
424
|
+
const q = closestOnSegment(p, lp, m).sub(m);
|
|
425
|
+
const R = length(b.xyz.sub(a.xyz)).mul(0.5).add(a.w).add(0.04).add(len.mul(0.18));
|
|
426
|
+
If(dot(q, q).lessThanEqual(R.mul(R)), () => {
|
|
427
|
+
const s = float(0.0).toVar();
|
|
428
|
+
const d = segSegDist(p, lp, a.xyz, b.xyz, s);
|
|
429
|
+
const pen = s.mul(len).mul(0.18).add(0.04);
|
|
430
|
+
vis.mulAssign(smoothstep(a.w.sub(pen.mul(0.5)), a.w.add(pen), d));
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
return vis;
|
|
434
|
+
});
|
|
435
|
+
// a JS function, not Fn: it takes the SpellLight struct (a plain object of nodes)
|
|
436
|
+
export function lightCookie(L: ISpellLight, p: V3): F {
|
|
437
|
+
const out = float(1.0).toVar();
|
|
438
|
+
If(L.cookie.x.greaterThanEqual(0.5), () => {
|
|
439
|
+
const q0 = p.xz.sub(L.pos.xz).mul(L.cookie.y);
|
|
440
|
+
const c = cos(L.cookie.z);
|
|
441
|
+
const s = sin(L.cookie.z);
|
|
442
|
+
// mat2(c, -s, s, c) * q (column-major)
|
|
443
|
+
const q = vec2(c.mul(q0.x).add(s.mul(q0.y)), s.negate().mul(q0.x).add(c.mul(q0.y)));
|
|
444
|
+
If(L.cookie.x.lessThan(1.5), () => {
|
|
445
|
+
// caustic: folded sines, cheap and shimmering
|
|
446
|
+
const w = q
|
|
447
|
+
.mul(3.0)
|
|
448
|
+
.add(
|
|
449
|
+
vec2(sin(q.y.mul(2.1).add(U.uTime.mul(1.3))), cos(q.x.mul(1.7).sub(U.uTime.mul(1.1)))),
|
|
450
|
+
);
|
|
451
|
+
const k = abs(sin(w.x).mul(sin(w.y)));
|
|
452
|
+
out.assign(pow(float(1.0).sub(k), 6.0).mul(2.2).add(0.35));
|
|
453
|
+
}).Else(() => {
|
|
454
|
+
const r = length(q); // rune ring
|
|
455
|
+
const ring = smoothstep(0.06, 0.0, abs(r.sub(1.0))).add(
|
|
456
|
+
smoothstep(0.04, 0.0, abs(r.sub(0.78))),
|
|
457
|
+
);
|
|
458
|
+
const spokes = smoothstep(0.92, 1.0, abs(cos(atan(q.y, q.x).mul(6.0))))
|
|
459
|
+
.mul(step(r, 1.0))
|
|
460
|
+
.mul(step(0.78, r));
|
|
461
|
+
out.assign(clamp(ring.add(spokes), 0.0, 1.0).mul(3.0).add(0.2));
|
|
462
|
+
});
|
|
463
|
+
});
|
|
464
|
+
return out;
|
|
465
|
+
}
|
|
466
|
+
// light vector + radiance arriving at p from light i (no BRDF). For line lights the point used
|
|
467
|
+
// is the closest one to the reflection ray r for specular (pass r) or to p for diffuse (r = 0).
|
|
468
|
+
// l, lp: out vars
|
|
469
|
+
// a JS function, not Fn: it takes the SpellLight struct
|
|
470
|
+
export function spellLightRaw(L: ISpellLight, p: V3, r: V3, l: V3, lp: V3): V3 {
|
|
471
|
+
lp.assign(L.pos);
|
|
472
|
+
If(L.type.equal(1), () => {
|
|
473
|
+
lp.assign(
|
|
474
|
+
select(
|
|
475
|
+
dot(r, r).greaterThan(0.0),
|
|
476
|
+
closestOnSegmentToRay(L.pos, L.end, p, r),
|
|
477
|
+
closestOnSegment(L.pos, L.end, p),
|
|
478
|
+
),
|
|
479
|
+
);
|
|
480
|
+
});
|
|
481
|
+
const d = lp.sub(p);
|
|
482
|
+
const dist = length(d);
|
|
483
|
+
l.assign(d.div(max(dist, 1e-4)));
|
|
484
|
+
const c = L.color.mul(lightFalloff(dist, L.range)).toVar();
|
|
485
|
+
If(L.type.equal(2), () => {
|
|
486
|
+
c.mulAssign(lightCookie(L, p));
|
|
487
|
+
});
|
|
488
|
+
return c;
|
|
489
|
+
}
|
|
490
|
+
// shadow only where the light actually arrives (outside its range the falloff is exactly 0)
|
|
491
|
+
export function spellLightShadow(L: ISpellLight, p: V3, lp: V3, c: V3, noShadow = false): F {
|
|
492
|
+
if (noShadow) return float(1.0);
|
|
493
|
+
const out = float(1.0).toVar();
|
|
494
|
+
If(L.shadow.greaterThan(0.5).and(dot(c, c).greaterThan(0.0)), () => {
|
|
495
|
+
out.assign(capsuleShadow(p, lp));
|
|
496
|
+
});
|
|
497
|
+
return out;
|
|
498
|
+
}
|
|
499
|
+
export function spellLightIncoming(L: ISpellLight, p: V3, r: V3, l: V3, noShadow = false): V3 {
|
|
500
|
+
const lp = vec3(0).toVar();
|
|
501
|
+
const c = spellLightRaw(L, p, r, l, lp);
|
|
502
|
+
return c.mul(spellLightShadow(L, p, lp, c, noShadow));
|
|
503
|
+
}
|
|
504
|
+
// sum of spell-light irradiance / pi on a surface (n) or, with n = 0, omnidirectional (media)
|
|
505
|
+
export function spellLightDiffuse(p: V3, n: V3, noShadow = false): V3 {
|
|
506
|
+
const sum = vec3(0.0).toVar();
|
|
507
|
+
const l = vec3(0).toVar();
|
|
508
|
+
const count = int(U.uWorldCounts.x);
|
|
509
|
+
Loop({ start: 0, end: MAX_LIGHTS, type: "int", condition: "<" }, ({ i }: { i: I }) => {
|
|
510
|
+
If(i.greaterThanEqual(count), () => {
|
|
511
|
+
Break();
|
|
512
|
+
});
|
|
513
|
+
const c = spellLightIncoming(spellLight(i), p, vec3(0.0), l, noShadow);
|
|
514
|
+
sum.addAssign(c.mul(select(dot(n, n).greaterThan(0.0), max(dot(n, l), 0.0), float(0.5))));
|
|
515
|
+
});
|
|
516
|
+
return sum;
|
|
517
|
+
}
|
|
518
|
+
// in-scatter for media / volumes: Henyey-Greenstein phase towards the viewer (v = to camera)
|
|
519
|
+
export function spellLightScatter(p: V3, v: V3, g: F, noShadow = false): V3 {
|
|
520
|
+
const sum = vec3(0.0).toVar();
|
|
521
|
+
const l = vec3(0).toVar();
|
|
522
|
+
const count = int(U.uWorldCounts.x);
|
|
523
|
+
Loop({ start: 0, end: MAX_LIGHTS, type: "int", condition: "<" }, ({ i }: { i: I }) => {
|
|
524
|
+
If(i.greaterThanEqual(count), () => {
|
|
525
|
+
Break();
|
|
526
|
+
});
|
|
527
|
+
const c = spellLightIncoming(spellLight(i), p, vec3(0.0), l, noShadow);
|
|
528
|
+
sum.addAssign(c.mul(hgPhase(dot(l, v.negate()), g)).mul(4.0 * Math.PI));
|
|
529
|
+
});
|
|
530
|
+
return sum;
|
|
531
|
+
}
|
|
532
|
+
// biome-ignore lint/style/useNamingConvention: upstream Ultra Elements code, copied verbatim
|
|
533
|
+
export const spellD_GGX = Fn(([nh, a]: [F, F]) => {
|
|
534
|
+
const a2 = a.mul(a);
|
|
535
|
+
const d = nh.mul(nh).mul(a2.sub(1.0)).add(1.0);
|
|
536
|
+
return a2.div(d.mul(d).mul(Math.PI));
|
|
537
|
+
});
|
|
538
|
+
// biome-ignore lint/style/useNamingConvention: upstream Ultra Elements code, copied verbatim
|
|
539
|
+
export const spellV_Smith = Fn(([nv, nl, a]: [F, F, F]) =>
|
|
540
|
+
float(0.5).div(mix(nl.mul(nv).mul(2.0), nl.add(nv), a)),
|
|
541
|
+
);
|
|
542
|
+
// GGX + Lambert from every spell light; diffuse and specular returned apart (radiance). diffuse,
|
|
543
|
+
// specular: out vars
|
|
544
|
+
export function spellLightPBR(
|
|
545
|
+
p: V3,
|
|
546
|
+
n: V3,
|
|
547
|
+
v: V3,
|
|
548
|
+
roughness: F,
|
|
549
|
+
f0: V3,
|
|
550
|
+
diffColor: V3,
|
|
551
|
+
diffuse: V3,
|
|
552
|
+
specular: V3,
|
|
553
|
+
noShadow = false,
|
|
554
|
+
): void {
|
|
555
|
+
diffuse.assign(vec3(0.0));
|
|
556
|
+
specular.assign(vec3(0.0));
|
|
557
|
+
const a = max(roughness.mul(roughness), 0.002);
|
|
558
|
+
const nv = max(dot(n, v), 1e-4);
|
|
559
|
+
const r = reflect(v.negate(), n);
|
|
560
|
+
const l = vec3(0).toVar();
|
|
561
|
+
const count = int(U.uWorldCounts.x);
|
|
562
|
+
Loop({ start: 0, end: MAX_LIGHTS, type: "int", condition: "<" }, ({ i }: { i: I }) => {
|
|
563
|
+
If(i.greaterThanEqual(count), () => {
|
|
564
|
+
Break();
|
|
565
|
+
});
|
|
566
|
+
const L = spellLight(i);
|
|
567
|
+
const lp = vec3(0).toVar();
|
|
568
|
+
const cd = spellLightRaw(L, p, vec3(0.0), l, lp).toVar();
|
|
569
|
+
If(dot(cd, cd).equal(0.0), () => {
|
|
570
|
+
Continue();
|
|
571
|
+
});
|
|
572
|
+
const sh = spellLightShadow(L, p, lp, cd, noShadow); // one shadow per light: the specular point reuses it
|
|
573
|
+
cd.mulAssign(sh);
|
|
574
|
+
diffuse.addAssign(
|
|
575
|
+
cd
|
|
576
|
+
.mul(diffColor)
|
|
577
|
+
.mul(max(dot(n, l), 0.0))
|
|
578
|
+
.div(Math.PI),
|
|
579
|
+
);
|
|
580
|
+
const cs = cd.toVar();
|
|
581
|
+
If(L.type.equal(1), () => {
|
|
582
|
+
cs.assign(spellLightRaw(L, p, r, l, lp).mul(sh));
|
|
583
|
+
});
|
|
584
|
+
const h = normalize(l.add(v));
|
|
585
|
+
const nl = max(dot(n, l), 0.0);
|
|
586
|
+
const nh = max(dot(n, h), 0.0);
|
|
587
|
+
const vh = max(dot(v, h), 0.0);
|
|
588
|
+
const Fr = f0.add(
|
|
589
|
+
vec3(1.0)
|
|
590
|
+
.sub(f0)
|
|
591
|
+
.mul(pow(float(1.0).sub(vh), 5.0)),
|
|
592
|
+
);
|
|
593
|
+
const aL = select(
|
|
594
|
+
L.type.equal(1),
|
|
595
|
+
min(a.add(float(0.25).div(max(distance(lp, p).mul(2.0), 0.25))), 1.0),
|
|
596
|
+
a,
|
|
597
|
+
); // line lights are ~0.25 m tubes, not hard streaks
|
|
598
|
+
specular.addAssign(
|
|
599
|
+
cs
|
|
600
|
+
.mul(Fr)
|
|
601
|
+
.mul(
|
|
602
|
+
spellD_GGX(nh, aL)
|
|
603
|
+
.mul(spellV_Smith(nv, nl, aL))
|
|
604
|
+
.mul(nl),
|
|
605
|
+
)
|
|
606
|
+
.mul(a.div(aL)),
|
|
607
|
+
);
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// FORCES_GLSL
|
|
612
|
+
// Forces act on every particle (scaled by its kind's force response) and warp fire volumes.
|
|
613
|
+
// 0 wind t2.xyz dir, t1 = (speed m/s, gustiness, coupling, -)
|
|
614
|
+
// 1 vortex pos = base, t2.xyz axis, t1 = (core R, circulation G, updraft W, inflow), t2.w height
|
|
615
|
+
// 2 attractor t1 = (strength, softening, kill radius, range) (negative strength repels)
|
|
616
|
+
// 3 orbit Kepler disk: t2.xyz axis, t1 = (GM, inner, outer, coupling)
|
|
617
|
+
// 4 shock expanding shell: t1 = (radius, thickness, impulse, -)
|
|
618
|
+
// 5 push cone gust: t2.xyz dir, t1 = (speed, radius, length, coupling)
|
|
619
|
+
// kill: inout var
|
|
620
|
+
export const forceAccel = Fn(([p, v, kill]: [V3, V3, F]) => {
|
|
621
|
+
const acc = vec3(0.0).toVar();
|
|
622
|
+
const n = int(U.uWorldCounts.y);
|
|
623
|
+
Loop({ start: 0, end: MAX_FORCES, type: "int", condition: "<" }, ({ i }: { i: I }) => {
|
|
624
|
+
If(i.greaterThanEqual(n), () => {
|
|
625
|
+
Break();
|
|
626
|
+
});
|
|
627
|
+
const a = worldTexel(int(1), i.mul(4));
|
|
628
|
+
const k = worldTexel(int(1), i.mul(4).add(1));
|
|
629
|
+
const b = worldTexel(int(1), i.mul(4).add(2));
|
|
630
|
+
const type = int(a.w);
|
|
631
|
+
const d = p.sub(a.xyz);
|
|
632
|
+
If(type.equal(0), () => {
|
|
633
|
+
const gust = k.y
|
|
634
|
+
.mul(sin(U.uTime.mul(1.7).add(dot(p, vec3(0.13, 0.05, 0.11)))))
|
|
635
|
+
.mul(sin(U.uTime.mul(0.53).add(p.x.mul(0.07))))
|
|
636
|
+
.add(1.0);
|
|
637
|
+
acc.addAssign(b.xyz.mul(k.x).mul(gust).sub(v).mul(k.z));
|
|
638
|
+
})
|
|
639
|
+
.ElseIf(type.equal(1), () => {
|
|
640
|
+
const ax = b.xyz;
|
|
641
|
+
const h = dot(d, ax);
|
|
642
|
+
const radial = d.sub(ax.mul(h));
|
|
643
|
+
const r = length(radial);
|
|
644
|
+
If(
|
|
645
|
+
h
|
|
646
|
+
.greaterThan(-0.5)
|
|
647
|
+
.and(h.lessThan(b.w))
|
|
648
|
+
.and(r.lessThan(k.x.mul(6.0))),
|
|
649
|
+
() => {
|
|
650
|
+
const er = radial.div(max(r, 1e-3));
|
|
651
|
+
const et = cross(ax, er);
|
|
652
|
+
const vt = select(
|
|
653
|
+
r.lessThan(k.x),
|
|
654
|
+
k.y
|
|
655
|
+
.div(2.0 * Math.PI)
|
|
656
|
+
.mul(r)
|
|
657
|
+
.div(k.x.mul(k.x)),
|
|
658
|
+
k.y.div(r.mul(2.0 * Math.PI)),
|
|
659
|
+
);
|
|
660
|
+
const up = k.z.mul(exp(r.mul(r).negate().div(k.x.mul(k.x).mul(2.0))));
|
|
661
|
+
const target = et
|
|
662
|
+
.mul(vt)
|
|
663
|
+
.add(ax.mul(up))
|
|
664
|
+
.sub(
|
|
665
|
+
er
|
|
666
|
+
.mul(k.w)
|
|
667
|
+
.mul(smoothstep(k.x.mul(6.0), k.x, r))
|
|
668
|
+
.mul(float(1.0).sub(smoothstep(0.0, 2.0, h).mul(0.6))),
|
|
669
|
+
);
|
|
670
|
+
acc.addAssign(
|
|
671
|
+
target
|
|
672
|
+
.sub(v)
|
|
673
|
+
.mul(3.0)
|
|
674
|
+
.mul(smoothstep(k.x.mul(6.0), k.x.mul(3.0), r)),
|
|
675
|
+
);
|
|
676
|
+
},
|
|
677
|
+
);
|
|
678
|
+
})
|
|
679
|
+
.ElseIf(type.equal(2), () => {
|
|
680
|
+
const r2 = dot(d, d);
|
|
681
|
+
If(r2.lessThan(k.w.mul(k.w)), () => {
|
|
682
|
+
acc.subAssign(
|
|
683
|
+
normalize(d)
|
|
684
|
+
.mul(k.x)
|
|
685
|
+
.div(r2.add(k.y.mul(k.y))),
|
|
686
|
+
);
|
|
687
|
+
If(r2.lessThan(k.z.mul(k.z)), () => {
|
|
688
|
+
kill.assign(1.0);
|
|
689
|
+
});
|
|
690
|
+
});
|
|
691
|
+
})
|
|
692
|
+
.ElseIf(type.equal(3), () => {
|
|
693
|
+
const ax = b.xyz;
|
|
694
|
+
const h = dot(d, ax);
|
|
695
|
+
const radial = d.sub(ax.mul(h));
|
|
696
|
+
const r = length(radial);
|
|
697
|
+
If(r.greaterThan(k.y.mul(0.5)).and(r.lessThan(k.z)), () => {
|
|
698
|
+
const et = cross(ax, radial.div(r));
|
|
699
|
+
const target = et.mul(sqrt(k.x.div(r))).sub(ax.mul(h).mul(4.0));
|
|
700
|
+
acc.addAssign(target.sub(v).mul(k.w));
|
|
701
|
+
});
|
|
702
|
+
If(r.lessThan(k.y), () => {
|
|
703
|
+
kill.assign(max(kill, step(abs(h), 0.5)));
|
|
704
|
+
});
|
|
705
|
+
})
|
|
706
|
+
.ElseIf(type.equal(4), () => {
|
|
707
|
+
const r = length(d);
|
|
708
|
+
const shell = float(1.0).sub(clamp(abs(r.sub(k.x)).div(max(k.y, 1e-3)), 0.0, 1.0));
|
|
709
|
+
acc.addAssign(d.div(max(r, 1e-3)).mul(k.z).mul(shell));
|
|
710
|
+
})
|
|
711
|
+
.ElseIf(type.equal(5), () => {
|
|
712
|
+
const along = dot(d, b.xyz);
|
|
713
|
+
const off = length(d.sub(b.xyz.mul(along)));
|
|
714
|
+
If(along.greaterThan(-0.5).and(along.lessThan(k.z)).and(off.lessThan(k.y)), () => {
|
|
715
|
+
acc.addAssign(
|
|
716
|
+
b.xyz
|
|
717
|
+
.mul(k.x)
|
|
718
|
+
.sub(v)
|
|
719
|
+
.mul(k.w)
|
|
720
|
+
.mul(float(1.0).sub(off.div(k.y))),
|
|
721
|
+
);
|
|
722
|
+
});
|
|
723
|
+
});
|
|
724
|
+
});
|
|
725
|
+
return acc;
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
// IMPACTS_GLSL
|
|
729
|
+
// Spherical "volume decals" for surfaces: weights per impact type at p (amount x falloff x fade).
|
|
730
|
+
// A: x scorch/heat, y frost, z wet, w magma glow. B: x acid, y holy glow, z corruption, w electric.
|
|
731
|
+
// Each impact also carries a colour (t2.rgb) for the glow types. wa, wb, glowColor: out vars
|
|
732
|
+
export const impactWeights = Fn(([p, wa, wb, glowColor]: [V3, V4, V4, V3]) => {
|
|
733
|
+
wa.assign(vec4(0.0));
|
|
734
|
+
wb.assign(vec4(0.0));
|
|
735
|
+
glowColor.assign(vec3(0.0));
|
|
736
|
+
const n = int(U.uWorldCounts.z);
|
|
737
|
+
Loop({ start: 0, end: MAX_IMPACTS, type: "int", condition: "<" }, ({ i }: { i: I }) => {
|
|
738
|
+
If(i.greaterThanEqual(n), () => {
|
|
739
|
+
Break();
|
|
740
|
+
});
|
|
741
|
+
const a = worldTexel(int(2), i.mul(4));
|
|
742
|
+
const b = worldTexel(int(2), i.mul(4).add(1));
|
|
743
|
+
const c = worldTexel(int(2), i.mul(4).add(2));
|
|
744
|
+
const w = b.z.mul(smoothstep(1.0, 0.55, length(p.sub(a.xyz)).div(a.w)));
|
|
745
|
+
If(w.greaterThan(0.0), () => {
|
|
746
|
+
const t = int(b.x);
|
|
747
|
+
// wa[t] += w (t < 4) else wb[t - 4] += w, as a one-hot lane mask
|
|
748
|
+
const lane = (k: number) => select(t.equal(k), float(1.0), float(0.0));
|
|
749
|
+
wa.addAssign(vec4(lane(0), lane(1), lane(2), lane(3)).mul(w));
|
|
750
|
+
wb.addAssign(vec4(lane(4), lane(5), lane(6), lane(7)).mul(w));
|
|
751
|
+
glowColor.addAssign(c.rgb.mul(w));
|
|
752
|
+
});
|
|
753
|
+
});
|
|
754
|
+
});
|
|
755
|
+
|
|
756
|
+
// GROUND_GLSL. uGroundRect: min x, min z, size, 1 / size
|
|
757
|
+
export const groundUV = Fn(([xz]: [V2]) => xz.sub(U.uGroundRect.xy).mul(U.uGroundRect.w));
|
|
758
|
+
export const groundA = Fn(([xz]: [V2]) => sample2(U.uGroundA.value, groundUV(xz), false)); // heat, char, wet, frost
|
|
759
|
+
export const groundB = Fn(([xz]: [V2]) => sample2(U.uGroundB.value, groundUV(xz), false)); // crack, acid, corruption, glow
|
|
760
|
+
|
|
761
|
+
// --- FX pass (half-res MRT, weighted-blended OIT; McGuire & Bavoil 2013) -------------
|
|
762
|
+
// Upstream: every FX-pass material is a RawShaderMaterial (GLSL3) using FX_VERT_HEAD / FX_FRAG_HEAD and
|
|
763
|
+
// ...FX_BLENDING, and ends with fxOut(). One blend state for the three attachments:
|
|
764
|
+
// 0 media (w*c*a, a) -> rgb sum, alpha = revealage product
|
|
765
|
+
// 1 weight (w*a, distortion.xy, a)
|
|
766
|
+
// 2 emission (rgb, 0) -> additive, never occluded by media unless the composite says so
|
|
767
|
+
// Engine: no CustomBlending (Normal and Additive only), no multi-target render target, and an
|
|
768
|
+
// offscreen pass draws before the frame's depth exists. So FX materials draw in the frame's own
|
|
769
|
+
// transparent queue (sorted back to front, which is exact where WBOIT approximates), read the frame's
|
|
770
|
+
// depth through viewportDepthTexture(), and fxOut() returns the composite of one layer,
|
|
771
|
+
// scene * rev + accum * (1 - rev) + em * mix(1, rev, smokeOccludesGlow), rev = 1 - a,
|
|
772
|
+
// as NormalBlending's src * a + dst * (1 - a): rgb = col + em * mix(1, 1 - a, k) / a. Exact while
|
|
773
|
+
// emission comes with extinction (volumes, media); pure glow (a = 0) uses FX_ADDITIVE and fxOutGlow().
|
|
774
|
+
// The distortion output (heat haze, shocks) needs viewportSharedTexture() in that material: open.
|
|
775
|
+
export const FX_BLENDING = {
|
|
776
|
+
transparent: true,
|
|
777
|
+
depthWrite: false,
|
|
778
|
+
depthTest: true,
|
|
779
|
+
blending: NormalBlending,
|
|
780
|
+
};
|
|
781
|
+
// glow-only FX (no coverage): rgb added whatever the alpha
|
|
782
|
+
export const FX_ADDITIVE = {
|
|
783
|
+
transparent: true,
|
|
784
|
+
depthWrite: false,
|
|
785
|
+
depthTest: true,
|
|
786
|
+
blending: AdditiveBlending,
|
|
787
|
+
};
|
|
788
|
+
|
|
789
|
+
// FX_OUT_GLSL. viewZ positive distance along the view axis.
|
|
790
|
+
// full-res scene depth under this fragment (upstream: the farthest of the half-res pixel's 2x2
|
|
791
|
+
// full-res block; the engine draws FX at full res, so the block is the pixel)
|
|
792
|
+
export const sceneViewZ = Fn(() => linearDepth(viewportDepthTexture(screenUV).x));
|
|
793
|
+
export const softFade = Fn(([viewZ, dist]: [F, F]) =>
|
|
794
|
+
clamp(sceneViewZ().sub(viewZ).div(dist), 0.0, 1.0),
|
|
795
|
+
);
|
|
796
|
+
export const wboitWeight = Fn(([viewZ, a]: [F, F]) =>
|
|
797
|
+
a.mul(
|
|
798
|
+
clamp(
|
|
799
|
+
float(10.0).div(
|
|
800
|
+
pow(viewZ.div(10.0), 2.0)
|
|
801
|
+
.add(pow(viewZ.div(200.0), 6.0))
|
|
802
|
+
.add(1e-5),
|
|
803
|
+
),
|
|
804
|
+
0.01,
|
|
805
|
+
30.0,
|
|
806
|
+
),
|
|
807
|
+
),
|
|
808
|
+
);
|
|
809
|
+
// col: lit media colour, a: coverage, emit: additive HDR emission, dist: screen-space UV offset
|
|
810
|
+
// (not composited yet, see above). Returns the fragment colour for FX_BLENDING.
|
|
811
|
+
export const fxOut = Fn(([col, a0, emit, _dist, _viewZ]: [V3, F, V3, V2, F]) => {
|
|
812
|
+
const a = clamp(a0, 0.0, 0.999); // 0.995 let a bright HDR sky print through opaque soot
|
|
813
|
+
const out = vec4(
|
|
814
|
+
col.add(emit.mul(mix(1.0, float(1.0).sub(a), U.uFxParams.x)).div(max(a, 2e-3))),
|
|
815
|
+
a,
|
|
816
|
+
).toVar();
|
|
817
|
+
If(U.uDebug.greaterThan(0.5).and(U.uDebug.lessThan(1.5)), () => {
|
|
818
|
+
out.assign(vec4(0.04, 0.012, 0.004, 1.0));
|
|
819
|
+
});
|
|
820
|
+
return out;
|
|
821
|
+
});
|
|
822
|
+
// for FX_ADDITIVE materials: emission only
|
|
823
|
+
export const fxOutGlow = Fn(([emit]: [V3]) => vec4(emit, 1.0));
|
|
824
|
+
|
|
825
|
+
// full-res additive emissive (sparks, motes, bolt cores, sigils) into the HDR target. Pure
|
|
826
|
+
// ONE/ONE on rgb whatever alpha the shader writes (three's AdditiveBlending scales by src alpha),
|
|
827
|
+
// destination alpha kept (the planar reflection uses alpha as its 'something drawn' mask).
|
|
828
|
+
// Engine: AdditiveBlending (SrcAlpha, One): write alpha 1.
|
|
829
|
+
export const LINES_BLENDING = {
|
|
830
|
+
transparent: true,
|
|
831
|
+
depthWrite: false,
|
|
832
|
+
depthTest: true,
|
|
833
|
+
blending: AdditiveBlending,
|
|
834
|
+
};
|
|
835
|
+
|
|
836
|
+
// --- JS side: pools packed into the world texture -------------------------------------
|
|
837
|
+
export interface IPoolItem {
|
|
838
|
+
index: number;
|
|
839
|
+
active: boolean;
|
|
840
|
+
}
|
|
841
|
+
function makePool<T extends object>(n: number, init: () => T) {
|
|
842
|
+
const items = Array.from({ length: n }, (_, i): T & IPoolItem => ({
|
|
843
|
+
index: i,
|
|
844
|
+
active: false,
|
|
845
|
+
...init(),
|
|
846
|
+
}));
|
|
847
|
+
return {
|
|
848
|
+
items,
|
|
849
|
+
acquire(): (T & IPoolItem) | null {
|
|
850
|
+
const it = items.find((x) => !x.active);
|
|
851
|
+
if (!it) return null;
|
|
852
|
+
Object.assign(it, init(), { active: true });
|
|
853
|
+
return it;
|
|
854
|
+
},
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
// light: pos, range, color (linear), intensity, type 0 point / 1 line / 2 cookie, end (line),
|
|
859
|
+
// shadow (wants capsule shadows), cookie [pattern, scale, rotation, 0]
|
|
860
|
+
export const lights = makePool(MAX_LIGHTS, () => ({
|
|
861
|
+
pos: new Vector3(),
|
|
862
|
+
range: 8,
|
|
863
|
+
color: new Color(1, 1, 1),
|
|
864
|
+
intensity: 0,
|
|
865
|
+
type: 0,
|
|
866
|
+
end: new Vector3(),
|
|
867
|
+
shadow: false,
|
|
868
|
+
cookie: [0, 1, 0, 0],
|
|
869
|
+
release(this: { active: boolean }) {
|
|
870
|
+
this.active = false;
|
|
871
|
+
},
|
|
872
|
+
}));
|
|
873
|
+
export type SpellLightHandle = (typeof lights.items)[number];
|
|
874
|
+
|
|
875
|
+
export const forces = makePool(MAX_FORCES, () => ({
|
|
876
|
+
pos: new Vector3(),
|
|
877
|
+
type: 0,
|
|
878
|
+
p: [0, 0, 0, 0],
|
|
879
|
+
dir: new Vector3(0, 1, 0),
|
|
880
|
+
w: 0,
|
|
881
|
+
release(this: { active: boolean }) {
|
|
882
|
+
this.active = false;
|
|
883
|
+
},
|
|
884
|
+
}));
|
|
885
|
+
|
|
886
|
+
// impacts fade on their own: amount * (1 - age / life), oldest replaced when full
|
|
887
|
+
const IMPACT_TYPES: Record<string, number> = {
|
|
888
|
+
scorch: 0,
|
|
889
|
+
frost: 1,
|
|
890
|
+
wet: 2,
|
|
891
|
+
magma: 3,
|
|
892
|
+
acid: 4,
|
|
893
|
+
holy: 5,
|
|
894
|
+
corruption: 6,
|
|
895
|
+
electric: 7,
|
|
896
|
+
};
|
|
897
|
+
const impactList = Array.from({ length: MAX_IMPACTS }, () => ({
|
|
898
|
+
pos: new Vector3(),
|
|
899
|
+
radius: 1,
|
|
900
|
+
type: 0,
|
|
901
|
+
age: 1e9,
|
|
902
|
+
life: 1,
|
|
903
|
+
amount: 0,
|
|
904
|
+
color: new Color(),
|
|
905
|
+
}));
|
|
906
|
+
const DEFAULT_IMPACT = new Color(1, 0.4, 0.1);
|
|
907
|
+
export const impacts = {
|
|
908
|
+
types: IMPACT_TYPES,
|
|
909
|
+
add(
|
|
910
|
+
type: string | number,
|
|
911
|
+
pos: Vector3,
|
|
912
|
+
radius: number,
|
|
913
|
+
amount = 1,
|
|
914
|
+
life = 6,
|
|
915
|
+
color: Color | null = null,
|
|
916
|
+
) {
|
|
917
|
+
let it = impactList[0] as (typeof impactList)[number];
|
|
918
|
+
for (const x of impactList) if (x.age / x.life > it.age / it.life) it = x;
|
|
919
|
+
it.pos.copy(pos);
|
|
920
|
+
it.radius = radius;
|
|
921
|
+
it.type = typeof type === "string" ? (IMPACT_TYPES[type] as number) : type;
|
|
922
|
+
it.amount = amount;
|
|
923
|
+
it.life = life;
|
|
924
|
+
it.age = 0;
|
|
925
|
+
it.color.copy(color ?? DEFAULT_IMPACT);
|
|
926
|
+
return it;
|
|
927
|
+
},
|
|
928
|
+
clear() {
|
|
929
|
+
for (const x of impactList) x.age = 1e9;
|
|
930
|
+
},
|
|
931
|
+
};
|
|
932
|
+
|
|
933
|
+
// capsules (a, b, radius); static ones are added once by the arena, dynamic ones (spikes,
|
|
934
|
+
// shields) are acquired and released. kind: 0 stone, 1 dummy, 2 spike, 3 shield, 4 pedestal
|
|
935
|
+
export interface IOccluder {
|
|
936
|
+
a: Vector3;
|
|
937
|
+
b: Vector3;
|
|
938
|
+
radius: number;
|
|
939
|
+
kind: number;
|
|
940
|
+
release(): void;
|
|
941
|
+
}
|
|
942
|
+
const occluderList: IOccluder[] = [];
|
|
943
|
+
export const occluders = {
|
|
944
|
+
list: occluderList,
|
|
945
|
+
add(a: Vector3, b: Vector3, radius: number, kind = 0): IOccluder | null {
|
|
946
|
+
if (occluderList.length >= MAX_OCCLUDERS) return null;
|
|
947
|
+
const o: IOccluder = {
|
|
948
|
+
a: a.clone(),
|
|
949
|
+
b: b.clone(),
|
|
950
|
+
radius,
|
|
951
|
+
kind,
|
|
952
|
+
release() {
|
|
953
|
+
const i = occluderList.indexOf(o);
|
|
954
|
+
if (i >= 0) occluderList.splice(i, 1);
|
|
955
|
+
},
|
|
956
|
+
};
|
|
957
|
+
occluderList.push(o);
|
|
958
|
+
return o;
|
|
959
|
+
},
|
|
960
|
+
};
|
|
961
|
+
|
|
962
|
+
// The CPU keeps at most `maxLights` lights and `maxShadowed` shadowed ones (the brightest).
|
|
963
|
+
const lightOrder: SpellLightHandle[] = [];
|
|
964
|
+
export function packWorld(dt: number, { maxLights = MAX_LIGHTS, maxShadowed = 4 } = {}): void {
|
|
965
|
+
const d = worldData;
|
|
966
|
+
d.fill(0);
|
|
967
|
+
// lights, brightest first so a tier cut drops the dim ones
|
|
968
|
+
lightOrder.length = 0;
|
|
969
|
+
for (const l of lights.items) if (l.active && l.intensity > 1e-3) lightOrder.push(l);
|
|
970
|
+
lightOrder.sort((a, b) => b.intensity * b.range - a.intensity * a.range);
|
|
971
|
+
const nl = Math.min(lightOrder.length, maxLights);
|
|
972
|
+
let shadowed = 0;
|
|
973
|
+
for (let i = 0; i < nl; i++) {
|
|
974
|
+
const l = lightOrder[i] as SpellLightHandle;
|
|
975
|
+
const o = (0 * WORLD_W + i * 4) * 4;
|
|
976
|
+
// biome-ignore lint/style/noCommaOperator: upstream Ultra Elements code, copied verbatim
|
|
977
|
+
const sh = l.shadow && shadowed < maxShadowed ? (shadowed++, 1) : 0;
|
|
978
|
+
d.set(
|
|
979
|
+
[
|
|
980
|
+
l.pos.x,
|
|
981
|
+
l.pos.y,
|
|
982
|
+
l.pos.z,
|
|
983
|
+
l.range,
|
|
984
|
+
l.color.r * l.intensity,
|
|
985
|
+
l.color.g * l.intensity,
|
|
986
|
+
l.color.b * l.intensity,
|
|
987
|
+
l.type,
|
|
988
|
+
l.end.x,
|
|
989
|
+
l.end.y,
|
|
990
|
+
l.end.z,
|
|
991
|
+
sh,
|
|
992
|
+
l.cookie[0] as number,
|
|
993
|
+
l.cookie[1] as number,
|
|
994
|
+
l.cookie[2] as number,
|
|
995
|
+
l.cookie[3] as number,
|
|
996
|
+
],
|
|
997
|
+
o,
|
|
998
|
+
);
|
|
999
|
+
}
|
|
1000
|
+
let nf = 0;
|
|
1001
|
+
for (const fo of forces.items) {
|
|
1002
|
+
if (!fo.active) continue;
|
|
1003
|
+
const o = (1 * WORLD_W + nf * 4) * 4;
|
|
1004
|
+
d.set(
|
|
1005
|
+
[
|
|
1006
|
+
fo.pos.x,
|
|
1007
|
+
fo.pos.y,
|
|
1008
|
+
fo.pos.z,
|
|
1009
|
+
fo.type,
|
|
1010
|
+
fo.p[0] as number,
|
|
1011
|
+
fo.p[1] as number,
|
|
1012
|
+
fo.p[2] as number,
|
|
1013
|
+
fo.p[3] as number,
|
|
1014
|
+
fo.dir.x,
|
|
1015
|
+
fo.dir.y,
|
|
1016
|
+
fo.dir.z,
|
|
1017
|
+
fo.w,
|
|
1018
|
+
],
|
|
1019
|
+
o,
|
|
1020
|
+
);
|
|
1021
|
+
nf++;
|
|
1022
|
+
}
|
|
1023
|
+
let ni = 0;
|
|
1024
|
+
for (const it of impactList) {
|
|
1025
|
+
it.age += dt;
|
|
1026
|
+
if (it.age >= it.life) continue;
|
|
1027
|
+
const o = (2 * WORLD_W + ni * 4) * 4;
|
|
1028
|
+
const fade = 1 - it.age / it.life;
|
|
1029
|
+
d.set(
|
|
1030
|
+
[
|
|
1031
|
+
it.pos.x,
|
|
1032
|
+
it.pos.y,
|
|
1033
|
+
it.pos.z,
|
|
1034
|
+
it.radius,
|
|
1035
|
+
it.type,
|
|
1036
|
+
it.age,
|
|
1037
|
+
it.amount * fade * fade,
|
|
1038
|
+
0,
|
|
1039
|
+
it.color.r,
|
|
1040
|
+
it.color.g,
|
|
1041
|
+
it.color.b,
|
|
1042
|
+
0,
|
|
1043
|
+
],
|
|
1044
|
+
o,
|
|
1045
|
+
);
|
|
1046
|
+
ni++;
|
|
1047
|
+
}
|
|
1048
|
+
const no = occluderList.length;
|
|
1049
|
+
for (let i = 0; i < no; i++) {
|
|
1050
|
+
const c = occluderList[i] as IOccluder;
|
|
1051
|
+
const o = (3 * WORLD_W + i * 2) * 4;
|
|
1052
|
+
d.set([c.a.x, c.a.y, c.a.z, c.radius, c.b.x, c.b.y, c.b.z, c.kind], o);
|
|
1053
|
+
}
|
|
1054
|
+
U.uWorldCounts.value.set(nl, nf, ni, no);
|
|
1055
|
+
worldTex.needsUpdate = true;
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
// Engine: upstream's arena shaded its own surfaces from the world texture (spellLightPBR). Our
|
|
1059
|
+
// scene is lit by three's lights, so the same lights also drive a fixed set of PointLights, parked
|
|
1060
|
+
// at intensity 0 (spell-lights.ts: a light count that never moves never recompiles). The radiance
|
|
1061
|
+
// scale is the same: upstream's c = color * intensity * window / (d^2 + 1), three's color * intensity / d^2.
|
|
1062
|
+
export function mirrorLights(
|
|
1063
|
+
points: readonly { position: Vector3; color: Color; intensity: number; distance: number }[],
|
|
1064
|
+
): void {
|
|
1065
|
+
let n = 0;
|
|
1066
|
+
for (const l of lights.items) {
|
|
1067
|
+
if (!l.active || l.intensity <= 1e-3 || n >= points.length) continue;
|
|
1068
|
+
const p = points[n++] as (typeof points)[number];
|
|
1069
|
+
p.position.copy(l.type === 1 ? l.end.clone().add(l.pos).multiplyScalar(0.5) : l.pos);
|
|
1070
|
+
p.color.copy(l.color);
|
|
1071
|
+
p.intensity = l.intensity;
|
|
1072
|
+
p.distance = l.range;
|
|
1073
|
+
}
|
|
1074
|
+
for (; n < points.length; n++) (points[n] as (typeof points)[number]).intensity = 0;
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
// --- JS mirror of arenaSDF (click targeting, projectile impacts, spawn placement) -----
|
|
1078
|
+
const _pa = new Vector3();
|
|
1079
|
+
const _ba = new Vector3();
|
|
1080
|
+
function sdCapsuleJS(p: Vector3, a: Vector3, b: Vector3, r: number): number {
|
|
1081
|
+
_pa.subVectors(p, a);
|
|
1082
|
+
_ba.subVectors(b, a);
|
|
1083
|
+
const h = Math.min(1, Math.max(0, _pa.dot(_ba) / Math.max(_ba.dot(_ba), 1e-6)));
|
|
1084
|
+
return _pa.addScaledVector(_ba, -h).length() - r;
|
|
1085
|
+
}
|
|
1086
|
+
export function arenaStaticJS(p: Vector3): number {
|
|
1087
|
+
const P = ARENA.pool;
|
|
1088
|
+
const inPool = Math.abs(p.x - P.center.x) < P.half.x && Math.abs(p.z - P.center.y) < P.half.y;
|
|
1089
|
+
const d = p.y - (inPool ? P.water : 0);
|
|
1090
|
+
const qx =
|
|
1091
|
+
Math.abs(Math.hypot(p.x, p.z) - (ARENA.radius + ARENA.wallThickness / 2)) -
|
|
1092
|
+
ARENA.wallThickness / 2;
|
|
1093
|
+
const qy = Math.abs(p.y - ARENA.wallHeight / 2) - ARENA.wallHeight / 2;
|
|
1094
|
+
return Math.min(d, Math.hypot(Math.max(qx, 0), Math.max(qy, 0)) + Math.min(Math.max(qx, qy), 0));
|
|
1095
|
+
}
|
|
1096
|
+
// upstream names this arenaSDF; the TSL function above has the GLSL's name, so the JS mirror is arenaSDFJS.
|
|
1097
|
+
// Engine scenes differ from upstream's arena: a game replaces the static part with setArenaStatic().
|
|
1098
|
+
let staticSDF: (p: Vector3) => number = arenaStaticJS;
|
|
1099
|
+
export function setArenaStatic(fn: (p: Vector3) => number): void {
|
|
1100
|
+
staticSDF = fn;
|
|
1101
|
+
}
|
|
1102
|
+
export function arenaSDFJS(p: Vector3): number {
|
|
1103
|
+
let d = staticSDF(p);
|
|
1104
|
+
for (const c of occluderList) d = Math.min(d, sdCapsuleJS(p, c.a, c.b, c.radius));
|
|
1105
|
+
return d;
|
|
1106
|
+
}
|
|
1107
|
+
const _rp = new Vector3();
|
|
1108
|
+
// sphere trace; returns distance along dir or -1
|
|
1109
|
+
export function arenaRaycast(origin: Vector3, dir: Vector3, maxDist = 200): number {
|
|
1110
|
+
let t = 0;
|
|
1111
|
+
for (let i = 0; i < 256 && t < maxDist; i++) {
|
|
1112
|
+
const d = arenaSDFJS(_rp.copy(origin).addScaledVector(dir, t));
|
|
1113
|
+
if (d < 0.002) return t;
|
|
1114
|
+
t += Math.max(d, 0.01);
|
|
1115
|
+
}
|
|
1116
|
+
return -1;
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
// a mat3 uniform the engine can bind: three row vectors (GLSL `m * v` = rows dotted with v)
|
|
1120
|
+
const vec3Uniform = (v: Vector3) => uniform(v);
|
|
1121
|
+
export type Vec3Uniform = ReturnType<typeof vec3Uniform>;
|
|
1122
|
+
export interface IMat3Rows {
|
|
1123
|
+
x: Vec3Uniform;
|
|
1124
|
+
y: Vec3Uniform;
|
|
1125
|
+
z: Vec3Uniform;
|
|
1126
|
+
}
|
|
1127
|
+
export function mat3Rows(): IMat3Rows {
|
|
1128
|
+
return {
|
|
1129
|
+
x: vec3Uniform(new Vector3(1, 0, 0)),
|
|
1130
|
+
y: vec3Uniform(new Vector3(0, 1, 0)),
|
|
1131
|
+
z: vec3Uniform(new Vector3(0, 0, 1)),
|
|
1132
|
+
};
|
|
1133
|
+
}
|
|
1134
|
+
export function setMat3Rows(rows: IMat3Rows, m: Matrix3): void {
|
|
1135
|
+
const e = m.elements;
|
|
1136
|
+
rows.x.value.set(e[0] as number, e[3] as number, e[6] as number);
|
|
1137
|
+
rows.y.value.set(e[1] as number, e[4] as number, e[7] as number);
|
|
1138
|
+
rows.z.value.set(e[2] as number, e[5] as number, e[8] as number);
|
|
1139
|
+
}
|
|
1140
|
+
export const mulMat3Rows = (rows: IMat3Rows, v: V3): V3 =>
|
|
1141
|
+
vec3(dot(rows.x, v), dot(rows.y, v), dot(rows.z, v));
|
|
1142
|
+
|
|
1143
|
+
// --- added for the GroundField, Ribbons and Shockwaves port -------------------------------------
|
|
1144
|
+
// GroundField ping-pongs U.uGroundA/B between two targets each step. A material's graph holds the
|
|
1145
|
+
// shared texture node of the texture it was built with, so the node follows the holder to its new
|
|
1146
|
+
// texture (one node per texture, see textureNode above). So each holder starts on its own placeholder.
|
|
1147
|
+
export function setGroundTexture(holder: { value: unknown }, tex: object): void {
|
|
1148
|
+
const node = shared.get(holder.value as object);
|
|
1149
|
+
if (node !== undefined && holder.value !== tex) {
|
|
1150
|
+
shared.delete(holder.value as object);
|
|
1151
|
+
(node as unknown as { value: object }).value = tex;
|
|
1152
|
+
shared.set(tex, node);
|
|
1153
|
+
}
|
|
1154
|
+
holder.value = tex;
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
// FxTarget's composite distortion and distortion-driven chromatic aberration, for one FX layer:
|
|
1158
|
+
// scene = (uScene(suv + ca).r, uScene(suv).g, uScene(suv - ca).b), suv = vUv + dist * distortion
|
|
1159
|
+
// Engine (see FX_BLENDING): there is no WBOIT target to resolve, so a distorting layer reads the
|
|
1160
|
+
// frame drawn so far (viewportSharedTexture) and writes one premultiplied colour under
|
|
1161
|
+
// FX_DISTORT_BLENDING: fxOut's media over alpha coverage, its emission, and the displaced scene minus
|
|
1162
|
+
// the scene under it, seen through (1 - alpha). Layers drawn under it stay; with alpha 0 it adds.
|
|
1163
|
+
// dist is upstream's screen-UV offset (GL, y up); screenUV runs y down. Arguments as upstream's
|
|
1164
|
+
// fxOut(media, alpha, emit, dist, viewZ), reordered: (emit, dist, media, alpha).
|
|
1165
|
+
export const FX_DISTORT_BLENDING = {
|
|
1166
|
+
transparent: true,
|
|
1167
|
+
depthWrite: false,
|
|
1168
|
+
depthTest: true,
|
|
1169
|
+
blending: CustomBlending,
|
|
1170
|
+
blendEquation: AddEquation,
|
|
1171
|
+
blendSrc: OneFactor,
|
|
1172
|
+
blendDst: OneMinusSrcAlphaFactor,
|
|
1173
|
+
blendEquationAlpha: AddEquation,
|
|
1174
|
+
blendSrcAlpha: ZeroFactor, // destination alpha kept (see LINES_BLENDING)
|
|
1175
|
+
blendDstAlpha: OneFactor,
|
|
1176
|
+
};
|
|
1177
|
+
const SCENE_COLOR = {};
|
|
1178
|
+
export const fxOutDistort = Fn(([emit, dist0, media, a0]: [V3, V2, V3, F]) => {
|
|
1179
|
+
const scene = textureNode(SCENE_COLOR, () => viewportSharedTexture()) as unknown as {
|
|
1180
|
+
sample(uv: V2): V4;
|
|
1181
|
+
};
|
|
1182
|
+
const dist = vec2(dist0.x, dist0.y.negate()).mul(U.uFxParams.y);
|
|
1183
|
+
const suv = screenUV.add(dist);
|
|
1184
|
+
const ca = dist.mul(U.uFxParams.z.mul(0.6));
|
|
1185
|
+
const col = vec3(scene.sample(suv.add(ca)).r, scene.sample(suv).g, scene.sample(suv.sub(ca)).b);
|
|
1186
|
+
const a = clamp(a0, 0.0, 0.999);
|
|
1187
|
+
const seen = col.sub(scene.sample(screenUV).rgb).mul(float(1.0).sub(a));
|
|
1188
|
+
return vec4(
|
|
1189
|
+
media
|
|
1190
|
+
.mul(a)
|
|
1191
|
+
.add(emit.mul(mix(1.0, float(1.0).sub(a), U.uFxParams.x)))
|
|
1192
|
+
.add(seen),
|
|
1193
|
+
a,
|
|
1194
|
+
);
|
|
1195
|
+
});
|