@adatechnology/game-interactables-three 0.6.3 → 0.8.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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ActiveEmitter, ObjectSpec, Motion, World, ItemStack, SpanShape, Span, ObjectControlsState } from '@adatechnology/game-interactables';
1
+ import { ActiveEmitter, ObjectSpec, Motion, World, ItemStack, ContainerSpec, SlotSpec, SpanShape, Span, ObjectControlsState } from '@adatechnology/game-interactables';
2
2
  import { LightBudget, SourceHandle } from '@adatechnology/game-render-three';
3
3
  import * as ThreeNS from 'three';
4
4
  import { PropsApi } from '@adatechnology/game-props3d';
@@ -453,11 +453,32 @@ interface ContentsView {
453
453
  /** Discards every item (`discardObject3D`) and the group. */
454
454
  dispose(): void;
455
455
  }
456
+ /**
457
+ * Puts a drawn item (origin at the bottom centre, y up) on a slot: turned by `yaw` about y, and laid on its side when the slot says (`SlotSpec.lay`), with the
458
+ * middle of what then touches the slot at `at`. Exported for the hosts that draw contents of their own, so every room lays the same item the same way.
459
+ */
460
+ declare function placeOnSlot(THREE: ThreeLib, object: ThreeNS.Object3D, at: readonly [number, number, number], yaw?: number, lay?: SlotSpec['lay']): void;
456
461
  /**
457
462
  * The contents of an object's containers as real items. Each slot on a part (a drawer, a door shelf) hangs under a carrier posed with the engine's own pose of
458
463
  * that part, so the items slide or swing with it from the first frame of the motion and `world.slotPosition` always agrees; nothing is built when the motion ends.
459
464
  */
460
465
  declare function createContentsView(THREE: ThreeLib, options: ContentsViewOptions): ContentsView;
466
+ /** What a host without a world (a furniture room) draws in a piece's containers: `drawSlotContents`. */
467
+ interface SlotContentsOptions {
468
+ /** The real model of each stack (the items catalogue's renderer, wear and all), null for an id it does not have. */
469
+ readonly buildItem: ContentsItemBuilder;
470
+ /** What fills the slots the piece leaves free (a pool rolled for a fridge): one stack or null per free slot, in their order. Left out, they stay empty. */
471
+ readonly roll?: (container: ContainerSpec, free: readonly SlotSpec[]) => ReadonlyArray<ItemStack | null>;
472
+ }
473
+ /**
474
+ * Draws the items in a piece's containers for a host that has no interactables world: first what the piece always comes with (`ContainerSpec.contents`, the soap on
475
+ * a washbasin's counter), slot by slot, then what `roll` puts in the slots left free. Each item is laid on its slot like `createContentsView` lays it
476
+ * (`placeOnSlot`), named `contents_<container>_<n>` (n its place among the slots that do not ride a part, which are left out) and tagged
477
+ * `userData.contents = { container, slot, item, qty, data? }`, `slot` its index in the container: the host takes it, and reads its data (a dish's dirt), by those.
478
+ */
479
+ declare function drawSlotContents(THREE: ThreeLib, group: ThreeNS.Object3D, spec: {
480
+ readonly containers?: readonly ContainerSpec[];
481
+ }, options: SlotContentsOptions): ThreeNS.Object3D[];
461
482
 
462
483
  type Wire = NonNullable<ObjectSpec['wires']>[number];
463
484
  /** The colour each kind of wire is drawn in (a grey PVC conduit, a black cable, a dark cord). */
@@ -513,4 +534,4 @@ interface ControlsView {
513
534
  }
514
535
  declare function createControlsView(THREE: ThreeLib, options: ControlsViewOptions): ControlsView;
515
536
 
516
- export { type CanvasFactory, type ContentsItemBuilder, type ContentsView, type ContentsViewItem, type ContentsViewOptions, type ControlsView, type ControlsViewOptions, type Effects, type EffectsOptions, type EffectsStats, type FinishOptions, type HouseholdApi, MATERIAL_KEYS, type MaterialKey, type MaterialMap, type MergeOptions, type MergedGroup, type MeshApi, type MeshApiOptions, type MeshResult, type MirrorCandidate, type MirrorOptions, type Mirrors, type PerspectiveLike, type PoolKind, type Pose, type PoseBatch, type Quality, type ReflectorConstructor, type Screen, type ScreenOptions, type SpanWire, type SpanWireOptions, type SwayBatch, type SwayHandle, type SwayOptions, type SwayUniforms, type SyncOptions, type ThreeAdapter, type ThreeAdapterOptions, type ThreeLib, type Vec3, WIRE_COLORS, WIRE_RADIUS, applyPose, attachSway, createContentsView, createControlsView, createDefaultMaterials, createEffects, createMeshApi, createMirrors, createScreen, createSpanWire, createThreeAdapter, mergeParts, patchSwayShader, poseOf, rememberRest, standOutCoincidentBoxes, swayMaterial, syncObject, wireMeshes };
537
+ export { type CanvasFactory, type ContentsItemBuilder, type ContentsView, type ContentsViewItem, type ContentsViewOptions, type ControlsView, type ControlsViewOptions, type Effects, type EffectsOptions, type EffectsStats, type FinishOptions, type HouseholdApi, MATERIAL_KEYS, type MaterialKey, type MaterialMap, type MergeOptions, type MergedGroup, type MeshApi, type MeshApiOptions, type MeshResult, type MirrorCandidate, type MirrorOptions, type Mirrors, type PerspectiveLike, type PoolKind, type Pose, type PoseBatch, type Quality, type ReflectorConstructor, type Screen, type ScreenOptions, type SlotContentsOptions, type SpanWire, type SpanWireOptions, type SwayBatch, type SwayHandle, type SwayOptions, type SwayUniforms, type SyncOptions, type ThreeAdapter, type ThreeAdapterOptions, type ThreeLib, type Vec3, WIRE_COLORS, WIRE_RADIUS, applyPose, attachSway, createContentsView, createControlsView, createDefaultMaterials, createEffects, createMeshApi, createMirrors, createScreen, createSpanWire, createThreeAdapter, drawSlotContents, mergeParts, patchSwayShader, placeOnSlot, poseOf, rememberRest, standOutCoincidentBoxes, swayMaterial, syncObject, wireMeshes };
@@ -37,8 +37,10 @@ var GameInteractablesThree = (() => {
37
37
  createScreen: () => createScreen,
38
38
  createSpanWire: () => createSpanWire,
39
39
  createThreeAdapter: () => createThreeAdapter,
40
+ drawSlotContents: () => drawSlotContents,
40
41
  mergeParts: () => mergeParts,
41
42
  patchSwayShader: () => patchSwayShader,
43
+ placeOnSlot: () => placeOnSlot,
42
44
  poseOf: () => poseOf2,
43
45
  rememberRest: () => rememberRest,
44
46
  standOutCoincidentBoxes: () => standOutCoincidentBoxes,
@@ -84,6 +86,8 @@ var GameInteractablesThree = (() => {
84
86
  [DEFERRED_QUEUE_ID.DESTRUCTION_PREFIT]: { priority: 40, phase: "idle", minEveryFrames: 30 }
85
87
  };
86
88
  var MUST_DRAIN_QUEUE_IDS = /* @__PURE__ */ new Set([DEFERRED_QUEUE_ID.DESTRUCTION_EVENTS, DEFERRED_QUEUE_ID.BREACH_CUT]);
89
+ var BODY_FOAM_SURFACES = ["hands", "arms", "torso", "hair", "mouth"];
90
+ var WASH_SURFACES = [...BODY_FOAM_SURFACES, "brush"];
87
91
  var WARNED_KEY = /* @__PURE__ */ Symbol.for("@adatechnology/game-contracts.warned");
88
92
  var warnedKeys = () => {
89
93
  const holder = globalThis;
@@ -3069,13 +3073,16 @@ void main() {
3069
3073
  for (const u of Object.values(uniforms)) if (isTexture(u?.value)) out.add(u.value);
3070
3074
  }
3071
3075
  }
3076
+ var isInstanced = (o) => o.isInstancedMesh === true;
3072
3077
  function collect(roots) {
3073
3078
  const geometries = /* @__PURE__ */ new Set();
3074
3079
  const materials = /* @__PURE__ */ new Set();
3075
3080
  const textures = /* @__PURE__ */ new Set();
3076
3081
  const skeletons = /* @__PURE__ */ new Set();
3082
+ const instanced = /* @__PURE__ */ new Set();
3077
3083
  for (const root of roots) {
3078
3084
  root.traverse((object2) => {
3085
+ if (isInstanced(object2)) instanced.add(object2);
3079
3086
  const o = object2;
3080
3087
  if (o.geometry && typeof o.geometry.dispose === "function") geometries.add(o.geometry);
3081
3088
  if (o.skeleton && typeof o.skeleton.dispose === "function") skeletons.add(o.skeleton);
@@ -3087,7 +3094,7 @@ void main() {
3087
3094
  }
3088
3095
  });
3089
3096
  }
3090
- return { geometries, materials, textures, skeletons };
3097
+ return { geometries, materials, textures, skeletons, instanced };
3091
3098
  }
3092
3099
  var marked = /* @__PURE__ */ new WeakSet();
3093
3100
  var shared = (r) => r.userData?.shared === true || marked.has(r);
@@ -3132,6 +3139,7 @@ void main() {
3132
3139
  report.textures++;
3133
3140
  }
3134
3141
  for (const s of found.skeletons) s.dispose();
3142
+ for (const mesh2 of found.instanced) mesh2.dispose();
3135
3143
  return report;
3136
3144
  }
3137
3145
  function discardObject3D(objects, options = {}) {
@@ -4552,6 +4560,24 @@ uniform float swayAmplitude;
4552
4560
  }
4553
4561
 
4554
4562
  // src/contents.ts
4563
+ var LAY_TURN = { x: [0, 0, -Math.PI / 2], z: [Math.PI / 2, 0, 0] };
4564
+ function placeOnSlot(THREE, object2, at, yaw = 0, lay) {
4565
+ object2.position.set(0, 0, 0);
4566
+ object2.rotation.set(0, yaw, 0);
4567
+ if (!lay) {
4568
+ object2.position.set(at[0], at[1], at[2]);
4569
+ return;
4570
+ }
4571
+ const [rx, ry, rz] = LAY_TURN[lay];
4572
+ object2.quaternion.multiply(new THREE.Quaternion().setFromEuler(new THREE.Euler(rx, ry, rz)));
4573
+ const parent = object2.parent;
4574
+ parent?.remove(object2);
4575
+ object2.updateMatrixWorld(true);
4576
+ const box2 = new THREE.Box3().setFromObject(object2);
4577
+ parent?.add(object2);
4578
+ const middle = box2.getCenter(new THREE.Vector3());
4579
+ object2.position.set(at[0] - middle.x, at[1] - box2.min.y, at[2] - middle.z);
4580
+ }
4555
4581
  function createContentsView(THREE, options) {
4556
4582
  const { world, objectId, root, buildItem } = options;
4557
4583
  const group = new THREE.Group();
@@ -4575,19 +4601,18 @@ uniform float swayAmplitude;
4575
4601
  }
4576
4602
  return carrier;
4577
4603
  };
4578
- const place = (object2, carrier, pos, part) => {
4604
+ const place = (object2, carrier, slot, part) => {
4579
4605
  carrier.add(object2);
4580
4606
  if (!part) {
4581
- object2.position.set(pos[0], pos[1], pos[2]);
4607
+ placeOnSlot(THREE, object2, slot.pos, 0, slot.lay);
4582
4608
  return;
4583
4609
  }
4584
4610
  const ry = part.ry ?? 0;
4585
- const x = pos[0] - part.pivot[0];
4586
- const z = pos[2] - part.pivot[2];
4611
+ const x = slot.pos[0] - part.pivot[0];
4612
+ const z = slot.pos[2] - part.pivot[2];
4587
4613
  const c = Math.cos(ry);
4588
4614
  const s = Math.sin(ry);
4589
- object2.position.set(c * x - s * z, pos[1] - part.pivot[1], s * x + c * z);
4590
- object2.rotation.set(0, -ry, 0);
4615
+ placeOnSlot(THREE, object2, [c * x - s * z, slot.pos[1] - part.pivot[1], s * x + c * z], -ry, slot.lay);
4591
4616
  };
4592
4617
  function sync() {
4593
4618
  const live = /* @__PURE__ */ new Set();
@@ -4609,8 +4634,8 @@ uniform float swayAmplitude;
4609
4634
  const object2 = buildItem(stack, THREE);
4610
4635
  if (!object2) return;
4611
4636
  object2.name = `contents_${c.id}_${i}`;
4612
- object2.userData.contents = { object: objectId, container: c.id, slot: i, item: stack.type, qty: stack.qty };
4613
- place(object2, carrierOf(slot.part), slot.pos, slot.part === void 0 ? void 0 : partsById.get(slot.part));
4637
+ object2.userData.contents = { object: objectId, container: c.id, slot: i, item: stack.type, qty: stack.qty, ...stack.data ? { data: stack.data } : {} };
4638
+ place(object2, carrierOf(slot.part), slot, slot.part === void 0 ? void 0 : partsById.get(slot.part));
4614
4639
  now = { type: stack.type, object: object2 };
4615
4640
  drawn.set(key, now);
4616
4641
  }
@@ -4641,6 +4666,33 @@ uniform float swayAmplitude;
4641
4666
  }
4642
4667
  };
4643
4668
  }
4669
+ function drawSlotContents(THREE, group, spec, options) {
4670
+ const drawn = [];
4671
+ const draw = (container, slot, index, at, stack) => {
4672
+ const object2 = options.buildItem(stack, THREE);
4673
+ if (!object2) return;
4674
+ group.add(object2);
4675
+ placeOnSlot(THREE, object2, slot.pos, 0, slot.lay);
4676
+ object2.name = `contents_${container.id}_${at}`;
4677
+ object2.userData.contents = { container: container.id, slot: index, item: stack.type, qty: stack.qty, ...stack.data ? { data: stack.data } : {} };
4678
+ drawn.push(object2);
4679
+ };
4680
+ for (const container of spec.containers ?? []) {
4681
+ const slots = container.slots.map((slot, index) => ({ slot, index })).filter(({ slot }) => !slot.part);
4682
+ const fixed = container.contents ?? [];
4683
+ slots.forEach(({ slot, index }, at) => {
4684
+ const stack = fixed[index];
4685
+ if (stack) draw(container, slot, index, at, stack);
4686
+ });
4687
+ const free = slots.map((entry, at) => ({ ...entry, at })).filter(({ index }) => !fixed[index]);
4688
+ if (!options.roll || free.length === 0) continue;
4689
+ options.roll(container, free.map(({ slot }) => slot)).forEach((stack, n) => {
4690
+ const entry = free[n];
4691
+ if (stack && entry) draw(container, entry.slot, entry.index, entry.at, stack);
4692
+ });
4693
+ }
4694
+ return drawn;
4695
+ }
4644
4696
 
4645
4697
  // src/wires.ts
4646
4698
  var WIRE_COLORS = { conduit: 13618628, cable: 2762532, drop: 1381653, cord: 1776413 };
@@ -4756,6 +4808,10 @@ uniform float swayAmplitude;
4756
4808
  var FECES = 5913116;
4757
4809
  var LIFT = 2e-3;
4758
4810
  var SOIL_DEPTH = 0.03;
4811
+ var PAPER = 15921386;
4812
+ var MAX_CONTENTS = 12;
4813
+ var SWIRL_STREAKS = 3;
4814
+ var SWIRL_ARC = Math.PI * 0.9;
4759
4815
  function screensOf(THREE, options, screens) {
4760
4816
  return (options.spec.devices ?? []).flatMap((device) => {
4761
4817
  if (device.kind !== "screen" || !device.display) return [];
@@ -4792,7 +4848,7 @@ uniform float swayAmplitude;
4792
4848
  });
4793
4849
  }
4794
4850
  function soilsOf(THREE, spec) {
4795
- return (spec.basins ?? []).filter((basin) => basin.soils).map((basin) => {
4851
+ return (spec.basins ?? []).filter((basin) => basin.soils && !basin.drain).map((basin) => {
4796
4852
  const radius = Math.min(basin.to[0] - basin.from[0], basin.to[2] - basin.from[2]) * 0.3;
4797
4853
  const lump = new THREE.Mesh(new THREE.SphereGeometry(radius, 12, 8), new THREE.MeshStandardMaterial({ color: FECES, roughness: 0.95 }));
4798
4854
  const tint = new THREE.Mesh(new THREE.CylinderGeometry(radius * 1.6, radius * 1.6, 4e-3, 20), new THREE.MeshStandardMaterial({ color: URINE, transparent: true, opacity: 0.55 }));
@@ -4813,12 +4869,85 @@ uniform float swayAmplitude;
4813
4869
  return { object: soil, apply: apply2 };
4814
4870
  });
4815
4871
  }
4872
+ function contentMesh(THREE, kind) {
4873
+ if (kind === "paper") return new THREE.Mesh(new THREE.SphereGeometry(0.022, 8, 6), new THREE.MeshStandardMaterial({ color: PAPER, roughness: 1 }));
4874
+ if (kind === "urine") return new THREE.Mesh(new THREE.CircleGeometry(1, 20), new THREE.MeshStandardMaterial({ color: URINE, transparent: true, opacity: 0.45, depthWrite: false }));
4875
+ const lump = new THREE.Mesh(new THREE.CapsuleGeometry(0.016, 0.07, 4, 8), new THREE.MeshStandardMaterial({ color: FECES, roughness: 0.95 }));
4876
+ return lump;
4877
+ }
4878
+ function drainOf(THREE, basin) {
4879
+ const group = new THREE.Group();
4880
+ group.name = `drain_${basin.id}`;
4881
+ const cx = (basin.from[0] + basin.to[0]) / 2;
4882
+ const cz = (basin.from[2] + basin.to[2]) / 2;
4883
+ const radius = Math.min(basin.to[0] - basin.from[0], basin.to[2] - basin.from[2]) / 2;
4884
+ const pool = /* @__PURE__ */ new Map();
4885
+ const take = (kind, used2) => {
4886
+ const list2 = pool.get(kind) ?? [];
4887
+ pool.set(kind, list2);
4888
+ const at = used2.get(kind) ?? 0;
4889
+ used2.set(kind, at + 1);
4890
+ let mesh2 = list2[at];
4891
+ if (!mesh2) {
4892
+ mesh2 = contentMesh(THREE, kind);
4893
+ mesh2.name = `content_${kind}_${at}`;
4894
+ list2.push(mesh2);
4895
+ group.add(mesh2);
4896
+ }
4897
+ mesh2.visible = true;
4898
+ return mesh2;
4899
+ };
4900
+ const streakMaterial = new THREE.MeshBasicMaterial({ color: 15398136, transparent: true, opacity: 0, depthWrite: false, side: THREE.DoubleSide });
4901
+ const streaks = Array.from({ length: SWIRL_STREAKS }, (_, i) => {
4902
+ const inner = radius * (0.15 + i * 0.22);
4903
+ const streak = new THREE.Mesh(new THREE.RingGeometry(inner, inner + radius * 0.1, 24, 1, 0, SWIRL_ARC), streakMaterial);
4904
+ streak.rotation.x = -Math.PI / 2;
4905
+ streak.name = `swirl_${i}`;
4906
+ group.add(streak);
4907
+ return streak;
4908
+ });
4909
+ const used = /* @__PURE__ */ new Map();
4910
+ let drawn;
4911
+ const apply2 = (state) => {
4912
+ const drain = state.drains?.[basin.id];
4913
+ if (drain === drawn) return;
4914
+ drawn = drain;
4915
+ for (const list2 of pool.values()) for (const mesh2 of list2) mesh2.visible = false;
4916
+ if (!drain) return;
4917
+ const surface = basin.from[1] + drain.level;
4918
+ streakMaterial.opacity = Math.min(0.55, drain.swirl * 0.7);
4919
+ for (let i = 0; i < streaks.length; i++) {
4920
+ const streak = streaks[i];
4921
+ streak.visible = drain.swirl > 0.05;
4922
+ streak.position.set(cx, surface + LIFT, cz);
4923
+ streak.rotation.z = drain.turned * (1 + (SWIRL_STREAKS - 1 - i) * 0.3) + i * Math.PI * 2 / SWIRL_STREAKS;
4924
+ }
4925
+ used.clear();
4926
+ for (let i = 0; i < Math.min(MAX_CONTENTS, drain.contents.length); i++) {
4927
+ const c = drain.contents[i];
4928
+ if (c.kind === "object") continue;
4929
+ const mesh2 = take(c.kind, used);
4930
+ const y = surface + (basin.from[1] - surface) * c.depth;
4931
+ const r = radius * 0.8 * c.radius;
4932
+ if (c.kind === "urine") {
4933
+ mesh2.scale.setScalar(radius * 0.9);
4934
+ mesh2.rotation.set(-Math.PI / 2, 0, 0);
4935
+ mesh2.position.set(cx, surface + LIFT * 2, cz);
4936
+ continue;
4937
+ }
4938
+ mesh2.position.set(cx + Math.cos(c.angle) * r, y - (c.depth === 0 ? 6e-3 : 0), cz + Math.sin(c.angle) * r);
4939
+ mesh2.rotation.set(Math.PI / 2, 0, c.angle);
4940
+ }
4941
+ };
4942
+ return { object: group, apply: apply2 };
4943
+ }
4816
4944
  function createControlsView(THREE, options) {
4817
4945
  const group = new THREE.Group();
4818
4946
  group.name = "controls-view";
4819
4947
  options.root.add(group);
4820
4948
  const screens = [];
4821
- const drawn = [...screensOf(THREE, options, screens), ...bowlsOf(THREE, options.spec), ...soilsOf(THREE, options.spec)];
4949
+ const drained = (options.spec.basins ?? []).filter((basin) => basin.drain).map((basin) => drainOf(THREE, basin));
4950
+ const drawn = [...screensOf(THREE, options, screens), ...bowlsOf(THREE, options.spec), ...soilsOf(THREE, options.spec), ...drained];
4822
4951
  for (const d of drawn) group.add(d.object);
4823
4952
  let disposed = false;
4824
4953
  return {