@adatechnology/game-destruction-three 0.24.0 → 0.24.2

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.
@@ -7829,6 +7829,7 @@ var GameDestructionThree = (() => {
7829
7829
  const key = event.stage !== void 0 ? String(event.stage) : event.material;
7830
7830
  return key === void 0 ? void 0 : entry[key];
7831
7831
  }
7832
+ var DEGREES = 180 / Math.PI;
7832
7833
 
7833
7834
  // ../game-render-three/dist/index.js
7834
7835
  var DEFAULT_ANGLE = Math.PI / 3;
@@ -9359,6 +9360,42 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
9359
9360
  }
9360
9361
  var LIGHT_BUDGET = EVENT_BUDGET.blastLights + EVENT_BUDGET.fireLights;
9361
9362
 
9363
+ // ../game-vfx-three/dist/index.js
9364
+ var NOISE = `float wHash(vec2 p){return fract(sin(dot(p,vec2(12.9898,78.233)))*43758.5453);}
9365
+ float wNoise(vec2 p){vec2 i=floor(p);vec2 f=fract(p);f=f*f*(3.-2.*f);return mix(mix(wHash(i),wHash(i+vec2(1.,0.)),f.x),mix(wHash(i+vec2(0.,1.)),wHash(i+vec2(1.,1.)),f.x),f.y);}`;
9366
+ var OUTPUT = `
9367
+ #include <tonemapping_fragment>
9368
+ #include <colorspace_fragment>`;
9369
+ var WATER_SURFACE_FRAGMENT = `uniform vec3 uFoam;varying vec2 vU;varying vec3 vD;${NOISE}
9370
+ void main(){float r=length(vU);if(r>1.)discard;float a=atan(vU.y,vU.x);float kind=vD.y;float s=vD.z;float m;
9371
+ if(kind>2.5){float n=wNoise(vU*7.+s*13.)*.6+wNoise(vU*19.-s*7.)*.4;m=smoothstep(1.,.55,r)*smoothstep(.25,.6,n);}
9372
+ else{float w=kind>1.5?.16:kind>.5?.05:.09;float band=smoothstep(1.,1.-w,r)*smoothstep(1.-w*3.,1.-w,r);float broken=kind>.5?1.:.65+.35*wNoise(vec2(a*5.+s*20.,s));m=band*broken;}
9373
+ float alpha=m*vD.x;if(alpha<.004)discard;gl_FragColor=vec4(uFoam,alpha);${OUTPUT}
9374
+ }`;
9375
+ var WATER_SPRITE_FRAGMENT = `uniform vec3 uFoam;uniform vec3 uDeep;varying vec2 vU;varying vec3 vD;${NOISE}
9376
+ void main(){float r=length(vU);if(r>1.)discard;float kind=vD.y;float m;vec3 c=uFoam;
9377
+ if(kind>1.5){m=exp(-r*r*3.)*(.75+.25*wNoise(vU*3.+vD.z*9.));}
9378
+ else if(kind>.5){m=smoothstep(.55,.85,r)*smoothstep(1.,.88,r)+.25*smoothstep(.5,.2,length(vU-vec2(-.3,.3)));c=mix(uDeep,uFoam,.8);}
9379
+ else{m=smoothstep(1.,.6,r);c=mix(uDeep,uFoam,.55+.45*smoothstep(.2,.8,1.-length(vU-vec2(-.3,.35))));}
9380
+ float alpha=m*vD.x;if(alpha<.004)discard;gl_FragColor=vec4(c,alpha);${OUTPUT}
9381
+ }`;
9382
+ var WATER_COLUMN_VERTEX = `attribute vec4 aP;attribute vec4 aD;attribute vec2 aK;varying float vY;varying float vA;varying vec2 vK;varying float vF;${NOISE}
9383
+ void main(){vY=position.y;float ang=atan(position.z,position.x);vK=aK;
9384
+ float dome=aK.x;float s=aK.y;float h=position.y;
9385
+ float jag=.8+.4*wNoise(vec2(ang*3.+s*17.,h*4.+s));
9386
+ float flare=dome>.5?1.:(.55+.9*h*h)*jag;
9387
+ vec3 p=vec3(position.x*aP.w*flare,position.y*aD.x,position.z*aP.w*flare);
9388
+ p.x+=aD.z*aD.x*h*h;p.z+=aD.w*aD.x*h*h;
9389
+ vec3 w=aP.xyz+p;vec3 n=normalize(vec3(position.x,dome>.5?position.y:.25,position.z));
9390
+ vec3 v=normalize(cameraPosition-w);vF=abs(dot(n,v));
9391
+ vA=aD.y;gl_Position=projectionMatrix*viewMatrix*vec4(w,1.);}`;
9392
+ var WATER_COLUMN_FRAGMENT = `uniform vec3 uFoam;uniform vec3 uDeep;varying float vY;varying float vA;varying vec2 vK;varying float vF;${NOISE}
9393
+ void main(){float dome=vK.x;float s=vK.y;
9394
+ float streak=wNoise(vec2(gl_FragCoord.x*.05+s*31.,vY*6.+s));
9395
+ float body=dome>.5?mix(.95,.5,vY)*(.6+.4*(1.-vF)):(1.-smoothstep(.55,1.,vY))*(.45+.55*streak)*(.55+.45*(1.-vF*.6));
9396
+ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)discard;gl_FragColor=vec4(c,alpha);${OUTPUT}
9397
+ }`;
9398
+
9362
9399
  // src/fitted-boxes.ts
9363
9400
  var SLICE_LENGTH = 0.12;
9364
9401
  var MAX_SLICES = 12;
@@ -9852,6 +9889,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
9852
9889
  rouse(f) {
9853
9890
  f.asleep = false;
9854
9891
  f.rest = 0;
9892
+ f.floor = null;
9855
9893
  f.mesh.matrixAutoUpdate = true;
9856
9894
  }
9857
9895
  /** What lies inside the disc looks at the ground again: it was dug or heaped under it. */
@@ -9897,6 +9935,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
9897
9935
  if (!f.asleep || f.dying) continue;
9898
9936
  const e = this.m.makeRotationFromQuaternion(f.mesh.quaternion).elements;
9899
9937
  const ext = Math.abs(e[1]) * f.half.x + Math.abs(e[5]) * f.half.y + Math.abs(e[9]) * f.half.z;
9938
+ f.floor = null;
9900
9939
  if (f.mesh.position.y - ext > this.floorUnder(f) + SUPPORT_GAP) this.rouse(f);
9901
9940
  }
9902
9941
  }
@@ -14407,6 +14446,28 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
14407
14446
  }
14408
14447
  };
14409
14448
 
14449
+ // src/emitter-anchor.ts
14450
+ function anchorOnMesh(THREE, mv, point, normal) {
14451
+ mv.mesh.updateWorldMatrix(true, false);
14452
+ const inverse = new THREE.Matrix4().copy(mv.mesh.matrixWorld).invert();
14453
+ const local2 = new THREE.Vector3(point[0], point[1], point[2]).applyMatrix4(inverse);
14454
+ const localNormal = new THREE.Vector3(normal[0], normal[1], normal[2]).transformDirection(inverse);
14455
+ return { mv, local: [local2.x, local2.y, local2.z], localNormal: [localNormal.x, localNormal.y, localNormal.z] };
14456
+ }
14457
+ function inScene(object) {
14458
+ let top = object;
14459
+ while (top.parent) top = top.parent;
14460
+ return top.isScene === true;
14461
+ }
14462
+ function anchorWorld(anchor, shown, point, normal) {
14463
+ const mesh2 = anchor.mv.mesh;
14464
+ if (!shown || !inScene(mesh2)) return false;
14465
+ mesh2.updateWorldMatrix(true, false);
14466
+ point.set(anchor.local[0], anchor.local[1], anchor.local[2]).applyMatrix4(mesh2.matrixWorld);
14467
+ normal.set(anchor.localNormal[0], anchor.localNormal[1], anchor.localNormal[2]).transformDirection(mesh2.matrixWorld);
14468
+ return true;
14469
+ }
14470
+
14410
14471
  // src/renderer.ts
14411
14472
  var rendererCount = 0;
14412
14473
  var COLLAPSE_CLOUD_KG = 1e3;
@@ -15100,11 +15161,11 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
15100
15161
  for (const b of this.bounds.values()) b.onFrame?.(dt);
15101
15162
  for (const b of this.bounds.values()) if (b.soilNode && this.world.state(b.id)?.soil?.status === "yielding") this.onSettle(b.id);
15102
15163
  this.settlement.update(dt);
15164
+ this.pieceSys.update(dt);
15165
+ this.fragments.update(dt);
15103
15166
  this.updateActive(dt);
15104
15167
  this.updateLeaks(dt);
15105
15168
  this.updateTrickles(dt);
15106
- this.pieceSys.update(dt);
15107
- this.fragments.update(dt);
15108
15169
  this.ground.update(dt);
15109
15170
  this.ownVfx?.update(dt);
15110
15171
  this.syncTransforms();
@@ -15495,9 +15556,12 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
15495
15556
  break;
15496
15557
  }
15497
15558
  }
15559
+ /** Where the object's root is now, in the world: a body that tipped or fell is there, not where it was bound. */
15498
15560
  rootPoint(objectId) {
15499
15561
  const b = this.bounds.get(objectId);
15500
- return b ? [b.root.position.x, b.root.position.y, b.root.position.z] : [0, 0, 0];
15562
+ if (!b) return [0, 0, 0];
15563
+ const p = b.root.getWorldPosition(new this.THREE.Vector3());
15564
+ return [p.x, p.y, p.z];
15501
15565
  }
15502
15566
  sound(key, point, volume, detail) {
15503
15567
  if (detail) this.o.onSound?.(key, point, volume, detail);
@@ -15663,15 +15727,19 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
15663
15727
  const core = def?.visual?.innerColor;
15664
15728
  if (core !== void 0) this.vfx.burst("chips", ev.point, [normal.x, normal.y, normal.z], 5, 2.5, { color: core });
15665
15729
  else this.playEvent({ kind: "spall", material: this.impactMaterial(def) }, ev.point);
15666
- if (core !== void 0) this.trickleFrom(ev.point, [normal.x, normal.y, normal.z], core);
15730
+ if (core !== void 0) this.trickleFrom(ev.point, [normal.x, normal.y, normal.z], core, mv);
15667
15731
  if (mv.mesh.userData.drawnSpalls?.includes(ev.part)) return;
15668
15732
  this.visuals.addSpall(mv, wp, normal, Math.sqrt(ev.area / Math.PI), core ?? fallbackColor(pv.material, def));
15669
15733
  }
15670
- /** 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. */
15671
- trickleFrom(point, normal, color) {
15734
+ /**
15735
+ * 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
15736
+ * 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.
15737
+ */
15738
+ trickleFrom(point, normal, color, mv) {
15672
15739
  if (this.trickles.length >= TRICKLE_MAX) this.trickles.shift();
15673
15740
  const total = TRICKLE_SECONDS[0] + (TRICKLE_SECONDS[1] - TRICKLE_SECONDS[0]) * this.rng();
15674
- this.trickles.push({ point: [point[0], point[1], point[2]], normal, color, left: total, total, next: TRICKLE_EVERY });
15741
+ const anchor = mv ? anchorOnMesh(this.THREE, mv, point, normal) : null;
15742
+ this.trickles.push({ point: [point[0], point[1], point[2]], normal, anchor, color, left: total, total, next: TRICKLE_EVERY });
15675
15743
  }
15676
15744
  updateTrickles(dt) {
15677
15745
  for (let i = this.trickles.length - 1; i >= 0; i--) {
@@ -15683,6 +15751,14 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
15683
15751
  continue;
15684
15752
  }
15685
15753
  if (t.next > 0) continue;
15754
+ if (t.anchor) {
15755
+ if (!anchorWorld(t.anchor, this.visuals.shown(t.anchor.mv), this.v1, this.v2)) {
15756
+ this.trickles.splice(i, 1);
15757
+ continue;
15758
+ }
15759
+ t.point = [this.v1.x, this.v1.y, this.v1.z];
15760
+ t.normal = [this.v2.x, this.v2.y, this.v2.z];
15761
+ }
15686
15762
  const share = t.left / t.total;
15687
15763
  t.next = TRICKLE_EVERY / Math.max(share, 0.25);
15688
15764
  const out = [t.normal[0] * 0.2, -1, t.normal[2] * 0.2];
@@ -15726,7 +15802,9 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
15726
15802
  const tone = 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);
15727
15803
  this.fragments.spawn(fr, this.fragments.materialFor(fr.material, tone, false), ev.object, maxAlive);
15728
15804
  }
15729
- if (def?.visual?.innerColor !== void 0 || pv?.material === "concrete" || pv?.material === "brick") this.trickleFrom(ev.center, [0, 0, 0], color);
15805
+ if (def?.visual?.innerColor !== void 0 || pv?.material === "concrete" || pv?.material === "brick") {
15806
+ 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);
15807
+ }
15730
15808
  if (ev.torn) {
15731
15809
  this.impactSound(pv?.material ?? "", ev.center, 0.5, ev.blow);
15732
15810
  return;
@@ -15829,6 +15907,41 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
15829
15907
  this.fragments.spawn(fragment, material, objectId, this.maxFragments, true);
15830
15908
  });
15831
15909
  }
15910
+ /**
15911
+ * 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
15912
+ * part is, as it falls, not where the object stood, and does not end because the part has a new id.
15913
+ */
15914
+ followSplit(from, to) {
15915
+ for (const part of to.parts.keys()) {
15916
+ const was = `${from.id}|${part}`;
15917
+ const now = `${to.id}|${part}`;
15918
+ const fire2 = this.fires.get(was);
15919
+ if (fire2) {
15920
+ this.fires.delete(was);
15921
+ fire2.object = to.id;
15922
+ this.fires.set(now, fire2);
15923
+ }
15924
+ const sound = this.burnSounds.get(was);
15925
+ if (sound) {
15926
+ this.burnSounds.delete(was);
15927
+ this.burnSounds.set(now, { ...sound, object: to.id });
15928
+ }
15929
+ if (this.active.delete(was)) this.active.set(now, { object: to.id, part });
15930
+ }
15931
+ from.root.updateWorldMatrix(true, false);
15932
+ to.root.updateWorldMatrix(true, false);
15933
+ const into = new this.THREE.Matrix4().copy(to.root.matrixWorld).invert();
15934
+ for (const leak of this.leaks) {
15935
+ if (leak.object !== from.id || !to.parts.has(leak.part)) continue;
15936
+ leak.object = to.id;
15937
+ const point = leak.localPoint ? this.v1.set(...leak.localPoint).applyMatrix4(from.root.matrixWorld) : this.v1.set(...leak.point);
15938
+ const direction = leak.localDirection ? this.v2.set(...leak.localDirection).transformDirection(from.root.matrixWorld) : this.v2.set(...leak.direction);
15939
+ point.applyMatrix4(into);
15940
+ direction.transformDirection(into);
15941
+ leak.localPoint = [point.x, point.y, point.z];
15942
+ leak.localDirection = [direction.x, direction.y, direction.z];
15943
+ }
15944
+ }
15832
15945
  /** Turn every loose group of the object (a detach, a collapse) into a piece of its own. */
15833
15946
  splitLoose(objectId) {
15834
15947
  if (!this.autoSplit) return;
@@ -15899,6 +16012,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
15899
16012
  }
15900
16013
  this.bounds.set(newId, nb);
15901
16014
  for (const pv of nb.parts.values()) this.tagPart(pv, newId);
16015
+ this.followSplit(from, nb);
15902
16016
  const meshes = this.meshesOf(nb);
15903
16017
  if (meshes.length === 0) return;
15904
16018
  const { half, center } = this.measure(group, meshes);