@adatechnology/game-interactables-three 0.3.7 → 0.4.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/README.md CHANGED
@@ -74,6 +74,13 @@ ones this api made with its own templates before the move (`tests/exact-golden.t
74
74
  merged blind or clothesline is masked and lit like everything else; meshes with different materials are never mixed into one merged mesh.
75
75
  - `createScreen` asks for a `willReadFrequently` 2D context (the canvas is re-uploaded every redraw; this avoids the browser's readback warning).
76
76
 
77
+ ### Solids for the physics (`userData.solids`)
78
+
79
+ Every mesh the mesh api makes carries `userData.solids`: the primitives it was drawn with, one box each, in the mesh's space (`ShapeSolid` of
80
+ `@adatechnology/game-shapes`). Parts joined by `mergeParts` carry the solids of every part, each in its place. `@adatechnology/game-destruction-three` holds the
81
+ mesh by that list, so what a builder draws is what collides (spec 019). `tests/solids-catalogue.test.ts` walks the whole catalogue: every vertex is in exactly
82
+ one solid and inside it.
83
+
77
84
  ### Poses and sync
78
85
 
79
86
  `applyPose(THREE, group, motion, t)` poses a part from its `Motion` (hinge / rocker / knob rotate, slide translates, scale scales along
@@ -342,7 +342,7 @@ void main() {
342
342
  depthWrite: false,
343
343
  blending: additive ? THREE.AdditiveBlending : THREE.NormalBlending
344
344
  });
345
- const points = new THREE.Points(geo, mat);
345
+ const points = markKitOrigin(new THREE.Points(geo, mat), { owner: "game-interactables-three", kind: "effect", id: kind });
346
346
  points.frustumCulled = false;
347
347
  points.name = `effects:${kind}`;
348
348
  scene.add(points);
@@ -461,11 +461,20 @@ void main() {
461
461
  arc.r0 = size * 0.45;
462
462
  arc.r1 = arc.r0 * 1.2;
463
463
  }
464
+ if (e.bowl) {
465
+ const most = Math.max(4e-3, e.bowl.radius * 0.7);
466
+ arc.r0 = Math.min(arc.r0, most);
467
+ arc.r1 = Math.min(arc.r1, most);
468
+ }
464
469
  return true;
465
470
  };
466
471
  const emitWater = (e, k, nSpray, nSplash, nMist) => {
467
472
  const size = e.size > 0 ? e.size : 0.05;
468
- const wide = size >= 0.1;
473
+ const bowl = e.bowl;
474
+ const wide = size >= 0.1 && !bowl;
475
+ const riseMax = bowl ? Math.sqrt(2 * 9.8 * Math.max(0.01, bowl.depth)) * 0.85 : Infinity;
476
+ const outMax = (vy) => bowl ? bowl.radius * 0.6 * 9.8 / (2 * Math.max(vy, 0.3)) : Infinity;
477
+ const within = bowl ? bowl.radius * 0.3 : Infinity;
469
478
  for (let j = 0; j < nSpray; j++) {
470
479
  const s = rand();
471
480
  const t = s * arc.t;
@@ -493,14 +502,21 @@ void main() {
493
502
  const ez = arc.z + arc.vz * arc.t;
494
503
  for (let j = 0; j < nSplash; j++) {
495
504
  const a2 = rand() * Math.PI * 2;
496
- const r = arc.r1 * Math.sqrt(rand());
497
- const sp = (wide ? 0.5 : 0.25) + rand() * 0.7;
498
- spawn(pools.water, ex + Math.cos(a2) * r, arc.stopY + 5e-3, ez + Math.sin(a2) * r, Math.cos(a2) * sp, 0.9 + rand() * 1.3, Math.sin(a2) * sp, 0.3 + rand() * 0.25, 0.014 + rand() * 0.018, 0.01, rand(), 3, arc.stopY);
505
+ const r = Math.min(arc.r1, within) * Math.sqrt(rand());
506
+ const sp0 = (wide ? 0.5 : 0.25) + rand() * 0.7;
507
+ const vy0 = 0.9 + rand() * 1.3;
508
+ const vy = bowl ? riseMax * (0.5 + 0.5 * (vy0 - 0.9) / 1.3) : vy0;
509
+ const sp = Math.min(sp0, outMax(vy));
510
+ spawn(pools.water, ex + Math.cos(a2) * r, arc.stopY + 5e-3, ez + Math.sin(a2) * r, Math.cos(a2) * sp, vy, Math.sin(a2) * sp, 0.3 + rand() * 0.25, 0.014 + rand() * 0.018, 0.01, rand(), 3, arc.stopY);
499
511
  }
500
512
  for (let j = 0; j < nMist; j++) {
501
513
  const a2 = rand() * Math.PI * 2;
502
- const r = arc.r1 * 1.4 * Math.sqrt(rand());
503
- spawn(pools.water, ex + Math.cos(a2) * r, arc.stopY + 0.02, ez + Math.sin(a2) * r, Math.cos(a2) * 0.15, 0.18 + rand() * 0.15, Math.sin(a2) * 0.15, 0.7 + rand() * 0.5, 0.06 + size * 0.2, 0.2 + size * 0.5, rand(), 4, arc.stopY);
514
+ const r = Math.min(arc.r1 * 1.4, within) * Math.sqrt(rand());
515
+ const up0 = 0.18 + rand() * 0.15;
516
+ const life = 0.7 + rand() * 0.5;
517
+ const up = bowl ? Math.min(up0, Math.max(5e-3, bowl.depth - 0.03) / life) : up0;
518
+ const drift = bowl ? Math.min(0.15, bowl.radius * 0.5 / life) : 0.15;
519
+ spawn(pools.water, ex + Math.cos(a2) * r, arc.stopY + 0.02, ez + Math.sin(a2) * r, Math.cos(a2) * drift, up, Math.sin(a2) * drift, life, 0.06 + size * 0.2, 0.2 + size * 0.5, rand(), 4, arc.stopY);
504
520
  }
505
521
  void k;
506
522
  };
@@ -867,7 +883,7 @@ void main() {
867
883
  };
868
884
  }
869
885
 
870
- // ../game-shapes/dist/chunk-E3X7UDDV.js
886
+ // ../game-shapes/dist/chunk-QETSTM6M.js
871
887
  var __defProp2 = Object.defineProperty;
872
888
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
873
889
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
@@ -2539,7 +2555,8 @@ void main() {
2539
2555
  fallbackMaterial: options.fallbackMaterial,
2540
2556
  materialAlias: options.materialAlias,
2541
2557
  instancing: options.instancing,
2542
- levels: options.levels
2558
+ levels: options.levels,
2559
+ ...options.solids ? { solids: true } : {}
2543
2560
  };
2544
2561
  }
2545
2562
  var shapeKey = (schema, options = {}) => hashValue({ schema, options: keyOptions(options) });
@@ -2587,6 +2604,7 @@ void main() {
2587
2604
  __publicField(this, "colors", new FloatList());
2588
2605
  __publicField(this, "indices", new IndexList());
2589
2606
  __publicField(this, "ranges", []);
2607
+ __publicField(this, "solids", []);
2590
2608
  __publicField(this, "vertices", 0);
2591
2609
  }
2592
2610
  };
@@ -2675,7 +2693,62 @@ void main() {
2675
2693
  }
2676
2694
  return { min, max };
2677
2695
  }
2678
- function finish(acc, indexed, withColors) {
2696
+ var templateBoxes = /* @__PURE__ */ new WeakMap();
2697
+ var templateBoxOf = (t) => {
2698
+ const known = templateBoxes.get(t);
2699
+ if (known) return known;
2700
+ const { min, max } = boundsOf(t.positions);
2701
+ const box2 = { center: [(min[0] + max[0]) / 2, (min[1] + max[1]) / 2, (min[2] + max[2]) / 2], half: [(max[0] - min[0]) / 2, (max[1] - min[1]) / 2, (max[2] - min[2]) / 2] };
2702
+ templateBoxes.set(t, box2);
2703
+ return box2;
2704
+ };
2705
+ var SQUARE = 1e-5;
2706
+ var quaternionOfAxes = (x, y, z) => {
2707
+ const [m00, m10, m20] = x;
2708
+ const [m01, m11, m21] = y;
2709
+ const [m02, m12, m22] = z;
2710
+ const trace = m00 + m11 + m22;
2711
+ if (trace > 0) {
2712
+ const s2 = 0.5 / Math.sqrt(trace + 1);
2713
+ return [(m21 - m12) * s2, (m02 - m20) * s2, (m10 - m01) * s2, 0.25 / s2];
2714
+ }
2715
+ if (m00 > m11 && m00 > m22) {
2716
+ const s2 = 2 * Math.sqrt(1 + m00 - m11 - m22);
2717
+ return [0.25 * s2, (m01 + m10) / s2, (m02 + m20) / s2, (m21 - m12) / s2];
2718
+ }
2719
+ if (m11 > m22) {
2720
+ const s2 = 2 * Math.sqrt(1 + m11 - m00 - m22);
2721
+ return [(m01 + m10) / s2, 0.25 * s2, (m12 + m21) / s2, (m02 - m20) / s2];
2722
+ }
2723
+ const s = 2 * Math.sqrt(1 + m22 - m00 - m11);
2724
+ return [(m02 + m20) / s, (m12 + m21) / s, 0.25 * s, (m10 - m01) / s];
2725
+ };
2726
+ function solidOf(kind, t, world, vertexStart, vertexCount) {
2727
+ const box2 = templateBoxOf(t);
2728
+ const e = world;
2729
+ const center = transformPoint(e, box2.center[0], box2.center[1], box2.center[2]);
2730
+ const axes = [[e[0], e[1], e[2]], [e[4], e[5], e[6]], [e[8], e[9], e[10]]];
2731
+ const lengths = axes.map((a) => Math.hypot(a[0], a[1], a[2]));
2732
+ const dot2 = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
2733
+ const square = (i, j) => Math.abs(dot2(axes[i], axes[j])) <= SQUARE * lengths[i] * lengths[j];
2734
+ if (lengths.every((l) => l > 0) && square(0, 1) && square(0, 2) && square(1, 2)) {
2735
+ const [x, y, z] = axes.map((a, k) => [a[0] / lengths[k], a[1] / lengths[k], a[2] / lengths[k]]);
2736
+ const handed = x[0] * (y[1] * z[2] - y[2] * z[1]) - x[1] * (y[0] * z[2] - y[2] * z[0]) + x[2] * (y[0] * z[1] - y[1] * z[0]);
2737
+ const third = handed < 0 ? [-z[0], -z[1], -z[2]] : z;
2738
+ return { kind, center, half: [box2.half[0] * lengths[0], box2.half[1] * lengths[1], box2.half[2] * lengths[2]], rotation: quaternionOfAxes(x, y, third), vertexStart, vertexCount };
2739
+ }
2740
+ const low = [Infinity, Infinity, Infinity];
2741
+ const high = [-Infinity, -Infinity, -Infinity];
2742
+ for (let corner = 0; corner < 8; corner++) {
2743
+ const p = transformPoint(e, box2.center[0] + (corner & 1 ? box2.half[0] : -box2.half[0]), box2.center[1] + (corner & 2 ? box2.half[1] : -box2.half[1]), box2.center[2] + (corner & 4 ? box2.half[2] : -box2.half[2]));
2744
+ for (let k = 0; k < 3; k++) {
2745
+ low[k] = Math.min(low[k], p[k]);
2746
+ high[k] = Math.max(high[k], p[k]);
2747
+ }
2748
+ }
2749
+ return { kind, center: [(low[0] + high[0]) / 2, (low[1] + high[1]) / 2, (low[2] + high[2]) / 2], half: [(high[0] - low[0]) / 2, (high[1] - low[1]) / 2, (high[2] - low[2]) / 2], rotation: [0, 0, 0, 1], vertexStart, vertexCount, fitted: true };
2750
+ }
2751
+ function finish(acc, indexed, withColors, withSolids) {
2679
2752
  const positions = acc.positions.done();
2680
2753
  const indices = indexed ? acc.vertices > 65535 ? acc.indices.array.slice(0, acc.indices.length) : Uint16Array.from(acc.indices.array.subarray(0, acc.indices.length)) : void 0;
2681
2754
  return {
@@ -2687,7 +2760,8 @@ void main() {
2687
2760
  ...withColors ? { colors: acc.colors.done() } : {},
2688
2761
  ...indices ? { indices } : {},
2689
2762
  ranges: acc.ranges,
2690
- bounds: boundsOf(positions)
2763
+ bounds: boundsOf(positions),
2764
+ ...withSolids ? { solids: acc.solids } : {}
2691
2765
  };
2692
2766
  }
2693
2767
  var radiusOf = (t, world) => {
@@ -2833,7 +2907,10 @@ void main() {
2833
2907
  return;
2834
2908
  }
2835
2909
  }
2836
- bake(accOf(target, next.material), template, m, rgbOf(next.color), next.uv, settings.indexed);
2910
+ const acc = accOf(target, next.material);
2911
+ const first = acc.vertices;
2912
+ bake(acc, template, m, rgbOf(next.color), next.uv, settings.indexed);
2913
+ if (settings.solids) acc.solids.push(solidOf(node.kind, template, m, first, acc.vertices - first));
2837
2914
  }
2838
2915
  if (snapshot) {
2839
2916
  for (const [material, acc] of target.accs) {
@@ -2866,7 +2943,7 @@ void main() {
2866
2943
  if (colors) colors.set(colorChannels(colorHex(instance.color), space ?? "srgb"), index * 3);
2867
2944
  });
2868
2945
  for (const acc of collector.accs.values()) {
2869
- const batch = finish(acc, settings.indexed, false);
2946
+ const batch = finish(acc, settings.indexed, false, false);
2870
2947
  instanced.push({
2871
2948
  material: acc.material,
2872
2949
  part: collector.part,
@@ -2895,7 +2972,7 @@ void main() {
2895
2972
  });
2896
2973
  }
2897
2974
  const batches = [];
2898
- for (const target of targets) for (const acc of target.accs.values()) if (acc.vertices > 0 && (!settings.indexed || acc.indices.length > 0)) batches.push(finish(acc, settings.indexed, space !== null));
2975
+ for (const target of targets) for (const acc of target.accs.values()) if (acc.vertices > 0 && (!settings.indexed || acc.indices.length > 0)) batches.push(finish(acc, settings.indexed, space !== null, settings.solids));
2899
2976
  return { level: { distance: level.distance, detail: level.detail, batches, instanced }, parts, points, primitives };
2900
2977
  }
2901
2978
  function generate(schema, options = {}) {
@@ -2909,7 +2986,8 @@ void main() {
2909
2986
  seed: options.seed ?? 0,
2910
2987
  ...options.generators ? { generators: options.generators } : {},
2911
2988
  ...options.isolatedGenerators ? { isolatedGenerators: true } : {},
2912
- ...options.instancing ? { autoMin: Math.max(2, options.instancing.minCount) } : {}
2989
+ ...options.instancing ? { autoMin: Math.max(2, options.instancing.minCount) } : {},
2990
+ solids: options.solids === true
2913
2991
  };
2914
2992
  const specs = [{ distance: 0, detail: 1, index: 0 }, ...(schema.lod ?? []).map((rule, index) => ({ ...rule, index: index + 1 }))];
2915
2993
  const wanted = options.levels ? specs.filter((spec) => options.levels.includes(spec.index)) : specs;
@@ -3204,6 +3282,9 @@ void main() {
3204
3282
  list2.push({ kind: "cylinder", radiusTop: 1, radiusBottom: 1, height: 1, radialSegments: cyl, ...place });
3205
3283
  }
3206
3284
  },
3285
+ lathe(M, material, x, y, z, points, color2) {
3286
+ into(M).push({ kind: "lathe", material, color: color2, position: [x, y, z], points: points.map(([r, py]) => [r, py]), segments: cyl });
3287
+ },
3207
3288
  leaf(M, x, y, z, w, h, color2, ry, rx) {
3208
3289
  into(M).push({ kind: "mesh", ...LEAF, material: "plant", color: color2, position: [x, y, z], rotation: [rx, ry, 0], rotationOrder: "YXZ", scale: [w, h, 1] });
3209
3290
  },
@@ -3443,6 +3524,32 @@ vec3 batchRotate(vec4 q, vec3 v) { return v + 2.0 * cross(q.xyz, cross(q.xyz, v)
3443
3524
  out.computeBoundingBox();
3444
3525
  return out;
3445
3526
  }
3527
+ function mergedSolids(THREE, pieces) {
3528
+ if (pieces.some((p) => !p.solids)) return void 0;
3529
+ const out = [];
3530
+ const position = new THREE.Vector3();
3531
+ const turn = new THREE.Quaternion();
3532
+ const scale = new THREE.Vector3();
3533
+ const centre = new THREE.Vector3();
3534
+ const own = new THREE.Quaternion();
3535
+ let first = 0;
3536
+ for (const p of pieces) {
3537
+ p.matrix.decompose(position, turn, scale);
3538
+ for (const solid of p.solids ?? []) {
3539
+ centre.set(solid.center[0], solid.center[1], solid.center[2]).applyMatrix4(p.matrix);
3540
+ own.set(solid.rotation[0], solid.rotation[1], solid.rotation[2], solid.rotation[3]).premultiply(turn).normalize();
3541
+ out.push({
3542
+ ...solid,
3543
+ center: [centre.x, centre.y, centre.z],
3544
+ half: [solid.half[0] * Math.abs(scale.x), solid.half[1] * Math.abs(scale.y), solid.half[2] * Math.abs(scale.z)],
3545
+ rotation: [own.x, own.y, own.z, own.w],
3546
+ vertexStart: solid.vertexStart + first
3547
+ });
3548
+ }
3549
+ first += p.geometry.getAttribute("position").count;
3550
+ }
3551
+ return out;
3552
+ }
3446
3553
  function meshesOf(group) {
3447
3554
  return group.children.filter((c) => c.isMesh);
3448
3555
  }
@@ -3472,15 +3579,17 @@ vec3 batchRotate(vec4 q, vec3 v) { return v + 2.0 * cross(q.xyz, cross(q.xyz, v)
3472
3579
  e = { base: mesh2.material, pieces: [], mesh: mesh2 };
3473
3580
  map.set(key, e);
3474
3581
  }
3475
- e.pieces.push({ geometry: mesh2.geometry, matrix: g.matrix, extra: extra(g, mesh2.geometry.getAttribute("position").count) });
3582
+ e.pieces.push({ geometry: mesh2.geometry, matrix: g.matrix, extra: extra(g, mesh2.geometry.getAttribute("position").count), solids: mesh2.userData.solids });
3476
3583
  }
3477
3584
  }
3478
3585
  return map;
3479
3586
  };
3480
- const finishMesh = (name, key, geometry, template, set, group) => {
3481
- const mesh2 = new THREE.Mesh(geometry, set.material);
3587
+ const finishMesh = (name, key, pieces, extraSizes, template, set, group) => {
3588
+ const mesh2 = new THREE.Mesh(concat(THREE, pieces, extraSizes), set.material);
3482
3589
  mesh2.name = `${name}:${key}`;
3483
3590
  mesh2.userData.material = key;
3591
+ const solids = mergedSolids(THREE, pieces);
3592
+ if (solids) mesh2.userData.solids = solids;
3484
3593
  mesh2.castShadow = template.castShadow;
3485
3594
  mesh2.receiveShadow = template.receiveShadow;
3486
3595
  mesh2.customDepthMaterial = set.depth;
@@ -3534,7 +3643,7 @@ uniform vec2 swayAxz[${n}];
3534
3643
  for (const e of plan.values()) {
3535
3644
  const key = String(e.mesh.userData.material);
3536
3645
  const set = patchedSet(THREE, e.base, `swayBatch:${n}:${amplitude}`, patch2);
3537
- finishMesh("merge:cloth", key, concat(THREE, e.pieces, { swayId: 1, swayAnchor: 1 }), e.mesh, set, merged);
3646
+ finishMesh("merge:cloth", key, e.pieces, { swayId: 1, swayAnchor: 1 }, e.mesh, set, merged);
3538
3647
  }
3539
3648
  const batch = {
3540
3649
  kind: "sway",
@@ -3590,7 +3699,7 @@ ${QROT}${shader.vertexShader}`;
3590
3699
  for (const e of plan.values()) {
3591
3700
  const key = String(e.mesh.userData.material);
3592
3701
  const set = patchedSet(THREE, e.base, "poseBatch", patch2);
3593
- finishMesh(merged.name, key, concat(THREE, e.pieces, { batchPivot: 3 }), e.mesh, set, merged);
3702
+ finishMesh(merged.name, key, e.pieces, { batchPivot: 3 }, e.mesh, set, merged);
3594
3703
  }
3595
3704
  const batch = {
3596
3705
  kind: "pose",
@@ -3924,6 +4033,7 @@ ${QROT}${shader.vertexShader}`;
3924
4033
  mesh2.name = id === null ? `static:${key}` : `part:${id}`;
3925
4034
  if (id !== null) mesh2.userData.part = id;
3926
4035
  mesh2.userData.material = key;
4036
+ if (batch.solids) mesh2.userData.solids = batch.solids;
3927
4037
  mesh2.castShadow = shadows;
3928
4038
  mesh2.receiveShadow = shadows;
3929
4039
  into.add(mesh2);
@@ -3957,7 +4067,8 @@ ${QROT}${shader.vertexShader}`;
3957
4067
  fallbackMaterial: "plain",
3958
4068
  materialAlias,
3959
4069
  validate: false,
3960
- key: false
4070
+ key: false,
4071
+ solids: true
3961
4072
  });
3962
4073
  const batches = buffers.levels[0]?.batches ?? [];
3963
4074
  const of = (id) => ({ batches: batches.filter((b) => b.part === id), points: buffers.points.filter((p) => p.part === id) });