@zaisma/effects 1.0.1 → 1.2.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/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 { CHUNK_BUDGET_SHARE, QUALITY_BUDGETS, QUALITY_ORDER, createFillGuard, createFrameWatcher, isTouchDevice, particleBudget, planEmitter, startingQualityLevel } from "./budget.js";
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 GUARDED_SCREEN_SHARE = 0.002;
67
- const MAXIMUM_LANDING_EFFECTS_PER_FRAME = 6;
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 >= MAXIMUM_LANDING_EFFECTS_PER_FRAME) return;
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((share) => Math.min(share, 1)));
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 lightenShare = style.lightenPct / 100;
234
- elementTable.write(row, ELEMENT_ROW.colorKeys + index, color.r + (1 - color.r) * lightenShare, color.g + (1 - color.g) * lightenShare, color.b + (1 - color.b) * lightenShare, fourth[index]);
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 countShare = style.countPct / 100;
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 more = Math.max(countShare, 1);
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 maxLifeSec = fade.life_sec * (1 + fade.life_variation_pct / 100);
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(countShare, 1);
264
- compiled.maxLifeSec = maxLifeSec;
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 * more),
269
- perSec: emit.per_sec * more,
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(maxLifeSec / loopDurationSec) : 1;
289
- compiled.wantedCount = Math.min(compiled.emitter.burstCount * runCount + Math.ceil(compiled.emitter.perSec * Math.min(maxLifeSec, flowSec * runCount)), 20000);
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 key = 0; key < 4; key += 1) elementTable.write(row, ELEMENT_ROW.colorKeys + key, color.r, color.g, color.b, fourth[key]);
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((count, element) => count + (element.element_kind === "particles" ? 1 : element.element_kind === "debris" ? element.colors.length : 0), 0);
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 nextPlayId = 1;
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
- const inertHandle = Object.freeze({ playId: 0, effectName: null, isEnded: true, stop() {}, moveTo() {}, set() {} });
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, at = {}, reason = "play") {
556
- if (at === null || typeof at !== "object" || Array.isArray(at)) refuse("bad_play", { message: "play takes where to play as an object.", field: "at", fix: "Give { positionM: { x, y, z } }." });
557
- for (const field of Object.keys(at)) {
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 = at.scalePct ?? 100;
562
- let strengthPct = at.strengthPct ?? 100;
563
- let tintHex = at.color ?? null;
564
- let surfaceKind = at.surfaceKind ?? null;
565
- if (at.follow !== undefined && at.follow !== null && !(at.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." });
566
- const follow = at.follow ?? null;
567
- if (!follow && !isPoint(at.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." });
568
- if (at.positionM !== undefined && !isPoint(at.positionM)) refuse("bad_play", { message: "positionM isn't a place.", field: "positionM", fix: "Give { x, y, z } in metres." });
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 (at[field] !== undefined && at[field] !== null && !isPoint(at[field])) refuse("bad_play", { message: `${field} isn't { x, y, z }.`, field, fix: "Give { x, y, z }." });
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 = at.startedAgoSec ?? 0;
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 = at.positionM ?? { x: 0, y: 0, z: 0 };
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 = at.direction ?? at.normal ?? null;
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
- playId: nextPlayId,
609
+ playNumber: nextPlayNumber,
609
610
  entry,
610
- seedNumber: Number.isInteger(at.seedNumber) ? at.seedNumber >>> 0 : (Math.random() * 4294967296) >>> 0,
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(at.positionM?.x ?? 0, at.positionM?.y ?? 0, at.positionM?.z ?? 0),
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(at.normal),
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(at.toPositionM),
626
- normal: at.normal ? new THREE.Vector3(at.normal.x, at.normal.y, at.normal.z).normalize() : UP,
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
- nextPlayId += 1;
646
+ nextPlayNumber += 1;
646
647
  placePlay(play);
647
648
  play.earlierPlace.copy(play.place);
648
649
  if (!follow) {
649
- if (at.normal) play.surface.set(play.normal.x, play.normal.y, play.normal.z, play.normal.dot(play.place));
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 (at.toPositionM) play.toPlace.set(at.toPositionM.x, at.toPositionM.y, at.toPositionM.z);
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.playId, effectName: name, positionM: { x: play.place.x, y: play.place.y, z: play.place.z }, scalePct, strengthPct, surfaceKind, reason });
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
- playId: play.playId,
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.playId, effectName: play.entry.name });
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 fullShare = compiled.isNeverThinned ? 1 : (compiled.lookCountShare * QUALITY_BUDGETS[qualityLevel].keptPct) / 100;
735
- if (state.keptShare < fullShare * 0.6) toldThinned(play.entry.name, state.keptShare / Math.max(fullShare, 1e-6));
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.maxLifeSec);
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.maxLifeSec) state.isDone = true;
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.maxLifeSec));
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 > GUARDED_SCREEN_SHARE) share *= fillGuard.fillShare;
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 back = Math.min(Math.max((nowSec - birthSec) / spanSec, 0), 1);
788
- x += (play.earlierPlace.x - play.place.x) * back;
789
- y += (play.earlierPlace.y - play.place.y) * back;
790
- z += (play.earlierPlace.z - play.place.z) * back;
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 strength = 0;
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 nearShare = Math.max(1 - shake.play.place.distanceTo(cameraPlace) / (shake.radiusM * shake.play.scaleShare), 0);
814
- const felt = shake.strengthShare * shake.play.strengthShare * nearShare;
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 (felt > 0.005) tell("shake", { strengthPct: Math.min(felt * 100, 100), durationSec: shake.durationSec - sinceSec });
819
+ if (feltNumber > 0.005) tell("shake", { strengthPct: Math.min(feltNumber * 100, 100), durationSec: shake.durationSec - sinceSec });
818
820
  }
819
- const fade = 1 - sinceSec / shake.durationSec;
820
- strength = Math.max(strength, felt * fade * fade);
821
+ const fadeNumber = 1 - sinceSec / shake.durationSec;
822
+ strengthNumber = Math.max(strengthNumber, feltNumber * fadeNumber * fadeNumber);
821
823
  }
822
- strength = Math.min(strength, 1);
823
- shakeNow.strengthPct = strength * 100;
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 = strength * 0.35;
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 = strength * 0.035 * Math.sin(nowSec * 43.1 + 0.7);
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 * CHUNK_BUDGET_SHARE), quality.debrisCount + 16);
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.count(nowMs) && qualityLevel !== "low") changeQuality(QUALITY_ORDER[QUALITY_ORDER.indexOf(qualityLevel) - 1], "slow_frames");
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.count(coveredScreenCount, qualityLevel) * 100);
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((count, mesh) => count + (mesh.visible ? 1 : 0), 0);
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, at) => startPlay(effectName, at, "play"),
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, at) => startPlay(effectName, at, "shared"),
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, value] of Object.entries(changes)) {
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(value) || (value === 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 value !== "number" || !(value >= statement.minimumNumber && value <= statement.maximumNumber) || (statement.valueKind === "integer" && !Number.isInteger(value)))) 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}.` };
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 turn = ageSec * motion.turbulencePerSec;
28
- const easeIn = Math.min(turn, 1);
29
- const phase = birth.seedNumber * 6.2831853;
30
- const reach = motion.turbulenceM * easeIn * 0.5;
31
- x += reach * (Math.sin(turn * 2.1 + phase * 3) + Math.sin(turn * 1.3 + phase * 7));
32
- y += reach * (Math.sin(turn * 1.7 + phase * 5) + Math.sin(turn * 2.9 + phase * 11));
33
- z += reach * (Math.sin(turn * 1.9 + phase * 13) + Math.sin(turn * 1.1 + phase * 17));
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 angle = motion.swirlRadPerSec * ageSec;
39
- const cos = Math.cos(angle);
40
- const sin = Math.sin(angle);
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 along = (offsetX * around.axisX + offsetY * around.axisY + offsetZ * around.axisZ) * (1 - cos);
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 * cos + crossX * sin + around.axisX * along;
49
- y = around.centerY + offsetY * cos + crossY * sin + around.axisY * along;
50
- z = around.centerZ + offsetZ * cos + crossZ * sin + around.axisZ * along;
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 lifeShare = Math.min(ageSec / birth.lifeSec, 1);
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 keep = motion.pullShare > 0 ? 1 - motion.pullShare * lifeShare * lifeShare : 1 - motion.pullShare * lifeShare;
57
- x = around.centerX + (x - around.centerX) * keep;
58
- y = around.centerY + (y - around.centerY) * keep;
59
- z = around.centerZ + (z - around.centerZ) * keep;
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 gust = 1 + (weather.windGustPct / 100) * 0.5 * (Math.sin(timeSec * 0.83) + Math.sin(timeSec * 0.31 + 1.7));
139
- const speed = weather.windSpeedMPerSec * Math.max(gust, 0);
140
- into.x = Math.cos(directionRad) * speed;
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) * speed;
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.1",
3
+ "version": "1.2.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",
@@ -42,7 +42,12 @@
42
42
  "EFFECT-FILES.md",
43
43
  "LICENSE.txt",
44
44
  "README.md",
45
- "batches",
45
+ "batches/chunks.js",
46
+ "batches/marks.js",
47
+ "batches/ribbons.js",
48
+ "batches/shared.js",
49
+ "batches/sprites.js",
50
+ "batches/warps.js",
46
51
  "budget.js",
47
52
  "debris.js",
48
53
  "emit.js",
@@ -50,7 +55,99 @@
50
55
  "format.js",
51
56
  "index.d.ts",
52
57
  "index.js",
53
- "library",
58
+ "library/bubbles_rising.json",
59
+ "library/bullet_impact.json",
60
+ "library/campfire.json",
61
+ "library/candle_flame.json",
62
+ "library/confetti.json",
63
+ "library/dash_burst.json",
64
+ "library/debris_glass.json",
65
+ "library/debris_metal.json",
66
+ "library/debris_stone.json",
67
+ "library/debris_wood.json",
68
+ "library/dust_motes.json",
69
+ "library/dust_puff.json",
70
+ "library/electric_sparks.json",
71
+ "library/embers_drift.json",
72
+ "library/exhaust_puff.json",
73
+ "library/explosion_air.json",
74
+ "library/explosion_large.json",
75
+ "library/explosion_magic.json",
76
+ "library/explosion_medium.json",
77
+ "library/explosion_small.json",
78
+ "library/explosion_water.json",
79
+ "library/falling_leaves.json",
80
+ "library/fire_burst.json",
81
+ "library/fire_large.json",
82
+ "library/fireball.json",
83
+ "library/fireflies.json",
84
+ "library/firework_burst.json",
85
+ "library/footstep.json",
86
+ "library/fountain_spray.json",
87
+ "library/frost_burst.json",
88
+ "library/ground_mist.json",
89
+ "library/ground_slam.json",
90
+ "library/heat_haze.json",
91
+ "library/hit_blade.json",
92
+ "library/hit_block.json",
93
+ "library/hit_critical.json",
94
+ "library/hit_heavy.json",
95
+ "library/hit_light.json",
96
+ "library/hit_magic.json",
97
+ "library/impact.json",
98
+ "library/impact_heavy.json",
99
+ "library/index.json",
100
+ "library/jump_puff.json",
101
+ "library/landing_heavy.json",
102
+ "library/landing_puff.json",
103
+ "library/level_up.json",
104
+ "library/lightning_arc.json",
105
+ "library/lightning_strike.json",
106
+ "library/magic_aura.json",
107
+ "library/magic_bolt.json",
108
+ "library/magic_bolt_impact.json",
109
+ "library/magic_cast.json",
110
+ "library/magic_charge.json",
111
+ "library/magic_heal.json",
112
+ "library/magic_shield.json",
113
+ "library/magic_teleport.json",
114
+ "library/mark_bullet_hole.json",
115
+ "library/mark_crack.json",
116
+ "library/mark_footprint.json",
117
+ "library/mark_frost.json",
118
+ "library/mark_puddle.json",
119
+ "library/mark_scorch.json",
120
+ "library/mark_slash.json",
121
+ "library/mark_splat.json",
122
+ "library/pickup_collect.json",
123
+ "library/pickup_glow.json",
124
+ "library/poison_cloud.json",
125
+ "library/portal.json",
126
+ "library/shockwave_air.json",
127
+ "library/shockwave_ground.json",
128
+ "library/slide_dust.json",
129
+ "library/smoke_column.json",
130
+ "library/smoke_puff.json",
131
+ "library/smoke_screen.json",
132
+ "library/sparks_burst.json",
133
+ "library/sparks_fountain.json",
134
+ "library/sparks_shower.json",
135
+ "library/splash_large.json",
136
+ "library/splash_small.json",
137
+ "library/steam_vent.json",
138
+ "library/summon_circle.json",
139
+ "library/swim_splash.json",
140
+ "library/torch_flame.json",
141
+ "library/trail_blade.json",
142
+ "library/trail_blade_fire.json",
143
+ "library/trail_dash.json",
144
+ "library/trail_magic.json",
145
+ "library/trail_projectile.json",
146
+ "library/trail_smoke.json",
147
+ "library/warp_pulse.json",
148
+ "library/warp_ripple.json",
149
+ "library/water_ripple.json",
150
+ "library/waterfall_mist.json",
54
151
  "looks.js",
55
152
  "motion.js",
56
153
  "online.d.ts",