@adatechnology/game-destruction-three 0.23.2 → 0.24.1

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/dist/index.js CHANGED
@@ -3547,6 +3547,28 @@ var SettlementView = class {
3547
3547
  }
3548
3548
  };
3549
3549
 
3550
+ // src/emitter-anchor.ts
3551
+ function anchorOnMesh(THREE, mv, point, normal) {
3552
+ mv.mesh.updateWorldMatrix(true, false);
3553
+ const inverse = new THREE.Matrix4().copy(mv.mesh.matrixWorld).invert();
3554
+ const local = new THREE.Vector3(point[0], point[1], point[2]).applyMatrix4(inverse);
3555
+ const localNormal = new THREE.Vector3(normal[0], normal[1], normal[2]).transformDirection(inverse);
3556
+ return { mv, local: [local.x, local.y, local.z], localNormal: [localNormal.x, localNormal.y, localNormal.z] };
3557
+ }
3558
+ function inScene(object) {
3559
+ let top = object;
3560
+ while (top.parent) top = top.parent;
3561
+ return top.isScene === true;
3562
+ }
3563
+ function anchorWorld(anchor, shown, point, normal) {
3564
+ const mesh = anchor.mv.mesh;
3565
+ if (!shown || !inScene(mesh)) return false;
3566
+ mesh.updateWorldMatrix(true, false);
3567
+ point.set(anchor.local[0], anchor.local[1], anchor.local[2]).applyMatrix4(mesh.matrixWorld);
3568
+ normal.set(anchor.localNormal[0], anchor.localNormal[1], anchor.localNormal[2]).transformDirection(mesh.matrixWorld);
3569
+ return true;
3570
+ }
3571
+
3550
3572
  // src/renderer.ts
3551
3573
  var rendererCount = 0;
3552
3574
  var COLLAPSE_CLOUD_KG = 1e3;
@@ -4240,11 +4262,11 @@ var Renderer = class {
4240
4262
  for (const b of this.bounds.values()) b.onFrame?.(dt);
4241
4263
  for (const b of this.bounds.values()) if (b.soilNode && this.world.state(b.id)?.soil?.status === "yielding") this.onSettle(b.id);
4242
4264
  this.settlement.update(dt);
4265
+ this.pieceSys.update(dt);
4266
+ this.fragments.update(dt);
4243
4267
  this.updateActive(dt);
4244
4268
  this.updateLeaks(dt);
4245
4269
  this.updateTrickles(dt);
4246
- this.pieceSys.update(dt);
4247
- this.fragments.update(dt);
4248
4270
  this.ground.update(dt);
4249
4271
  this.ownVfx?.update(dt);
4250
4272
  this.syncTransforms();
@@ -4635,9 +4657,12 @@ var Renderer = class {
4635
4657
  break;
4636
4658
  }
4637
4659
  }
4660
+ /** Where the object's root is now, in the world: a body that tipped or fell is there, not where it was bound. */
4638
4661
  rootPoint(objectId) {
4639
4662
  const b = this.bounds.get(objectId);
4640
- return b ? [b.root.position.x, b.root.position.y, b.root.position.z] : [0, 0, 0];
4663
+ if (!b) return [0, 0, 0];
4664
+ const p = b.root.getWorldPosition(new this.THREE.Vector3());
4665
+ return [p.x, p.y, p.z];
4641
4666
  }
4642
4667
  sound(key, point, volume, detail) {
4643
4668
  if (detail) this.o.onSound?.(key, point, volume, detail);
@@ -4803,15 +4828,19 @@ var Renderer = class {
4803
4828
  const core = def?.visual?.innerColor;
4804
4829
  if (core !== void 0) this.vfx.burst("chips", ev.point, [normal.x, normal.y, normal.z], 5, 2.5, { color: core });
4805
4830
  else this.playEvent({ kind: "spall", material: this.impactMaterial(def) }, ev.point);
4806
- if (core !== void 0) this.trickleFrom(ev.point, [normal.x, normal.y, normal.z], core);
4831
+ if (core !== void 0) this.trickleFrom(ev.point, [normal.x, normal.y, normal.z], core, mv);
4807
4832
  if (mv.mesh.userData.drawnSpalls?.includes(ev.part)) return;
4808
4833
  this.visuals.addSpall(mv, wp, normal, Math.sqrt(ev.area / Math.PI), core ?? fallbackColor(pv.material, def));
4809
4834
  }
4810
- /** Grit and dust go on falling from where a blow broke a hard face (masonry, concrete) for `TRICKLE_SECONDS`, a pinch at a time and less and less. */
4811
- trickleFrom(point, normal, color) {
4835
+ /**
4836
+ * Grit and dust go on falling from where a blow broke a hard face (masonry, concrete, wood) for `TRICKLE_SECONDS`, a pinch at a time and less and
4837
+ * less. The place is held on the mesh it broke on (`mv`), so it falls with the object when it tips or a piece of it comes away.
4838
+ */
4839
+ trickleFrom(point, normal, color, mv) {
4812
4840
  if (this.trickles.length >= TRICKLE_MAX) this.trickles.shift();
4813
4841
  const total = TRICKLE_SECONDS[0] + (TRICKLE_SECONDS[1] - TRICKLE_SECONDS[0]) * this.rng();
4814
- this.trickles.push({ point: [point[0], point[1], point[2]], normal, color, left: total, total, next: TRICKLE_EVERY });
4842
+ const anchor = mv ? anchorOnMesh(this.THREE, mv, point, normal) : null;
4843
+ this.trickles.push({ point: [point[0], point[1], point[2]], normal, anchor, color, left: total, total, next: TRICKLE_EVERY });
4815
4844
  }
4816
4845
  updateTrickles(dt) {
4817
4846
  for (let i = this.trickles.length - 1; i >= 0; i--) {
@@ -4823,6 +4852,14 @@ var Renderer = class {
4823
4852
  continue;
4824
4853
  }
4825
4854
  if (t.next > 0) continue;
4855
+ if (t.anchor) {
4856
+ if (!anchorWorld(t.anchor, this.visuals.shown(t.anchor.mv), this.v1, this.v2)) {
4857
+ this.trickles.splice(i, 1);
4858
+ continue;
4859
+ }
4860
+ t.point = [this.v1.x, this.v1.y, this.v1.z];
4861
+ t.normal = [this.v2.x, this.v2.y, this.v2.z];
4862
+ }
4826
4863
  const share = t.left / t.total;
4827
4864
  t.next = TRICKLE_EVERY / Math.max(share, 0.25);
4828
4865
  const out = [t.normal[0] * 0.2, -1, t.normal[2] * 0.2];
@@ -4863,10 +4900,12 @@ var Renderer = class {
4863
4900
  const maxAlive = this.low ? Math.floor(this.maxFragments / 2) : this.maxFragments;
4864
4901
  for (const fr of ev.fragments) {
4865
4902
  const fdef = this.world.materials.get(fr.material);
4866
- const tone3 = fr.inner && fdef?.visual?.innerColor !== void 0 ? fdef.visual.innerColor : fdef === def ? color : fallbackColor(fr.material, fdef);
4903
+ const tone3 = fr.unit && fdef?.visual?.unitColor !== void 0 ? fdef.visual.unitColor : fr.inner && fdef?.visual?.innerColor !== void 0 ? fdef.visual.innerColor : fdef === def ? color : fallbackColor(fr.material, fdef);
4867
4904
  this.fragments.spawn(fr, this.fragments.materialFor(fr.material, tone3, false), ev.object, maxAlive);
4868
4905
  }
4869
- if (def?.visual?.innerColor !== void 0 || pv?.material === "concrete" || pv?.material === "brick") this.trickleFrom(ev.center, [0, 0, 0], color);
4906
+ if (def?.visual?.innerColor !== void 0 || pv?.material === "concrete" || pv?.material === "brick") {
4907
+ this.trickleFrom(ev.center, [0, 0, 0], color, pv ? this.nearestMesh(pv, new this.THREE.Vector3(ev.center[0], ev.center[1], ev.center[2])) : null);
4908
+ }
4870
4909
  if (ev.torn) {
4871
4910
  this.impactSound(pv?.material ?? "", ev.center, 0.5, ev.blow);
4872
4911
  return;
@@ -4969,6 +5008,41 @@ var Renderer = class {
4969
5008
  this.fragments.spawn(fragment, material, objectId, this.maxFragments, true);
4970
5009
  });
4971
5010
  }
5011
+ /**
5012
+ * What goes on at a part (its fire, its leak, its burn sound, its heat) moves with the part to the object a split made of it: it goes on where the
5013
+ * part is, as it falls, not where the object stood, and does not end because the part has a new id.
5014
+ */
5015
+ followSplit(from, to) {
5016
+ for (const part of to.parts.keys()) {
5017
+ const was = `${from.id}|${part}`;
5018
+ const now = `${to.id}|${part}`;
5019
+ const fire2 = this.fires.get(was);
5020
+ if (fire2) {
5021
+ this.fires.delete(was);
5022
+ fire2.object = to.id;
5023
+ this.fires.set(now, fire2);
5024
+ }
5025
+ const sound = this.burnSounds.get(was);
5026
+ if (sound) {
5027
+ this.burnSounds.delete(was);
5028
+ this.burnSounds.set(now, { ...sound, object: to.id });
5029
+ }
5030
+ if (this.active.delete(was)) this.active.set(now, { object: to.id, part });
5031
+ }
5032
+ from.root.updateWorldMatrix(true, false);
5033
+ to.root.updateWorldMatrix(true, false);
5034
+ const into = new this.THREE.Matrix4().copy(to.root.matrixWorld).invert();
5035
+ for (const leak of this.leaks) {
5036
+ if (leak.object !== from.id || !to.parts.has(leak.part)) continue;
5037
+ leak.object = to.id;
5038
+ const point = leak.localPoint ? this.v1.set(...leak.localPoint).applyMatrix4(from.root.matrixWorld) : this.v1.set(...leak.point);
5039
+ const direction = leak.localDirection ? this.v2.set(...leak.localDirection).transformDirection(from.root.matrixWorld) : this.v2.set(...leak.direction);
5040
+ point.applyMatrix4(into);
5041
+ direction.transformDirection(into);
5042
+ leak.localPoint = [point.x, point.y, point.z];
5043
+ leak.localDirection = [direction.x, direction.y, direction.z];
5044
+ }
5045
+ }
4972
5046
  /** Turn every loose group of the object (a detach, a collapse) into a piece of its own. */
4973
5047
  splitLoose(objectId) {
4974
5048
  if (!this.autoSplit) return;
@@ -5039,6 +5113,7 @@ var Renderer = class {
5039
5113
  }
5040
5114
  this.bounds.set(newId, nb);
5041
5115
  for (const pv of nb.parts.values()) this.tagPart(pv, newId);
5116
+ this.followSplit(from, nb);
5042
5117
  const meshes = this.meshesOf(nb);
5043
5118
  if (meshes.length === 0) return;
5044
5119
  const { half, center } = this.measure(group, meshes);