@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,295 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The four things a spell draws besides particles.
|
|
3
|
+
*
|
|
4
|
+
* Each is pooled and reused: a spell allocates its drawables once at `create`, and a cast only
|
|
5
|
+
* moves uniforms and a transform. Nothing here is added to the scene at cast time, because an
|
|
6
|
+
* object added mid-frame changes the frame's draw list and, in Three.js, can cost a program.
|
|
7
|
+
*
|
|
8
|
+
* All shader code is TSL. There is no GLSL string in this package.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
abs,
|
|
13
|
+
float,
|
|
14
|
+
length,
|
|
15
|
+
mix,
|
|
16
|
+
oneMinus,
|
|
17
|
+
pow,
|
|
18
|
+
smoothstep,
|
|
19
|
+
uniform,
|
|
20
|
+
uv,
|
|
21
|
+
vec2,
|
|
22
|
+
vec3,
|
|
23
|
+
vec4,
|
|
24
|
+
} from "three/tsl";
|
|
25
|
+
import {
|
|
26
|
+
Color,
|
|
27
|
+
DoubleSide,
|
|
28
|
+
Mesh,
|
|
29
|
+
MeshBasicNodeMaterial,
|
|
30
|
+
type Object3D,
|
|
31
|
+
PlaneGeometry,
|
|
32
|
+
SphereGeometry,
|
|
33
|
+
Sprite,
|
|
34
|
+
SpriteNodeMaterial,
|
|
35
|
+
Vector3,
|
|
36
|
+
} from "three/webgpu";
|
|
37
|
+
|
|
38
|
+
const TAU = Math.PI * 2;
|
|
39
|
+
|
|
40
|
+
const finite = (label: string, value: number): number => {
|
|
41
|
+
if (!Number.isFinite(value)) throw new TypeError(`${label} must be finite`);
|
|
42
|
+
return value;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** A hot, soft ball: the thing the player is actually aiming. */
|
|
46
|
+
export class SpellCore {
|
|
47
|
+
readonly sprite = new Sprite();
|
|
48
|
+
readonly #colour = uniform(new Color(1, 0.72, 0.34));
|
|
49
|
+
readonly #highlight = uniform(new Color(1, 0.95, 0.72));
|
|
50
|
+
readonly #radius = uniform(0.35);
|
|
51
|
+
readonly #intensity = uniform(1);
|
|
52
|
+
readonly #material: SpriteNodeMaterial;
|
|
53
|
+
|
|
54
|
+
constructor(parent: Object3D) {
|
|
55
|
+
this.#material = new SpriteNodeMaterial({
|
|
56
|
+
depthWrite: false,
|
|
57
|
+
toneMapped: false,
|
|
58
|
+
transparent: true,
|
|
59
|
+
});
|
|
60
|
+
const point = uv().sub(0.5);
|
|
61
|
+
const radius = length(point);
|
|
62
|
+
// A flat core inside a hot rim: the eye reads a sphere, not a disc.
|
|
63
|
+
const core = float(1).sub(smoothstep(0.04, 0.44, radius));
|
|
64
|
+
const rim = pow(oneMinus(smoothstep(0.3, 0.5, radius)), 2.5);
|
|
65
|
+
this.#material.colorNode = vec4(
|
|
66
|
+
mix(this.#colour, this.#highlight, core.mul(0.85).add(0.15)),
|
|
67
|
+
core.add(rim.mul(0.55)).mul(this.#intensity),
|
|
68
|
+
);
|
|
69
|
+
this.#material.scaleNode = vec2(this.#radius, this.#radius).mul(2.6);
|
|
70
|
+
this.sprite.material = this.#material;
|
|
71
|
+
this.sprite.frustumCulled = false;
|
|
72
|
+
this.sprite.visible = false;
|
|
73
|
+
parent.add(this.sprite);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Metres across, the tint, and how hard it burns. */
|
|
77
|
+
show(
|
|
78
|
+
position: Vector3,
|
|
79
|
+
radius: number,
|
|
80
|
+
colour: Color,
|
|
81
|
+
highlight: Color,
|
|
82
|
+
intensity: number,
|
|
83
|
+
): void {
|
|
84
|
+
this.sprite.position.copy(position);
|
|
85
|
+
this.#radius.value = Math.max(0.001, finite("core.radius", radius));
|
|
86
|
+
this.#colour.value.copy(colour);
|
|
87
|
+
this.#highlight.value.copy(highlight);
|
|
88
|
+
this.#intensity.value = Math.max(0, finite("core.intensity", intensity));
|
|
89
|
+
this.sprite.visible = true;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
hide(): void {
|
|
93
|
+
this.sprite.visible = false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** An expanding shell: the pressure wave of an impact, and the edge of a shield. */
|
|
98
|
+
export class ShockShell {
|
|
99
|
+
readonly mesh: Mesh;
|
|
100
|
+
readonly #colour = uniform(new Color(1, 0.7, 0.3));
|
|
101
|
+
readonly #progress = uniform(0);
|
|
102
|
+
readonly #strength = uniform(0);
|
|
103
|
+
readonly #material: MeshBasicNodeMaterial;
|
|
104
|
+
|
|
105
|
+
constructor(parent: Object3D) {
|
|
106
|
+
this.#material = new MeshBasicNodeMaterial({
|
|
107
|
+
depthWrite: false,
|
|
108
|
+
side: DoubleSide,
|
|
109
|
+
toneMapped: false,
|
|
110
|
+
transparent: true,
|
|
111
|
+
});
|
|
112
|
+
// Rim-bright: the silhouette carries the wave, the middle stays clear.
|
|
113
|
+
const rim = pow(oneMinus(abs(uv().sub(0.5).length().mul(2).sub(1))), 3);
|
|
114
|
+
this.#material.colorNode = vec4(this.#colour, rim.mul(this.#strength));
|
|
115
|
+
this.mesh = new Mesh(new SphereGeometry(1, 24, 16), this.#material);
|
|
116
|
+
this.mesh.frustumCulled = false;
|
|
117
|
+
this.mesh.visible = false;
|
|
118
|
+
parent.add(this.mesh);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** `progress` 0..1 drives both the radius and the fade, so a shell cannot outlive its impact. */
|
|
122
|
+
show(
|
|
123
|
+
centre: Vector3,
|
|
124
|
+
progress: number,
|
|
125
|
+
radius: number,
|
|
126
|
+
colour: Color,
|
|
127
|
+
strength: number,
|
|
128
|
+
fade = 1 - Math.min(1, Math.max(0, progress)),
|
|
129
|
+
): void {
|
|
130
|
+
this.mesh.position.copy(centre);
|
|
131
|
+
this.#progress.value = Math.min(1, Math.max(0, progress));
|
|
132
|
+
// A wave at progress 0 is a point; keep a floor so the first frame is not an empty sphere.
|
|
133
|
+
const scale =
|
|
134
|
+
Math.max(0.001, finite("shell.radius", radius)) * Math.max(0.02, this.#progress.value);
|
|
135
|
+
this.mesh.scale.setScalar(scale);
|
|
136
|
+
this.#colour.value.copy(colour);
|
|
137
|
+
this.#strength.value = Math.max(0, finite("shell.strength", strength)) * Math.max(0, fade);
|
|
138
|
+
this.mesh.visible = this.#strength.value > 0.001;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
hide(): void {
|
|
142
|
+
this.mesh.visible = false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** A crackling column from the sky to a point, or from a hand to a point. */
|
|
147
|
+
export class BoltColumn {
|
|
148
|
+
readonly mesh: Mesh;
|
|
149
|
+
readonly #colour = uniform(new Color(0.85, 0.93, 1));
|
|
150
|
+
readonly #core = uniform(new Color(1, 1, 1));
|
|
151
|
+
readonly #strength = uniform(0);
|
|
152
|
+
readonly #seed = uniform(0);
|
|
153
|
+
readonly #material: MeshBasicNodeMaterial;
|
|
154
|
+
readonly #up = new Vector3(0, 1, 0);
|
|
155
|
+
|
|
156
|
+
constructor(parent: Object3D, segments = 28) {
|
|
157
|
+
this.#material = new MeshBasicNodeMaterial({
|
|
158
|
+
depthWrite: false,
|
|
159
|
+
side: DoubleSide,
|
|
160
|
+
toneMapped: false,
|
|
161
|
+
transparent: true,
|
|
162
|
+
});
|
|
163
|
+
// The jitter is evaluated per vertex from a hash of the segment and the seed, so a bolt's shape
|
|
164
|
+
// is a number the spell can hold still or re-roll without rebuilding the geometry.
|
|
165
|
+
const across = uv().x.sub(0.5);
|
|
166
|
+
const along = uv().y;
|
|
167
|
+
const jag = along
|
|
168
|
+
.mul(41.7)
|
|
169
|
+
.add(this.#seed)
|
|
170
|
+
.fract()
|
|
171
|
+
.sub(0.5)
|
|
172
|
+
.mul(0.055)
|
|
173
|
+
.add(along.mul(97.3).add(this.#seed.mul(1.7)).fract().sub(0.5).mul(0.022));
|
|
174
|
+
const coreWidth = float(0.16).mul(oneMinus(smoothstep(0.0, 0.5, across.abs().mul(2))));
|
|
175
|
+
const glowWidth = oneMinus(smoothstep(0.0, 0.5, across.abs().mul(2).sub(coreWidth)));
|
|
176
|
+
this.#material.colorNode = vec4(
|
|
177
|
+
mix(this.#colour, this.#core, coreWidth),
|
|
178
|
+
glowWidth.mul(this.#strength).mul(oneMinus(along.mul(0.25))),
|
|
179
|
+
);
|
|
180
|
+
this.#material.positionNode = vec3(across.add(jag), along, 0);
|
|
181
|
+
this.mesh = new Mesh(new PlaneGeometry(1, 1, 1, segments), this.#material);
|
|
182
|
+
this.mesh.frustumCulled = false;
|
|
183
|
+
this.mesh.visible = false;
|
|
184
|
+
parent.add(this.mesh);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Stretch the ribbon from `from` to `to`, facing the camera enough to read from any angle. */
|
|
188
|
+
show(
|
|
189
|
+
from: Vector3,
|
|
190
|
+
to: Vector3,
|
|
191
|
+
width: number,
|
|
192
|
+
colour: Color,
|
|
193
|
+
core: Color,
|
|
194
|
+
strength: number,
|
|
195
|
+
seed: number,
|
|
196
|
+
camera?: { position: Vector3 },
|
|
197
|
+
): void {
|
|
198
|
+
const direction = new Vector3().subVectors(to, from);
|
|
199
|
+
const span = direction.length();
|
|
200
|
+
if (!(span > 1e-4)) {
|
|
201
|
+
this.hide();
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
this.mesh.position.copy(from).addScaledVector(direction, 0.5);
|
|
205
|
+
const axis = direction.clone().normalize();
|
|
206
|
+
this.mesh.quaternion.setFromUnitVectors(this.#up, axis);
|
|
207
|
+
// A flat column disappears edge-on, so the ribbon twists about its own axis to face the camera.
|
|
208
|
+
if (camera !== undefined) {
|
|
209
|
+
const toCamera = new Vector3().subVectors(camera.position, this.mesh.position);
|
|
210
|
+
toCamera.addScaledVector(axis, -toCamera.dot(axis));
|
|
211
|
+
if (toCamera.lengthSq() > 1e-6) {
|
|
212
|
+
const basis = new Vector3(1, 0, 0).applyQuaternion(this.mesh.quaternion);
|
|
213
|
+
this.mesh.rotateY(
|
|
214
|
+
Math.atan2(new Vector3().crossVectors(basis, toCamera).dot(axis), basis.dot(toCamera)),
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
this.mesh.scale.set(Math.max(0.01, finite("bolt.width", width)), span, 1);
|
|
219
|
+
this.#colour.value.copy(colour);
|
|
220
|
+
this.#core.value.copy(core);
|
|
221
|
+
this.#strength.value = Math.max(0, finite("bolt.strength", strength));
|
|
222
|
+
this.#seed.value = seed % 1;
|
|
223
|
+
this.mesh.visible = this.#strength.value > 0.001;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
hide(): void {
|
|
227
|
+
this.mesh.visible = false;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** A shield: a bubble whose rim brightens as it takes a hit. */
|
|
232
|
+
export class ShieldBubble {
|
|
233
|
+
readonly mesh: Mesh;
|
|
234
|
+
readonly #colour = uniform(new Color(1, 0.4, 0.9));
|
|
235
|
+
readonly #strength = uniform(0);
|
|
236
|
+
readonly #pulse = uniform(0);
|
|
237
|
+
readonly #material: MeshBasicNodeMaterial;
|
|
238
|
+
|
|
239
|
+
constructor(parent: Object3D) {
|
|
240
|
+
this.#material = new MeshBasicNodeMaterial({
|
|
241
|
+
depthWrite: false,
|
|
242
|
+
side: DoubleSide,
|
|
243
|
+
toneMapped: false,
|
|
244
|
+
transparent: true,
|
|
245
|
+
});
|
|
246
|
+
// A six-fold cell pattern on the sphere reads as a faceted ward, not a soap bubble.
|
|
247
|
+
const cells = uv().mul(TAU * 6);
|
|
248
|
+
const facet = abs(cells.x.sin().mul(cells.y.sin())).mul(0.5).add(0.5);
|
|
249
|
+
const rim = pow(oneMinus(abs(uv().x.mul(2).sub(1)).mul(0.6)), 1.6);
|
|
250
|
+
this.#material.colorNode = vec4(
|
|
251
|
+
this.#colour,
|
|
252
|
+
facet.mul(0.85).add(rim.mul(0.9)).mul(this.#strength).mul(this.#pulse.add(0.75)),
|
|
253
|
+
);
|
|
254
|
+
this.mesh = new Mesh(new SphereGeometry(1, 32, 24), this.#material);
|
|
255
|
+
this.mesh.frustumCulled = false;
|
|
256
|
+
this.mesh.visible = false;
|
|
257
|
+
parent.add(this.mesh);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
show(centre: Vector3, radius: number, colour: Color, strength: number, pulse: number): void {
|
|
261
|
+
this.mesh.position.copy(centre);
|
|
262
|
+
this.mesh.scale.setScalar(Math.max(0.001, finite("shield.radius", radius)));
|
|
263
|
+
this.#colour.value.copy(colour);
|
|
264
|
+
this.#strength.value = Math.max(0, finite("shield.strength", strength));
|
|
265
|
+
this.#pulse.value = Math.min(1, Math.max(0, pulse));
|
|
266
|
+
this.mesh.visible = this.#strength.value > 0.001;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
hide(): void {
|
|
270
|
+
this.mesh.visible = false;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** The pool of drawables one spell owns. */
|
|
275
|
+
export interface ISpellDrawables {
|
|
276
|
+
readonly core: SpellCore;
|
|
277
|
+
readonly shell: ShockShell;
|
|
278
|
+
readonly bolt: BoltColumn;
|
|
279
|
+
readonly shield: ShieldBubble;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Build a spell's drawables and add them to the world's scene.
|
|
284
|
+
*
|
|
285
|
+
* A spell builds these at `create`; a cast only shows and hides them. That is the whole reason the
|
|
286
|
+
* first cast of a spell is not slower than its hundredth.
|
|
287
|
+
*/
|
|
288
|
+
export function createDrawables(parent: Object3D): ISpellDrawables {
|
|
289
|
+
return {
|
|
290
|
+
bolt: new BoltColumn(parent),
|
|
291
|
+
core: new SpellCore(parent),
|
|
292
|
+
shell: new ShockShell(parent),
|
|
293
|
+
shield: new ShieldBubble(parent),
|
|
294
|
+
};
|
|
295
|
+
}
|
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the world keeps what a spell did to it.
|
|
3
|
+
*
|
|
4
|
+
* A fireball that only draws a fireball has not landed anywhere. Upstream keeps this in
|
|
5
|
+
* `GroundField.js`: every spell paints a patch of floor with heat, char, water, frost, a crack or a
|
|
6
|
+
* residual glow, and the floor's own shader reads those channels back. The arena SDF is what makes
|
|
7
|
+
* that file impossible to reuse, so this is the same idea with the arena removed — the game says
|
|
8
|
+
* where the floor is, and the game's material decides what heat looks like.
|
|
9
|
+
*
|
|
10
|
+
* Same contract as `RippleField`: it draws nothing, owns no mesh and no colour. It reports values,
|
|
11
|
+
* the game reads them, and a game that never looks at a channel pays a float array for it and
|
|
12
|
+
* nothing else.
|
|
13
|
+
*
|
|
14
|
+
* The solve is a CPU grid because a game reads it for combos: "cast ice on wet floor" has to be a
|
|
15
|
+
* branch, not a GPU readback that arrives two frames late.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** The channels a spell writes. Each is independent and decays on its own clock. */
|
|
19
|
+
export const REACTION_CHANNELS = [
|
|
20
|
+
/** Fire, lightning and lava: what a floor reads as "still burning". */
|
|
21
|
+
"heat",
|
|
22
|
+
/** Burnt floor. Decays far slower than heat and is what a scorch looks like. */
|
|
23
|
+
"char",
|
|
24
|
+
/** Water and ice melt: a puddle a lightning spell spreads across. */
|
|
25
|
+
"wet",
|
|
26
|
+
/** Ice. Reads cold, and a fire spell wants to melt it back. */
|
|
27
|
+
"frost",
|
|
28
|
+
/** A fissure or a shattered floor: a crack pattern whose strength fades. */
|
|
29
|
+
"crack",
|
|
30
|
+
/** Residual magic light, for a floor that glows after impact. */
|
|
31
|
+
"glow",
|
|
32
|
+
] as const;
|
|
33
|
+
|
|
34
|
+
export type ReactionChannel = (typeof REACTION_CHANNELS)[number];
|
|
35
|
+
|
|
36
|
+
/** Where a splat landed and how much of it the spell laid down. Every channel is 0..1. */
|
|
37
|
+
export interface IReactionSplat {
|
|
38
|
+
readonly x: number;
|
|
39
|
+
readonly z: number;
|
|
40
|
+
/** Metres. The Gaussian reaches about three of these. */
|
|
41
|
+
readonly radius: number;
|
|
42
|
+
/** `radial` fades from the centre; `ring` leaves a rim; `streak` is stretched along `angle`. */
|
|
43
|
+
readonly pattern?: ReactionPattern;
|
|
44
|
+
/** Radians. Turns the `streak` axis, and is ignored by every other pattern. */
|
|
45
|
+
readonly angle?: number;
|
|
46
|
+
/** Pattern seed, so two impacts of the same spell are not the same mark. */
|
|
47
|
+
readonly seed?: number;
|
|
48
|
+
readonly heat?: number;
|
|
49
|
+
readonly char?: number;
|
|
50
|
+
readonly wet?: number;
|
|
51
|
+
readonly frost?: number;
|
|
52
|
+
readonly crack?: number;
|
|
53
|
+
readonly glow?: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type ReactionPattern = "radial" | "ring" | "streak";
|
|
57
|
+
|
|
58
|
+
/** A whole-cell rectangle of changed cells, in grid indices. */
|
|
59
|
+
export interface IReactionDirty {
|
|
60
|
+
readonly x: number;
|
|
61
|
+
readonly z: number;
|
|
62
|
+
readonly width: number;
|
|
63
|
+
readonly height: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface IReactionFieldOptions {
|
|
67
|
+
/** Cells per side. Cost is quadratic in this; 96 over a 120 m patch is ~1.25 m per cell. */
|
|
68
|
+
readonly resolution?: number;
|
|
69
|
+
/** Width of the patch in metres. */
|
|
70
|
+
readonly size?: number;
|
|
71
|
+
/** Per-channel seconds for a full-strength value to halve. `char` is the slow one. */
|
|
72
|
+
readonly halfLife?: Partial<Readonly<Record<ReactionChannel, number>>>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const DEFAULT_HALF_LIFE: Readonly<Record<ReactionChannel, number>> = {
|
|
76
|
+
// ponytail: one half-life per channel and no wave transport. Upgrade to a GPU solve with
|
|
77
|
+
// advection if a game needs fire to spread uphill or water to run across a floor.
|
|
78
|
+
heat: 3.5,
|
|
79
|
+
char: 24,
|
|
80
|
+
crack: 40,
|
|
81
|
+
frost: 8,
|
|
82
|
+
glow: 1.6,
|
|
83
|
+
wet: 12,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const clamp01 = (value: number): number => (value < 0 ? 0 : value > 1 ? 1 : value);
|
|
87
|
+
|
|
88
|
+
const finite = (value: number, label: string): number => {
|
|
89
|
+
if (!Number.isFinite(value)) throw new TypeError(`${label} must be finite`);
|
|
90
|
+
return value;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/** A small integer hash, so a seed alone reproduces the mark without touching `Math.random`. */
|
|
94
|
+
const hash2 = (x: number, y: number, seed: number): number => {
|
|
95
|
+
let h = Math.imul(x, 0x27d4eb2d) ^ Math.imul(y, 0x165667b1) ^ Math.imul(seed, 0x9e3779b1);
|
|
96
|
+
h = Math.imul(h ^ (h >>> 15), 0x2545f491);
|
|
97
|
+
return (h ^ (h >>> 13)) >>> 0;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* A patch of world that spells leave marks on.
|
|
102
|
+
*
|
|
103
|
+
* One float array per channel, decayed on `update`, written by `splat`. `consumeDirty` reports the
|
|
104
|
+
* cells that changed since the last call, so a material uploads a strip instead of the whole grid.
|
|
105
|
+
*/
|
|
106
|
+
export class ReactionField {
|
|
107
|
+
readonly resolution: number;
|
|
108
|
+
readonly size: number;
|
|
109
|
+
readonly dx: number;
|
|
110
|
+
/** Patch centre in world metres. */
|
|
111
|
+
centerX = 0;
|
|
112
|
+
centerZ = 0;
|
|
113
|
+
/** Bumped whenever any cell changes, so an upload can skip an unchanged frame. */
|
|
114
|
+
version = 0;
|
|
115
|
+
|
|
116
|
+
readonly #channels: Record<ReactionChannel, Float32Array>;
|
|
117
|
+
readonly #halfLife: Record<ReactionChannel, number>;
|
|
118
|
+
#dirty: { minX: number; minZ: number; maxX: number; maxZ: number } | undefined;
|
|
119
|
+
#empty = true;
|
|
120
|
+
|
|
121
|
+
constructor(options: IReactionFieldOptions = {}) {
|
|
122
|
+
const resolution = options.resolution ?? 96;
|
|
123
|
+
if (!Number.isInteger(resolution) || resolution < 16 || resolution > 512)
|
|
124
|
+
throw new RangeError("resolution must be an integer in 16..512");
|
|
125
|
+
const size = options.size ?? 120;
|
|
126
|
+
if (!Number.isFinite(size) || size <= 0) throw new RangeError("size must be a positive length");
|
|
127
|
+
this.resolution = resolution;
|
|
128
|
+
this.size = size;
|
|
129
|
+
this.dx = size / resolution;
|
|
130
|
+
const count = resolution * resolution;
|
|
131
|
+
this.#channels = {} as Record<ReactionChannel, Float32Array>;
|
|
132
|
+
this.#halfLife = { ...DEFAULT_HALF_LIFE };
|
|
133
|
+
for (const channel of REACTION_CHANNELS) {
|
|
134
|
+
this.#channels[channel] = new Float32Array(count);
|
|
135
|
+
const halfLife = options.halfLife?.[channel];
|
|
136
|
+
if (halfLife !== undefined) {
|
|
137
|
+
if (!Number.isFinite(halfLife) || halfLife <= 0)
|
|
138
|
+
throw new RangeError(`halfLife.${channel} must be positive and finite`);
|
|
139
|
+
this.#halfLife[channel] = halfLife;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The live values of one channel, row-major from the patch's -x/-z corner. Read-only to a game. */
|
|
145
|
+
data(channel: ReactionChannel): Float32Array {
|
|
146
|
+
return this.#channels[channel];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** True while every cell of every channel is exactly zero. */
|
|
150
|
+
get empty(): boolean {
|
|
151
|
+
return this.#empty;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** True while the point is inside the patch, `margin` metres in from its rim. */
|
|
155
|
+
contains(x: number, z: number, margin = 0): boolean {
|
|
156
|
+
const half = this.size / 2 - margin;
|
|
157
|
+
return Math.abs(x - this.centerX) < half && Math.abs(z - this.centerZ) < half;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Nearest-neighbour read at a world point, 0 outside the patch. Cheap enough for a combo check. */
|
|
161
|
+
sample(channel: ReactionChannel, x: number, z: number): number {
|
|
162
|
+
if (!this.contains(x, z)) return 0;
|
|
163
|
+
const n = this.resolution;
|
|
164
|
+
const i = Math.min(n - 1, Math.max(0, Math.floor(((x - this.centerX) / this.size + 0.5) * n)));
|
|
165
|
+
const j = Math.min(n - 1, Math.max(0, Math.floor(((z - this.centerZ) / this.size + 0.5) * n)));
|
|
166
|
+
return (this.#channels[channel] as Float32Array)[j * n + i] as number;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Strongest channel at a point, for a game that only asks "is anything happening here". */
|
|
170
|
+
strongest(x: number, z: number): ReactionChannel | undefined {
|
|
171
|
+
let best: ReactionChannel | undefined;
|
|
172
|
+
let bestValue = 0;
|
|
173
|
+
for (const channel of REACTION_CHANNELS) {
|
|
174
|
+
const value = this.sample(channel, x, z);
|
|
175
|
+
if (value > bestValue) {
|
|
176
|
+
bestValue = value;
|
|
177
|
+
best = channel;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return best;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Lay a mark on the patch. Every channel is additive and saturates at 1, so two fireballs on the
|
|
185
|
+
* same cell burn hotter than one but never past the top of the range. Off-patch splats return
|
|
186
|
+
* false and change nothing.
|
|
187
|
+
*/
|
|
188
|
+
splat(splat: IReactionSplat): boolean {
|
|
189
|
+
// Validate before anything else: off-patch and malformed are different answers, and a caller
|
|
190
|
+
// that hands in a NaN must be told so rather than silently told "off patch".
|
|
191
|
+
const radius = validateSplat(splat);
|
|
192
|
+
if (!this.contains(splat.x, splat.z)) return false;
|
|
193
|
+
const amounts = amountsOf(splat);
|
|
194
|
+
if (amounts === undefined) return true;
|
|
195
|
+
const { cells, hiX, hiZ, ix, iz, loX, loZ } = this.#cellRect(splat, radius);
|
|
196
|
+
const seed = splat.seed ?? 0;
|
|
197
|
+
const shape = {
|
|
198
|
+
angleCos: Math.cos(splat.angle ?? 0),
|
|
199
|
+
angleSin: Math.sin(splat.angle ?? 0),
|
|
200
|
+
cells,
|
|
201
|
+
pattern: splat.pattern ?? "radial",
|
|
202
|
+
};
|
|
203
|
+
const n = this.resolution;
|
|
204
|
+
for (let j = loZ; j <= hiZ; j++) {
|
|
205
|
+
for (let i = loX; i <= hiX; i++) {
|
|
206
|
+
const shaped = cellShape(shape, i - ix, j - iz);
|
|
207
|
+
if (shaped <= 0.002) continue;
|
|
208
|
+
// A per-cell wobble keeps a mark from reading as an airbrushed circle.
|
|
209
|
+
const grain = 0.7 + 0.6 * ((hash2(i, j, seed) & 0xffff) / 0xffff);
|
|
210
|
+
const index = j * n + i;
|
|
211
|
+
for (const [channel, amount] of amounts) {
|
|
212
|
+
const data = this.#channels[channel];
|
|
213
|
+
data[index] = clamp01((data[index] as number) + amount * shaped * grain);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
this.#markDirty(loX, loZ, hiX, hiZ);
|
|
218
|
+
this.#empty = false;
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Forget everything now, and report one last full-patch upload. */
|
|
223
|
+
clear(): void {
|
|
224
|
+
for (const channel of REACTION_CHANNELS) (this.#channels[channel] as Float32Array).fill(0);
|
|
225
|
+
this.#empty = true;
|
|
226
|
+
this.#markDirty(0, 0, this.resolution - 1, this.resolution - 1);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Move the patch with the action. Newly exposed cells arrive clean. */
|
|
230
|
+
recenter(x: number, z: number): void {
|
|
231
|
+
finite(x, "x");
|
|
232
|
+
finite(z, "z");
|
|
233
|
+
const di = Math.round((x - this.centerX) / this.dx);
|
|
234
|
+
const dj = Math.round((z - this.centerZ) / this.dx);
|
|
235
|
+
if (di === 0 && dj === 0) return;
|
|
236
|
+
this.centerX += di * this.dx;
|
|
237
|
+
this.centerZ += dj * this.dx;
|
|
238
|
+
if (this.#empty) return;
|
|
239
|
+
if (Math.abs(di) >= this.resolution || Math.abs(dj) >= this.resolution) {
|
|
240
|
+
for (const channel of REACTION_CHANNELS) (this.#channels[channel] as Float32Array).fill(0);
|
|
241
|
+
this.#empty = true;
|
|
242
|
+
} else {
|
|
243
|
+
const scratch = new Float32Array(this.resolution * this.resolution);
|
|
244
|
+
for (const channel of REACTION_CHANNELS)
|
|
245
|
+
shift(this.#channels[channel], this.resolution, di, dj, scratch);
|
|
246
|
+
}
|
|
247
|
+
this.#markDirty(0, 0, this.resolution - 1, this.resolution - 1);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Decay every channel on its own half-life. Time passing on a clean patch changes no cell. */
|
|
251
|
+
update(dt: number): void {
|
|
252
|
+
if (!Number.isFinite(dt) || dt < 0)
|
|
253
|
+
throw new RangeError("ReactionField.update delta must be finite and non-negative");
|
|
254
|
+
if (this.#empty || dt === 0) return;
|
|
255
|
+
let live = false;
|
|
256
|
+
for (const channel of REACTION_CHANNELS) live = this.#decay(channel, dt) || live;
|
|
257
|
+
if (!live) this.#empty = true;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Hand back the cells that changed since the last call, and forget them.
|
|
262
|
+
* `undefined` means nothing changed, so an upload can be skipped whole.
|
|
263
|
+
*/
|
|
264
|
+
consumeDirty(): IReactionDirty | undefined {
|
|
265
|
+
const dirty = this.#dirty;
|
|
266
|
+
this.#dirty = undefined;
|
|
267
|
+
if (dirty === undefined) return undefined;
|
|
268
|
+
return {
|
|
269
|
+
height: dirty.maxZ - dirty.minZ + 1,
|
|
270
|
+
width: dirty.maxX - dirty.minX + 1,
|
|
271
|
+
x: dirty.minX,
|
|
272
|
+
z: dirty.minZ,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** The cells a splat can reach, and the splat's centre in grid coordinates. */
|
|
277
|
+
#cellRect(
|
|
278
|
+
splat: IReactionSplat,
|
|
279
|
+
radius: number,
|
|
280
|
+
): {
|
|
281
|
+
ix: number;
|
|
282
|
+
iz: number;
|
|
283
|
+
cells: number;
|
|
284
|
+
loX: number;
|
|
285
|
+
hiX: number;
|
|
286
|
+
loZ: number;
|
|
287
|
+
hiZ: number;
|
|
288
|
+
} {
|
|
289
|
+
const n = this.resolution;
|
|
290
|
+
const ix = ((splat.x - this.centerX) / this.size + 0.5) * n - 0.5;
|
|
291
|
+
const iz = ((splat.z - this.centerZ) / this.size + 0.5) * n - 0.5;
|
|
292
|
+
const cells = Math.max(0.75, radius / this.dx);
|
|
293
|
+
// A streak stretches 2.4x along its axis, so its reach has to cover that or the mark is cut off.
|
|
294
|
+
const reach = 2.2 * cells * (splat.pattern === "streak" ? 2.4 : 1);
|
|
295
|
+
return {
|
|
296
|
+
cells,
|
|
297
|
+
hiX: Math.min(n - 1, Math.ceil(ix + reach)),
|
|
298
|
+
hiZ: Math.min(n - 1, Math.ceil(iz + reach)),
|
|
299
|
+
ix,
|
|
300
|
+
iz,
|
|
301
|
+
loX: Math.max(0, Math.floor(ix - reach)),
|
|
302
|
+
loZ: Math.max(0, Math.floor(iz - reach)),
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** Decay one channel for a step. True while any cell is still lit. */
|
|
307
|
+
#decay(channel: ReactionChannel, dt: number): boolean {
|
|
308
|
+
const data = this.#channels[channel];
|
|
309
|
+
// exp2(-dt / halfLife): the fraction still left after this step.
|
|
310
|
+
const keep = 2 ** (-dt / (this.#halfLife[channel] as number));
|
|
311
|
+
const extent = { hiX: -1, hiZ: -1, loX: this.resolution, loZ: this.resolution };
|
|
312
|
+
const live = decayCells(data, keep, this.resolution, extent);
|
|
313
|
+
// Only the cells that were lit changed, so an upload stays a strip and not the whole patch.
|
|
314
|
+
if (extent.hiX >= 0) this.#markDirty(extent.loX, extent.loZ, extent.hiX, extent.hiZ);
|
|
315
|
+
return live;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
#markDirty(minX: number, minZ: number, maxX: number, maxZ: number): void {
|
|
319
|
+
this.version += 1;
|
|
320
|
+
const dirty = this.#dirty;
|
|
321
|
+
if (dirty === undefined) this.#dirty = { maxX, maxZ, minX, minZ };
|
|
322
|
+
else {
|
|
323
|
+
dirty.minX = Math.min(dirty.minX, minX);
|
|
324
|
+
dirty.minZ = Math.min(dirty.minZ, minZ);
|
|
325
|
+
dirty.maxX = Math.max(dirty.maxX, maxX);
|
|
326
|
+
dirty.maxZ = Math.max(dirty.maxZ, maxZ);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** Decay one channel's cells in place, recording the lit rectangle and whether anything survives. */
|
|
332
|
+
function decayCells(
|
|
333
|
+
data: Float32Array,
|
|
334
|
+
keep: number,
|
|
335
|
+
n: number,
|
|
336
|
+
extent: { loX: number; loZ: number; hiX: number; hiZ: number },
|
|
337
|
+
): boolean {
|
|
338
|
+
let live = false;
|
|
339
|
+
for (let index = 0; index < data.length; index++) {
|
|
340
|
+
const value = data[index] as number;
|
|
341
|
+
if (value === 0) continue;
|
|
342
|
+
const next = value * keep;
|
|
343
|
+
data[index] = next < 1e-4 ? 0 : next;
|
|
344
|
+
const i = index % n;
|
|
345
|
+
const j = (index - i) / n;
|
|
346
|
+
if (i < extent.loX) extent.loX = i;
|
|
347
|
+
if (i > extent.hiX) extent.hiX = i;
|
|
348
|
+
if (j < extent.loZ) extent.loZ = j;
|
|
349
|
+
if (j > extent.hiZ) extent.hiZ = j;
|
|
350
|
+
if (data[index] !== 0) live = true;
|
|
351
|
+
}
|
|
352
|
+
return live;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** Reject a splat that is not a real mark, before any of it is applied. */
|
|
356
|
+
function validateSplat(splat: IReactionSplat): number {
|
|
357
|
+
const radius = finite(splat.radius, "splat.radius");
|
|
358
|
+
if (!(radius > 0)) throw new RangeError("splat.radius must be positive");
|
|
359
|
+
if (!Number.isFinite(splat.x)) throw new TypeError("splat.x must be finite");
|
|
360
|
+
if (!Number.isFinite(splat.z)) throw new TypeError("splat.z must be finite");
|
|
361
|
+
if (splat.angle !== undefined && !Number.isFinite(splat.angle))
|
|
362
|
+
throw new TypeError("splat.angle must be finite");
|
|
363
|
+
// A NaN amount would saturate nothing and never decay, leaving the patch permanently dirty.
|
|
364
|
+
for (const channel of REACTION_CHANNELS) {
|
|
365
|
+
const amount = splat[channel];
|
|
366
|
+
if (amount !== undefined && !Number.isFinite(amount))
|
|
367
|
+
throw new TypeError(`splat.${channel} must be finite`);
|
|
368
|
+
}
|
|
369
|
+
return radius;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** The channels a splat actually writes, or undefined when it asks for none. */
|
|
373
|
+
function amountsOf(splat: IReactionSplat): [ReactionChannel, number][] | undefined {
|
|
374
|
+
const amounts: [ReactionChannel, number][] = [];
|
|
375
|
+
for (const channel of REACTION_CHANNELS) {
|
|
376
|
+
const amount = splat[channel] ?? 0;
|
|
377
|
+
if (amount !== 0) amounts.push([channel, amount]);
|
|
378
|
+
}
|
|
379
|
+
return amounts.length === 0 ? undefined : amounts;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** How much of a splat lands on one cell, from the pattern and the distance to its centre. */
|
|
383
|
+
function cellShape(
|
|
384
|
+
shape: {
|
|
385
|
+
readonly pattern: ReactionPattern;
|
|
386
|
+
readonly cells: number;
|
|
387
|
+
readonly angleCos: number;
|
|
388
|
+
readonly angleSin: number;
|
|
389
|
+
},
|
|
390
|
+
ddx: number,
|
|
391
|
+
ddz: number,
|
|
392
|
+
): number {
|
|
393
|
+
const { cells, pattern } = shape;
|
|
394
|
+
if (pattern === "streak") {
|
|
395
|
+
// Stretch along the impact direction, so a slid-in spell leaves a line, not a disc.
|
|
396
|
+
const along = (ddx * shape.angleCos + ddz * shape.angleSin) / 2.4;
|
|
397
|
+
const across = (-ddx * shape.angleSin + ddz * shape.angleCos) / 0.8;
|
|
398
|
+
return Math.exp(-((along * along + across * across) / (cells * cells)));
|
|
399
|
+
}
|
|
400
|
+
const q = (ddx * ddx + ddz * ddz) / (cells * cells);
|
|
401
|
+
return pattern === "ring" ? Math.exp(-((Math.sqrt(q) - 0.8) ** 2) * 6) : Math.exp(-q);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function shift(data: Float32Array, n: number, di: number, dj: number, scratch: Float32Array): void {
|
|
405
|
+
scratch.fill(0);
|
|
406
|
+
const i0 = Math.max(0, -di);
|
|
407
|
+
const i1 = Math.min(n, n - di);
|
|
408
|
+
const j0 = Math.max(0, -dj);
|
|
409
|
+
const j1 = Math.min(n, n - dj);
|
|
410
|
+
for (let j = j0; j < j1; j++) {
|
|
411
|
+
const source = (j + dj) * n + i0 + di;
|
|
412
|
+
scratch.set(data.subarray(source, source + (i1 - i0)), j * n + i0);
|
|
413
|
+
}
|
|
414
|
+
data.set(scratch);
|
|
415
|
+
}
|