@threenative/core 0.3.11 → 0.3.13
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/README.md +1 -1
- package/capabilities.json +1988 -204
- package/dist/{assets-YUbXa3-Y.d.ts → assets-CfMKnOsK.d.ts} +78 -1
- package/dist/camera-shake-CZKOfGn4.d.ts +46 -0
- package/dist/{canvas-layer-vCtanIeB.d.ts → canvas-layer-G7ttVy7y.d.ts} +1 -1
- package/dist/chunk-4SRAQFTU.js +133 -0
- package/dist/chunk-CFGTPFUH.js +231 -0
- package/dist/chunk-DIPIJICH.js +8916 -0
- package/dist/chunk-EEABDLUC.js +2237 -0
- package/dist/chunk-EFWZWLPY.js +1895 -0
- package/dist/chunk-GXY7B3JG.js +46 -0
- package/dist/chunk-GYUZJQ7U.js +280 -0
- package/dist/chunk-H4J6IAJE.js +504 -0
- package/dist/chunk-KIRUKTBQ.js +187 -0
- package/dist/chunk-QWJEMUUT.js +94 -0
- package/dist/chunk-SV4QVUTQ.js +31 -0
- package/dist/chunk-U4YHUKKL.js +615 -0
- package/dist/chunk-VP43GIWW.js +18 -0
- package/dist/chunk-XDTZ4QMO.js +160 -0
- package/dist/default-tone-mapping-GgKPTiBP.d.ts +4 -0
- package/dist/{game-DzMcitDO.d.ts → game-k8K3hZai.d.ts} +85 -9
- package/dist/hot.d.ts +5 -4
- package/dist/hot.js +5 -52
- package/dist/index.d.ts +633 -1351
- package/dist/index.js +12873 -23068
- package/dist/{model-lod-CTpZ6imz.d.ts → model-lod-DPTzeCcD.d.ts} +35 -3
- package/dist/playtest.d.ts +5 -4
- package/dist/playtest.js +25 -75
- package/dist/quality-BYqKGAwo.d.ts +868 -0
- package/dist/react-css.js +1 -215
- package/dist/react.d.ts +3 -2
- package/dist/render-environment.d.ts +170 -0
- package/dist/render-environment.js +352 -0
- package/dist/{renderer-rZm0hVqq.d.ts → renderer-BtOSseYf.d.ts} +42 -35
- 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 +6 -384
- package/dist/worker.js +13 -43
- package/dist/world.d.ts +988 -268
- package/dist/world.js +17464 -18290
- package/mcp/animation-server.mjs +1 -1
- package/mcp/animation.mjs +2 -3
- package/mcp/engine-server.mjs +245 -52
- package/mcp/servers.mjs +10 -3
- package/mcp/terrain.mjs +8 -0
- package/package.json +19 -7
- package/patches/three@0.187.0-prd269-upgrade.patch +2102 -0
- package/patches/three@0.187.0.patch +2004 -407
- package/src/animation-batch.ts +193 -0
- package/src/animation-layers.ts +7 -3
- package/src/animation.ts +209 -29
- package/src/asset-cache.ts +3 -3
- package/src/asset-memo.ts +202 -0
- package/src/assets.ts +437 -125
- package/src/auto-batch.ts +190 -44
- package/src/card-lod.ts +152 -0
- package/src/compressed-texture-preparation.ts +1008 -0
- package/src/compute-driven.ts +1 -1
- package/src/config.ts +8 -0
- package/src/debug.ts +103 -22
- package/src/dev-global.ts +54 -0
- package/src/engine-hooks.ts +37 -0
- package/src/frame-budget.ts +50 -23
- package/src/game-boot-phase.ts +269 -0
- package/src/game-dev-tools.ts +86 -0
- package/src/game-frame-loop.ts +958 -0
- package/src/game-renderer-metrics.ts +77 -0
- package/src/game-startup.ts +1076 -0
- package/src/game-ui.ts +84 -0
- package/src/game-validate.ts +70 -0
- package/src/game.ts +198 -2264
- package/src/gamepad-mappings.ts +269 -0
- package/src/generated/console-markers.ts +13 -0
- package/src/geometry-capture.ts +28 -4
- package/src/gpu-readings.ts +98 -0
- package/src/gpu-scene-bvh.ts +149 -36
- package/src/hot.ts +4 -7
- package/src/index.ts +141 -9
- package/src/input.ts +237 -25
- package/src/instanced-batch-lod.ts +3 -8
- package/src/interpolation.ts +171 -40
- package/src/launch-diagnostics.ts +22 -12
- package/src/look/ao-admission.ts +25 -0
- package/src/look/auto-exposure.ts +5 -16
- package/src/look/default-tone-mapping.ts +41 -5
- package/src/look/filmic-tone-mapping.ts +15 -13
- package/src/look/local-exposure.ts +2 -7
- package/src/look/look-diagnostics.ts +3 -3
- package/src/look/quad-pass.ts +21 -0
- package/src/look/quality.ts +14 -5
- package/src/look/unreal-bloom.ts +4 -13
- package/src/look/world-environment.ts +113 -103
- package/src/loop.ts +29 -0
- package/src/merge-parts.ts +20 -24
- package/src/model-lod.ts +139 -17
- package/src/particle-kinds.ts +515 -0
- package/src/particle-significance.ts +12 -0
- package/src/particles.ts +19 -10
- package/src/picking.ts +17 -3
- package/src/platform.ts +23 -4
- package/src/playtest.ts +27 -1
- package/src/profiling/FrameStages.ts +5 -6
- package/src/profiling/Spans.ts +3 -17
- package/src/profiling/debug-telemetry.ts +2 -6
- package/src/profiling/render-list-validate.ts +6 -15
- package/src/projection-apply.ts +4 -1
- package/src/projection-marker.ts +2 -1
- package/src/projection-plan.ts +9 -6
- package/src/quiet-markers.ts +3 -10
- package/src/render/cpu-bvh.ts +100 -0
- package/src/render/depth-pyramid.ts +6 -14
- package/src/render/environment/environmentContribution.ts +202 -0
- package/src/render/environment/environmentRadiance.ts +44 -0
- package/src/render/environment/environmentSampleCache.ts +56 -0
- package/src/render/environment/environmentSampling.ts +201 -0
- package/src/render/environment/environmentSnapshot.ts +71 -0
- package/src/render/environment/gate.ts +57 -0
- package/src/render/mesh-pool.ts +4 -20
- package/src/render/probe-trace.ts +872 -0
- package/src/render/probe-volume.ts +780 -34
- package/src/render/radiance-cache.ts +17 -0
- 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-partial.ts +13 -0
- package/src/render/virtual-shadow.ts +231 -1008
- package/src/render/world-impostor-surface.ts +402 -40
- package/src/render/world-impostor.ts +5 -2
- package/src/render-camera-cull.ts +98 -3
- package/src/render-environment.ts +41 -0
- package/src/render-pass-budget.ts +34 -7
- package/src/renderProjection.ts +22 -0
- package/src/renderer-config.ts +36 -0
- package/src/renderer-facts.ts +37 -0
- package/src/renderer.ts +144 -65
- package/src/resolution-scaler.ts +3 -2
- package/src/scene-entry-inspectors.ts +61 -0
- package/src/snow-field.ts +2 -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-offscreen-bytes.ts +78 -0
- package/src/startup-readiness.ts +128 -34
- package/src/static-transform.ts +25 -19
- package/src/streamed-textures.ts +112 -0
- package/src/streaming.ts +68 -4
- package/src/texture-streaming.ts +331 -0
- package/src/version.ts +1 -1
- package/src/warmup.ts +56 -8
- package/src/water-surface.ts +9 -3
- package/src/worker.ts +12 -2
- package/src/world-bundle-safety.ts +446 -0
- package/src/world-cell-batches.ts +1178 -0
- package/src/world-cell-chunks.ts +1128 -0
- package/src/world-cell-data.ts +995 -0
- package/src/world-cell-impostors.ts +1461 -0
- package/src/world-cell-lod-bias.ts +151 -0
- package/src/world-cell-models.ts +1074 -0
- package/src/world-cell-options.ts +177 -0
- package/src/world-cell-placements.ts +798 -0
- package/src/world-cell-prewarm.ts +592 -0
- package/src/world-cell-readiness.ts +357 -0
- package/src/world-cell-records.ts +401 -0
- package/src/world-cell-render-bundles.ts +459 -0
- package/src/world-cell-render-gpu-dress.ts +710 -0
- package/src/world-cell-render-main-cull.ts +540 -0
- package/src/world-cell-render-shadow-claims.ts +198 -0
- package/src/world-cell-render-util.ts +259 -0
- package/src/world-cell-render.ts +1501 -0
- package/src/world-cell-residency.ts +754 -0
- package/src/world-cell-stale.ts +238 -0
- package/src/world-cells.ts +1335 -10418
- package/src/world-chunk-shadow.ts +308 -0
- package/src/world-collar.ts +315 -0
- package/src/world-gpu-flags.ts +131 -0
- package/src/world-gpu-readback.ts +881 -0
- package/src/world-gpu-reference.ts +524 -0
- package/src/world-gpu-scene.ts +256 -2286
- package/src/world-gpu-shadow.ts +203 -0
- package/src/world-gpu-types.ts +409 -0
- package/src/world-gpu-validation.ts +518 -0
- package/src/world-heightfield.ts +802 -0
- package/src/world-load-limiter.ts +111 -0
- package/src/world-model-sections.ts +80 -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-river-ribbon.ts +282 -0
- package/src/world-root-frame.ts +39 -0
- package/src/world-shore-field.ts +91 -0
- package/src/world-static-pose.ts +163 -0
- package/src/world-surface-sampler.ts +48 -0
- package/src/world-terrain-splat.ts +361 -153
- package/src/world-tile-budget.ts +249 -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 +247 -1755
- package/src/world-validate.ts +6 -14
- package/src/world-warm.ts +51 -0
- package/src/world-water-bodies.ts +359 -0
- package/src/world.ts +48 -741
|
@@ -0,0 +1,1475 @@
|
|
|
1
|
+
// Ported from Ultra Elements src/vfx/Particles.js @698b720 (MIT, Copyright (c) 2026 Humpalumps). JavaScript copied verbatim; GLSL transliterated to TSL.
|
|
2
|
+
import {
|
|
3
|
+
Break,
|
|
4
|
+
Discard,
|
|
5
|
+
Fn,
|
|
6
|
+
If,
|
|
7
|
+
Loop,
|
|
8
|
+
PI,
|
|
9
|
+
abs,
|
|
10
|
+
cameraPosition,
|
|
11
|
+
cameraProjectionMatrix,
|
|
12
|
+
cameraViewMatrix,
|
|
13
|
+
clamp,
|
|
14
|
+
cos,
|
|
15
|
+
dot,
|
|
16
|
+
exp,
|
|
17
|
+
float,
|
|
18
|
+
fract,
|
|
19
|
+
instanceIndex,
|
|
20
|
+
instancedArray,
|
|
21
|
+
int,
|
|
22
|
+
ivec2,
|
|
23
|
+
length,
|
|
24
|
+
max,
|
|
25
|
+
min,
|
|
26
|
+
mix,
|
|
27
|
+
mod,
|
|
28
|
+
normalize,
|
|
29
|
+
positionGeometry,
|
|
30
|
+
pow,
|
|
31
|
+
select,
|
|
32
|
+
sin,
|
|
33
|
+
smoothstep,
|
|
34
|
+
sqrt,
|
|
35
|
+
step,
|
|
36
|
+
texture,
|
|
37
|
+
textureLoad,
|
|
38
|
+
uniform,
|
|
39
|
+
varying,
|
|
40
|
+
vec2,
|
|
41
|
+
vec3,
|
|
42
|
+
vec4,
|
|
43
|
+
} from "three/tsl";
|
|
44
|
+
import {
|
|
45
|
+
BufferAttribute,
|
|
46
|
+
BufferGeometry,
|
|
47
|
+
Color,
|
|
48
|
+
type ColorRepresentation,
|
|
49
|
+
CylinderGeometry,
|
|
50
|
+
DataTexture,
|
|
51
|
+
DoubleSide,
|
|
52
|
+
Float32BufferAttribute,
|
|
53
|
+
FloatType,
|
|
54
|
+
FrontSide,
|
|
55
|
+
IcosahedronGeometry,
|
|
56
|
+
InstancedBufferGeometry,
|
|
57
|
+
Mesh,
|
|
58
|
+
MeshBasicNodeMaterial,
|
|
59
|
+
MeshStandardNodeMaterial,
|
|
60
|
+
NearestFilter,
|
|
61
|
+
type Node,
|
|
62
|
+
PlaneGeometry,
|
|
63
|
+
RGBAFormat,
|
|
64
|
+
Vector2,
|
|
65
|
+
Vector3,
|
|
66
|
+
} from "three/webgpu";
|
|
67
|
+
import { Q } from "../engine/quality.js";
|
|
68
|
+
import {
|
|
69
|
+
ARENA,
|
|
70
|
+
FX_BLENDING,
|
|
71
|
+
LINES_BLENDING,
|
|
72
|
+
U,
|
|
73
|
+
arenaNormal,
|
|
74
|
+
arenaSDF,
|
|
75
|
+
basisFrom,
|
|
76
|
+
blackbody,
|
|
77
|
+
curl3,
|
|
78
|
+
forceAccel,
|
|
79
|
+
fxOut,
|
|
80
|
+
groundUV,
|
|
81
|
+
hash11,
|
|
82
|
+
hash12,
|
|
83
|
+
hash31,
|
|
84
|
+
hash41,
|
|
85
|
+
hgPhase,
|
|
86
|
+
noise3,
|
|
87
|
+
randomInSphere,
|
|
88
|
+
randomOnSphere,
|
|
89
|
+
sceneViewZ,
|
|
90
|
+
spellLightScatter,
|
|
91
|
+
} from "./common.js";
|
|
92
|
+
|
|
93
|
+
type F = Node<"float">;
|
|
94
|
+
type V2 = Node<"vec2">;
|
|
95
|
+
type V3 = Node<"vec3">;
|
|
96
|
+
type V4 = Node<"vec4">;
|
|
97
|
+
type I = Node<"int">;
|
|
98
|
+
|
|
99
|
+
// One GPU particle pool for every spell (PLAN 2.5).
|
|
100
|
+
// - State: three RGBA32F textures, ping-ponged and written by one MRT draw:
|
|
101
|
+
// S0 = (pos.xyz, age) S1 = (vel.xyz, life) S2 = (kind + size * 0.1, seed, lastCollideTime, custom)
|
|
102
|
+
// fp16 isn't enough: v * dt drops below fp16 precision at arena scale.
|
|
103
|
+
// - Behaviour lives in a kind table texture (Niagara-style parameters, not code): sliders write
|
|
104
|
+
// texels, nothing recompiles.
|
|
105
|
+
// - Three render styles own row ranges so each draw is homogeneous:
|
|
106
|
+
// glow full-res additive velocity-stretched sparks / motes (lines scene)
|
|
107
|
+
// media half-res spherical billboards, weighted-blended OIT (fx scene)
|
|
108
|
+
// debris instanced lit meshes (opaque scene)
|
|
109
|
+
// Each range starts with the field-emitter blocks, then a ring buffer the CPU allocates from.
|
|
110
|
+
// - Emission: per-frame records in a small float texture; texels inside a record respawn from
|
|
111
|
+
// it. Records only exist on frames that emit, and a texel tests the frame's spans first.
|
|
112
|
+
// - Kind morphing on collision replaces GPU sub-emitters (droplet -> splash, ember -> smoulder);
|
|
113
|
+
// field emitters respawn dead texels on the floor where the ground field says so (steam, ground
|
|
114
|
+
// flames, frost mist) with no CPU involvement.
|
|
115
|
+
//
|
|
116
|
+
// Engine (ThreeNative's C++ engine over WebGPU) differences, each forced:
|
|
117
|
+
// - compute in place of the MRT ping-pong (the brief allows it): the state is three vec4 storage buffers (S0, S1,
|
|
118
|
+
// S2, one element per texel, index = y * W + x) and the sim is one compute kernel over W x H
|
|
119
|
+
// threads with the same per-texel maths in the same order. Every thread reads and writes only
|
|
120
|
+
// its own element, so updating in place is the ping-pong. Kind, emission and field tables are
|
|
121
|
+
// upstream's float DataTextures, read with texelFetch (textureLoad).
|
|
122
|
+
// - no int uniforms or uniform arrays: the int uniforms are float uniforms read through int(), and
|
|
123
|
+
// uSpan[6] is six vec2 uniforms.
|
|
124
|
+
// - no lines / opaque scenes: createFx adds all three styles to its fx group in the world scene;
|
|
125
|
+
// debris draws in the opaque queue, glow and media in the transparent one.
|
|
126
|
+
// - ENGINE-GAP: no multi-target render target and no point size, so painting the ground field
|
|
127
|
+
// (PAINT_VERT / PAINT_FRAG, Points into the ground's two targets) is not ported yet; `paint`
|
|
128
|
+
// stays empty.
|
|
129
|
+
// - the debris material gets the spell lights through the PointLights createFx mirrors them into
|
|
130
|
+
// (common.ts mirrorLights), where upstream added spellLightPBR to reflectedLight.
|
|
131
|
+
|
|
132
|
+
const KW = 11; // kind table texels per kind
|
|
133
|
+
const MAX_KINDS = 1024; // ~120 spells x ~6 kinds; one 10x1024 RGBA32F table (160 KB)
|
|
134
|
+
const MAX_REC = 64; // emission records per frame
|
|
135
|
+
const REC_W = 5; // texels per record
|
|
136
|
+
const MAX_FIELD = 8;
|
|
137
|
+
const LOG_N = 2048; // emission log entries per style
|
|
138
|
+
const STYLES = ["glow", "media", "debris"] as const;
|
|
139
|
+
type Style = (typeof STYLES)[number];
|
|
140
|
+
const PAINT_CHANNELS = ["heat", "char", "wet", "frost", "crack", "acid", "corruption", "glow"];
|
|
141
|
+
const SHAPES = { sphere: 0, shell: 1, disk: 2, ring: 3, line: 4, cone: 5 };
|
|
142
|
+
const MESHES = ["rock", "shard", "leaf"];
|
|
143
|
+
|
|
144
|
+
const KIND_DEFAULTS = {
|
|
145
|
+
style: "glow" as Style,
|
|
146
|
+
flicker: 0,
|
|
147
|
+
drag: 0.5,
|
|
148
|
+
gravity: 0,
|
|
149
|
+
buoyancy: 0,
|
|
150
|
+
curl: 0,
|
|
151
|
+
curlScale: 0.15,
|
|
152
|
+
forceResponse: 1,
|
|
153
|
+
restitution: -1,
|
|
154
|
+
friction: 0.2,
|
|
155
|
+
life: [1, 1] as number[],
|
|
156
|
+
size: [0.05, 0.05] as number[],
|
|
157
|
+
stretch: 0,
|
|
158
|
+
emissive: 1,
|
|
159
|
+
temp: null as number[] | null,
|
|
160
|
+
color: ["#ffffff", "#ffffff"] as ColorRepresentation[],
|
|
161
|
+
alpha: [1, 0] as number[],
|
|
162
|
+
paints: null as string | null,
|
|
163
|
+
paintAmount: 0.3,
|
|
164
|
+
morph: null as string | number | null,
|
|
165
|
+
killOnCollide: false,
|
|
166
|
+
absorbing: false,
|
|
167
|
+
toSmokeAt: 0,
|
|
168
|
+
smokeAlbedo: 0.12,
|
|
169
|
+
density: 3,
|
|
170
|
+
distortion: 0,
|
|
171
|
+
fadeIn: 0.05,
|
|
172
|
+
fadeOut: 0.3,
|
|
173
|
+
spin: 0,
|
|
174
|
+
sizePow: 1,
|
|
175
|
+
softness: 1,
|
|
176
|
+
lightResponse: 1,
|
|
177
|
+
mesh: "rock",
|
|
178
|
+
erosion: 0.5,
|
|
179
|
+
orbit: false,
|
|
180
|
+
};
|
|
181
|
+
export type KindDef = typeof KIND_DEFAULTS;
|
|
182
|
+
|
|
183
|
+
// --- shared GLSL ---------------------------------------------------------------------
|
|
184
|
+
// STATE_GLSL: the state buffers and the kind table (one shared texture node, read with texelFetch)
|
|
185
|
+
const storageOf = (n: number) => instancedArray(n, "vec4");
|
|
186
|
+
type Storage = ReturnType<typeof storageOf>;
|
|
187
|
+
const floatU = (v: number) => uniform(v);
|
|
188
|
+
type UF = ReturnType<typeof floatU>;
|
|
189
|
+
const vec2U = (v: Vector2) => uniform(v);
|
|
190
|
+
type UV2 = ReturnType<typeof vec2U>;
|
|
191
|
+
const texOf = (t: DataTexture) => texture(t);
|
|
192
|
+
type Tex = ReturnType<typeof texOf>;
|
|
193
|
+
// a float table (kind / emission / field): RGBA32F DataTexture written from the CPU, read with texelFetch
|
|
194
|
+
const tableOf = (data: Float32Array, w: number, h: number) => {
|
|
195
|
+
const t = new DataTexture(data, w, h, RGBAFormat, FloatType);
|
|
196
|
+
t.magFilter = t.minFilter = NearestFilter;
|
|
197
|
+
return t;
|
|
198
|
+
};
|
|
199
|
+
// texelFetch(tex, ivec2(x, y), 0)
|
|
200
|
+
const texelFetch = (tex: Tex, x: I, y: I): V4 =>
|
|
201
|
+
textureLoad(tex as Parameters<typeof textureLoad>[0], ivec2(x, y), int(0)) as unknown as V4;
|
|
202
|
+
interface IState {
|
|
203
|
+
uS0: Storage;
|
|
204
|
+
uS1: Storage;
|
|
205
|
+
uS2: Storage;
|
|
206
|
+
uKinds: Tex;
|
|
207
|
+
}
|
|
208
|
+
const kindTexel = (st: IState, kind: I, i: number): V4 => texelFetch(st.uKinds, int(i), kind);
|
|
209
|
+
const stateAt = (b: Storage, idx: I): V4 => b.element(idx) as unknown as V4;
|
|
210
|
+
|
|
211
|
+
// INDEX_GLSL: instance -> texel: field blocks first, then the live window of the ring
|
|
212
|
+
interface IStyleU {
|
|
213
|
+
uStyleStart: UF;
|
|
214
|
+
uStyleCount: UF;
|
|
215
|
+
uFieldCount: UF;
|
|
216
|
+
uRingBase: UF;
|
|
217
|
+
}
|
|
218
|
+
function particleIndex(su: IStyleU, id: I): I {
|
|
219
|
+
const field = int(su.uFieldCount);
|
|
220
|
+
const ring = int(su.uStyleCount).sub(field);
|
|
221
|
+
// GLSL int %: a - (a / b) * b (int division; TSL's mod is the float one)
|
|
222
|
+
const a = int(su.uRingBase).add(id).sub(field);
|
|
223
|
+
const b = select(ring.lessThan(int(1)), int(1), ring); // max(ring, 1)
|
|
224
|
+
const local = select(id.lessThan(field), id, field.add(a.sub(a.div(b).mul(b))));
|
|
225
|
+
return int(su.uStyleStart).add(local) as I;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// LOOK_GLSL: colour / alpha / size over life, shared by every style (struct Look -> plain object)
|
|
229
|
+
interface ILook {
|
|
230
|
+
color: V3;
|
|
231
|
+
alpha: F;
|
|
232
|
+
size: F;
|
|
233
|
+
smoke: F;
|
|
234
|
+
emissive: F;
|
|
235
|
+
}
|
|
236
|
+
function particleLook(st: IState, kind: I, t: F, sizeScale: F): ILook {
|
|
237
|
+
const k2 = kindTexel(st, kind, 2);
|
|
238
|
+
const k3 = kindTexel(st, kind, 3);
|
|
239
|
+
const k4 = kindTexel(st, kind, 4);
|
|
240
|
+
const k5 = kindTexel(st, kind, 5);
|
|
241
|
+
const k7 = kindTexel(st, kind, 7);
|
|
242
|
+
const k8 = kindTexel(st, kind, 8);
|
|
243
|
+
const size = mix(k2.z, k2.w, pow(t, k8.w)).mul(sizeScale);
|
|
244
|
+
const fade = smoothstep(0.0, max(k8.x, 1e-3), t).mul(
|
|
245
|
+
float(1.0).sub(smoothstep(float(1.0).sub(max(k8.y, 1e-3)), 1.0, t)),
|
|
246
|
+
);
|
|
247
|
+
const alpha = mix(k4.a, k5.a, t).mul(fade);
|
|
248
|
+
const emissive = k3.y;
|
|
249
|
+
const T = mix(k3.z, k3.w, t);
|
|
250
|
+
const color = select(
|
|
251
|
+
k3.z.greaterThan(0.0),
|
|
252
|
+
blackbody(T).mul(pow(T.div(max(k3.z, 1.0)), 3.0)), // hot starts bright and cools fast
|
|
253
|
+
mix(k4.rgb, k5.rgb, t),
|
|
254
|
+
);
|
|
255
|
+
const smoke = select(
|
|
256
|
+
k7.x.greaterThan(0.0),
|
|
257
|
+
smoothstep(k7.x, min(k7.x.add(0.25), 1.0), t),
|
|
258
|
+
float(0.0),
|
|
259
|
+
);
|
|
260
|
+
return {
|
|
261
|
+
color: color as V3,
|
|
262
|
+
alpha: alpha as F,
|
|
263
|
+
size: size as F,
|
|
264
|
+
smoke: smoke as F,
|
|
265
|
+
emissive: emissive as F,
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// --- sim ---------------------------------------------------------------------------------
|
|
270
|
+
interface ISimU {
|
|
271
|
+
uEmitTex: Tex;
|
|
272
|
+
uEmitCount: UF;
|
|
273
|
+
uSpan: UV2[];
|
|
274
|
+
uSpanCount: UF;
|
|
275
|
+
uFieldTex: Tex;
|
|
276
|
+
uFieldCount: UF;
|
|
277
|
+
uSimDt: UF;
|
|
278
|
+
uGroundA: Tex;
|
|
279
|
+
uGroundB: Tex;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function simKernel(W: number, H: number, st: IState, u: ISimU) {
|
|
283
|
+
// the sim reads the ground field from a compute kernel: level 0 (upstream's fragment derivatives)
|
|
284
|
+
const groundA = (xz: V2): V4 => u.uGroundA.sample(groundUV(xz)).level(float(0)) as unknown as V4;
|
|
285
|
+
const groundB = (xz: V2): V4 => u.uGroundB.sample(groundUV(xz)).level(float(0)) as unknown as V4;
|
|
286
|
+
return Fn(() => {
|
|
287
|
+
const idx = int(instanceIndex).toVar();
|
|
288
|
+
const o0 = vec4(0.0).toVar();
|
|
289
|
+
const o1 = vec4(0.0).toVar();
|
|
290
|
+
const o2 = vec4(0.0).toVar();
|
|
291
|
+
|
|
292
|
+
const spawn = (kind: I, pos: V3, vel: V3, lifeScale: F, sizeScale: F, seed: F) => {
|
|
293
|
+
const k2 = kindTexel(st, kind, 2);
|
|
294
|
+
const life = mix(k2.x, k2.y, hash11(seed.mul(7.13))).mul(lifeScale);
|
|
295
|
+
o0.assign(vec4(pos, 0.0));
|
|
296
|
+
o1.assign(vec4(vel, max(life, 1e-3)));
|
|
297
|
+
o2.assign(vec4(float(kind).add(clamp(sizeScale, 0.0, 9.99).mul(0.1)), seed, -1e4, 0.0));
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const tryEmit = () => {
|
|
301
|
+
const emitted = int(0).toVar();
|
|
302
|
+
let inSpan = float(idx).lessThan(-1.0);
|
|
303
|
+
for (let s = 0; s < 6; s++) {
|
|
304
|
+
const span = u.uSpan[s] as UV2;
|
|
305
|
+
inSpan = inSpan.or(
|
|
306
|
+
u.uSpanCount
|
|
307
|
+
.greaterThan(s)
|
|
308
|
+
.and(float(idx).greaterThanEqual(span.x))
|
|
309
|
+
.and(float(idx).lessThan(span.y)),
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
If(inSpan, () => {
|
|
313
|
+
Loop({ start: 0, end: MAX_REC, type: "int", condition: "<" }, ({ i: r }: { i: I }) => {
|
|
314
|
+
If(r.greaterThanEqual(int(u.uEmitCount)), () => {
|
|
315
|
+
Break();
|
|
316
|
+
});
|
|
317
|
+
const e0 = texelFetch(u.uEmitTex, int(0), r);
|
|
318
|
+
const j = idx.sub(int(e0.x)).toVar();
|
|
319
|
+
If(j.greaterThanEqual(int(0)).and(j.lessThan(int(e0.y))), () => {
|
|
320
|
+
const e1 = texelFetch(u.uEmitTex, int(1), r);
|
|
321
|
+
const e2 = texelFetch(u.uEmitTex, int(2), r);
|
|
322
|
+
const e3 = texelFetch(u.uEmitTex, int(3), r);
|
|
323
|
+
const e4 = texelFetch(u.uEmitTex, int(4), r);
|
|
324
|
+
const seed = fract(e3.w.add(float(j).mul(0.618034)))
|
|
325
|
+
.mul(1000.0)
|
|
326
|
+
.add(float(j).mul(0.001))
|
|
327
|
+
.toVar();
|
|
328
|
+
const h = hash41(seed).toVar();
|
|
329
|
+
const h2 = hash31(seed.add(17.0)).toVar();
|
|
330
|
+
const shape = int(e0.w).toVar();
|
|
331
|
+
const pos = e1.xyz.toVar();
|
|
332
|
+
const radial = vec3(0.0).toVar();
|
|
333
|
+
If(shape.equal(int(0)), () => {
|
|
334
|
+
const d = randomInSphere(h.xyz).toVar();
|
|
335
|
+
pos.addAssign(d.mul(e1.w));
|
|
336
|
+
radial.assign(normalize(d.add(1e-5)));
|
|
337
|
+
})
|
|
338
|
+
.ElseIf(shape.equal(int(1)), () => {
|
|
339
|
+
const d = randomOnSphere(h.xy).toVar();
|
|
340
|
+
pos.addAssign(d.mul(e1.w));
|
|
341
|
+
radial.assign(d);
|
|
342
|
+
})
|
|
343
|
+
.ElseIf(shape.equal(int(2)).or(shape.equal(int(3))), () => {
|
|
344
|
+
const a = h.x.mul(2.0).mul(PI);
|
|
345
|
+
const rr = select(shape.equal(int(2)), sqrt(h.y), float(1.0));
|
|
346
|
+
radial.assign(vec3(cos(a), 0.0, sin(a)));
|
|
347
|
+
pos.addAssign(radial.mul(rr).mul(e1.w));
|
|
348
|
+
})
|
|
349
|
+
.ElseIf(shape.equal(int(4)), () => {
|
|
350
|
+
const d = randomInSphere(h2).toVar();
|
|
351
|
+
pos.assign(mix(e1.xyz, e3.xyz, h.x).add(d.mul(e1.w)));
|
|
352
|
+
radial.assign(normalize(d.add(1e-5)));
|
|
353
|
+
})
|
|
354
|
+
.Else(() => {
|
|
355
|
+
// cone around e3.xyz, half-angle e4.w
|
|
356
|
+
pos.addAssign(randomInSphere(h2).mul(e1.w));
|
|
357
|
+
const ax = normalize(e3.xyz.add(1e-6));
|
|
358
|
+
const ct = mix(1.0, cos(e4.w), h.x).toVar();
|
|
359
|
+
const stt = sqrt(float(1.0).sub(ct.mul(ct)));
|
|
360
|
+
const ph = h.y.mul(2.0).mul(PI);
|
|
361
|
+
radial.assign(basisFrom(ax).mul(vec3(stt.mul(cos(ph)), stt.mul(sin(ph)), ct)));
|
|
362
|
+
});
|
|
363
|
+
const vel = e2.xyz
|
|
364
|
+
.add(randomInSphere(hash31(seed.add(31.0))).mul(e2.w))
|
|
365
|
+
.add(radial.mul(e4.x).mul(mix(0.6, 1.0, h.w)));
|
|
366
|
+
spawn(int(e0.z), pos, vel as V3, e4.z, e4.y, seed);
|
|
367
|
+
emitted.assign(int(1));
|
|
368
|
+
Break();
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
return emitted;
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
// field emitters: dead texels in a block respawn on the floor where the ground field qualifies
|
|
376
|
+
const tryField = () => {
|
|
377
|
+
Loop({ start: 0, end: MAX_FIELD, type: "int", condition: "<" }, ({ i: f }: { i: I }) => {
|
|
378
|
+
If(f.greaterThanEqual(int(u.uFieldCount)), () => {
|
|
379
|
+
Break();
|
|
380
|
+
});
|
|
381
|
+
const f0 = texelFetch(u.uFieldTex, int(0), f);
|
|
382
|
+
const j = idx.sub(int(f0.x)).toVar();
|
|
383
|
+
If(j.greaterThanEqual(int(0)).and(j.lessThan(int(f0.y))), () => {
|
|
384
|
+
const wa = texelFetch(u.uFieldTex, int(1), f);
|
|
385
|
+
const wb = texelFetch(u.uFieldTex, int(2), f);
|
|
386
|
+
const f3 = texelFetch(u.uFieldTex, int(3), f);
|
|
387
|
+
const seed = hash12(vec2(float(idx), mod(U.uFrame, 4096.0)))
|
|
388
|
+
.mul(1000.0)
|
|
389
|
+
.toVar();
|
|
390
|
+
const h = hash41(seed).toVar();
|
|
391
|
+
const a = h.x.mul(2.0).mul(PI);
|
|
392
|
+
const r = sqrt(h.y).mul(Number((ARENA.radius - 0.4).toFixed(2)));
|
|
393
|
+
const xz = vec2(cos(a), sin(a)).mul(r).toVar();
|
|
394
|
+
const s = dot(groundA(xz), wa)
|
|
395
|
+
.add(dot(groundB(xz), wb))
|
|
396
|
+
.sub(f0.w)
|
|
397
|
+
.toVar();
|
|
398
|
+
// h.z > s * rate * dt: stays dead this frame
|
|
399
|
+
If(h.z.lessThanEqual(s.mul(f3.x).mul(u.uSimDt)), () => {
|
|
400
|
+
const pos = vec3(xz.x, 0.04, xz.y).toVar();
|
|
401
|
+
const pq = abs(
|
|
402
|
+
xz.sub(
|
|
403
|
+
vec2(
|
|
404
|
+
Number(ARENA.pool.center.x.toFixed(3)),
|
|
405
|
+
Number(ARENA.pool.center.y.toFixed(3)),
|
|
406
|
+
),
|
|
407
|
+
),
|
|
408
|
+
);
|
|
409
|
+
If(
|
|
410
|
+
pq.x
|
|
411
|
+
.lessThan(Number(ARENA.pool.half.x.toFixed(3)))
|
|
412
|
+
.and(pq.y.lessThan(Number(ARENA.pool.half.y.toFixed(3)))),
|
|
413
|
+
() => {
|
|
414
|
+
pos.y.addAssign(Number(ARENA.pool.water.toFixed(3)));
|
|
415
|
+
},
|
|
416
|
+
);
|
|
417
|
+
const vel = vec3(0.0, f3.w, 0.0).add(randomInSphere(hash31(seed.add(5.0))).mul(0.3));
|
|
418
|
+
spawn(
|
|
419
|
+
int(f0.z),
|
|
420
|
+
pos,
|
|
421
|
+
vel as V3,
|
|
422
|
+
f3.y,
|
|
423
|
+
f3.z.mul(clamp(s.mul(2.0), 0.5, 1.5)) as F,
|
|
424
|
+
seed,
|
|
425
|
+
);
|
|
426
|
+
});
|
|
427
|
+
Break();
|
|
428
|
+
});
|
|
429
|
+
});
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
If(idx.lessThan(int(W * H)), () => {
|
|
433
|
+
const s0 = stateAt(st.uS0, idx).toVar();
|
|
434
|
+
const s1 = stateAt(st.uS1, idx).toVar();
|
|
435
|
+
const dead = s0.w.greaterThanEqual(s1.w).toVar();
|
|
436
|
+
If(dead, () => {
|
|
437
|
+
o0.assign(vec4(0.0, -1000.0, 0.0, 1e9));
|
|
438
|
+
o1.assign(vec4(0.0, 0.0, 0.0, 1.0));
|
|
439
|
+
o2.assign(vec4(0.0, 0.0, -1e4, 0.0));
|
|
440
|
+
});
|
|
441
|
+
// a texel re-allocated by the ring this frame restarts even if still alive (pool full: oldest die)
|
|
442
|
+
const emitted = tryEmit();
|
|
443
|
+
If(emitted.equal(int(0)), () => {
|
|
444
|
+
If(dead, () => {
|
|
445
|
+
tryField();
|
|
446
|
+
}).Else(() => {
|
|
447
|
+
const s2 = stateAt(st.uS2, idx).toVar();
|
|
448
|
+
const kind = int(s2.x).toVar();
|
|
449
|
+
const sizeScale = fract(s2.x).mul(10.0).toVar();
|
|
450
|
+
const k0 = kindTexel(st, kind, 0).toVar();
|
|
451
|
+
const k1 = kindTexel(st, kind, 1).toVar();
|
|
452
|
+
const k2 = kindTexel(st, kind, 2).toVar();
|
|
453
|
+
const k6 = kindTexel(st, kind, 6).toVar();
|
|
454
|
+
const dt = u.uSimDt;
|
|
455
|
+
const pos = s0.xyz.toVar();
|
|
456
|
+
const vel = s1.xyz.toVar();
|
|
457
|
+
const age = s0.w.add(dt).toVar();
|
|
458
|
+
const life = s1.w.toVar();
|
|
459
|
+
const t = age.div(life).toVar();
|
|
460
|
+
const kill = float(0.0).toVar();
|
|
461
|
+
|
|
462
|
+
const acc = vec3(0.0, k0.z.mul(float(1.0).sub(t.mul(0.6))).sub(k0.y), 0.0).toVar();
|
|
463
|
+
If(k0.w.notEqual(0.0), () => {
|
|
464
|
+
acc.addAssign(
|
|
465
|
+
curl3(
|
|
466
|
+
pos
|
|
467
|
+
.mul(k1.x)
|
|
468
|
+
.add(vec3(0.0, U.uTime.mul(0.05), 0.0))
|
|
469
|
+
.add(s2.y.mul(0.01)),
|
|
470
|
+
).mul(k0.w),
|
|
471
|
+
);
|
|
472
|
+
});
|
|
473
|
+
If(k1.y.notEqual(0.0), () => {
|
|
474
|
+
acc.addAssign(forceAccel(pos, vel, kill).mul(k1.y));
|
|
475
|
+
});
|
|
476
|
+
vel.addAssign(acc.mul(dt));
|
|
477
|
+
vel.mulAssign(exp(k0.x.negate().mul(dt)));
|
|
478
|
+
const np = pos.add(vel.mul(dt)).toVar();
|
|
479
|
+
|
|
480
|
+
// collision with the arena (floor, rim, pillars, dummies, dynamic capsules)
|
|
481
|
+
const custom = s2.w.toVar();
|
|
482
|
+
const collide = s2.z.toVar();
|
|
483
|
+
If(k1.z.greaterThanEqual(0.0), () => {
|
|
484
|
+
const rad = max(mix(k2.z, k2.w, t).mul(sizeScale).mul(0.5), 0.01).toVar();
|
|
485
|
+
const d = arenaSDF(np).toVar();
|
|
486
|
+
If(d.lessThan(rad), () => {
|
|
487
|
+
const n = arenaNormal(np).toVar();
|
|
488
|
+
np.addAssign(n.mul(rad.sub(d)));
|
|
489
|
+
const vn = dot(vel, n).toVar();
|
|
490
|
+
If(vn.lessThan(0.0), () => {
|
|
491
|
+
const vt = vel.sub(n.mul(vn)).toVar();
|
|
492
|
+
vel.assign(vt.mul(float(1.0).sub(k1.w)).sub(n.mul(vn).mul(k1.z)));
|
|
493
|
+
});
|
|
494
|
+
collide.assign(U.uTime);
|
|
495
|
+
const flags = int(k6.w);
|
|
496
|
+
If(flags.bitAnd(int(2)).notEqual(int(0)), () => {
|
|
497
|
+
kill.assign(1.0);
|
|
498
|
+
});
|
|
499
|
+
If(k6.z.greaterThan(0.5), () => {
|
|
500
|
+
// morph into another kind, keeping position and the bounce
|
|
501
|
+
const m = int(k6.z).sub(int(1)).toVar();
|
|
502
|
+
const m2 = kindTexel(st, m, 2);
|
|
503
|
+
kind.assign(m);
|
|
504
|
+
age.assign(0.0);
|
|
505
|
+
life.assign(mix(m2.x, m2.y, hash11(s2.y.mul(3.7))));
|
|
506
|
+
});
|
|
507
|
+
// debris: remember when it came to rest (its spin freezes there)
|
|
508
|
+
If(
|
|
509
|
+
custom
|
|
510
|
+
.lessThanEqual(0.0)
|
|
511
|
+
.and(dot(vel, vel).lessThan(0.04))
|
|
512
|
+
.and(k6.x.greaterThan(2.5)),
|
|
513
|
+
() => {
|
|
514
|
+
custom.assign(age);
|
|
515
|
+
},
|
|
516
|
+
);
|
|
517
|
+
});
|
|
518
|
+
});
|
|
519
|
+
// orbit hook: keep a Kepler-ish disk tidy (accretion), uses force 'orbit' for speed
|
|
520
|
+
If(kill.greaterThan(0.5), () => {
|
|
521
|
+
age.assign(life);
|
|
522
|
+
});
|
|
523
|
+
o0.assign(vec4(np, age));
|
|
524
|
+
o1.assign(vec4(vel, life));
|
|
525
|
+
o2.assign(vec4(float(kind).add(sizeScale.mul(0.1)), s2.y, collide, custom));
|
|
526
|
+
});
|
|
527
|
+
});
|
|
528
|
+
stateAt(st.uS0, idx).assign(o0);
|
|
529
|
+
stateAt(st.uS1, idx).assign(o1);
|
|
530
|
+
stateAt(st.uS2, idx).assign(o2);
|
|
531
|
+
});
|
|
532
|
+
})().compute(W * H);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// upstream's clearState draw: every texel back to zero (age 0 >= life 0 counts as dead)
|
|
536
|
+
function clearKernel(W: number, H: number, st: IState) {
|
|
537
|
+
return Fn(() => {
|
|
538
|
+
const idx = int(instanceIndex);
|
|
539
|
+
If(idx.lessThan(int(W * H)), () => {
|
|
540
|
+
stateAt(st.uS0, idx).assign(vec4(0.0));
|
|
541
|
+
stateAt(st.uS1, idx).assign(vec4(0.0));
|
|
542
|
+
stateAt(st.uS2, idx).assign(vec4(0.0));
|
|
543
|
+
});
|
|
544
|
+
})().compute(W * H);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// projectionMatrix[1][1] (column 1, row 1)
|
|
548
|
+
const projYY = (): F => cameraProjectionMatrix.mul(vec4(0.0, 1.0, 0.0, 0.0)).y as unknown as F;
|
|
549
|
+
// gl_Position for a dead instance: outside the clip volume
|
|
550
|
+
const OFFSCREEN = () => vec4(2.0, 2.0, 2.0, 1.0);
|
|
551
|
+
|
|
552
|
+
// --- glow: velocity-stretched additive quads, full res ---------------------------------
|
|
553
|
+
function glowMaterial(st: IState, su: IStyleU) {
|
|
554
|
+
const mat = new MeshBasicNodeMaterial({ ...LINES_BLENDING });
|
|
555
|
+
mat.fog = false;
|
|
556
|
+
const idx = particleIndex(su, int(instanceIndex));
|
|
557
|
+
const s0 = stateAt(st.uS0, idx);
|
|
558
|
+
const s1 = stateAt(st.uS1, idx);
|
|
559
|
+
const dead = s0.w.greaterThanEqual(s1.w);
|
|
560
|
+
const s2 = stateAt(st.uS2, idx);
|
|
561
|
+
const kind = int(s2.x);
|
|
562
|
+
const t = s0.w.div(s1.w);
|
|
563
|
+
const l = particleLook(st, kind, t, fract(s2.x).mul(10.0));
|
|
564
|
+
const k3 = kindTexel(st, kind, 3);
|
|
565
|
+
const headV = cameraViewMatrix.mul(vec4(s0.xyz, 1.0)).xyz;
|
|
566
|
+
const tailV = cameraViewMatrix.mul(vec4(s0.xyz.sub(s1.xyz.mul(k3.x)), 1.0)).xyz;
|
|
567
|
+
const viewZ = max(headV.z.negate(), 0.05);
|
|
568
|
+
const pxWorld = viewZ.mul(2.0).div(projYY().mul(U.uResolution.y));
|
|
569
|
+
const size = l.size;
|
|
570
|
+
const w = max(size, pxWorld.mul(1.5));
|
|
571
|
+
const aa = size.div(w); // Humus phone-wire AA: keep energy when clamped to 1.5 px
|
|
572
|
+
const axis = tailV.xy.sub(headV.xy);
|
|
573
|
+
const len = length(axis);
|
|
574
|
+
const dir = select(len.greaterThan(1e-4), axis.div(len), vec2(1.0, 0.0));
|
|
575
|
+
const perp = vec2(dir.y.negate(), dir.x);
|
|
576
|
+
// quad: x in [-1, 1] spans head (-1) .. tail (+1) plus caps, y across
|
|
577
|
+
const position = positionGeometry;
|
|
578
|
+
const off = dir
|
|
579
|
+
.mul(select(position.x.lessThan(0.0), w.negate(), len.add(w)))
|
|
580
|
+
.add(perp.mul(position.y).mul(w));
|
|
581
|
+
const pv = headV.add(vec3(off, 0.0));
|
|
582
|
+
mat.vertexNode = select(dead, OFFSCREEN(), cameraProjectionMatrix.mul(vec4(pv, 1.0)));
|
|
583
|
+
const vUv = varying(vec2(select(position.x.lessThan(0.0), -1.0, 1.0), position.y));
|
|
584
|
+
const vSoft = varying(len.div(len.add(w.mul(2.0)))); // how much of the quad is the stretched body
|
|
585
|
+
const nearFade = smoothstep(0.3, 1.2, viewZ);
|
|
586
|
+
// stretching spreads the same energy over a longer streak (motion-blur conservation)
|
|
587
|
+
const fk = kindTexel(st, kind, 10).x;
|
|
588
|
+
const ph = s2.y.mul(97.0).add(s0.w.mul(mix(9.0, 23.0, fract(s2.y.mul(13.7)))));
|
|
589
|
+
const vColor = varying(
|
|
590
|
+
select(
|
|
591
|
+
dead,
|
|
592
|
+
vec3(0.0),
|
|
593
|
+
l.color
|
|
594
|
+
.mul(l.emissive)
|
|
595
|
+
.mul(l.alpha)
|
|
596
|
+
.mul(aa)
|
|
597
|
+
.mul(nearFade)
|
|
598
|
+
.mul(w.mul(2.0).div(len.add(w.mul(2.0))))
|
|
599
|
+
.mul(
|
|
600
|
+
float(1.0).sub(
|
|
601
|
+
fk.mul(
|
|
602
|
+
float(0.5).add(
|
|
603
|
+
float(0.5)
|
|
604
|
+
.mul(sin(ph))
|
|
605
|
+
.mul(sin(ph.mul(0.37).add(1.3))),
|
|
606
|
+
),
|
|
607
|
+
),
|
|
608
|
+
),
|
|
609
|
+
),
|
|
610
|
+
),
|
|
611
|
+
);
|
|
612
|
+
|
|
613
|
+
// GLOW_FRAG
|
|
614
|
+
mat.fragmentNode = Fn(() => {
|
|
615
|
+
// capsule falloff: distance from the centre segment in quad units
|
|
616
|
+
const x = vUv.x.mul(0.5).add(0.5); // 0 head .. 1 tail
|
|
617
|
+
const ends = float(1.0).sub(vSoft);
|
|
618
|
+
const he = ends.mul(0.5);
|
|
619
|
+
const dx = select(
|
|
620
|
+
x.lessThan(he),
|
|
621
|
+
he.sub(x).div(max(he, 1e-3)),
|
|
622
|
+
select(x.greaterThan(float(1.0).sub(he)), x.sub(1.0).add(he).div(max(he, 1e-3)), float(0.0)),
|
|
623
|
+
);
|
|
624
|
+
const d2 = dx.mul(dx).add(vUv.y.mul(vUv.y));
|
|
625
|
+
const core = exp(d2.negate().mul(4.0));
|
|
626
|
+
const tailFade = mix(1.0, 0.35, x); // brighter at the head
|
|
627
|
+
// engine: LINES_BLENDING is AdditiveBlending (SrcAlpha, One), so alpha 1 is upstream's ONE/ONE
|
|
628
|
+
return vec4(vColor.mul(core).mul(tailFade), 1.0);
|
|
629
|
+
})();
|
|
630
|
+
return mat;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
// --- media: spherical billboards into the half-res WBOIT pass ---------------------------
|
|
634
|
+
// spellLightScatter loops over the lights: a Fn, so the loop has a function to live in (gLightNoShadow)
|
|
635
|
+
const mediaScatter = Fn(([p, v]: [V3, V3]) => spellLightScatter(p, v, float(0.35), true));
|
|
636
|
+
function mediaMaterial(st: IState, su: IStyleU) {
|
|
637
|
+
const mat = new MeshBasicNodeMaterial({ ...FX_BLENDING });
|
|
638
|
+
mat.fog = false;
|
|
639
|
+
const idx = particleIndex(su, int(instanceIndex));
|
|
640
|
+
const s0 = stateAt(st.uS0, idx);
|
|
641
|
+
const s1 = stateAt(st.uS1, idx);
|
|
642
|
+
const s2 = stateAt(st.uS2, idx);
|
|
643
|
+
const kind = int(s2.x);
|
|
644
|
+
const t = s0.w.div(s1.w);
|
|
645
|
+
const l = particleLook(st, kind, t, fract(s2.x).mul(10.0));
|
|
646
|
+
const k6 = kindTexel(st, kind, 6);
|
|
647
|
+
const k7 = kindTexel(st, kind, 7);
|
|
648
|
+
const k9 = kindTexel(st, kind, 9);
|
|
649
|
+
const cv = cameraViewMatrix.mul(vec4(s0.xyz, 1.0)).xyz;
|
|
650
|
+
const viewZ = cv.z.negate();
|
|
651
|
+
const dead = s0.w.greaterThanEqual(s1.w).or(viewZ.lessThan(0.05));
|
|
652
|
+
// overdraw guard: never wider than 12% of the screen height
|
|
653
|
+
const maxR = viewZ.mul(0.12).div(projYY());
|
|
654
|
+
const r = min(l.size.mul(0.5), maxR);
|
|
655
|
+
const spin = s2.y.add(s0.w.mul(kindTexel(st, kind, 8).z));
|
|
656
|
+
const c = vec2(cos(spin), sin(spin));
|
|
657
|
+
const position = positionGeometry;
|
|
658
|
+
// mat2(c.x, -c.y, c.y, c.x) * position.xy (GLSL mat2 is column-major)
|
|
659
|
+
const q = vec2(
|
|
660
|
+
c.x.mul(position.x).add(c.y.mul(position.y)),
|
|
661
|
+
c.y.negate().mul(position.x).add(c.x.mul(position.y)),
|
|
662
|
+
);
|
|
663
|
+
mat.vertexNode = select(
|
|
664
|
+
dead,
|
|
665
|
+
OFFSCREEN(),
|
|
666
|
+
cameraProjectionMatrix.mul(vec4(cv.add(vec3(q.mul(r), 0.0)), 1.0)),
|
|
667
|
+
);
|
|
668
|
+
const vUv = varying(position.xy);
|
|
669
|
+
const vViewZ = varying(viewZ);
|
|
670
|
+
const vRadius = varying(r);
|
|
671
|
+
const vDensity = varying(k7.z);
|
|
672
|
+
const vErode = varying(k9.z.mul(t)); // erosion grows with age
|
|
673
|
+
const vDistort = varying(k7.w.mul(l.alpha));
|
|
674
|
+
const vNoise = varying(s0.xyz.mul(0.35).add(s2.y));
|
|
675
|
+
const absorbing = int(k6.w).bitAnd(int(1)).notEqual(int(0));
|
|
676
|
+
const smoke = select(absorbing, float(1.0), l.smoke);
|
|
677
|
+
// lighting for the scattering part: sun with forward scattering, sky, spell lights
|
|
678
|
+
// gLightNoShadow = true: spellLightScatter(..., noShadow) below
|
|
679
|
+
const v = normalize(cameraPosition.sub(s0.xyz));
|
|
680
|
+
const g = float(0.35);
|
|
681
|
+
// flames glow; what scatters light is their soot, never their own blackbody colour
|
|
682
|
+
const albedo = select(
|
|
683
|
+
absorbing,
|
|
684
|
+
l.color,
|
|
685
|
+
select(l.emissive.greaterThan(0.0), vec3(k7.y), mix(l.color, vec3(k7.y), smoke)),
|
|
686
|
+
);
|
|
687
|
+
const vSun = varying(
|
|
688
|
+
albedo
|
|
689
|
+
.mul(U.uSunColor)
|
|
690
|
+
.mul(hgPhase(dot(U.uSunDir, v.negate()), g))
|
|
691
|
+
.mul(4.0 * Math.PI * 0.6),
|
|
692
|
+
);
|
|
693
|
+
const vAmb = varying(albedo.mul(U.uAmbient.mul(1.4).add(mediaScatter(s0.xyz, v).mul(k9.x))));
|
|
694
|
+
// sun and up in the sprite's own frame (view space turned back by the spin), so the fragment
|
|
695
|
+
// can shade the billboard as the sphere it stands for
|
|
696
|
+
const sv = cameraViewMatrix.mul(vec4(U.uSunDir, 0.0)).xyz;
|
|
697
|
+
const up = cameraViewMatrix.mul(vec4(0.0, 1.0, 0.0, 0.0)).xyz;
|
|
698
|
+
// unspin = mat2(c.x, c.y, -c.y, c.x)
|
|
699
|
+
const unspin = (p: V2) => vec2(c.x.mul(p.x).sub(c.y.mul(p.y)), c.y.mul(p.x).add(c.x.mul(p.y)));
|
|
700
|
+
const vSunL = varying(vec3(unspin(sv.xy), sv.z));
|
|
701
|
+
const vUpL = varying(vec3(unspin(up.xy), up.z));
|
|
702
|
+
// emission fades as fire turns to smoke
|
|
703
|
+
const vEmit = varying(
|
|
704
|
+
select(
|
|
705
|
+
l.emissive.greaterThan(0.0).and(absorbing.not()),
|
|
706
|
+
l.color.mul(l.emissive).mul(float(1.0).sub(smoke)),
|
|
707
|
+
vec3(0.0),
|
|
708
|
+
),
|
|
709
|
+
);
|
|
710
|
+
// pure emitters (flames before they smoke) are mostly glow, little occlusion
|
|
711
|
+
const vAlpha = varying(
|
|
712
|
+
select(
|
|
713
|
+
dead,
|
|
714
|
+
float(0.0),
|
|
715
|
+
l.alpha
|
|
716
|
+
.mul(mix(float(0.25).add(smoke.mul(0.75)), 1.0, step(l.emissive, 0.0)))
|
|
717
|
+
.mul(smoothstep(0.2, 1.0, viewZ)),
|
|
718
|
+
),
|
|
719
|
+
);
|
|
720
|
+
|
|
721
|
+
// MEDIA_FRAG
|
|
722
|
+
mat.fragmentNode = Fn(() => {
|
|
723
|
+
If(vAlpha.lessThanEqual(0.001), () => {
|
|
724
|
+
Discard();
|
|
725
|
+
});
|
|
726
|
+
const r2 = dot(vUv, vUv);
|
|
727
|
+
If(r2.greaterThanEqual(1.0), () => {
|
|
728
|
+
Discard();
|
|
729
|
+
});
|
|
730
|
+
// spherical billboard (Umenhoffer et al. 2006): visible thickness of a sphere, cut by the scene
|
|
731
|
+
const h = sqrt(max(float(1.0).sub(r2), 0.0)).mul(vRadius);
|
|
732
|
+
const sceneZ = sceneViewZ();
|
|
733
|
+
const thick = clamp(
|
|
734
|
+
min(vViewZ.add(h), sceneZ).sub(max(vViewZ.sub(h), U.uNearFar.x)),
|
|
735
|
+
0.0,
|
|
736
|
+
h.mul(2.0),
|
|
737
|
+
);
|
|
738
|
+
// eroded by world-space noise so a sprite reads as a lumpy volume; a finer octave rags the edges
|
|
739
|
+
const n = noise3(vNoise.add(vec3(vUv.mul(0.42), 0.0)))
|
|
740
|
+
.mul(0.72)
|
|
741
|
+
.add(noise3(vNoise.mul(2.7).add(vec3(vUv.mul(1.1), 0.0))).mul(0.28));
|
|
742
|
+
const er = smoothstep(vErode, vErode.add(0.35), n.mul(float(1.15).sub(r2.mul(0.6))));
|
|
743
|
+
// billows and gaps from the first frame of a puff's life, not only once it erodes
|
|
744
|
+
const b = smoothstep(0.22, 0.62, n);
|
|
745
|
+
const a = float(1.0)
|
|
746
|
+
.sub(
|
|
747
|
+
exp(
|
|
748
|
+
vDensity
|
|
749
|
+
.negate()
|
|
750
|
+
.mul(thick)
|
|
751
|
+
.mul(float(0.35).add(b.mul(0.65))),
|
|
752
|
+
),
|
|
753
|
+
)
|
|
754
|
+
.mul(er)
|
|
755
|
+
.mul(vAlpha);
|
|
756
|
+
// shading: a dense puff is a lit sphere with a dark side, a dark core against the sun and billows
|
|
757
|
+
// that shadow each other (noise denser towards the sun = in shadow, one extra tap); thin wisps
|
|
758
|
+
// scatter evenly. Normalised so a front-lit puff keeps about its old mean brightness.
|
|
759
|
+
const N = vec3(vUv, sqrt(max(float(1.0).sub(r2), 0.0)));
|
|
760
|
+
const dense = float(1.0).sub(exp(vDensity.negate().mul(vRadius).mul(2.0)));
|
|
761
|
+
const ns = noise3(vNoise.add(vec3(vUv.add(vSunL.xy.mul(0.3)).mul(0.42), 0.0)));
|
|
762
|
+
const self = clamp(float(0.5).add(n.sub(ns).mul(4.5)), 0.0, 1.0);
|
|
763
|
+
const lam = clamp(dot(N, vSunL).add(0.35).div(1.35), 0.0, 1.0);
|
|
764
|
+
const through = exp(vDensity.negate().mul(thick));
|
|
765
|
+
const sunShade = mix(through, lam, clamp(vSunL.z.mul(0.5).add(0.5), 0.0, 1.0))
|
|
766
|
+
.mul(mix(1.0, self, 0.85))
|
|
767
|
+
.mul(2.2);
|
|
768
|
+
const ambShade = float(0.65)
|
|
769
|
+
.add(dot(N, vUpL).mul(0.35))
|
|
770
|
+
.mul(float(0.5).add(sqrt(r2).mul(0.5)))
|
|
771
|
+
.mul(1.7);
|
|
772
|
+
const vLit = vSun.mul(mix(1.0, sunShade, dense)).add(vAmb.mul(mix(1.0, ambShade, dense)));
|
|
773
|
+
const dist = vec2(
|
|
774
|
+
noise3(vNoise.mul(1.7).add(U.uTime.mul(0.6))),
|
|
775
|
+
noise3(vNoise.zxy.mul(1.7).sub(U.uTime.mul(0.6))),
|
|
776
|
+
)
|
|
777
|
+
.sub(0.5)
|
|
778
|
+
.mul(vDistort)
|
|
779
|
+
.mul(0.02)
|
|
780
|
+
.mul(float(1.0).sub(r2));
|
|
781
|
+
// fire is hottest deep in a billow: thin edges and gaps run redder and much dimmer
|
|
782
|
+
const core = b.mul(float(1.0).sub(r2));
|
|
783
|
+
const emit = vEmit.mul(mix(vec3(0.5, 0.2, 0.08), vec3(1.5, 1.35, 1.2), core.mul(core)));
|
|
784
|
+
return fxOut(vLit, a, emit.mul(a).mul(2.0), dist, vViewZ);
|
|
785
|
+
})();
|
|
786
|
+
return mat;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
// --- geometry for debris variants -----------------------------------------------------
|
|
790
|
+
function rockGeometry() {
|
|
791
|
+
const g = new IcosahedronGeometry(0.5, 1);
|
|
792
|
+
const p = g.attributes.position as BufferAttribute;
|
|
793
|
+
for (let i = 0; i < p.count; i++) {
|
|
794
|
+
const x = p.getX(i);
|
|
795
|
+
const y = p.getY(i);
|
|
796
|
+
const z = p.getZ(i);
|
|
797
|
+
const n = 0.75 + 0.35 * Math.abs(Math.sin(x * 9.1 + y * 4.3) * Math.cos(z * 7.7 - x * 2.1));
|
|
798
|
+
p.setXYZ(i, x * n, y * n * 0.8, z * n);
|
|
799
|
+
}
|
|
800
|
+
g.computeVertexNormals();
|
|
801
|
+
return g; // polyhedra are already non-indexed, so the normals are faceted
|
|
802
|
+
}
|
|
803
|
+
function shardGeometry() {
|
|
804
|
+
// hexagonal ice shard: prism with a pointed tip, flat facets
|
|
805
|
+
const g = new CylinderGeometry(0.0, 0.22, 1, 6, 1, false);
|
|
806
|
+
g.translate(0, 0.25, 0);
|
|
807
|
+
const tail = new CylinderGeometry(0.22, 0.12, 0.3, 6, 1, false);
|
|
808
|
+
tail.translate(0, -0.4, 0);
|
|
809
|
+
const merged = mergeTwo(g.toNonIndexed(), tail.toNonIndexed());
|
|
810
|
+
merged.computeVertexNormals();
|
|
811
|
+
return merged;
|
|
812
|
+
}
|
|
813
|
+
function leafGeometry() {
|
|
814
|
+
const g = new PlaneGeometry(1, 0.6, 2, 1);
|
|
815
|
+
const p = g.attributes.position as BufferAttribute;
|
|
816
|
+
for (let i = 0; i < p.count; i++) p.setZ(i, Math.abs(p.getX(i)) * 0.25);
|
|
817
|
+
g.computeVertexNormals();
|
|
818
|
+
return g.toNonIndexed();
|
|
819
|
+
}
|
|
820
|
+
function mergeTwo(a: BufferGeometry, b: BufferGeometry) {
|
|
821
|
+
const out = new BufferGeometry();
|
|
822
|
+
for (const name of ["position", "normal", "uv"]) {
|
|
823
|
+
const A = a.attributes[name] as BufferAttribute | undefined;
|
|
824
|
+
const B = b.attributes[name] as BufferAttribute | undefined;
|
|
825
|
+
if (!A || !B) continue;
|
|
826
|
+
const arr = new Float32Array(A.array.length + B.array.length);
|
|
827
|
+
arr.set(A.array);
|
|
828
|
+
arr.set(B.array, A.array.length);
|
|
829
|
+
out.setAttribute(name, new BufferAttribute(arr, A.itemSize));
|
|
830
|
+
}
|
|
831
|
+
return out;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
// --- JS ------------------------------------------------------------------------------
|
|
835
|
+
function colorArr(c: ColorRepresentation) {
|
|
836
|
+
const k = new Color(c);
|
|
837
|
+
return [k.r, k.g, k.b];
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
interface IKind {
|
|
841
|
+
name: string;
|
|
842
|
+
id: number;
|
|
843
|
+
def: KindDef;
|
|
844
|
+
}
|
|
845
|
+
interface IStyleRange {
|
|
846
|
+
start: number;
|
|
847
|
+
count: number;
|
|
848
|
+
field: number;
|
|
849
|
+
cursor: number;
|
|
850
|
+
log: Float64Array;
|
|
851
|
+
head: number;
|
|
852
|
+
tail: number;
|
|
853
|
+
base: number;
|
|
854
|
+
window: number;
|
|
855
|
+
first: number;
|
|
856
|
+
}
|
|
857
|
+
interface IFieldDef {
|
|
858
|
+
kind: number;
|
|
859
|
+
count: number;
|
|
860
|
+
wa: number[];
|
|
861
|
+
wb: number[];
|
|
862
|
+
threshold: number;
|
|
863
|
+
rate: number;
|
|
864
|
+
lifeScale: number;
|
|
865
|
+
sizeScale: number;
|
|
866
|
+
up: number;
|
|
867
|
+
}
|
|
868
|
+
/** what the particle pool calls on the ground field (GroundField.js): touch() widens its live area */
|
|
869
|
+
export interface IParticleGround {
|
|
870
|
+
touch?(x: number, z: number, reach: number, life: number): void;
|
|
871
|
+
}
|
|
872
|
+
/** what the particle pool needs of the renderer: `compute` runs the sim kernel */
|
|
873
|
+
export interface IParticleRenderer {
|
|
874
|
+
compute(node: unknown): unknown;
|
|
875
|
+
}
|
|
876
|
+
export interface IEmitterOptions {
|
|
877
|
+
shape?: keyof typeof SHAPES;
|
|
878
|
+
radius?: number;
|
|
879
|
+
spread?: number;
|
|
880
|
+
radial?: number;
|
|
881
|
+
size?: number;
|
|
882
|
+
life?: number;
|
|
883
|
+
cone?: number;
|
|
884
|
+
}
|
|
885
|
+
export type IEmitter = ReturnType<Particles["emitter"]>;
|
|
886
|
+
|
|
887
|
+
// the ground texture before GroundField sets the real one: field emitters stay off until then
|
|
888
|
+
const GROUND_PLACEHOLDER = U.uGroundA.value;
|
|
889
|
+
|
|
890
|
+
export class Particles {
|
|
891
|
+
renderer: IParticleRenderer | null;
|
|
892
|
+
ground: IParticleGround | null;
|
|
893
|
+
W: number;
|
|
894
|
+
H: number;
|
|
895
|
+
kinds: IKind[] = []; // { name, def, id }
|
|
896
|
+
kindIndex = new Map<string, number>();
|
|
897
|
+
fieldDefs: IFieldDef[] = []; // { kind, style, count, wa, wb, threshold, rate, lifeScale, sizeScale, up }
|
|
898
|
+
kindData: Float32Array;
|
|
899
|
+
kindTex: DataTexture;
|
|
900
|
+
emitData: Float32Array;
|
|
901
|
+
emitTex: DataTexture;
|
|
902
|
+
fieldData: Float32Array;
|
|
903
|
+
fieldTex: DataTexture;
|
|
904
|
+
recCount = 0;
|
|
905
|
+
built = false;
|
|
906
|
+
time = 0;
|
|
907
|
+
alive = 0;
|
|
908
|
+
seed = 1;
|
|
909
|
+
glow: Mesh = null as unknown as Mesh; // meshes after build()
|
|
910
|
+
media: Mesh = null as unknown as Mesh;
|
|
911
|
+
debris: Mesh[] = [];
|
|
912
|
+
paint: Mesh[] = [];
|
|
913
|
+
style = {} as Record<Style, IStyleRange>;
|
|
914
|
+
fieldCount = 0;
|
|
915
|
+
spans: UV2[] = [];
|
|
916
|
+
stateU: IState | null = null;
|
|
917
|
+
simU: ISimU | null = null;
|
|
918
|
+
sim: unknown = null;
|
|
919
|
+
clear: unknown = null;
|
|
920
|
+
styleU = {} as Record<Style, IStyleU>;
|
|
921
|
+
|
|
922
|
+
constructor(
|
|
923
|
+
renderer: IParticleRenderer | null,
|
|
924
|
+
{ ground = null }: { ground?: IParticleGround | null } = {},
|
|
925
|
+
) {
|
|
926
|
+
this.renderer = renderer;
|
|
927
|
+
this.ground = ground;
|
|
928
|
+
[this.W, this.H] = Q.particles as [number, number];
|
|
929
|
+
this.kindData = new Float32Array(KW * MAX_KINDS * 4);
|
|
930
|
+
this.kindTex = tableOf(this.kindData, KW, MAX_KINDS);
|
|
931
|
+
this.emitData = new Float32Array(REC_W * MAX_REC * 4);
|
|
932
|
+
this.emitTex = tableOf(this.emitData, REC_W, MAX_REC);
|
|
933
|
+
this.fieldData = new Float32Array(4 * MAX_FIELD * 4);
|
|
934
|
+
this.fieldTex = tableOf(this.fieldData, 4, MAX_FIELD);
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
// seeded LCG so ?shot / ?bench frames repeat exactly
|
|
938
|
+
rand() {
|
|
939
|
+
this.seed = (Math.imul(this.seed, 1664525) + 1013904223) >>> 0;
|
|
940
|
+
return this.seed / 4294967296;
|
|
941
|
+
}
|
|
942
|
+
reseed(n: number) {
|
|
943
|
+
this.seed = n >>> 0 || 1;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
// --- authoring (spell create() at load) ---
|
|
947
|
+
addKind(name: string, def: Partial<KindDef>) {
|
|
948
|
+
if (this.kindIndex.has(name)) return this.kindIndex.get(name) as number;
|
|
949
|
+
if (this.kinds.length >= MAX_KINDS) throw new Error("particle kinds full");
|
|
950
|
+
const id = this.kinds.length;
|
|
951
|
+
// media is lit smoke/dust/mist unless it says it glows (or is a blackbody flame)
|
|
952
|
+
const lit = def.style === "media" && !def.temp && !def.absorbing ? { emissive: 0 } : null;
|
|
953
|
+
const k = { name, id, def: { ...KIND_DEFAULTS, ...lit, ...def } };
|
|
954
|
+
this.kinds.push(k);
|
|
955
|
+
this.kindIndex.set(name, id);
|
|
956
|
+
return id;
|
|
957
|
+
}
|
|
958
|
+
kind(name: string) {
|
|
959
|
+
return this.kindIndex.get(name);
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
// live slider edits: no recompile, just texels
|
|
963
|
+
setKind(id: string | number, patch: object) {
|
|
964
|
+
const k = this.kinds[typeof id === "string" ? (this.kindIndex.get(id) as number) : id] as IKind;
|
|
965
|
+
Object.assign(k.def, patch);
|
|
966
|
+
if (this.built) {
|
|
967
|
+
this.writeKind(k);
|
|
968
|
+
this.kindTex.needsUpdate = true;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
// GPU-driven emission from the ground field: weights on (heat, char, wet, frost) and
|
|
973
|
+
// (crack, acid, corruption, glow), minus threshold, times rate = spawn chance per slot per second
|
|
974
|
+
fieldEmitter(
|
|
975
|
+
kind: string | number,
|
|
976
|
+
{
|
|
977
|
+
slots = 1024,
|
|
978
|
+
a = [0, 0, 0, 0],
|
|
979
|
+
b = [0, 0, 0, 0],
|
|
980
|
+
threshold = 0.2,
|
|
981
|
+
rate = 2,
|
|
982
|
+
lifeScale = 1,
|
|
983
|
+
sizeScale = 1,
|
|
984
|
+
up = 0.5,
|
|
985
|
+
} = {},
|
|
986
|
+
) {
|
|
987
|
+
const id = typeof kind === "string" ? (this.kindIndex.get(kind) as number) : kind;
|
|
988
|
+
this.fieldDefs.push({
|
|
989
|
+
kind: id,
|
|
990
|
+
count: Math.max(64, Math.round(slots * Q.emission)),
|
|
991
|
+
wa: a,
|
|
992
|
+
wb: b,
|
|
993
|
+
threshold,
|
|
994
|
+
rate,
|
|
995
|
+
lifeScale,
|
|
996
|
+
sizeScale,
|
|
997
|
+
up,
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
writeKind(k: IKind) {
|
|
1002
|
+
const d = k.def;
|
|
1003
|
+
const o = k.id * KW * 4;
|
|
1004
|
+
const D = this.kindData;
|
|
1005
|
+
const [ca, cb] = [
|
|
1006
|
+
colorArr(d.color[0] as ColorRepresentation),
|
|
1007
|
+
colorArr((d.color[1] ?? d.color[0]) as ColorRepresentation),
|
|
1008
|
+
];
|
|
1009
|
+
const style = STYLES.indexOf(d.style);
|
|
1010
|
+
const paint = d.paints ? PAINT_CHANNELS.indexOf(d.paints) + 1 : 0;
|
|
1011
|
+
const morph =
|
|
1012
|
+
d.morph != null
|
|
1013
|
+
? (typeof d.morph === "string" ? (this.kindIndex.get(d.morph) as number) : d.morph) + 1
|
|
1014
|
+
: 0;
|
|
1015
|
+
const flags = (d.absorbing ? 1 : 0) | (d.killOnCollide ? 2 : 0) | (d.orbit ? 4 : 0);
|
|
1016
|
+
D.set(
|
|
1017
|
+
[
|
|
1018
|
+
d.drag,
|
|
1019
|
+
d.gravity,
|
|
1020
|
+
d.buoyancy,
|
|
1021
|
+
d.curl,
|
|
1022
|
+
d.curlScale,
|
|
1023
|
+
d.forceResponse,
|
|
1024
|
+
d.restitution,
|
|
1025
|
+
d.friction,
|
|
1026
|
+
d.life[0] as number,
|
|
1027
|
+
d.life[1] as number,
|
|
1028
|
+
d.size[0] as number,
|
|
1029
|
+
(d.size[1] ?? d.size[0]) as number,
|
|
1030
|
+
d.stretch,
|
|
1031
|
+
d.emissive,
|
|
1032
|
+
d.temp ? (d.temp[0] as number) : 0,
|
|
1033
|
+
d.temp ? ((d.temp[1] ?? d.temp[0]) as number) : 0,
|
|
1034
|
+
...ca,
|
|
1035
|
+
d.alpha[0] as number,
|
|
1036
|
+
...cb,
|
|
1037
|
+
(d.alpha[1] ?? d.alpha[0]) as number,
|
|
1038
|
+
// debris is 3 + variant / 10 (the sim tests > 2.5, the debris VS reads the variant)
|
|
1039
|
+
style === 2 ? 3 + MESHES.indexOf(d.mesh) * 0.1 : style,
|
|
1040
|
+
paint,
|
|
1041
|
+
morph,
|
|
1042
|
+
flags,
|
|
1043
|
+
d.toSmokeAt,
|
|
1044
|
+
d.smokeAlbedo,
|
|
1045
|
+
d.density,
|
|
1046
|
+
d.distortion,
|
|
1047
|
+
d.fadeIn,
|
|
1048
|
+
d.fadeOut,
|
|
1049
|
+
d.spin,
|
|
1050
|
+
d.sizePow,
|
|
1051
|
+
d.lightResponse,
|
|
1052
|
+
d.softness,
|
|
1053
|
+
d.erosion,
|
|
1054
|
+
d.paintAmount,
|
|
1055
|
+
d.flicker,
|
|
1056
|
+
0,
|
|
1057
|
+
0,
|
|
1058
|
+
0, // glow only: 0..1 per-particle twinkle (embers)
|
|
1059
|
+
],
|
|
1060
|
+
o,
|
|
1061
|
+
);
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
// --- build once every spell has registered ---
|
|
1065
|
+
build() {
|
|
1066
|
+
const { W, H } = this;
|
|
1067
|
+
for (const k of this.kinds) this.writeKind(k);
|
|
1068
|
+
this.kindTex.needsUpdate = true;
|
|
1069
|
+
|
|
1070
|
+
// row ranges: debris rows sized to Q.debris, the rest split glow 55% / media 45%
|
|
1071
|
+
const debrisRows = Math.max(1, Math.ceil(Q.debris / W));
|
|
1072
|
+
const rest = H - debrisRows;
|
|
1073
|
+
const glowRows = Math.round(rest * 0.55);
|
|
1074
|
+
const mediaRows = rest - glowRows;
|
|
1075
|
+
const rows: Record<Style, [number, number]> = {
|
|
1076
|
+
glow: [0, glowRows],
|
|
1077
|
+
media: [glowRows, mediaRows],
|
|
1078
|
+
debris: [glowRows + mediaRows, debrisRows],
|
|
1079
|
+
};
|
|
1080
|
+
for (const s of STYLES) {
|
|
1081
|
+
const [r0, nr] = rows[s];
|
|
1082
|
+
this.style[s] = {
|
|
1083
|
+
start: r0 * W,
|
|
1084
|
+
count: nr * W,
|
|
1085
|
+
field: 0,
|
|
1086
|
+
cursor: 0,
|
|
1087
|
+
log: new Float64Array(LOG_N * 3),
|
|
1088
|
+
head: 0,
|
|
1089
|
+
tail: 0,
|
|
1090
|
+
base: 0,
|
|
1091
|
+
window: 0,
|
|
1092
|
+
first: -1,
|
|
1093
|
+
};
|
|
1094
|
+
}
|
|
1095
|
+
// field blocks at the start of their style's range
|
|
1096
|
+
let fi = 0;
|
|
1097
|
+
for (const fd of this.fieldDefs) {
|
|
1098
|
+
const st = this.style[(this.kinds[fd.kind] as IKind).def.style];
|
|
1099
|
+
const count = Math.min(fd.count, Math.floor(st.count / 4));
|
|
1100
|
+
const o = fi * 4 * 4;
|
|
1101
|
+
this.fieldData.set(
|
|
1102
|
+
[
|
|
1103
|
+
st.start + st.field,
|
|
1104
|
+
count,
|
|
1105
|
+
fd.kind,
|
|
1106
|
+
fd.threshold,
|
|
1107
|
+
...fd.wa,
|
|
1108
|
+
...fd.wb,
|
|
1109
|
+
fd.rate,
|
|
1110
|
+
fd.lifeScale,
|
|
1111
|
+
fd.sizeScale,
|
|
1112
|
+
fd.up,
|
|
1113
|
+
],
|
|
1114
|
+
o,
|
|
1115
|
+
);
|
|
1116
|
+
st.field += count;
|
|
1117
|
+
fi++;
|
|
1118
|
+
}
|
|
1119
|
+
this.fieldCount = fi;
|
|
1120
|
+
this.fieldTex.needsUpdate = true;
|
|
1121
|
+
|
|
1122
|
+
// compute port: storage buffers in place of the two RGBA32F MRT targets
|
|
1123
|
+
const stateU: IState = {
|
|
1124
|
+
uS0: storageOf(W * H),
|
|
1125
|
+
uS1: storageOf(W * H),
|
|
1126
|
+
uS2: storageOf(W * H),
|
|
1127
|
+
uKinds: texOf(this.kindTex),
|
|
1128
|
+
};
|
|
1129
|
+
this.stateU = stateU;
|
|
1130
|
+
this.spans = Array.from({ length: 6 }, () => vec2U(new Vector2()));
|
|
1131
|
+
this.simU = {
|
|
1132
|
+
uEmitTex: texOf(this.emitTex),
|
|
1133
|
+
uEmitCount: uniform(0),
|
|
1134
|
+
uSpan: this.spans,
|
|
1135
|
+
uSpanCount: uniform(0),
|
|
1136
|
+
uFieldTex: texOf(this.fieldTex),
|
|
1137
|
+
uFieldCount: uniform(this.fieldCount),
|
|
1138
|
+
uSimDt: uniform(1 / 60),
|
|
1139
|
+
uGroundA: texture(U.uGroundA.value),
|
|
1140
|
+
uGroundB: texture(U.uGroundB.value),
|
|
1141
|
+
};
|
|
1142
|
+
this.sim = simKernel(W, H, stateU, this.simU);
|
|
1143
|
+
this.clear = clearKernel(W, H, stateU);
|
|
1144
|
+
// initialise every texel dead: the buffers start as zero (age 0 >= life 0 counts as dead)
|
|
1145
|
+
this.clearState(false);
|
|
1146
|
+
|
|
1147
|
+
const quad = new InstancedBufferGeometry();
|
|
1148
|
+
quad.setAttribute(
|
|
1149
|
+
"position",
|
|
1150
|
+
new Float32BufferAttribute([-1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0], 3),
|
|
1151
|
+
);
|
|
1152
|
+
quad.setIndex([0, 1, 2, 0, 2, 3]);
|
|
1153
|
+
quad.instanceCount = 0;
|
|
1154
|
+
const styleU = (s: Style): IStyleU => ({
|
|
1155
|
+
uStyleStart: uniform(this.style[s].start),
|
|
1156
|
+
uStyleCount: uniform(this.style[s].count),
|
|
1157
|
+
uFieldCount: uniform(this.style[s].field),
|
|
1158
|
+
uRingBase: uniform(0),
|
|
1159
|
+
});
|
|
1160
|
+
for (const s of STYLES) this.styleU[s] = styleU(s);
|
|
1161
|
+
|
|
1162
|
+
// glow (lines scene)
|
|
1163
|
+
const gq = quad.clone();
|
|
1164
|
+
this.glow = new Mesh(gq, glowMaterial(stateU, this.styleU.glow));
|
|
1165
|
+
this.glow.frustumCulled = false;
|
|
1166
|
+
this.glow.renderOrder = 10;
|
|
1167
|
+
this.glow.layers.enable(1); // shows in the floor reflection
|
|
1168
|
+
this.glow.name = "ultra-particles-glow";
|
|
1169
|
+
|
|
1170
|
+
// media (fx scene)
|
|
1171
|
+
const mq = quad.clone();
|
|
1172
|
+
this.media = new Mesh(mq, mediaMaterial(stateU, this.styleU.media));
|
|
1173
|
+
this.media.frustumCulled = false;
|
|
1174
|
+
this.media.name = "ultra-particles-media";
|
|
1175
|
+
|
|
1176
|
+
// debris (opaque scene): one mesh per variant, each skips instances of other variants
|
|
1177
|
+
const debrisU = this.styleU.debris; // shared by the three variant materials
|
|
1178
|
+
this.debris = MESHES.map((variant, vi) => {
|
|
1179
|
+
const src =
|
|
1180
|
+
variant === "rock"
|
|
1181
|
+
? rockGeometry()
|
|
1182
|
+
: variant === "shard"
|
|
1183
|
+
? shardGeometry()
|
|
1184
|
+
: leafGeometry();
|
|
1185
|
+
const g = new InstancedBufferGeometry();
|
|
1186
|
+
for (const n of ["position", "normal", "uv"])
|
|
1187
|
+
if (src.attributes[n]) g.setAttribute(n, src.attributes[n] as BufferAttribute);
|
|
1188
|
+
g.instanceCount = 0;
|
|
1189
|
+
const mat = debrisMaterial(stateU, debrisU, vi, variant);
|
|
1190
|
+
mat.userData.u = debrisU;
|
|
1191
|
+
const m = new Mesh(g, mat);
|
|
1192
|
+
m.frustumCulled = false;
|
|
1193
|
+
m.layers.enable(1);
|
|
1194
|
+
m.name = `ultra-particles-debris-${variant}`;
|
|
1195
|
+
return m;
|
|
1196
|
+
});
|
|
1197
|
+
|
|
1198
|
+
// paint into the ground field: one Points draw per style window
|
|
1199
|
+
// ENGINE-GAP: not ported (Points with gl_PointSize into the ground's MRT); see the header
|
|
1200
|
+
this.paint = [];
|
|
1201
|
+
this.built = true;
|
|
1202
|
+
for (const s of STYLES) this.setDraw(s, 0, 0);
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
clearState(dispatch = true) {
|
|
1206
|
+
this.recCount = 0;
|
|
1207
|
+
const u = this.simU;
|
|
1208
|
+
if (u) {
|
|
1209
|
+
u.uEmitCount.value = 0;
|
|
1210
|
+
u.uSpanCount.value = 0;
|
|
1211
|
+
u.uFieldCount.value = 0;
|
|
1212
|
+
}
|
|
1213
|
+
// the buffers go back to zero: age 0 >= life 0 counts as dead
|
|
1214
|
+
if (dispatch && this.clear) this.renderer?.compute(this.clear);
|
|
1215
|
+
for (const s of STYLES)
|
|
1216
|
+
if (this.style[s])
|
|
1217
|
+
Object.assign(this.style[s], { cursor: 0, head: 0, tail: 0, base: 0, window: 0 });
|
|
1218
|
+
if (u) u.uFieldCount.value = this.fieldCount ?? 0;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
// --- emitters: reusable handles so continuous emission allocates nothing ---
|
|
1222
|
+
emitter(kind: string | number, opts: IEmitterOptions = {}) {
|
|
1223
|
+
const id = typeof kind === "string" ? this.kindIndex.get(kind) : kind;
|
|
1224
|
+
if (id === undefined) throw new Error(`unknown particle kind ${kind}`);
|
|
1225
|
+
const p = this;
|
|
1226
|
+
return {
|
|
1227
|
+
kind: id,
|
|
1228
|
+
shape: SHAPES[opts.shape ?? "sphere"],
|
|
1229
|
+
pos: new Vector3(),
|
|
1230
|
+
radius: opts.radius ?? 0.1,
|
|
1231
|
+
vel: new Vector3(),
|
|
1232
|
+
spread: opts.spread ?? 0,
|
|
1233
|
+
p: new Vector3(),
|
|
1234
|
+
radial: opts.radial ?? 0,
|
|
1235
|
+
size: opts.size ?? 1,
|
|
1236
|
+
life: opts.life ?? 1,
|
|
1237
|
+
cone: opts.cone ?? 0.4,
|
|
1238
|
+
acc: 0,
|
|
1239
|
+
burst(count: number) {
|
|
1240
|
+
p.emit(this, count);
|
|
1241
|
+
},
|
|
1242
|
+
// continuous: fractional accumulator so low rates still emit
|
|
1243
|
+
rate(perSecond: number, dt: number) {
|
|
1244
|
+
this.acc += perSecond * dt * Q.emission * Q.auto.emission;
|
|
1245
|
+
const n = Math.floor(this.acc);
|
|
1246
|
+
if (n > 0) {
|
|
1247
|
+
this.acc -= n;
|
|
1248
|
+
p.emit(this, n, true);
|
|
1249
|
+
}
|
|
1250
|
+
},
|
|
1251
|
+
setShape(name: keyof typeof SHAPES) {
|
|
1252
|
+
this.shape = SHAPES[name];
|
|
1253
|
+
return this;
|
|
1254
|
+
},
|
|
1255
|
+
};
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
emit(e: IEmitter, count: number, prescaled = false) {
|
|
1259
|
+
if (!this.built) return;
|
|
1260
|
+
const st = this.style[(this.kinds[e.kind] as IKind).def.style];
|
|
1261
|
+
const ring = st.count - st.field;
|
|
1262
|
+
let n = prescaled ? count : Math.round(count * Q.emission * Q.auto.emission);
|
|
1263
|
+
n = Math.min(n, ring);
|
|
1264
|
+
if (n <= 0) return;
|
|
1265
|
+
const def = (this.kinds[e.kind] as IKind).def;
|
|
1266
|
+
const life = (def.life[1] as number) * e.life;
|
|
1267
|
+
// the ground field only simulates (and accepts paint) where it was touched: cover the reach
|
|
1268
|
+
const mk = typeof def.morph === "string" ? this.kindIndex.get(def.morph) : def.morph;
|
|
1269
|
+
if ((def.paints || this.kinds[mk as number]?.def.paints) && this.ground?.touch) {
|
|
1270
|
+
const reach = Math.min(8, e.radius + 1 + 0.6 * life * (e.spread + e.vel.length()));
|
|
1271
|
+
this.ground.touch(e.pos.x, e.pos.z, reach, life);
|
|
1272
|
+
}
|
|
1273
|
+
let start = st.cursor;
|
|
1274
|
+
while (n > 0 && this.recCount < MAX_REC) {
|
|
1275
|
+
const take = Math.min(n, ring - start);
|
|
1276
|
+
const o = this.recCount * REC_W * 4;
|
|
1277
|
+
const D = this.emitData;
|
|
1278
|
+
D[o] = st.start + st.field + start;
|
|
1279
|
+
D[o + 1] = take;
|
|
1280
|
+
D[o + 2] = e.kind;
|
|
1281
|
+
D[o + 3] = e.shape;
|
|
1282
|
+
D[o + 4] = e.pos.x;
|
|
1283
|
+
D[o + 5] = e.pos.y;
|
|
1284
|
+
D[o + 6] = e.pos.z;
|
|
1285
|
+
D[o + 7] = e.radius;
|
|
1286
|
+
D[o + 8] = e.vel.x;
|
|
1287
|
+
D[o + 9] = e.vel.y;
|
|
1288
|
+
D[o + 10] = e.vel.z;
|
|
1289
|
+
D[o + 11] = e.spread;
|
|
1290
|
+
D[o + 12] = e.p.x;
|
|
1291
|
+
D[o + 13] = e.p.y;
|
|
1292
|
+
D[o + 14] = e.p.z;
|
|
1293
|
+
D[o + 15] = this.rand();
|
|
1294
|
+
D[o + 16] = e.radial;
|
|
1295
|
+
D[o + 17] = e.size;
|
|
1296
|
+
D[o + 18] = e.life;
|
|
1297
|
+
D[o + 19] = e.cone;
|
|
1298
|
+
this.recCount++;
|
|
1299
|
+
// emission log (ring): entries expire in order, the oldest live one is the draw window base
|
|
1300
|
+
const h = (st.head % LOG_N) * 3;
|
|
1301
|
+
st.log[h] = start;
|
|
1302
|
+
st.log[h + 1] = take;
|
|
1303
|
+
st.log[h + 2] = this.time + life;
|
|
1304
|
+
st.head++;
|
|
1305
|
+
if (st.head - st.tail > LOG_N) st.tail++;
|
|
1306
|
+
start = (start + take) % ring;
|
|
1307
|
+
n -= take;
|
|
1308
|
+
}
|
|
1309
|
+
st.cursor = start;
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
// once per frame, before rendering. dt is the scaled sim step.
|
|
1313
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: upstream Ultra Elements code, copied verbatim
|
|
1314
|
+
update(dt: number, time: number) {
|
|
1315
|
+
if (!this.built) return;
|
|
1316
|
+
this.time = time;
|
|
1317
|
+
const u = this.simU as ISimU;
|
|
1318
|
+
// spans this frame: per style, from the first record start to the cursor (wrapping)
|
|
1319
|
+
let sc = 0;
|
|
1320
|
+
for (const s of STYLES) this.style[s].first = -1;
|
|
1321
|
+
for (let r = 0; r < this.recCount; r++) {
|
|
1322
|
+
const o = r * REC_W * 4;
|
|
1323
|
+
const st = this.style[(this.kinds[this.emitData[o + 2] as number] as IKind).def.style];
|
|
1324
|
+
if (st.first < 0) st.first = this.emitData[o] as number;
|
|
1325
|
+
}
|
|
1326
|
+
for (const s of STYLES) {
|
|
1327
|
+
const st = this.style[s];
|
|
1328
|
+
if (st.first < 0) continue;
|
|
1329
|
+
const ringStart = st.start + st.field;
|
|
1330
|
+
const ring = st.count - st.field;
|
|
1331
|
+
const a = st.first;
|
|
1332
|
+
const b = ringStart + st.cursor;
|
|
1333
|
+
if (b > a) (this.spans[sc++] as UV2).value.set(a, b);
|
|
1334
|
+
else {
|
|
1335
|
+
(this.spans[sc++] as UV2).value.set(a, ringStart + ring);
|
|
1336
|
+
(this.spans[sc++] as UV2).value.set(ringStart, b === ringStart ? ringStart + ring : b);
|
|
1337
|
+
}
|
|
1338
|
+
}
|
|
1339
|
+
u.uSpanCount.value = sc;
|
|
1340
|
+
u.uEmitCount.value = this.recCount;
|
|
1341
|
+
if (this.recCount) this.emitTex.needsUpdate = true;
|
|
1342
|
+
u.uSimDt.value = dt;
|
|
1343
|
+
// field emitters read the ground field: off until GroundField has set its texture
|
|
1344
|
+
const groundReady = U.uGroundA.value !== GROUND_PLACEHOLDER;
|
|
1345
|
+
if (groundReady) {
|
|
1346
|
+
u.uGroundA.value = U.uGroundA.value;
|
|
1347
|
+
u.uGroundB.value = U.uGroundB.value;
|
|
1348
|
+
}
|
|
1349
|
+
u.uFieldCount.value = groundReady ? this.fieldCount : 0;
|
|
1350
|
+
this.renderer?.compute(this.sim); // the state is read and written in place (see the header)
|
|
1351
|
+
this.recCount = 0;
|
|
1352
|
+
|
|
1353
|
+
// live windows: drop expired log entries, base = oldest live start
|
|
1354
|
+
this.alive = 0;
|
|
1355
|
+
for (const s of STYLES) {
|
|
1356
|
+
const st = this.style[s];
|
|
1357
|
+
const ring = st.count - st.field;
|
|
1358
|
+
const L = st.log;
|
|
1359
|
+
while (st.tail < st.head && (L[((st.tail % LOG_N) * 3 + 2) as number] as number) < time)
|
|
1360
|
+
st.tail++;
|
|
1361
|
+
for (let j = st.tail; j < st.head; j++) this.alive += L[(j % LOG_N) * 3 + 1] as number;
|
|
1362
|
+
const live = st.head > st.tail;
|
|
1363
|
+
st.base = live ? (L[(st.tail % LOG_N) * 3] as number) : st.cursor;
|
|
1364
|
+
st.window = live ? (st.cursor - st.base + ring) % ring || ring : 0;
|
|
1365
|
+
this.setDraw(s, st.base, st.field + st.window);
|
|
1366
|
+
}
|
|
1367
|
+
this.alive = Math.min(this.alive, this.W * this.H);
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
setDraw(s: Style, base: number, n: number) {
|
|
1371
|
+
const meshes = s === "glow" ? [this.glow] : s === "media" ? [this.media] : this.debris;
|
|
1372
|
+
for (const m of meshes) {
|
|
1373
|
+
(m.geometry as InstancedBufferGeometry).instanceCount = n;
|
|
1374
|
+
this.styleU[s].uRingBase.value = base;
|
|
1375
|
+
m.visible = n > 0;
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
// everything visible for one warm-up frame (shader variants are built at first draw)
|
|
1380
|
+
warm(on: boolean) {
|
|
1381
|
+
if (!this.built) return;
|
|
1382
|
+
for (const s of STYLES) {
|
|
1383
|
+
const st = this.style[s];
|
|
1384
|
+
if (on) this.setDraw(s, 0, 1);
|
|
1385
|
+
else this.setDraw(s, st.base, st.field + st.window);
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
function debrisMaterial(st: IState, su: IStyleU, variant: number, name: string) {
|
|
1391
|
+
const mat = new MeshStandardNodeMaterial({
|
|
1392
|
+
color: 0xffffff,
|
|
1393
|
+
roughness: name === "shard" ? 0.08 : 0.85,
|
|
1394
|
+
metalness: 0,
|
|
1395
|
+
side: name === "leaf" ? DoubleSide : FrontSide,
|
|
1396
|
+
});
|
|
1397
|
+
// axisAngle(a, t) as its three GLSL mat3 columns
|
|
1398
|
+
const axisAngle = (a: V3, t: F): [V3, V3, V3] => {
|
|
1399
|
+
const c = cos(t);
|
|
1400
|
+
const s = sin(t);
|
|
1401
|
+
const C = float(1.0).sub(c);
|
|
1402
|
+
return [
|
|
1403
|
+
vec3(
|
|
1404
|
+
c.add(a.x.mul(a.x).mul(C)),
|
|
1405
|
+
a.y.mul(a.x).mul(C).add(a.z.mul(s)),
|
|
1406
|
+
a.z.mul(a.x).mul(C).sub(a.y.mul(s)),
|
|
1407
|
+
),
|
|
1408
|
+
vec3(
|
|
1409
|
+
a.x.mul(a.y).mul(C).sub(a.z.mul(s)),
|
|
1410
|
+
c.add(a.y.mul(a.y).mul(C)),
|
|
1411
|
+
a.z.mul(a.y).mul(C).add(a.x.mul(s)),
|
|
1412
|
+
),
|
|
1413
|
+
vec3(
|
|
1414
|
+
a.x.mul(a.z).mul(C).add(a.y.mul(s)),
|
|
1415
|
+
a.y.mul(a.z).mul(C).sub(a.x.mul(s)),
|
|
1416
|
+
c.add(a.z.mul(a.z).mul(C)),
|
|
1417
|
+
),
|
|
1418
|
+
] as [V3, V3, V3];
|
|
1419
|
+
};
|
|
1420
|
+
const mulCols = (m: [V3, V3, V3], v: V3): V3 =>
|
|
1421
|
+
m[0].mul(v.x).add(m[1].mul(v.y)).add(m[2].mul(v.z)) as V3;
|
|
1422
|
+
const hash3 = Fn(([p]: [F]) => {
|
|
1423
|
+
const p3 = fract(vec3(p).mul(vec3(0.1031, 0.103, 0.0973))).toVar();
|
|
1424
|
+
p3.addAssign(dot(p3, p3.yzx.add(33.33)));
|
|
1425
|
+
return fract(p3.xxy.add(p3.yzz).mul(p3.zyx));
|
|
1426
|
+
});
|
|
1427
|
+
// debrisSetup()
|
|
1428
|
+
const idx = particleIndex(su, int(instanceIndex));
|
|
1429
|
+
const s0 = stateAt(st.uS0, idx);
|
|
1430
|
+
const s1 = stateAt(st.uS1, idx);
|
|
1431
|
+
const s2 = stateAt(st.uS2, idx);
|
|
1432
|
+
const kind = int(s2.x);
|
|
1433
|
+
const k2 = kindTexel(st, kind, 2);
|
|
1434
|
+
const k4 = kindTexel(st, kind, 4);
|
|
1435
|
+
const k5 = kindTexel(st, kind, 5);
|
|
1436
|
+
const k6 = kindTexel(st, kind, 6);
|
|
1437
|
+
const k8 = kindTexel(st, kind, 8);
|
|
1438
|
+
const t = s0.w.div(max(s1.w, 1e-4));
|
|
1439
|
+
const gDead = s0.w
|
|
1440
|
+
.greaterThanEqual(s1.w)
|
|
1441
|
+
.or(abs(fract(k6.x).mul(10.0).sub(variant)).greaterThan(0.5));
|
|
1442
|
+
const h = hash3(s2.y) as V3;
|
|
1443
|
+
const ax = normalize(h.sub(0.5).add(1e-4));
|
|
1444
|
+
const spinT = select(s2.w.greaterThan(0.0), s2.w, s0.w);
|
|
1445
|
+
const gRot = axisAngle(ax as V3, h.x.mul(6.283).add(k8.z.mul(spinT)) as F);
|
|
1446
|
+
const fade = float(1.0).sub(smoothstep(float(1.0).sub(max(k8.y, 1e-3)), 1.0, t));
|
|
1447
|
+
const gScale = mix(k2.z, k2.w, t)
|
|
1448
|
+
.mul(fract(s2.x))
|
|
1449
|
+
.mul(10.0)
|
|
1450
|
+
.mul(float(0.6).add(h.y.mul(0.8)))
|
|
1451
|
+
.mul(mix(0.0, 1.0, fade))
|
|
1452
|
+
.mul(smoothstep(0.0, 0.03, s0.w.add(0.01)));
|
|
1453
|
+
const gPos = s0.xyz;
|
|
1454
|
+
const vKindColor = varying(k4.rgb.mul(float(0.75).add(h.z.mul(0.5))));
|
|
1455
|
+
const vKindEmit = varying(k5.rgb.mul(pow(float(1.0).sub(t), 2.0)));
|
|
1456
|
+
const vRough = varying(k5.a);
|
|
1457
|
+
// begin_vertex
|
|
1458
|
+
mat.positionNode = select(
|
|
1459
|
+
gDead,
|
|
1460
|
+
vec3(0.0, -1000.0, 0.0),
|
|
1461
|
+
mulCols(gRot, positionGeometry).mul(gScale).add(gPos),
|
|
1462
|
+
);
|
|
1463
|
+
// beginnormal_vertex: objectNormal = gRot * normal. ENGINE-GAP: the Wasm engine has no normalLocal and
|
|
1464
|
+
// refuses a normalNode while the post chain reads the normal target, so the normal is the face normal
|
|
1465
|
+
// (flatShading). Rock and shard are faceted (non-indexed, computeVertexNormals), so that is the rotated
|
|
1466
|
+
// normal exactly; the leaf's two bent facets lose their smoothing.
|
|
1467
|
+
mat.flatShading = true;
|
|
1468
|
+
// color_fragment: diffuseColor.rgb *= vKindColor
|
|
1469
|
+
mat.colorNode = vKindColor;
|
|
1470
|
+
// roughnessmap_fragment
|
|
1471
|
+
mat.roughnessNode = select(vRough.greaterThan(0.0), vRough, float(mat.roughness));
|
|
1472
|
+
// emissivemap_fragment: totalEmissiveRadiance += vKindEmit
|
|
1473
|
+
mat.emissiveNode = vKindEmit;
|
|
1474
|
+
return mat;
|
|
1475
|
+
}
|