@threenative/core 0.3.10 → 0.3.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/capabilities.json +1096 -26
- package/dist/camera-shake-CZKOfGn4.d.ts +46 -0
- package/dist/{canvas-layer-vCtanIeB.d.ts → canvas-layer-CWuFRT7G.d.ts} +1 -1
- package/dist/chunk-2Z44XSI2.js +35 -0
- package/dist/chunk-5O73CDMR.js +1849 -0
- package/dist/chunk-6KLNISRJ.js +153 -0
- package/dist/chunk-7UUT3TY5.js +133 -0
- package/dist/chunk-CFGTPFUH.js +231 -0
- package/dist/chunk-DBELTTTM.js +615 -0
- package/dist/chunk-GMHATRPW.js +7701 -0
- package/dist/chunk-GXY7B3JG.js +46 -0
- package/dist/chunk-KIRUKTBQ.js +187 -0
- package/dist/chunk-N5LRP4BR.js +220 -0
- package/dist/chunk-OKXUCASC.js +2238 -0
- package/dist/chunk-QWJEMUUT.js +94 -0
- package/dist/chunk-VAZM36TV.js +497 -0
- package/dist/{game-DzMcitDO.d.ts → game-D7pAzZUY.d.ts} +15 -3
- package/dist/hot.d.ts +3 -3
- package/dist/hot.js +2 -46
- package/dist/index.d.ts +329 -302
- package/dist/index.js +10565 -22533
- package/dist/{model-lod-CTpZ6imz.d.ts → model-lod-B6BMqS1r.d.ts} +33 -3
- package/dist/playtest.d.ts +3 -3
- package/dist/playtest.js +24 -75
- package/dist/react-css.js +1 -215
- package/dist/react.d.ts +2 -2
- package/dist/{renderer-rZm0hVqq.d.ts → renderer-DvFVoAgj.d.ts} +13 -4
- package/dist/spells/index.d.ts +643 -0
- package/dist/spells/index.js +1558 -0
- package/dist/spells/ultra/index.d.ts +1600 -0
- package/dist/spells/ultra/index.js +6804 -0
- package/dist/terrain-jobs-worker.js +1 -123
- package/dist/two-sided-foliage-W523J3UM.js +1 -0
- package/dist/ui-layer.js +5 -384
- package/dist/worker.js +4 -41
- package/dist/world.d.ts +725 -259
- package/dist/world.js +16714 -18284
- package/mcp/animation-server.mjs +1 -1
- package/mcp/animation.mjs +2 -3
- package/mcp/engine-server.mjs +142 -47
- package/mcp/servers.mjs +9 -2
- package/mcp/terrain.mjs +8 -0
- package/package.json +14 -6
- package/patches/three@0.187.0-prd269-upgrade.patch +2102 -0
- package/patches/three@0.187.0.patch +2004 -407
- package/src/assets.ts +9 -2
- package/src/card-lod.ts +152 -0
- package/src/compressed-texture-preparation.ts +1008 -0
- package/src/frame-budget.ts +31 -20
- package/src/game-dev-tools.ts +93 -0
- package/src/game-frame-loop.ts +995 -0
- package/src/game-renderer-metrics.ts +77 -0
- package/src/game-startup.ts +1001 -0
- package/src/game-validate.ts +70 -0
- package/src/game.ts +253 -1935
- package/src/index.ts +27 -2
- package/src/launch-diagnostics.ts +22 -12
- package/src/look/ao-admission.ts +24 -0
- package/src/look/filmic-tone-mapping.ts +15 -13
- package/src/look/look-diagnostics.ts +3 -3
- package/src/look/quality.ts +10 -5
- package/src/look/world-environment.ts +76 -9
- package/src/loop.ts +29 -0
- package/src/model-lod.ts +42 -3
- package/src/particle-kinds.ts +515 -0
- package/src/picking.ts +17 -3
- package/src/playtest.ts +27 -1
- package/src/projection-apply.ts +4 -1
- package/src/projection-plan.ts +9 -6
- package/src/render/mesh-pool.ts +1 -1
- package/src/render/resolution-subrect.ts +2 -0
- package/src/render/rough-diffuse.ts +46 -12
- package/src/render/soft-shadow.ts +27 -12
- package/src/render/virtual-shadow-adaptive.ts +55 -0
- package/src/render/virtual-shadow-casters.ts +175 -0
- package/src/render/virtual-shadow-layers.ts +76 -0
- package/src/render/virtual-shadow-level.ts +224 -0
- package/src/render/virtual-shadow-markers.ts +193 -0
- package/src/render/virtual-shadow-options.ts +370 -0
- package/src/render/virtual-shadow-pages.ts +5 -0
- package/src/render/virtual-shadow.ts +213 -992
- package/src/render/world-impostor-surface.ts +293 -34
- package/src/render-camera-cull.ts +32 -1
- package/src/render-pass-budget.ts +34 -7
- package/src/renderProjection.ts +22 -0
- package/src/renderer.ts +92 -37
- package/src/snow-field.ts +1 -1
- package/src/spells/cast-pool.ts +361 -0
- package/src/spells/index.ts +177 -0
- package/src/spells/mechanisms.ts +295 -0
- package/src/spells/reaction-field.ts +415 -0
- package/src/spells/spell-lights.ts +139 -0
- package/src/spells/spell-world.ts +187 -0
- package/src/spells/spell.ts +397 -0
- package/src/spells/spells.ts +283 -0
- package/src/spells/ultra/engine/camera.ts +184 -0
- package/src/spells/ultra/engine/quality.ts +96 -0
- package/src/spells/ultra/index.ts +52 -0
- package/src/spells/ultra/main.ts +305 -0
- package/src/spells/ultra/scene/arenaMaterial.ts +325 -0
- package/src/spells/ultra/spells/cast.ts +233 -0
- package/src/spells/ultra/spells/fire.ts +1267 -0
- package/src/spells/ultra/vfx/FluidVolume.ts +816 -0
- package/src/spells/ultra/vfx/FluidVolume.tsl.ts +855 -0
- package/src/spells/ultra/vfx/FxTarget.ts +61 -0
- package/src/spells/ultra/vfx/GroundField.ts +1399 -0
- package/src/spells/ultra/vfx/Particles.ts +1475 -0
- package/src/spells/ultra/vfx/Ribbons.ts +645 -0
- package/src/spells/ultra/vfx/Shockwaves.ts +286 -0
- package/src/spells/ultra/vfx/Volumes.ts +489 -0
- package/src/spells/ultra/vfx/common.ts +1195 -0
- package/src/spells/ultra/vfx/noise.ts +441 -0
- package/src/startup-readiness.ts +128 -34
- package/src/version.ts +1 -1
- package/src/warmup.ts +56 -8
- package/src/water-surface.ts +9 -3
- package/src/world-bundle-safety.ts +401 -0
- package/src/world-cell-batches.ts +1216 -0
- package/src/world-cell-chunks.ts +1132 -0
- package/src/world-cell-data.ts +995 -0
- package/src/world-cell-impostors.ts +1449 -0
- package/src/world-cell-lod-bias.ts +151 -0
- package/src/world-cell-models.ts +1074 -0
- package/src/world-cell-options.ts +189 -0
- package/src/world-cell-placements.ts +694 -0
- package/src/world-cell-prewarm.ts +557 -0
- package/src/world-cell-readiness.ts +357 -0
- package/src/world-cell-render-bundles.ts +459 -0
- package/src/world-cell-render-gpu-dress.ts +714 -0
- package/src/world-cell-render-main-cull.ts +484 -0
- package/src/world-cell-render-shadow-claims.ts +198 -0
- package/src/world-cell-render-util.ts +246 -0
- package/src/world-cell-render.ts +1493 -0
- package/src/world-cell-residency.ts +745 -0
- package/src/world-cells.ts +1318 -10418
- package/src/world-chunk-shadow.ts +308 -0
- package/src/world-collar.ts +315 -0
- package/src/world-gpu-flags.ts +163 -0
- package/src/world-gpu-readback.ts +881 -0
- package/src/world-gpu-reference.ts +510 -0
- package/src/world-gpu-scene.ts +214 -2264
- package/src/world-gpu-shadow.ts +190 -0
- package/src/world-gpu-types.ts +381 -0
- package/src/world-gpu-validation.ts +518 -0
- package/src/world-heightfield.ts +802 -0
- package/src/world-load-limiter.ts +106 -0
- package/src/world-noise.ts +241 -0
- package/src/world-package.ts +42 -0
- package/src/world-region.ts +1 -1
- package/src/world-terrain-splat.ts +365 -149
- package/src/world-tile-budget.ts +256 -0
- package/src/world-tile-geometry.ts +469 -0
- package/src/world-tile-lod.ts +408 -0
- package/src/world-tile-observation.ts +566 -0
- package/src/world-tile-stitch.ts +166 -0
- package/src/world-tiles.ts +170 -1745
- package/src/world-water-bodies.ts +359 -0
- package/src/world.ts +35 -741
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The world a spell lands in.
|
|
3
|
+
*
|
|
4
|
+
* A spell that reads `scene`, `camera` and a floor function directly has to be rewritten for every
|
|
5
|
+
* game, and the upstream files show what happens when it is: 34 of them read the arena SDF, so the
|
|
6
|
+
* arena is load-bearing and a game cannot replace it. Everything a spell needs therefore arrives
|
|
7
|
+
* through here — the scene to add its pooled objects to, the floor to land on, the light pool that
|
|
8
|
+
* never changes size, the surface field it paints, one shake and one flash limiter.
|
|
9
|
+
*
|
|
10
|
+
* Damage, mana, cooldowns and who the target actually is stay in the game. `SpellWorld` knows
|
|
11
|
+
* nothing about them, and a game that wants a spell to hurt something reads the cast's `to` point
|
|
12
|
+
* from its own update and decides.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { type Camera, Color, type Object3D, type Scene, Vector3 } from "three";
|
|
16
|
+
import { CameraShake, type CameraShakeCurve, type ICameraShakeOptions } from "../camera-shake.js";
|
|
17
|
+
import type { ICastEnvironment } from "./cast-pool.js";
|
|
18
|
+
import { type IReactionFieldOptions, ReactionField } from "./reaction-field.js";
|
|
19
|
+
import { SpellLights } from "./spell-lights.js";
|
|
20
|
+
|
|
21
|
+
export interface ISpellWorldShakeOptions {
|
|
22
|
+
/** Metres. Defaults to 0.12, which reads as a thump and not as a broken camera. */
|
|
23
|
+
readonly amplitude?: number;
|
|
24
|
+
/** Radians. Defaults to 0.012. */
|
|
25
|
+
readonly rotationAmplitude?: number;
|
|
26
|
+
/** Cycles per second. Defaults to 11. */
|
|
27
|
+
readonly frequency?: number;
|
|
28
|
+
/** Exponential decay per second. Defaults to 5. */
|
|
29
|
+
readonly decay?: number;
|
|
30
|
+
readonly curve?: CameraShakeCurve;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ISpellWorldOptions {
|
|
34
|
+
/** Where every pooled object is added. The game owns it, and it outlives every cast. */
|
|
35
|
+
readonly scene: Scene;
|
|
36
|
+
/** The camera a spell may read for billboarding. Optional: most spells never look at it. */
|
|
37
|
+
readonly camera?: Camera;
|
|
38
|
+
/** Floor height under a point in metres. A spell lands here instead of at y = 0. */
|
|
39
|
+
readonly groundAt?: (x: number, z: number) => number;
|
|
40
|
+
/** Cast records. Defaults to 16. */
|
|
41
|
+
readonly castSlots?: number;
|
|
42
|
+
/** Lights in the pool. Defaults to 4. */
|
|
43
|
+
readonly lightCount?: number;
|
|
44
|
+
/** Surface field. Defaults to 96 cells over 120 m. `false` skips it and saves the float arrays. */
|
|
45
|
+
readonly reaction?: IReactionFieldOptions | false;
|
|
46
|
+
/**
|
|
47
|
+
* Keep every spell to at most three flashes per second (WCAG 2.3.1). Defaults to false, which is
|
|
48
|
+
* the upstream behaviour; a game with an accessibility setting turns it on.
|
|
49
|
+
*/
|
|
50
|
+
readonly reducedFlashes?: boolean;
|
|
51
|
+
/** Spells that shake the camera. `false` gives them nothing to shake. */
|
|
52
|
+
readonly shake?: ISpellWorldShakeOptions | false;
|
|
53
|
+
/**
|
|
54
|
+
* Parent an object a spell built. A `GPUParticles3D` also needs a renderer before it can emit, and
|
|
55
|
+
* only the game knows its own context: pass the game context's `add`, which parents *and* attaches.
|
|
56
|
+
* The default parents to `scene`, which is enough for a test and not enough to draw.
|
|
57
|
+
*/
|
|
58
|
+
readonly add?: (object: Object3D) => void;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The stock shake: one thump, gone in about a fifth of a second. */
|
|
62
|
+
const DEFAULT_SHAKE: Required<Omit<ISpellWorldShakeOptions, "curve">> = {
|
|
63
|
+
amplitude: 0.12,
|
|
64
|
+
decay: 5,
|
|
65
|
+
frequency: 11,
|
|
66
|
+
rotationAmplitude: 0.012,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const DEFAULT_CURVE: CameraShakeCurve = (phase) => Math.sin(phase) * Math.cos(phase * 1.7);
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Everything a spell is allowed to touch.
|
|
73
|
+
*
|
|
74
|
+
* One instance per game. `update` advances the light decay, the surface field and the shake; the
|
|
75
|
+
* casts themselves live in a `CastPool` the game steps itself, so a paused game can stop casting
|
|
76
|
+
* without also freezing a scorch that is still fading.
|
|
77
|
+
*/
|
|
78
|
+
export class SpellWorld implements ICastEnvironment {
|
|
79
|
+
readonly scene: Scene;
|
|
80
|
+
readonly camera: Camera | undefined;
|
|
81
|
+
readonly groundAt: ((x: number, z: number) => number) | undefined;
|
|
82
|
+
readonly castSlots: number;
|
|
83
|
+
readonly lights: SpellLights;
|
|
84
|
+
readonly reaction: ReactionField | undefined;
|
|
85
|
+
readonly shake: CameraShake | undefined;
|
|
86
|
+
readonly reducedFlashes: boolean;
|
|
87
|
+
/** Where a spell with no `from` starts: the caster's hand, in world metres. */
|
|
88
|
+
readonly focus = new Vector3();
|
|
89
|
+
/** Parent a spell's pooled object. See `ISpellWorldOptions.add`. */
|
|
90
|
+
readonly add: (object: Object3D) => void;
|
|
91
|
+
/** Everything a spell has parented through `add`, in creation order. */
|
|
92
|
+
readonly objects: Object3D[] = [];
|
|
93
|
+
/** Seconds since the world was created. */
|
|
94
|
+
time = 0;
|
|
95
|
+
/** Flashes the limiter refused, so a game can see its own accessibility setting working. */
|
|
96
|
+
suppressedFlashes = 0;
|
|
97
|
+
|
|
98
|
+
/** Millisecond stamps of the flashes that were allowed, newest last, at most `FLASH_WINDOW`. */
|
|
99
|
+
#flashes: number[] = [];
|
|
100
|
+
|
|
101
|
+
constructor(options: ISpellWorldOptions) {
|
|
102
|
+
if (options.scene === undefined || options.scene === null)
|
|
103
|
+
throw new Error("SpellWorld.scene is required.");
|
|
104
|
+
this.scene = options.scene;
|
|
105
|
+
this.camera = options.camera;
|
|
106
|
+
this.groundAt = options.groundAt;
|
|
107
|
+
this.castSlots = options.castSlots ?? 16;
|
|
108
|
+
this.add = (object: Object3D) => {
|
|
109
|
+
if (this.objects.includes(object)) return;
|
|
110
|
+
this.objects.push(object);
|
|
111
|
+
if (options.add === undefined) options.scene.add(object);
|
|
112
|
+
else options.add(object);
|
|
113
|
+
};
|
|
114
|
+
this.reducedFlashes = options.reducedFlashes ?? false;
|
|
115
|
+
this.lights = new SpellLights({ count: options.lightCount ?? 4, parent: options.scene });
|
|
116
|
+
this.reaction =
|
|
117
|
+
options.reaction === false ? undefined : new ReactionField(options.reaction ?? {});
|
|
118
|
+
if (options.shake === false) this.shake = undefined;
|
|
119
|
+
else {
|
|
120
|
+
const shake = options.shake ?? {};
|
|
121
|
+
const amplitude = shake.amplitude ?? DEFAULT_SHAKE.amplitude;
|
|
122
|
+
const rotationAmplitude = shake.rotationAmplitude ?? DEFAULT_SHAKE.rotationAmplitude;
|
|
123
|
+
const settings: ICameraShakeOptions = {
|
|
124
|
+
amplitude: new Vector3(amplitude, amplitude * 0.6, amplitude),
|
|
125
|
+
curve: shake.curve ?? DEFAULT_CURVE,
|
|
126
|
+
decay: shake.decay ?? DEFAULT_SHAKE.decay,
|
|
127
|
+
frequency: shake.frequency ?? DEFAULT_SHAKE.frequency,
|
|
128
|
+
rotationAmplitude: new Vector3(
|
|
129
|
+
rotationAmplitude,
|
|
130
|
+
rotationAmplitude,
|
|
131
|
+
rotationAmplitude * 0.5,
|
|
132
|
+
),
|
|
133
|
+
};
|
|
134
|
+
this.shake = new CameraShake(settings);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Ask the limiter whether a flash may be shown.
|
|
140
|
+
*
|
|
141
|
+
* With reduced flashes on, a fourth flash inside one second is refused and the spell draws its
|
|
142
|
+
* impact without one. Returning a boolean rather than quietly dimming keeps the spell honest:
|
|
143
|
+
* the spell still happens, the flash does not.
|
|
144
|
+
*/
|
|
145
|
+
flash(): boolean {
|
|
146
|
+
if (!this.reducedFlashes) return true;
|
|
147
|
+
const now = this.time * 1000;
|
|
148
|
+
this.#flashes = this.#flashes.filter((at) => now - at < 1000);
|
|
149
|
+
if (this.#flashes.length >= FLASH_PER_SECOND) {
|
|
150
|
+
this.suppressedFlashes += 1;
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
this.#flashes.push(now);
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Surface height of the floor under a point, or 0 when the game supplied none. */
|
|
158
|
+
floorAt(x: number, z: number): number {
|
|
159
|
+
return this.groundAt?.(x, z) ?? 0;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
update(dt: number): void {
|
|
163
|
+
if (!Number.isFinite(dt) || dt < 0)
|
|
164
|
+
throw new RangeError("SpellWorld.update delta must be finite and non-negative");
|
|
165
|
+
this.time += dt;
|
|
166
|
+
this.lights.update(dt);
|
|
167
|
+
this.reaction?.update(dt);
|
|
168
|
+
this.shake?.update(dt);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Put out every light, clear the floor and stop the shake, keeping every pool warm. */
|
|
172
|
+
clear(): void {
|
|
173
|
+
this.lights.clear();
|
|
174
|
+
this.reaction?.clear();
|
|
175
|
+
this.shake?.stop();
|
|
176
|
+
this.#flashes = [];
|
|
177
|
+
this.suppressedFlashes = 0;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** WCAG 2.3.1: no more than three flashes in any one-second window. */
|
|
182
|
+
const FLASH_PER_SECOND = 3;
|
|
183
|
+
|
|
184
|
+
/** A colour the spell factories share, so a stock look and an override read the same. */
|
|
185
|
+
export function color(value: string | Color): Color {
|
|
186
|
+
return value instanceof Color ? value : new Color(value);
|
|
187
|
+
}
|
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One recipe, six spells.
|
|
3
|
+
*
|
|
4
|
+
* Every spell in the first wave is the same four beats with different numbers in them: something
|
|
5
|
+
* charges, something crosses the room, something lands, and the room stays changed. So this is one
|
|
6
|
+
* factory that builds a spell out of a recipe, and each `create<Spell>` is a recipe plus a look.
|
|
7
|
+
* That is what keeps the cost honest — a game that imports one spell gets one factory, not a
|
|
8
|
+
* catalog.
|
|
9
|
+
*
|
|
10
|
+
* Drawables (the ball, the shell, the bolt, the shield) and emitters are built once at `create` and
|
|
11
|
+
* only shown, hidden and re-emitted at cast time, so the first cast of a spell costs exactly what
|
|
12
|
+
* its hundredth does. Every drawable is per pool slot: two fireballs in the air at once do not fight
|
|
13
|
+
* over one sprite.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { Color, Vector3 } from "three/webgpu";
|
|
17
|
+
import {
|
|
18
|
+
type ParticleKindName,
|
|
19
|
+
ParticleKindTable,
|
|
20
|
+
type ParticleStyle,
|
|
21
|
+
createParticleKindEmitter,
|
|
22
|
+
} from "../particle-kinds.js";
|
|
23
|
+
import { GPUParticles3D } from "../particles.js";
|
|
24
|
+
import type { CastShape, ICast, ICastTiming, ISpell } from "./cast-pool.js";
|
|
25
|
+
import { type ISpellDrawables, createDrawables } from "./mechanisms.js";
|
|
26
|
+
import type { ReactionPattern } from "./reaction-field.js";
|
|
27
|
+
import type { SpellWorld } from "./spell-world.js";
|
|
28
|
+
|
|
29
|
+
/** What a spell is at birth, and what it cools to. */
|
|
30
|
+
export interface ISpellTint {
|
|
31
|
+
readonly colour: Color;
|
|
32
|
+
readonly highlight: Color;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A particle role. `burst` is particles per emission. */
|
|
36
|
+
export interface ISpellEmitter {
|
|
37
|
+
readonly kind: ParticleKindName;
|
|
38
|
+
readonly burst: number;
|
|
39
|
+
/** Seconds between repeated emissions. Ignored while travelling, which emits every frame. */
|
|
40
|
+
readonly interval?: number;
|
|
41
|
+
readonly style?: ParticleStyle;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface ISpellImpact {
|
|
45
|
+
/**
|
|
46
|
+
* `hold` keeps the shell at full strength for the whole linger instead of expanding once. A ward
|
|
47
|
+
* around the caster holds; an impact wave expands.
|
|
48
|
+
*/
|
|
49
|
+
readonly shell?: {
|
|
50
|
+
readonly radius: number;
|
|
51
|
+
readonly seconds: number;
|
|
52
|
+
readonly strength: number;
|
|
53
|
+
readonly hold?: boolean;
|
|
54
|
+
};
|
|
55
|
+
readonly light?: { readonly intensity: number; readonly range: number; readonly seconds: number };
|
|
56
|
+
readonly splat?: {
|
|
57
|
+
readonly radius: number;
|
|
58
|
+
readonly pattern?: ReactionPattern;
|
|
59
|
+
readonly heat?: number;
|
|
60
|
+
readonly char?: number;
|
|
61
|
+
readonly wet?: number;
|
|
62
|
+
readonly frost?: number;
|
|
63
|
+
readonly crack?: number;
|
|
64
|
+
readonly glow?: number;
|
|
65
|
+
};
|
|
66
|
+
/** Camera shake amplitude in metres. */
|
|
67
|
+
readonly shake?: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Everything a spell is. A factory is a recipe; the look is the tint. */
|
|
71
|
+
export interface ISpellRecipe {
|
|
72
|
+
readonly name: string;
|
|
73
|
+
readonly shape: CastShape;
|
|
74
|
+
readonly speed?: number;
|
|
75
|
+
readonly radius?: number;
|
|
76
|
+
readonly timing: Required<ICastTiming>;
|
|
77
|
+
readonly tint: ISpellTint;
|
|
78
|
+
/** Metres across the ball, and how hard it burns. */
|
|
79
|
+
readonly core?: { readonly radius: number; readonly intensity: number };
|
|
80
|
+
/** Motes pulled in while the spell winds up. */
|
|
81
|
+
readonly charge?: ISpellEmitter;
|
|
82
|
+
/** What the ball sheds as it flies. */
|
|
83
|
+
readonly trail?: ISpellEmitter;
|
|
84
|
+
/** What the impact throws. */
|
|
85
|
+
readonly burst?: ISpellEmitter;
|
|
86
|
+
/** What keeps burning after the impact. */
|
|
87
|
+
readonly linger?: ISpellEmitter;
|
|
88
|
+
readonly impact: ISpellImpact;
|
|
89
|
+
/** A column from the sky to the point. `height` is how far up it starts. */
|
|
90
|
+
readonly bolt?: { readonly width: number; readonly height: number; readonly seconds: number };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Per-cast scratch, one object per pool slot, so a cast allocates nothing. */
|
|
94
|
+
interface ICastScratch {
|
|
95
|
+
sinceImpact: number;
|
|
96
|
+
emitTimer: number;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
interface IBuilt {
|
|
100
|
+
readonly drawables: ISpellDrawables[];
|
|
101
|
+
readonly scratch: ICastScratch[];
|
|
102
|
+
readonly charge: GPUParticles3D | undefined;
|
|
103
|
+
readonly trail: GPUParticles3D | undefined;
|
|
104
|
+
readonly burst: GPUParticles3D | undefined;
|
|
105
|
+
readonly linger: GPUParticles3D | undefined;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Build a spell from a recipe.
|
|
110
|
+
*
|
|
111
|
+
* The returned object is an `ISpell`: hand it to `CastPool.cast` and the pool drives it. It reads
|
|
112
|
+
* the world only through `SpellWorld`, so a game can swap the scene, the floor or the light pool
|
|
113
|
+
* without touching the spell.
|
|
114
|
+
*/
|
|
115
|
+
export function createSpell(world: SpellWorld, recipe: ISpellRecipe): ISpell {
|
|
116
|
+
const built = build(world, recipe);
|
|
117
|
+
const position = new Vector3();
|
|
118
|
+
const sky = new Vector3();
|
|
119
|
+
const lightColour = new Color();
|
|
120
|
+
const shellColour = new Color();
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
name: recipe.name,
|
|
124
|
+
radius: recipe.radius,
|
|
125
|
+
shape: recipe.shape,
|
|
126
|
+
speed: recipe.speed,
|
|
127
|
+
timing: recipe.timing,
|
|
128
|
+
|
|
129
|
+
phase(cast, phase) {
|
|
130
|
+
const scratch = at(built.scratch, cast);
|
|
131
|
+
const drawables = at(built.drawables, cast);
|
|
132
|
+
if (phase === "charge") {
|
|
133
|
+
scratch.sinceImpact = 0;
|
|
134
|
+
scratch.emitTimer = 0;
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (phase === "impact") {
|
|
138
|
+
scratch.sinceImpact = 0;
|
|
139
|
+
scratch.emitTimer = 0;
|
|
140
|
+
land(world, drawables, recipe, cast, sky, lightColour, shellColour);
|
|
141
|
+
emit(built.burst, recipe.burst, cast.pos, recipe.burst?.burst ?? 0);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (phase === "linger") {
|
|
145
|
+
emit(built.linger, recipe.linger, cast.pos, recipe.linger?.burst ?? 0);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
// Retired: put every drawable this cast owns away. The floor mark belongs to the world.
|
|
149
|
+
drawables.core.hide();
|
|
150
|
+
drawables.shell.hide();
|
|
151
|
+
drawables.bolt.hide();
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
update(cast, dt) {
|
|
155
|
+
const scratch = at(built.scratch, cast);
|
|
156
|
+
const drawables = at(built.drawables, cast);
|
|
157
|
+
if (cast.phase === "charge") {
|
|
158
|
+
position.copy(cast.from);
|
|
159
|
+
chargeUp(drawables, recipe, position, cast.t / Math.max(1e-4, cast.durations.charge));
|
|
160
|
+
emit(built.charge, recipe.charge, position, (recipe.charge?.burst ?? 0) * dt * 60);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (cast.phase === "travel") {
|
|
164
|
+
position.copy(cast.pos);
|
|
165
|
+
drawBall(drawables, recipe, position);
|
|
166
|
+
emit(built.trail, recipe.trail, position, (recipe.trail?.burst ?? 0) * dt * 60);
|
|
167
|
+
if (recipe.bolt !== undefined) {
|
|
168
|
+
sky.copy(cast.pos).setY(cast.pos.y + recipe.bolt.height);
|
|
169
|
+
drawBolt(drawables, recipe, sky, cast.pos, 1, world.camera);
|
|
170
|
+
}
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
scratch.sinceImpact += dt;
|
|
174
|
+
stepImpact(drawables, recipe, cast, scratch.sinceImpact, world.camera);
|
|
175
|
+
if (cast.phase === "linger") {
|
|
176
|
+
scratch.emitTimer += dt;
|
|
177
|
+
const interval = Math.max(0.02, recipe.linger?.interval ?? 0.12);
|
|
178
|
+
if (scratch.emitTimer >= interval) {
|
|
179
|
+
// One emission per interval, never a backlog: a long frame must not spend the burst twice.
|
|
180
|
+
scratch.emitTimer = 0;
|
|
181
|
+
emit(built.linger, recipe.linger, cast.pos, recipe.linger?.burst ?? 0);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Build every pool slot's drawables and the four emitters. Called once, at `create`. */
|
|
189
|
+
function build(world: SpellWorld, recipe: ISpellRecipe): IBuilt {
|
|
190
|
+
const table = new ParticleKindTable(
|
|
191
|
+
uniqueKinds([recipe.charge, recipe.trail, recipe.burst, recipe.linger]),
|
|
192
|
+
);
|
|
193
|
+
for (const kind of table.kinds) {
|
|
194
|
+
table.set(kind, {
|
|
195
|
+
colour: [recipe.tint.colour.r, recipe.tint.colour.g, recipe.tint.colour.b],
|
|
196
|
+
highlight: [recipe.tint.highlight.r, recipe.tint.highlight.g, recipe.tint.highlight.b],
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
burst: buildEmitter(world, table, recipe.burst, recipe.name, 1, recipe.tint),
|
|
201
|
+
charge: buildEmitter(world, table, recipe.charge, recipe.name, 2, recipe.tint),
|
|
202
|
+
drawables: Array.from({ length: world.castSlots }, () => createDrawables(world.scene)),
|
|
203
|
+
linger: buildEmitter(world, table, recipe.linger, recipe.name, 3, recipe.tint),
|
|
204
|
+
scratch: Array.from({ length: world.castSlots }, () => ({ emitTimer: 0, sinceImpact: 0 })),
|
|
205
|
+
trail: buildEmitter(world, table, recipe.trail, recipe.name, 4, recipe.tint),
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Land the impact: the flash, the shell, the mark and the shake. */
|
|
210
|
+
function land(
|
|
211
|
+
world: SpellWorld,
|
|
212
|
+
drawables: ISpellDrawables,
|
|
213
|
+
recipe: ISpellRecipe,
|
|
214
|
+
cast: ICast,
|
|
215
|
+
sky: Vector3,
|
|
216
|
+
lightColour: Color,
|
|
217
|
+
shellColour: Color,
|
|
218
|
+
): void {
|
|
219
|
+
const impact = recipe.impact;
|
|
220
|
+
if (impact.light !== undefined) {
|
|
221
|
+
const light = impact.light;
|
|
222
|
+
// The limiter can refuse a flash. The spell still lands; only the flash does not happen.
|
|
223
|
+
if (world.flash()) {
|
|
224
|
+
lightColour.copy(recipe.tint.colour).lerp(recipe.tint.highlight, 0.4);
|
|
225
|
+
world.lights.flash({
|
|
226
|
+
color: lightColour,
|
|
227
|
+
intensity: light.intensity,
|
|
228
|
+
range: light.range,
|
|
229
|
+
seconds: light.seconds,
|
|
230
|
+
x: cast.pos.x,
|
|
231
|
+
y: cast.pos.y,
|
|
232
|
+
z: cast.pos.z,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (impact.splat !== undefined) {
|
|
237
|
+
const splat = impact.splat;
|
|
238
|
+
world.reaction?.splat({
|
|
239
|
+
char: splat.char,
|
|
240
|
+
crack: splat.crack,
|
|
241
|
+
frost: splat.frost,
|
|
242
|
+
glow: splat.glow,
|
|
243
|
+
heat: splat.heat,
|
|
244
|
+
pattern: splat.pattern ?? "radial",
|
|
245
|
+
radius: splat.radius,
|
|
246
|
+
seed: cast.seed,
|
|
247
|
+
wet: splat.wet,
|
|
248
|
+
x: cast.pos.x,
|
|
249
|
+
z: cast.pos.z,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
if (impact.shake !== undefined && impact.shake > 0) world.shake?.trigger();
|
|
253
|
+
if (recipe.bolt !== undefined) {
|
|
254
|
+
sky.copy(cast.pos).setY(cast.pos.y + recipe.bolt.height);
|
|
255
|
+
drawBolt(drawables, recipe, sky, cast.pos, 1, world.camera);
|
|
256
|
+
}
|
|
257
|
+
if (impact.shell !== undefined) {
|
|
258
|
+
shellColour.copy(recipe.tint.colour).lerp(recipe.tint.highlight, 0.5);
|
|
259
|
+
drawables.shell.show(cast.pos, 0.02, impact.shell.radius, shellColour, impact.shell.strength);
|
|
260
|
+
}
|
|
261
|
+
drawables.core.hide();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Advance the shell and the bolt through the impact and the linger. */
|
|
265
|
+
function stepImpact(
|
|
266
|
+
drawables: ISpellDrawables,
|
|
267
|
+
recipe: ISpellRecipe,
|
|
268
|
+
cast: ICast,
|
|
269
|
+
sinceImpact: number,
|
|
270
|
+
camera: { readonly position: Vector3 } | undefined,
|
|
271
|
+
): void {
|
|
272
|
+
const shell = recipe.impact.shell;
|
|
273
|
+
if (shell !== undefined) {
|
|
274
|
+
const colour = recipe.tint.colour.clone().lerp(recipe.tint.highlight, 0.5);
|
|
275
|
+
if (shell.hold === true) {
|
|
276
|
+
// A held shell does not expand: it sits at its radius and pulses until the cast retires.
|
|
277
|
+
const pulse = 0.9 + 0.1 * Math.sin(sinceImpact * 9);
|
|
278
|
+
drawables.shell.show(cast.pos, 0.999, shell.radius, colour, shell.strength * pulse, 1);
|
|
279
|
+
} else {
|
|
280
|
+
const progress = Math.min(1, sinceImpact / Math.max(0.05, shell.seconds));
|
|
281
|
+
drawables.shell.show(cast.pos, progress, shell.radius, colour, shell.strength);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
const bolt = recipe.bolt;
|
|
285
|
+
if (bolt !== undefined) {
|
|
286
|
+
const life = Math.min(1, sinceImpact / Math.max(0.05, bolt.seconds));
|
|
287
|
+
const sky = new Vector3(cast.pos.x, cast.pos.y + bolt.height, cast.pos.z);
|
|
288
|
+
drawBolt(drawables, recipe, sky, cast.pos, 1 - life, camera);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function drawBall(drawables: ISpellDrawables, recipe: ISpellRecipe, at: Vector3): void {
|
|
293
|
+
const core = recipe.core;
|
|
294
|
+
if (core === undefined) return;
|
|
295
|
+
drawables.core.show(at, core.radius, recipe.tint.colour, recipe.tint.highlight, core.intensity);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function chargeUp(
|
|
299
|
+
drawables: ISpellDrawables,
|
|
300
|
+
recipe: ISpellRecipe,
|
|
301
|
+
at: Vector3,
|
|
302
|
+
progress: number,
|
|
303
|
+
): void {
|
|
304
|
+
const core = recipe.core;
|
|
305
|
+
if (core === undefined) return;
|
|
306
|
+
const eased = Math.min(1, Math.max(0, progress));
|
|
307
|
+
drawables.core.show(
|
|
308
|
+
at,
|
|
309
|
+
core.radius * (0.25 + 0.75 * eased),
|
|
310
|
+
recipe.tint.colour,
|
|
311
|
+
recipe.tint.highlight,
|
|
312
|
+
core.intensity * eased,
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function drawBolt(
|
|
317
|
+
drawables: ISpellDrawables,
|
|
318
|
+
recipe: ISpellRecipe,
|
|
319
|
+
from: Vector3,
|
|
320
|
+
to: Vector3,
|
|
321
|
+
strength: number,
|
|
322
|
+
camera: { readonly position: Vector3 } | undefined,
|
|
323
|
+
): void {
|
|
324
|
+
const bolt = recipe.bolt;
|
|
325
|
+
if (bolt === undefined || strength <= 0.001) {
|
|
326
|
+
drawables.bolt.hide();
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
drawables.bolt.show(
|
|
330
|
+
from,
|
|
331
|
+
to,
|
|
332
|
+
bolt.width,
|
|
333
|
+
recipe.tint.highlight,
|
|
334
|
+
recipe.tint.colour,
|
|
335
|
+
strength,
|
|
336
|
+
0.37,
|
|
337
|
+
camera,
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function buildEmitter(
|
|
342
|
+
world: SpellWorld,
|
|
343
|
+
table: ParticleKindTable,
|
|
344
|
+
role: ISpellEmitter | undefined,
|
|
345
|
+
name: string,
|
|
346
|
+
seedOffset: number,
|
|
347
|
+
tint: ISpellTint,
|
|
348
|
+
): GPUParticles3D | undefined {
|
|
349
|
+
if (role === undefined) return undefined;
|
|
350
|
+
const options = createParticleKindEmitter({
|
|
351
|
+
// burst is particles per emission and may be fractional (a trail emits 2.4 per frame), but a
|
|
352
|
+
// ring capacity is a whole number of slots.
|
|
353
|
+
amount: Math.max(64, Math.ceil(role.burst * 64)),
|
|
354
|
+
kind: role.kind,
|
|
355
|
+
seed: seedOffset * 1013 + hashName(name),
|
|
356
|
+
style: role.style,
|
|
357
|
+
table,
|
|
358
|
+
});
|
|
359
|
+
const particles = new GPUParticles3D(options);
|
|
360
|
+
particles.name = `${name}-${role.kind}-${seedOffset}`;
|
|
361
|
+
world.add(particles);
|
|
362
|
+
return particles;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function emit(
|
|
366
|
+
emitter: GPUParticles3D | undefined,
|
|
367
|
+
role: ISpellEmitter | undefined,
|
|
368
|
+
at: Vector3,
|
|
369
|
+
count: number,
|
|
370
|
+
): void {
|
|
371
|
+
if (emitter === undefined || role === undefined) return;
|
|
372
|
+
emitter.emitParticle({ count: Math.max(1, Math.round(count)), origin: at });
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function uniqueKinds(roles: readonly (ISpellEmitter | undefined)[]): ParticleKindName[] {
|
|
376
|
+
const kinds: ParticleKindName[] = [];
|
|
377
|
+
for (const role of roles) {
|
|
378
|
+
if (role !== undefined && !kinds.includes(role.kind)) kinds.push(role.kind);
|
|
379
|
+
}
|
|
380
|
+
return kinds.length > 0 ? kinds : ["spark"];
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function at<T>(list: readonly T[], cast: ICast): T {
|
|
384
|
+
const value = list[cast.index];
|
|
385
|
+
if (value === undefined) throw new RangeError(`cast slot ${cast.index} is outside the pool`);
|
|
386
|
+
return value;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** A small stable hash of a name, so two spells' emitters do not share a random stream. */
|
|
390
|
+
function hashName(name: string): number {
|
|
391
|
+
let hash = 2166136261;
|
|
392
|
+
for (let index = 0; index < name.length; index++) {
|
|
393
|
+
hash ^= name.charCodeAt(index);
|
|
394
|
+
hash = Math.imul(hash, 16777619);
|
|
395
|
+
}
|
|
396
|
+
return hash >>> 8;
|
|
397
|
+
}
|