@zaisma/effects 1.0.1 → 1.1.0
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/batches/chunks.js +45 -45
- package/batches/marks.js +15 -15
- package/batches/ribbons.js +33 -33
- package/batches/shared.js +1 -1
- package/batches/sprites.js +47 -47
- package/batches/warps.js +18 -18
- package/budget.js +17 -17
- package/debris.js +33 -33
- package/emit.js +35 -35
- package/flashes.js +7 -7
- package/format.js +39 -39
- package/index.d.ts +5 -5
- package/index.js +75 -73
- package/looks.js +3 -3
- package/motion.js +23 -23
- package/package.json +1 -1
- package/partners.js +30 -30
- package/piece.json +3 -3
- package/piece.json.js +3 -3
- package/shapes.js +5 -5
- package/sync.js +113 -113
- package/tables.js +20 -20
- package/weather.js +17 -17
package/index.js
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
import * as THREE from "three";
|
|
13
13
|
import { openPiece } from "../piece/index.js";
|
|
14
14
|
import statement from "./piece.json.js";
|
|
15
|
-
import {
|
|
15
|
+
import { CHUNK_BUDGET_SHARE_NUMBER, QUALITY_BUDGETS, QUALITY_ORDER, createFillGuard, createFrameWatcher, isTouchDevice, particleBudget, planEmitter, startingQualityLevel } from "./budget.js";
|
|
16
16
|
import { createChunkBatch } from "./batches/chunks.js";
|
|
17
17
|
import { createMarkBatch } from "./batches/marks.js";
|
|
18
18
|
import { createRibbonBatch } from "./batches/ribbons.js";
|
|
@@ -63,8 +63,8 @@ const CLOCK_RESTART_SPAN_SEC = 512;
|
|
|
63
63
|
const RETRY_INTERVAL_SEC = 0.5;
|
|
64
64
|
const LIGHT_SEARCH_INTERVAL_SEC = 3;
|
|
65
65
|
/** Each see-through particle that covers more of the screen than this is one the fill guard may thin. */
|
|
66
|
-
const
|
|
67
|
-
const
|
|
66
|
+
const GUARDED_SCREEN_SHARE_NUMBER = 0.002;
|
|
67
|
+
const MAXIMUM_LANDING_EFFECT_COUNT_PER_FRAME = 6;
|
|
68
68
|
const LIBRARY_URL = new URL("./library/", import.meta.url);
|
|
69
69
|
|
|
70
70
|
const isPoint = (given) => given !== null && typeof given === "object" && Number.isFinite(given.x) && Number.isFinite(given.y) && Number.isFinite(given.z);
|
|
@@ -188,7 +188,7 @@ export function createEffects(given = {}) {
|
|
|
188
188
|
chunks: () => chunks,
|
|
189
189
|
findSurface,
|
|
190
190
|
onLanded(chunk, met) {
|
|
191
|
-
if (!chunk.bounceEffectName || !registry.has(chunk.bounceEffectName) || landingEffectCount >=
|
|
191
|
+
if (!chunk.bounceEffectName || !registry.has(chunk.bounceEffectName) || landingEffectCount >= MAXIMUM_LANDING_EFFECT_COUNT_PER_FRAME) return;
|
|
192
192
|
landingEffectCount += 1;
|
|
193
193
|
startPlay(chunk.bounceEffectName, { positionM: { x: met.x, y: met.y, z: met.z }, normal: { x: met.normalX, y: met.normalY, z: met.normalZ }, scalePct: Math.min(Math.max(chunk.halfSizeM * 800, 25), 200), seedNumber: hashNumbers(chunk.play.seedNumber, chunk.offset, 31) }, "chunk_landed");
|
|
194
194
|
},
|
|
@@ -224,14 +224,14 @@ export function createEffects(given = {}) {
|
|
|
224
224
|
elementTable.write(row, ELEMENT_ROW.motionA, move.drag_per_sec, (move.gravity_pct / 100) * GRAVITY_M_PER_SEC2, move.wind_pct / 100, move.turbulence_m);
|
|
225
225
|
elementTable.write(row, ELEMENT_ROW.motionB, move.turbulence_per_sec, move.swirl_deg_per_sec * RAD_PER_DEG, move.pull_pct / 100, move.is_attached ? 1 : 0);
|
|
226
226
|
elementTable.write(row, ELEMENT_ROW.sizeKeys, ...padKeys(fade.size_over_life_m, style.sizePct / 100));
|
|
227
|
-
elementTable.write(row, ELEMENT_ROW.opacityKeys, ...padKeys(fade.opacity_over_life_pct, style.opacityPct / 10000).map((
|
|
227
|
+
elementTable.write(row, ELEMENT_ROW.opacityKeys, ...padKeys(fade.opacity_over_life_pct, style.opacityPct / 10000).map((shareNumber) => Math.min(shareNumber, 1)));
|
|
228
228
|
elementTable.write(row, ELEMENT_ROW.keyCounts, fade.size_over_life_m.length, fade.opacity_over_life_pct.length, fade.colors_over_life.length, fade.size_variation_pct / 100);
|
|
229
229
|
const fourth = [(fade.glow_pct / 100) * (style.glowPct / 100), style.addPct / 100, style.litPct / 100, style.toneStepCount];
|
|
230
230
|
for (let index = 0; index < 4; index += 1) {
|
|
231
231
|
color.set(fade.colors_over_life[Math.min(index, fade.colors_over_life.length - 1)]);
|
|
232
232
|
// A look that draws a material solid lightens it: what reads as grey smoke when see-through is coal when it isn't.
|
|
233
|
-
const
|
|
234
|
-
elementTable.write(row, ELEMENT_ROW.colorKeys + index, color.r + (1 - color.r) *
|
|
233
|
+
const lightenShareNumber = style.lightenPct / 100;
|
|
234
|
+
elementTable.write(row, ELEMENT_ROW.colorKeys + index, color.r + (1 - color.r) * lightenShareNumber, color.g + (1 - color.g) * lightenShareNumber, color.b + (1 - color.b) * lightenShareNumber, fourth[index]);
|
|
235
235
|
}
|
|
236
236
|
// A shape the element names is drawn as named; otherwise the look's own, which may be a moving picture.
|
|
237
237
|
const filmName = element.shape === null ? style.filmName : null;
|
|
@@ -252,21 +252,21 @@ export function createEffects(given = {}) {
|
|
|
252
252
|
// A look sizes and thins what an effect leaves open. A shape the effect names (a ring, a bubble, a star) is a
|
|
253
253
|
// figure it drew on purpose: it keeps its own size and count in every look.
|
|
254
254
|
const style = element.shape === null ? styles[element.material] : { ...styles[element.material], countPct: 100, sizePct: 100 };
|
|
255
|
-
const
|
|
255
|
+
const countShareNumber = style.countPct / 100;
|
|
256
256
|
// A look that asks for more particles gets more; one that asks for fewer draws a share of the same ones.
|
|
257
|
-
const
|
|
257
|
+
const growthNumber = Math.max(countShareNumber, 1);
|
|
258
258
|
const loopDurationSec = file.is_looping ? file.duration_sec : 0;
|
|
259
259
|
const emitDurationSec = emit.emit_duration_sec ?? file.duration_sec;
|
|
260
|
-
const
|
|
260
|
+
const maximumLifeSec = fade.life_sec * (1 + fade.life_variation_pct / 100);
|
|
261
261
|
compiled.style = style;
|
|
262
262
|
compiled.isChunk = style.chunkShapeName !== null && element.shape === null && (fade.facing_kind === "camera" || fade.facing_kind === "velocity");
|
|
263
|
-
compiled.lookCountShare = Math.min(
|
|
264
|
-
compiled.
|
|
263
|
+
compiled.lookCountShare = Math.min(countShareNumber, 1);
|
|
264
|
+
compiled.maximumLifeSec = maximumLifeSec;
|
|
265
265
|
compiled.isAttached = move.is_attached;
|
|
266
266
|
compiled.emitter = {
|
|
267
267
|
elementIndex: compiled.elementIndex,
|
|
268
|
-
burstCount: Math.round(emit.burst_count *
|
|
269
|
-
perSec: emit.per_sec *
|
|
268
|
+
burstCount: Math.round(emit.burst_count * growthNumber),
|
|
269
|
+
perSec: emit.per_sec * growthNumber,
|
|
270
270
|
emitDurationSec,
|
|
271
271
|
loopDurationSec,
|
|
272
272
|
fromKind: emit.from_kind,
|
|
@@ -285,8 +285,8 @@ export function createEffects(given = {}) {
|
|
|
285
285
|
compiled.fillSizeM = Math.max(...fade.size_over_life_m) * (style.sizePct / 100);
|
|
286
286
|
// How many are alive at once, drawing every one.
|
|
287
287
|
const flowSec = loopDurationSec > 0 ? Math.min(emitDurationSec, loopDurationSec) : emitDurationSec;
|
|
288
|
-
const runCount = loopDurationSec > 0 ? Math.ceil(
|
|
289
|
-
compiled.wantedCount = Math.min(compiled.emitter.burstCount * runCount + Math.ceil(compiled.emitter.perSec * Math.min(
|
|
288
|
+
const runCount = loopDurationSec > 0 ? Math.ceil(maximumLifeSec / loopDurationSec) : 1;
|
|
289
|
+
compiled.wantedCount = Math.min(compiled.emitter.burstCount * runCount + Math.ceil(compiled.emitter.perSec * Math.min(maximumLifeSec, flowSec * runCount)), 20000);
|
|
290
290
|
writeParticleRow(compiled.row, element, style, 0);
|
|
291
291
|
}
|
|
292
292
|
|
|
@@ -302,7 +302,7 @@ export function createEffects(given = {}) {
|
|
|
302
302
|
elementTable.write(row, ELEMENT_ROW.keyCounts, 2, 4, 1, element.size_variation_pct / 100);
|
|
303
303
|
color.set(element.colors[index]);
|
|
304
304
|
const fourth = [style.glowPct / 100, 0, Math.max(style.litPct, 60) / 100, 0];
|
|
305
|
-
for (let
|
|
305
|
+
for (let keyIndex = 0; keyIndex < 4; keyIndex += 1) elementTable.write(row, ELEMENT_ROW.colorKeys + keyIndex, color.r, color.g, color.b, fourth[keyIndex]);
|
|
306
306
|
elementTable.write(row, ELEMENT_ROW.cell, 0, 0, 0, 0);
|
|
307
307
|
elementTable.write(row, ELEMENT_ROW.facing, 0, 0, 0, 0);
|
|
308
308
|
elementTable.write(row, ELEMENT_ROW.style, 1, 0, 0, Math.max(CHUNK_SHAPE_NAMES.indexOf(style.chunkShapeName ?? "cube"), 0));
|
|
@@ -362,7 +362,7 @@ export function createEffects(given = {}) {
|
|
|
362
362
|
const earlier = registry.get(file.name);
|
|
363
363
|
// A changed file takes the rows of the one before it, as far as they go.
|
|
364
364
|
const spareRows = earlier ? earlier.elements.flatMap((compiled) => (compiled.elementKind === "particles" ? [compiled.row] : compiled.elementKind === "debris" ? compiled.rows : [])) : [];
|
|
365
|
-
const neededCount = file.elements.reduce((
|
|
365
|
+
const neededCount = file.elements.reduce((runningCount, element) => runningCount + (element.element_kind === "particles" ? 1 : element.element_kind === "debris" ? element.colors.length : 0), 0);
|
|
366
366
|
if (neededCount > spareRows.length + elementTable.freeRowCount) {
|
|
367
367
|
refuse("too_many_effects", { message: `There's no room left for "${file.name}": the effects added hold ${MAXIMUM_ELEMENT_ROW_COUNT} particle and debris elements between them.`, field: "name", fix: "Add only the effects the game plays: load them by name instead of all of them." });
|
|
368
368
|
}
|
|
@@ -409,7 +409,7 @@ export function createEffects(given = {}) {
|
|
|
409
409
|
|
|
410
410
|
// ---- Plays ----
|
|
411
411
|
const plays = new Set();
|
|
412
|
-
let
|
|
412
|
+
let nextPlayNumber = 1;
|
|
413
413
|
const followPlace = new THREE.Vector3();
|
|
414
414
|
const followTurn = new THREE.Quaternion();
|
|
415
415
|
const turned = new THREE.Vector3();
|
|
@@ -550,34 +550,35 @@ export function createEffects(given = {}) {
|
|
|
550
550
|
}
|
|
551
551
|
|
|
552
552
|
const standsOn = { x: 0, y: 0, z: 0, normalX: 0, normalY: 1, normalZ: 0, materialName: null };
|
|
553
|
-
|
|
553
|
+
// playId is playNumber's name in 1.0.1 and before: still given beside it, so games written for 1.0 keep running.
|
|
554
|
+
const inertHandle = Object.freeze({ playNumber: 0, playId: 0, effectName: null, isEnded: true, stop() {}, moveTo() {}, set() {} });
|
|
554
555
|
|
|
555
|
-
function startPlay(effectName,
|
|
556
|
-
if (
|
|
557
|
-
for (const field of Object.keys(
|
|
556
|
+
function startPlay(effectName, playAt = {}, reason = "play") {
|
|
557
|
+
if (playAt === null || typeof playAt !== "object" || Array.isArray(playAt)) refuse("bad_play", { message: "play takes where to play as an object.", field: "playAt", fix: "Give { positionM: { x, y, z } }." });
|
|
558
|
+
for (const field of Object.keys(playAt)) {
|
|
558
559
|
if (!AT_FIELDS.includes(field)) refuse("unknown_argument", { message: `play has no "${field}".`, field, fix: `Give only: ${AT_FIELDS.join(", ")}.` });
|
|
559
560
|
}
|
|
560
561
|
let name = effectName;
|
|
561
|
-
let scalePct =
|
|
562
|
-
let strengthPct =
|
|
563
|
-
let tintHex =
|
|
564
|
-
let surfaceKind =
|
|
565
|
-
if (
|
|
566
|
-
const follow =
|
|
567
|
-
if (!follow && !isPoint(
|
|
568
|
-
if (
|
|
562
|
+
let scalePct = playAt.scalePct ?? 100;
|
|
563
|
+
let strengthPct = playAt.strengthPct ?? 100;
|
|
564
|
+
let tintHex = playAt.color ?? null;
|
|
565
|
+
let surfaceKind = playAt.surfaceKind ?? null;
|
|
566
|
+
if (playAt.follow !== undefined && playAt.follow !== null && !(playAt.follow instanceof THREE.Object3D)) refuse("bad_play", { message: "follow isn't a three.js object.", field: "follow", fix: "Give the object the effect rides on (any THREE.Object3D), or leave it out." });
|
|
567
|
+
const follow = playAt.follow ?? null;
|
|
568
|
+
if (!follow && !isPoint(playAt.positionM)) refuse("bad_play", { message: "play needs where to play.", field: "positionM", fix: "Give positionM: { x, y, z } in metres, or follow: an object the effect rides on." });
|
|
569
|
+
if (playAt.positionM !== undefined && !isPoint(playAt.positionM)) refuse("bad_play", { message: "positionM isn't a place.", field: "positionM", fix: "Give { x, y, z } in metres." });
|
|
569
570
|
for (const field of ["direction", "normal", "toPositionM"]) {
|
|
570
|
-
if (
|
|
571
|
+
if (playAt[field] !== undefined && playAt[field] !== null && !isPoint(playAt[field])) refuse("bad_play", { message: `${field} isn't { x, y, z }.`, field, fix: "Give { x, y, z }." });
|
|
571
572
|
}
|
|
572
573
|
if (!isNumberIn(scalePct, 0.01, 10000)) refuse("bad_play", { message: "scalePct isn't a number above 0, up to 10000.", field: "scalePct", fix: "Give how big to play it against its file: 100 is as written." });
|
|
573
574
|
if (!isNumberIn(strengthPct, 0, 1000)) refuse("bad_play", { message: "strengthPct isn't a number from 0 to 1000.", field: "strengthPct", fix: "Give how strong to play it: 100 is as written, 50 half as solid and bright." });
|
|
574
575
|
if (tintHex !== null && !(typeof tintHex === "string" && HEX_COLOR_PATTERN.test(tintHex))) refuse("bad_play", { message: "color isn't a colour.", field: "color", fix: 'Give a colour as six hex digits in lowercase, like "#ff7a1a", or leave it out.' });
|
|
575
576
|
if (surfaceKind !== null && !SURFACE_KINDS.includes(surfaceKind)) refuse("bad_play", { message: `surfaceKind is ${JSON.stringify(surfaceKind)}, which isn't a surface kind.`, field: "surfaceKind", fix: `Give one of: ${SURFACE_KINDS.join(", ")}.` });
|
|
576
|
-
const startedAgoSec =
|
|
577
|
+
const startedAgoSec = playAt.startedAgoSec ?? 0;
|
|
577
578
|
if (!isNumberIn(startedAgoSec, 0, Infinity)) refuse("bad_play", { message: "startedAgoSec isn't a number from 0 up.", field: "startedAgoSec", fix: "Give how long ago the effect started, in seconds: 0 starts it now." });
|
|
578
579
|
|
|
579
580
|
if (hasHandlers("beforeEffect")) {
|
|
580
|
-
const position =
|
|
581
|
+
const position = playAt.positionM ?? { x: 0, y: 0, z: 0 };
|
|
581
582
|
const answer = ask("beforeEffect", { effectName: name, positionM: { x: position.x, y: position.y, z: position.z }, scalePct, strengthPct, color: tintHex, surfaceKind, reason });
|
|
582
583
|
if (answer === null) return inertHandle;
|
|
583
584
|
if (answer && typeof answer === "object") {
|
|
@@ -598,32 +599,32 @@ export function createEffects(given = {}) {
|
|
|
598
599
|
return inertHandle;
|
|
599
600
|
}
|
|
600
601
|
|
|
601
|
-
const way =
|
|
602
|
+
const way = playAt.direction ?? playAt.normal ?? null;
|
|
602
603
|
const ownTurn = new THREE.Quaternion();
|
|
603
604
|
if (way) {
|
|
604
605
|
turned.set(way.x, way.y, way.z);
|
|
605
606
|
if (turned.lengthSq() > 1e-12) ownTurn.setFromUnitVectors(UP, turned.normalize());
|
|
606
607
|
}
|
|
607
608
|
const play = {
|
|
608
|
-
|
|
609
|
+
playNumber: nextPlayNumber,
|
|
609
610
|
entry,
|
|
610
|
-
seedNumber: Number.isInteger(
|
|
611
|
+
seedNumber: Number.isInteger(playAt.seedNumber) ? playAt.seedNumber >>> 0 : (Math.random() * 4294967296) >>> 0,
|
|
611
612
|
startSec: nowSec - startedAgoSec,
|
|
612
613
|
stopSec: null,
|
|
613
614
|
isEnded: false,
|
|
614
615
|
row,
|
|
615
616
|
follow,
|
|
616
|
-
offset: new THREE.Vector3(
|
|
617
|
+
offset: new THREE.Vector3(playAt.positionM?.x ?? 0, playAt.positionM?.y ?? 0, playAt.positionM?.z ?? 0),
|
|
617
618
|
ownTurn,
|
|
618
619
|
place: new THREE.Vector3(),
|
|
619
620
|
turn: new THREE.Quaternion(),
|
|
620
621
|
earlierPlace: new THREE.Vector3(),
|
|
621
622
|
earlierSec: nowSec,
|
|
622
623
|
hasMoved: false,
|
|
623
|
-
hasNormal: Boolean(
|
|
624
|
+
hasNormal: Boolean(playAt.normal),
|
|
624
625
|
// A beam runs to toPositionM when one was given; without it, out along the effect's direction.
|
|
625
|
-
hasEnd: isPoint(
|
|
626
|
-
normal:
|
|
626
|
+
hasEnd: isPoint(playAt.toPositionM),
|
|
627
|
+
normal: playAt.normal ? new THREE.Vector3(playAt.normal.x, playAt.normal.y, playAt.normal.z).normalize() : UP,
|
|
627
628
|
toPlace: new THREE.Vector3(),
|
|
628
629
|
// The surface it stands on, as a normal and how far along it: soft shapes fade into it. All 0 with none.
|
|
629
630
|
surface: new THREE.Vector4(0, 0, 0, 0),
|
|
@@ -642,23 +643,24 @@ export function createEffects(given = {}) {
|
|
|
642
643
|
},
|
|
643
644
|
};
|
|
644
645
|
play.view = Object.create(play);
|
|
645
|
-
|
|
646
|
+
nextPlayNumber += 1;
|
|
646
647
|
placePlay(play);
|
|
647
648
|
play.earlierPlace.copy(play.place);
|
|
648
649
|
if (!follow) {
|
|
649
|
-
if (
|
|
650
|
+
if (playAt.normal) play.surface.set(play.normal.x, play.normal.y, play.normal.z, play.normal.dot(play.place));
|
|
650
651
|
else if (findSurface(play.place.x, play.place.y + 0.25, play.place.z, 0, -1, 0, 0.25 + 0.75 * play.scaleShare, standsOn)) play.surface.set(standsOn.normalX, standsOn.normalY, standsOn.normalZ, standsOn.normalX * standsOn.x + standsOn.normalY * standsOn.y + standsOn.normalZ * standsOn.z);
|
|
651
652
|
}
|
|
652
|
-
if (
|
|
653
|
+
if (playAt.toPositionM) play.toPlace.set(playAt.toPositionM.x, playAt.toPositionM.y, playAt.toPositionM.z);
|
|
653
654
|
else play.toPlace.copy(play.place).addScaledVector(turned.set(0, 1, 0).applyQuaternion(play.turn), 10 * play.scaleShare);
|
|
654
655
|
writePlayRow(play);
|
|
655
656
|
makeEmitters(play);
|
|
656
657
|
startPassingElements(play, false);
|
|
657
658
|
plays.add(play);
|
|
658
|
-
tell("effectStarted", { playId: play.
|
|
659
|
+
tell("effectStarted", { playNumber: play.playNumber, playId: play.playNumber, effectName: name, positionM: { x: play.place.x, y: play.place.y, z: play.place.z }, scalePct, strengthPct, surfaceKind, reason });
|
|
659
660
|
|
|
660
661
|
return {
|
|
661
|
-
|
|
662
|
+
playNumber: play.playNumber,
|
|
663
|
+
playId: play.playNumber,
|
|
662
664
|
effectName: name,
|
|
663
665
|
get isEnded() {
|
|
664
666
|
return play.isEnded;
|
|
@@ -700,7 +702,7 @@ export function createEffects(given = {}) {
|
|
|
700
702
|
plays.delete(play);
|
|
701
703
|
// Its row waits a frame, so the batches that still name it let go first.
|
|
702
704
|
endedRows.push(play.row);
|
|
703
|
-
tell("effectEnded", { playId: play.
|
|
705
|
+
tell("effectEnded", { playNumber: play.playNumber, playId: play.playNumber, effectName: play.entry.name });
|
|
704
706
|
}
|
|
705
707
|
let endedRows = [];
|
|
706
708
|
let freedRows = [];
|
|
@@ -731,10 +733,10 @@ export function createEffects(given = {}) {
|
|
|
731
733
|
state.slab = slab;
|
|
732
734
|
state.batch = batch;
|
|
733
735
|
state.keptShare = slab.capacity < plan.slotCount ? plan.keptShare * (slab.capacity / plan.slotCount) : plan.keptShare;
|
|
734
|
-
const
|
|
735
|
-
if (state.keptShare <
|
|
736
|
+
const fullShareNumber = compiled.isNeverThinned ? 1 : (compiled.lookCountShare * QUALITY_BUDGETS[qualityLevel].keptPct) / 100;
|
|
737
|
+
if (state.keptShare < fullShareNumber * 0.6) toldThinned(play.entry.name, state.keptShare / Math.max(fullShareNumber, 1e-6));
|
|
736
738
|
// Only what's still alive is written: an effect a player joins partway through, or one filled in again.
|
|
737
|
-
state.bornCount = bornCountAt(compiled.emitter, sinceSec - compiled.
|
|
739
|
+
state.bornCount = bornCountAt(compiled.emitter, sinceSec - compiled.maximumLifeSec);
|
|
738
740
|
return true;
|
|
739
741
|
}
|
|
740
742
|
|
|
@@ -751,19 +753,19 @@ export function createEffects(given = {}) {
|
|
|
751
753
|
}
|
|
752
754
|
const isOver = isStopped || (base.loopDurationSec === 0 && sinceSec >= base.emitDurationSec);
|
|
753
755
|
if (!state.slab && !startEmitter(play, state, sinceSec, cameraPlace)) {
|
|
754
|
-
if (isOver && nowSec - startSec > base.emitDurationSec + compiled.
|
|
756
|
+
if (isOver && nowSec - startSec > base.emitDurationSec + compiled.maximumLifeSec) state.isDone = true;
|
|
755
757
|
return;
|
|
756
758
|
}
|
|
757
759
|
const targetCount = bornCountAt(base, sinceSec);
|
|
758
760
|
let number = state.bornCount;
|
|
759
761
|
// After a long gap (a hidden tab), only what's still alive.
|
|
760
|
-
if (targetCount - number > state.slab.capacity * 2) number = Math.max(number, bornCountAt(base, sinceSec - compiled.
|
|
762
|
+
if (targetCount - number > state.slab.capacity * 2) number = Math.max(number, bornCountAt(base, sinceSec - compiled.maximumLifeSec));
|
|
761
763
|
if (number < targetCount) {
|
|
762
764
|
let share = state.keptShare;
|
|
763
765
|
if (compiled.isSeeThrough && !compiled.isNeverThinned && pxPerM > 0) {
|
|
764
766
|
// A see-through particle that covers a good part of the screen is what the fill guard thins.
|
|
765
767
|
const acrossPx = (compiled.fillSizeM * play.scaleShare * pxPerM) / Math.max(play.place.distanceTo(cameraPlace), 0.5);
|
|
766
|
-
if ((acrossPx * acrossPx) / screenPxCount >
|
|
768
|
+
if ((acrossPx * acrossPx) / screenPxCount > GUARDED_SCREEN_SHARE_NUMBER) share *= fillGuard.fillShare;
|
|
767
769
|
}
|
|
768
770
|
const { slab, batch } = state;
|
|
769
771
|
const scale = play.scaleShare;
|
|
@@ -784,10 +786,10 @@ export function createEffects(given = {}) {
|
|
|
784
786
|
let z = play.place.z + turned.z;
|
|
785
787
|
if (play.hasMoved && spanSec > 0) {
|
|
786
788
|
// Let go where the effect was at that moment, so a fast one leaves an even trail.
|
|
787
|
-
const
|
|
788
|
-
x += (play.earlierPlace.x - play.place.x) *
|
|
789
|
-
y += (play.earlierPlace.y - play.place.y) *
|
|
790
|
-
z += (play.earlierPlace.z - play.place.z) *
|
|
789
|
+
const backShareNumber = Math.min(Math.max((nowSec - birthSec) / spanSec, 0), 1);
|
|
790
|
+
x += (play.earlierPlace.x - play.place.x) * backShareNumber;
|
|
791
|
+
y += (play.earlierPlace.y - play.place.y) * backShareNumber;
|
|
792
|
+
z += (play.earlierPlace.z - play.place.z) * backShareNumber;
|
|
791
793
|
}
|
|
792
794
|
turned.set(born.velocityX * scale, born.velocityY * scale, born.velocityZ * scale).applyQuaternion(turn);
|
|
793
795
|
batch.write(slab, x, y, z, turned.x, turned.y, turned.z, toBatchSec(birthSec), born.lifeSec, born.seedNumber, compiled.row, play.row);
|
|
@@ -802,7 +804,7 @@ export function createEffects(given = {}) {
|
|
|
802
804
|
/** How hard the camera shakes right now, for a game with a camera of its own: strengthPct, and an offset to add to it. */
|
|
803
805
|
const shakeNow = { strengthPct: 0, offsetM: new THREE.Vector3(), rollRad: 0 };
|
|
804
806
|
function updateShakes(cameraPlace) {
|
|
805
|
-
let
|
|
807
|
+
let strengthNumber = 0;
|
|
806
808
|
for (const shake of shakes) {
|
|
807
809
|
const sinceSec = nowSec - shake.startSec;
|
|
808
810
|
if (sinceSec >= shake.durationSec || shake.play.isEnded) {
|
|
@@ -810,21 +812,21 @@ export function createEffects(given = {}) {
|
|
|
810
812
|
continue;
|
|
811
813
|
}
|
|
812
814
|
if (sinceSec < 0) continue;
|
|
813
|
-
const
|
|
814
|
-
const
|
|
815
|
+
const nearShareNumber = Math.max(1 - shake.play.place.distanceTo(cameraPlace) / (shake.radiusM * shake.play.scaleShare), 0);
|
|
816
|
+
const feltNumber = shake.strengthShare * shake.play.strengthShare * nearShareNumber;
|
|
815
817
|
if (!shake.isTold) {
|
|
816
818
|
shake.isTold = true;
|
|
817
|
-
if (
|
|
819
|
+
if (feltNumber > 0.005) tell("shake", { strengthPct: Math.min(feltNumber * 100, 100), durationSec: shake.durationSec - sinceSec });
|
|
818
820
|
}
|
|
819
|
-
const
|
|
820
|
-
|
|
821
|
+
const fadeNumber = 1 - sinceSec / shake.durationSec;
|
|
822
|
+
strengthNumber = Math.max(strengthNumber, feltNumber * fadeNumber * fadeNumber);
|
|
821
823
|
}
|
|
822
|
-
|
|
823
|
-
shakeNow.strengthPct =
|
|
824
|
+
strengthNumber = Math.min(strengthNumber, 1);
|
|
825
|
+
shakeNow.strengthPct = strengthNumber * 100;
|
|
824
826
|
// Three waves that never line up on each axis: a shake, never a wobble.
|
|
825
|
-
const reachM =
|
|
827
|
+
const reachM = strengthNumber * 0.35;
|
|
826
828
|
shakeNow.offsetM.set(Math.sin(nowSec * 61.3) + Math.sin(nowSec * 37.1 + 1.3), Math.sin(nowSec * 53.7 + 2.1) + Math.sin(nowSec * 29.9 + 0.4), Math.sin(nowSec * 47.9 + 4.2) + Math.sin(nowSec * 33.3 + 2.9)).multiplyScalar(reachM * 0.5);
|
|
827
|
-
shakeNow.rollRad =
|
|
829
|
+
shakeNow.rollRad = strengthNumber * 0.035 * Math.sin(nowSec * 43.1 + 0.7);
|
|
828
830
|
}
|
|
829
831
|
|
|
830
832
|
// ---- The scene's light ----
|
|
@@ -901,7 +903,7 @@ export function createEffects(given = {}) {
|
|
|
901
903
|
function applyQuality() {
|
|
902
904
|
const quality = QUALITY_BUDGETS[qualityLevel];
|
|
903
905
|
const particleCount = particleBudget(qualityLevel, settings.get("particleBudgetPct"));
|
|
904
|
-
const chunkCount = Math.max(Math.round(particleCount *
|
|
906
|
+
const chunkCount = Math.max(Math.round(particleCount * CHUNK_BUDGET_SHARE_NUMBER), quality.debrisCount + 16);
|
|
905
907
|
if (!sprites || sprites.capacity !== particleCount || chunks.capacity !== chunkCount) {
|
|
906
908
|
for (const play of plays) for (const state of play.emitters) state.slab = null;
|
|
907
909
|
debris.setCapacity(0);
|
|
@@ -975,7 +977,7 @@ export function createEffects(given = {}) {
|
|
|
975
977
|
sharedUniforms.uTime.value = batchNowSec;
|
|
976
978
|
sharedUniforms.uGroundHeightM.value = settings.get("groundHeightM");
|
|
977
979
|
|
|
978
|
-
if (settings.get("qualityLevel") === "auto" && !lighting && frameWatcher.
|
|
980
|
+
if (settings.get("qualityLevel") === "auto" && !lighting && frameWatcher.countFrame(nowMs) && qualityLevel !== "low") changeQuality(QUALITY_ORDER[QUALITY_ORDER.indexOf(qualityLevel) - 1], "slow_frames");
|
|
979
981
|
const quality = QUALITY_BUDGETS[qualityLevel];
|
|
980
982
|
|
|
981
983
|
camera.getWorldPosition(cameraPlace);
|
|
@@ -1025,7 +1027,7 @@ export function createEffects(given = {}) {
|
|
|
1025
1027
|
}
|
|
1026
1028
|
play.earlierSec = nowSec;
|
|
1027
1029
|
}
|
|
1028
|
-
stats.keptPct = Math.round(fillGuard.
|
|
1030
|
+
stats.keptPct = Math.round(fillGuard.keptShareOf(coveredScreenCount, qualityLevel) * 100);
|
|
1029
1031
|
|
|
1030
1032
|
debris.update(nowSec, frameSec, toBatchSec);
|
|
1031
1033
|
flashes.update(nowSec, cameraPlace, settings.get("flashLightsEnabled"));
|
|
@@ -1067,7 +1069,7 @@ export function createEffects(given = {}) {
|
|
|
1067
1069
|
stats.markCount = marks.liveCount;
|
|
1068
1070
|
stats.debrisCount = debris.liveCount;
|
|
1069
1071
|
stats.flashCount = flashes.liveCount;
|
|
1070
|
-
stats.drawCallCount = group.children.reduce((
|
|
1072
|
+
stats.drawCallCount = group.children.reduce((runningCount, mesh) => runningCount + (mesh.visible ? 1 : 0), 0);
|
|
1071
1073
|
stats.updateDurationMs = timer.now() - startedMs;
|
|
1072
1074
|
}
|
|
1073
1075
|
|
|
@@ -1085,9 +1087,9 @@ export function createEffects(given = {}) {
|
|
|
1085
1087
|
/** Every effect loaded: { name, family, summary, importance, isLooping }. */
|
|
1086
1088
|
list: () => [...registry.values()].map(({ file }) => ({ name: file.name, family: file.family, summary: file.summary, importance: file.importance, isLooping: file.is_looping })),
|
|
1087
1089
|
/** Plays an effect. at: { positionM, direction, normal, toPositionM, scalePct, strengthPct, color, surfaceKind, follow, seedNumber, startedAgoSec }. */
|
|
1088
|
-
play: (effectName,
|
|
1090
|
+
play: (effectName, playAt) => startPlay(effectName, playAt, "play"),
|
|
1089
1091
|
/** What the online kind calls for an effect another player's game or the game server played: as play, told with the reason shared. */
|
|
1090
|
-
playShared: (effectName,
|
|
1092
|
+
playShared: (effectName, playAt) => startPlay(effectName, playAt, "shared"),
|
|
1091
1093
|
/** Stops everything playing. { isImmediate } takes what's on screen away at once. */
|
|
1092
1094
|
stopAll(options = {}) {
|
|
1093
1095
|
for (const play of [...plays]) {
|
package/looks.js
CHANGED
|
@@ -142,13 +142,13 @@ export function ownLookProblem(look) {
|
|
|
142
142
|
for (const [materialName, changes] of Object.entries(materials)) {
|
|
143
143
|
if (!MATERIAL_NAMES.includes(materialName)) return { message: `"${materialName}" isn't a material.`, field: `materials.${materialName}`, fix: `Give any of: ${MATERIAL_NAMES.join(", ")}.` };
|
|
144
144
|
if (!changes || typeof changes !== "object") return { message: `materials.${materialName} isn't an object.`, field: `materials.${materialName}`, fix: "Give the fields that differ." };
|
|
145
|
-
for (const [field,
|
|
145
|
+
for (const [field, fieldValue] of Object.entries(changes)) {
|
|
146
146
|
const statement = LOOK_STYLE_FIELDS[field];
|
|
147
147
|
const path = `materials.${materialName}.${field}`;
|
|
148
148
|
if (!statement) return { message: `${path} isn't a field of a material's style.`, field: path, fix: `Give only: ${Object.keys(LOOK_STYLE_FIELDS).join(", ")}.` };
|
|
149
149
|
const isChoice = statement.valueKind === "choice" || statement.valueKind === "choice_or_null";
|
|
150
|
-
if (isChoice && !(statement.choices.includes(
|
|
151
|
-
if (!isChoice && (typeof
|
|
150
|
+
if (isChoice && !(statement.choices.includes(fieldValue) || (fieldValue === null && statement.valueKind === "choice_or_null"))) return { message: `${path} isn't one of its choices.`, field: path, fix: `Give one of: ${statement.choices.join(", ")}${statement.valueKind === "choice_or_null" ? ", or null" : ""}.` };
|
|
151
|
+
if (!isChoice && (typeof fieldValue !== "number" || !(fieldValue >= statement.minimumNumber && fieldValue <= statement.maximumNumber) || (statement.valueKind === "integer" && !Number.isInteger(fieldValue)))) return { message: `${path} isn't a number from ${statement.minimumNumber} to ${statement.maximumNumber}.`, field: path, fix: `Give a number from ${statement.minimumNumber} to ${statement.maximumNumber}.` };
|
|
152
152
|
}
|
|
153
153
|
}
|
|
154
154
|
return null;
|
package/motion.js
CHANGED
|
@@ -24,39 +24,39 @@ export function particlePlaceAt(birth, ageSec, motion, around, into = { x: 0, y:
|
|
|
24
24
|
let z = birth.z + birth.velocityZ * travelSec;
|
|
25
25
|
|
|
26
26
|
if (motion.turbulenceM > 0) {
|
|
27
|
-
const
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
x +=
|
|
32
|
-
y +=
|
|
33
|
-
z +=
|
|
27
|
+
const turnNumber = ageSec * motion.turbulencePerSec;
|
|
28
|
+
const easeInNumber = Math.min(turnNumber, 1);
|
|
29
|
+
const phaseRad = birth.seedNumber * 6.2831853;
|
|
30
|
+
const reachM = motion.turbulenceM * easeInNumber * 0.5;
|
|
31
|
+
x += reachM * (Math.sin(turnNumber * 2.1 + phaseRad * 3) + Math.sin(turnNumber * 1.3 + phaseRad * 7));
|
|
32
|
+
y += reachM * (Math.sin(turnNumber * 1.7 + phaseRad * 5) + Math.sin(turnNumber * 2.9 + phaseRad * 11));
|
|
33
|
+
z += reachM * (Math.sin(turnNumber * 1.9 + phaseRad * 13) + Math.sin(turnNumber * 1.1 + phaseRad * 17));
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
if (motion.swirlRadPerSec !== 0) {
|
|
37
37
|
// Rodrigues' rotation of the offset from the centre, round the axis.
|
|
38
|
-
const
|
|
39
|
-
const
|
|
40
|
-
const
|
|
38
|
+
const angleRad = motion.swirlRadPerSec * ageSec;
|
|
39
|
+
const cosNumber = Math.cos(angleRad);
|
|
40
|
+
const sinNumber = Math.sin(angleRad);
|
|
41
41
|
const offsetX = x - around.centerX;
|
|
42
42
|
const offsetY = y - around.centerY;
|
|
43
43
|
const offsetZ = z - around.centerZ;
|
|
44
|
-
const
|
|
44
|
+
const alongM = (offsetX * around.axisX + offsetY * around.axisY + offsetZ * around.axisZ) * (1 - cosNumber);
|
|
45
45
|
const crossX = around.axisY * offsetZ - around.axisZ * offsetY;
|
|
46
46
|
const crossY = around.axisZ * offsetX - around.axisX * offsetZ;
|
|
47
47
|
const crossZ = around.axisX * offsetY - around.axisY * offsetX;
|
|
48
|
-
x = around.centerX + offsetX *
|
|
49
|
-
y = around.centerY + offsetY *
|
|
50
|
-
z = around.centerZ + offsetZ *
|
|
48
|
+
x = around.centerX + offsetX * cosNumber + crossX * sinNumber + around.axisX * alongM;
|
|
49
|
+
y = around.centerY + offsetY * cosNumber + crossY * sinNumber + around.axisY * alongM;
|
|
50
|
+
z = around.centerZ + offsetZ * cosNumber + crossZ * sinNumber + around.axisZ * alongM;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
if (motion.pullShare !== 0) {
|
|
54
|
-
const
|
|
54
|
+
const lifeShareNumber = Math.min(ageSec / birth.lifeSec, 1);
|
|
55
55
|
// Toward the centre: all the way there as life ends. Away from it: up to twice as far.
|
|
56
|
-
const
|
|
57
|
-
x = around.centerX + (x - around.centerX) *
|
|
58
|
-
y = around.centerY + (y - around.centerY) *
|
|
59
|
-
z = around.centerZ + (z - around.centerZ) *
|
|
56
|
+
const keepNumber = motion.pullShare > 0 ? 1 - motion.pullShare * lifeShareNumber * lifeShareNumber : 1 - motion.pullShare * lifeShareNumber;
|
|
57
|
+
x = around.centerX + (x - around.centerX) * keepNumber;
|
|
58
|
+
y = around.centerY + (y - around.centerY) * keepNumber;
|
|
59
|
+
z = around.centerZ + (z - around.centerZ) * keepNumber;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
// Gravity and the wind act in the world, after everything the effect itself does.
|
|
@@ -135,11 +135,11 @@ float zaismaHash(float seed, float salt) {
|
|
|
135
135
|
export function windAt(weather, timeSec, into = { x: 0, y: 0, z: 0 }) {
|
|
136
136
|
const directionRad = (weather.windDirectionDeg * Math.PI) / 180;
|
|
137
137
|
// Two slow swells that never line up, so gusts don't repeat.
|
|
138
|
-
const
|
|
139
|
-
const
|
|
140
|
-
into.x = Math.cos(directionRad) *
|
|
138
|
+
const gustNumber = 1 + (weather.windGustPct / 100) * 0.5 * (Math.sin(timeSec * 0.83) + Math.sin(timeSec * 0.31 + 1.7));
|
|
139
|
+
const speedMPerSec = weather.windSpeedMPerSec * Math.max(gustNumber, 0);
|
|
140
|
+
into.x = Math.cos(directionRad) * speedMPerSec;
|
|
141
141
|
into.y = 0;
|
|
142
142
|
// Counterclockwise seen from above, with y up: 90 degrees blows toward -z.
|
|
143
|
-
into.z = -Math.sin(directionRad) *
|
|
143
|
+
into.z = -Math.sin(directionRad) * speedMPerSec;
|
|
144
144
|
return into;
|
|
145
145
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zaisma/effects",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Spectacle that holds a phone's frame rate, for three.js: thousands of lit particles, hits, explosions, magic, trails, screen warps, marks on surfaces, debris that bounces and weather, in the look a game picks. An effect is a small file an AI writes; 92 come with it. Flashes light the scene, the character and physics bring effects free, and online every player sees the same effect at once.",
|
|
5
5
|
"license": "PolyForm-Shield-1.0.0",
|
|
6
6
|
"homepage": "https://zaisma.com",
|
package/partners.js
CHANGED
|
@@ -23,10 +23,10 @@ const IMPACT_SPEED_M_PER_SEC = 1.5;
|
|
|
23
23
|
const HEAVY_IMPACT_SPEED_M_PER_SEC = 9;
|
|
24
24
|
// A pile settling knocks many times a second: one body's knocks are this far apart, and a frame plays this many.
|
|
25
25
|
const IMPACT_GAP_SEC = 0.18;
|
|
26
|
-
const
|
|
26
|
+
const MAXIMUM_IMPACT_COUNT_PER_FRAME = 4;
|
|
27
27
|
|
|
28
|
-
const clamp = (
|
|
29
|
-
const isPoint = (
|
|
28
|
+
const clamp = (amount, low, high) => Math.min(Math.max(amount, low), high);
|
|
29
|
+
const isPoint = (candidate) => candidate !== null && typeof candidate === "object" && Number.isFinite(candidate.x) && Number.isFinite(candidate.y) && Number.isFinite(candidate.z);
|
|
30
30
|
const hookNamesOf = (piece) => new Set(typeof piece?.hooks?.list === "function" ? piece.hooks.list().map((hook) => hook.name) : []);
|
|
31
31
|
|
|
32
32
|
/**
|
|
@@ -56,9 +56,9 @@ export function connectPartners({ effects, given, settings, startPlay, findSurfa
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
/** Plays a free effect once its file is here; before that, nothing: the first footfalls of a game go without. */
|
|
59
|
-
function playFree(effectName,
|
|
59
|
+
function playFree(effectName, playAt, reason) {
|
|
60
60
|
if (!effects.has(effectName)) return;
|
|
61
|
-
startPlay(effectName,
|
|
61
|
+
startPlay(effectName, playAt, reason);
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
function loadFree(names) {
|
|
@@ -119,33 +119,33 @@ export function connectPartners({ effects, given, settings, startPlay, findSurfa
|
|
|
119
119
|
}
|
|
120
120
|
const { x, y, z } = object.position;
|
|
121
121
|
const acrossM = Math.hypot(x - seen.x, z - seen.z);
|
|
122
|
-
const
|
|
122
|
+
const riseSpeedMPerSec = (y - seen.y) / frameSec;
|
|
123
123
|
// A first frame, or a jump of metres in one (a respawn): start again from here.
|
|
124
124
|
if (!seen.hasPlace || acrossM > 3 || Math.abs(y - seen.y) > 3) {
|
|
125
125
|
seen.hasPlace = true;
|
|
126
126
|
seen.fallSpeed = 0;
|
|
127
127
|
seen.groundSec = 0;
|
|
128
128
|
seen.strideM = 0;
|
|
129
|
-
} else if (
|
|
130
|
-
seen.fallSpeed = Math.max(seen.fallSpeed, -
|
|
129
|
+
} else if (riseSpeedMPerSec < -3.5) {
|
|
130
|
+
seen.fallSpeed = Math.max(seen.fallSpeed, -riseSpeedMPerSec);
|
|
131
131
|
seen.groundSec = 0;
|
|
132
|
-
} else if (
|
|
132
|
+
} else if (riseSpeedMPerSec > 3.5) {
|
|
133
133
|
if (seen.groundSec > 0.1) tellWatched("jump", seen.x, seen.y, seen.z, 0, 0);
|
|
134
134
|
seen.groundSec = 0;
|
|
135
|
-
} else if (Math.abs(
|
|
135
|
+
} else if (Math.abs(riseSpeedMPerSec) < 3) {
|
|
136
136
|
if (seen.fallSpeed > 0) tellWatched("land", x, y, z, 0, seen.fallSpeed);
|
|
137
137
|
seen.fallSpeed = 0;
|
|
138
138
|
seen.groundSec += frameSec;
|
|
139
|
-
const
|
|
140
|
-
if (
|
|
139
|
+
const speedMPerSec = acrossM / frameSec;
|
|
140
|
+
if (speedMPerSec > 0.6) {
|
|
141
141
|
seen.strideM += acrossM;
|
|
142
142
|
// A stride lengthens with speed: a walk's is short, a sprint's long.
|
|
143
|
-
if (seen.strideM >= clamp(0.5 +
|
|
143
|
+
if (seen.strideM >= clamp(0.5 + speedMPerSec * 0.13, 0.55, 1.5)) {
|
|
144
144
|
seen.strideM = 0;
|
|
145
145
|
// The foot lands a little to one side of the way it's going.
|
|
146
|
-
const
|
|
146
|
+
const sideNumber = (seen.isLeftNext ? 0.11 : -0.11) / Math.max(acrossM, 1e-6);
|
|
147
147
|
seen.isLeftNext = !seen.isLeftNext;
|
|
148
|
-
tellWatched("step", x - (z - seen.z) *
|
|
148
|
+
tellWatched("step", x - (z - seen.z) * sideNumber, y, z + (x - seen.x) * sideNumber, speedMPerSec, 0);
|
|
149
149
|
}
|
|
150
150
|
} else seen.strideM = 0;
|
|
151
151
|
}
|
|
@@ -173,38 +173,38 @@ export function connectPartners({ effects, given, settings, startPlay, findSurfa
|
|
|
173
173
|
// ---- Physics ----
|
|
174
174
|
/** bodyId -> when its last knock played. */
|
|
175
175
|
const knockedSec = new Map();
|
|
176
|
-
let
|
|
176
|
+
let frameImpactCount = 0;
|
|
177
177
|
const { physics } = given;
|
|
178
178
|
if (physics && hookNamesOf(physics).has("bodyHit")) {
|
|
179
179
|
stops.push(
|
|
180
180
|
physics.hooks.on("bodyHit", (hit) => {
|
|
181
|
-
if (!hit || !settings.get("impactEffectsEnabled") ||
|
|
181
|
+
if (!hit || !settings.get("impactEffectsEnabled") || frameImpactCount >= MAXIMUM_IMPACT_COUNT_PER_FRAME) return;
|
|
182
182
|
const speed = hit.speedMPerSec;
|
|
183
183
|
if (!(speed >= IMPACT_SPEED_M_PER_SEC)) return;
|
|
184
184
|
const bodyId = hit.bodyId ?? hit.otherBodyId ?? null;
|
|
185
185
|
if (bodyId !== null && nowSec - (knockedSec.get(bodyId) ?? -Infinity) < IMPACT_GAP_SEC) return;
|
|
186
|
-
const
|
|
187
|
-
if (isPoint(hit.positionM))
|
|
186
|
+
const playAt = {};
|
|
187
|
+
if (isPoint(hit.positionM)) playAt.positionM = hit.positionM;
|
|
188
188
|
else {
|
|
189
189
|
// A physics that tells no place: the body's own, dropped to what's under it.
|
|
190
190
|
const middle = bodyId !== null && typeof physics.bodyState === "function" ? physics.bodyState(bodyId)?.positionM : null;
|
|
191
191
|
if (!isPoint(middle)) return;
|
|
192
192
|
if (findSurface(middle.x, middle.y, middle.z, 0, -1, 0, 2.5, surface)) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
} else
|
|
193
|
+
playAt.positionM = { x: surface.x, y: surface.y, z: surface.z };
|
|
194
|
+
playAt.normal = { x: surface.normalX, y: surface.normalY, z: surface.normalZ };
|
|
195
|
+
} else playAt.positionM = { x: middle.x, y: middle.y, z: middle.z };
|
|
196
196
|
}
|
|
197
|
-
if (!isWithin(
|
|
197
|
+
if (!isWithin(playAt.positionM, IMPACT_REACH_M)) return;
|
|
198
198
|
// Heavy things land hard at a lower speed: a crate at 9 m/s, a car at 4.
|
|
199
199
|
const massKg = Number.isFinite(hit.massKg) ? hit.massKg : 30;
|
|
200
|
-
const
|
|
201
|
-
const isHeavy =
|
|
202
|
-
|
|
200
|
+
const forceNumber = speed * clamp(Math.cbrt(massKg / 30), 0.6, 2.5);
|
|
201
|
+
const isHeavy = forceNumber >= HEAVY_IMPACT_SPEED_M_PER_SEC;
|
|
202
|
+
playAt.scalePct = isHeavy ? clamp(50 + forceNumber * 6, 90, 240) : clamp(35 + forceNumber * 12, 50, 150);
|
|
203
203
|
// What it landed on says what flies up; a physics that tells neither is stone until the game says.
|
|
204
|
-
|
|
204
|
+
playAt.surfaceKind = surfaceKindOf(hit.otherMaterialName ?? hit.materialName ?? surface.materialName ?? null, playAt.positionM, "body_hit", bodyId);
|
|
205
205
|
if (bodyId !== null) knockedSec.set(bodyId, nowSec);
|
|
206
|
-
|
|
207
|
-
playFree(isHeavy ? "impact_heavy" : "impact",
|
|
206
|
+
frameImpactCount += 1;
|
|
207
|
+
playFree(isHeavy ? "impact_heavy" : "impact", playAt, "body_hit");
|
|
208
208
|
}),
|
|
209
209
|
);
|
|
210
210
|
loadFree(PHYSICS_EFFECT_NAMES);
|
|
@@ -232,7 +232,7 @@ export function connectPartners({ effects, given, settings, startPlay, findSurfa
|
|
|
232
232
|
/** Every frame, before the effects are drawn. camera: the three.js camera the game draws with. */
|
|
233
233
|
update(sec, frameSec, camera) {
|
|
234
234
|
nowSec = sec;
|
|
235
|
-
|
|
235
|
+
frameImpactCount = 0;
|
|
236
236
|
if (camera?.isCamera) {
|
|
237
237
|
const place = camera.matrixWorld.elements;
|
|
238
238
|
cameraPlace.x = place[12];
|