@adatechnology/game-interactables-three 0.2.18 → 0.3.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 } from '@adatechnology/game-interactables';
1
+ import { ActiveEmitter, ObjectSpec, Motion, World, ItemStack } 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';
@@ -423,6 +423,42 @@ type MergedGroup = ThreeNS.Group & {
423
423
  */
424
424
  declare function mergeParts(THREE: ThreeLib, root: ThreeNS.Group, parts: Record<string, ThreeNS.Group>, spec: ObjectSpec, options?: MergeOptions): void;
425
425
 
426
+ /** Draws one stack as the real item with the host's `THREE`: origin at the bottom centre, y up, metres (`createContainedItemModel` of `game-items-three`). Null draws nothing. */
427
+ type ContentsItemBuilder = (stack: ItemStack, THREE: ThreeLib) => ThreeNS.Object3D | null;
428
+ interface ContentsViewOptions {
429
+ world: World;
430
+ objectId: string;
431
+ /** The object's group, in the object-local frame (the `group` of `createMeshApi().finish`). The contents hang under it. */
432
+ root: ThreeNS.Object3D;
433
+ /** The real model of each stack; the host injects it (the items catalogue's renderer), so this package never draws a stand-in. */
434
+ buildItem: ContentsItemBuilder;
435
+ }
436
+ /** One drawn stack. */
437
+ interface ContentsViewItem {
438
+ readonly container: string;
439
+ readonly slot: number;
440
+ readonly type: string;
441
+ readonly object: ThreeNS.Object3D;
442
+ }
443
+ /** The items in an object's containers, drawn and moving with the part their slot rides on. */
444
+ interface ContentsView {
445
+ /** The group under `root` that holds every item. */
446
+ readonly group: ThreeNS.Group;
447
+ /**
448
+ * Brings the drawing in line with the world: draws the stacks that arrived, discards the ones taken, poses each slot's part at its current t and shows a
449
+ * container from the first instant its access part leaves 0 (closed, it is hidden). Call it when the object is built and on every frame one of its parts moves.
450
+ */
451
+ sync(): void;
452
+ items(): ContentsViewItem[];
453
+ /** Discards every item (`discardObject3D`) and the group. */
454
+ dispose(): void;
455
+ }
456
+ /**
457
+ * 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
+ * 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
+ */
460
+ declare function createContentsView(THREE: ThreeLib, options: ContentsViewOptions): ContentsView;
461
+
426
462
  type Wire = NonNullable<ObjectSpec['wires']>[number];
427
463
  /** The colour each kind of wire is drawn in (a grey PVC conduit, a black cable, a dark cord). */
428
464
  declare const WIRE_COLORS: Readonly<Record<Wire['kind'], number>>;
@@ -431,4 +467,4 @@ declare const WIRE_RADIUS = 0.006;
431
467
  /** One tube per wire with two points or more, named by the wire's id, in the object's own space. */
432
468
  declare function wireMeshes(THREE: ThreeLib, wires: ObjectSpec['wires']): ThreeNS.Mesh[];
433
469
 
434
- export { type CanvasFactory, 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 SwayBatch, type SwayHandle, type SwayOptions, type SwayUniforms, type SyncOptions, type ThreeAdapter, type ThreeAdapterOptions, type ThreeLib, type Vec3, WIRE_COLORS, WIRE_RADIUS, applyPose, attachSway, createDefaultMaterials, createEffects, createMeshApi, createMirrors, createScreen, createThreeAdapter, mergeParts, patchSwayShader, poseOf, rememberRest, standOutCoincidentBoxes, swayMaterial, syncObject, wireMeshes };
470
+ export { type CanvasFactory, type ContentsItemBuilder, type ContentsView, type ContentsViewItem, type ContentsViewOptions, 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 SwayBatch, type SwayHandle, type SwayOptions, type SwayUniforms, type SyncOptions, type ThreeAdapter, type ThreeAdapterOptions, type ThreeLib, type Vec3, WIRE_COLORS, WIRE_RADIUS, applyPose, attachSway, createContentsView, createDefaultMaterials, createEffects, createMeshApi, createMirrors, createScreen, createThreeAdapter, mergeParts, patchSwayShader, poseOf, rememberRest, standOutCoincidentBoxes, swayMaterial, syncObject, wireMeshes };
@@ -27,6 +27,7 @@ var GameInteractablesThree = (() => {
27
27
  applyPose: () => applyPose,
28
28
  attachSway: () => attachSway,
29
29
  catenary: () => catenary,
30
+ createContentsView: () => createContentsView,
30
31
  createCord: () => createCord,
31
32
  createDefaultMaterials: () => createDefaultMaterials,
32
33
  createEffects: () => createEffects,
@@ -2977,6 +2978,90 @@ void main() {
2977
2978
  mesh2.castShadow = true;
2978
2979
  return mesh2;
2979
2980
  }
2981
+ var isTexture = (v) => !!v && typeof v === "object" && v.isTexture === true;
2982
+ function texturesOf(material, out) {
2983
+ for (const value of Object.values(material)) if (isTexture(value)) out.add(value);
2984
+ const uniforms = material.uniforms;
2985
+ if (uniforms) {
2986
+ for (const u of Object.values(uniforms)) if (isTexture(u?.value)) out.add(u.value);
2987
+ }
2988
+ }
2989
+ function collect(roots) {
2990
+ const geometries = /* @__PURE__ */ new Set();
2991
+ const materials = /* @__PURE__ */ new Set();
2992
+ const textures = /* @__PURE__ */ new Set();
2993
+ const skeletons = /* @__PURE__ */ new Set();
2994
+ for (const root of roots) {
2995
+ root.traverse((object2) => {
2996
+ const o = object2;
2997
+ if (o.geometry && typeof o.geometry.dispose === "function") geometries.add(o.geometry);
2998
+ if (o.skeleton && typeof o.skeleton.dispose === "function") skeletons.add(o.skeleton);
2999
+ const list2 = Array.isArray(o.material) ? o.material : o.material ? [o.material] : [];
3000
+ for (const m of list2) {
3001
+ if (!m || typeof m.dispose !== "function") continue;
3002
+ materials.add(m);
3003
+ texturesOf(m, textures);
3004
+ }
3005
+ });
3006
+ }
3007
+ return { geometries, materials, textures, skeletons };
3008
+ }
3009
+ var marked = /* @__PURE__ */ new WeakSet();
3010
+ var shared = (r) => r.userData?.shared === true || marked.has(r);
3011
+ function releaseObject3D(roots, options = {}) {
3012
+ const list2 = Array.isArray(roots) ? roots : [roots];
3013
+ const report = { geometries: 0, materials: 0, textures: 0 };
3014
+ if (list2.length === 0) return report;
3015
+ const found = collect(list2);
3016
+ const keepRoots = options.keep === void 0 ? [] : Array.isArray(options.keep) ? options.keep : [options.keep];
3017
+ const releasing = new Set(list2);
3018
+ for (const keep of keepRoots) {
3019
+ const walk = (o) => {
3020
+ if (releasing.has(o)) return;
3021
+ const owner = o;
3022
+ if (owner.geometry) found.geometries.delete(owner.geometry);
3023
+ if (owner.skeleton) found.skeletons.delete(owner.skeleton);
3024
+ const mats = Array.isArray(owner.material) ? owner.material : owner.material ? [owner.material] : [];
3025
+ for (const m of mats) {
3026
+ if (!m) continue;
3027
+ found.materials.delete(m);
3028
+ const t = /* @__PURE__ */ new Set();
3029
+ texturesOf(m, t);
3030
+ for (const x of t) found.textures.delete(x);
3031
+ }
3032
+ for (const c of o.children) walk(c);
3033
+ };
3034
+ walk(keep);
3035
+ }
3036
+ for (const g of found.geometries) {
3037
+ if (shared(g)) continue;
3038
+ g.dispose();
3039
+ report.geometries++;
3040
+ }
3041
+ for (const m of found.materials) {
3042
+ if (shared(m)) continue;
3043
+ m.dispose();
3044
+ report.materials++;
3045
+ }
3046
+ for (const t of found.textures) {
3047
+ if (shared(t)) continue;
3048
+ t.dispose();
3049
+ report.textures++;
3050
+ }
3051
+ for (const s of found.skeletons) s.dispose();
3052
+ return report;
3053
+ }
3054
+ function discardObject3D(objects, options = {}) {
3055
+ const list2 = Array.isArray(objects) ? objects : [objects];
3056
+ const keep = options.keep === void 0 ? [] : Array.isArray(options.keep) ? [...options.keep] : [options.keep];
3057
+ for (const object2 of list2) {
3058
+ let top = object2;
3059
+ while (top.parent) top = top.parent;
3060
+ object2.removeFromParent();
3061
+ if (top !== object2 && !keep.includes(top)) keep.push(top);
3062
+ }
3063
+ return releaseObject3D(list2, { keep });
3064
+ }
2980
3065
 
2981
3066
  // ../game-render-three/dist/index.js
2982
3067
  var DEFAULT_ANGLE = Math.PI / 3;
@@ -3568,13 +3653,13 @@ ${QROT}${shader.vertexShader}`;
3568
3653
  const offset = apply(parentRotation, node.position);
3569
3654
  return { node, parentRotation, center: [parentOrigin[0] + offset[0], parentOrigin[1] + offset[1], parentOrigin[2] + offset[2]], axes, half, isCylinder, isPlane };
3570
3655
  }
3571
- function collect(nodes, origin, rotation, out) {
3656
+ function collect2(nodes, origin, rotation, out) {
3572
3657
  for (const source of nodes) {
3573
3658
  const node = source;
3574
3659
  if (node.kind === "group") {
3575
3660
  if (node.matrix || node.scale !== void 0) continue;
3576
3661
  const offset = apply(rotation, node.position ?? [0, 0, 0]);
3577
- collect(node.children ?? [], [origin[0] + offset[0], origin[1] + offset[1], origin[2] + offset[2]], multiply2(rotation, rotationOf(node)), out);
3662
+ collect2(node.children ?? [], [origin[0] + offset[0], origin[1] + offset[1], origin[2] + offset[2]], multiply2(rotation, rotationOf(node)), out);
3578
3663
  } else if (node.kind === "box" || node.kind === "cylinder" || node.kind === "plane") {
3579
3664
  const piece = pieceOf(node, origin, rotation);
3580
3665
  if (piece) out.push(piece);
@@ -3743,7 +3828,7 @@ ${QROT}${shader.vertexShader}`;
3743
3828
  }
3744
3829
  function standOutCoincidentBoxes(schema) {
3745
3830
  const pieces = [];
3746
- collect(schema.root.children ?? [], [0, 0, 0], IDENTITY, pieces);
3831
+ collect2(schema.root.children ?? [], [0, 0, 0], IDENTITY, pieces);
3747
3832
  const cylinders = pieces.filter((piece) => piece.isCylinder);
3748
3833
  let movedLast;
3749
3834
  for (let round = 0; round < MAX_ROUNDS; round++) {
@@ -4268,6 +4353,97 @@ uniform float swayAmplitude;
4268
4353
  };
4269
4354
  }
4270
4355
 
4356
+ // src/contents.ts
4357
+ function createContentsView(THREE, options) {
4358
+ const { world, objectId, root, buildItem } = options;
4359
+ const group = new THREE.Group();
4360
+ group.name = `contents:${objectId}`;
4361
+ root.add(group);
4362
+ const carriers = /* @__PURE__ */ new Map();
4363
+ const drawn = /* @__PURE__ */ new Map();
4364
+ const spec = world.get(objectId)?.spec;
4365
+ const partsById = new Map((spec?.parts ?? []).map((p) => [p.id, p]));
4366
+ const carrierOf = (partId) => {
4367
+ const part = partId === void 0 ? void 0 : partsById.get(partId);
4368
+ if (!part) return group;
4369
+ let carrier = carriers.get(part.id);
4370
+ if (!carrier) {
4371
+ carrier = new THREE.Group();
4372
+ carrier.name = `contents:${part.id}`;
4373
+ carrier.position.set(part.pivot[0], part.pivot[1], part.pivot[2]);
4374
+ carrier.rotation.set(0, part.ry ?? 0, 0);
4375
+ group.add(carrier);
4376
+ carriers.set(part.id, carrier);
4377
+ }
4378
+ return carrier;
4379
+ };
4380
+ const place = (object2, carrier, pos, part) => {
4381
+ carrier.add(object2);
4382
+ if (!part) {
4383
+ object2.position.set(pos[0], pos[1], pos[2]);
4384
+ return;
4385
+ }
4386
+ const ry = part.ry ?? 0;
4387
+ const x = pos[0] - part.pivot[0];
4388
+ const z = pos[2] - part.pivot[2];
4389
+ const c = Math.cos(ry);
4390
+ const s = Math.sin(ry);
4391
+ object2.position.set(c * x - s * z, pos[1] - part.pivot[1], s * x + c * z);
4392
+ object2.rotation.set(0, -ry, 0);
4393
+ };
4394
+ function sync() {
4395
+ const live = /* @__PURE__ */ new Set();
4396
+ for (const c of spec?.containers ?? []) {
4397
+ const shown = c.access === void 0 || world.partT(objectId, c.access) > 0;
4398
+ const stacks = world.contents(objectId, c.id);
4399
+ c.slots.forEach((slot, i) => {
4400
+ const stack = stacks[i];
4401
+ const key = `${c.id}#${i}`;
4402
+ const had = drawn.get(key);
4403
+ if (had && (!stack || stack.type !== had.type)) {
4404
+ discardObject3D(had.object);
4405
+ drawn.delete(key);
4406
+ }
4407
+ if (!stack) return;
4408
+ live.add(key);
4409
+ let now = drawn.get(key);
4410
+ if (!now) {
4411
+ const object2 = buildItem(stack, THREE);
4412
+ if (!object2) return;
4413
+ object2.name = `contents_${c.id}_${i}`;
4414
+ object2.userData.contents = { object: objectId, container: c.id, slot: i, item: stack.type, qty: stack.qty };
4415
+ place(object2, carrierOf(slot.part), slot.pos, slot.part === void 0 ? void 0 : partsById.get(slot.part));
4416
+ now = { type: stack.type, object: object2 };
4417
+ drawn.set(key, now);
4418
+ }
4419
+ now.object.visible = shown;
4420
+ });
4421
+ }
4422
+ for (const [key, d] of drawn) {
4423
+ if (live.has(key)) continue;
4424
+ discardObject3D(d.object);
4425
+ drawn.delete(key);
4426
+ }
4427
+ for (const [partId, carrier] of carriers) {
4428
+ const part = partsById.get(partId);
4429
+ if (part) applyPose(THREE, carrier, part.motion, world.partT(objectId, partId));
4430
+ }
4431
+ }
4432
+ return {
4433
+ group,
4434
+ sync,
4435
+ items: () => [...drawn].map(([key, d]) => {
4436
+ const cut = key.lastIndexOf("#");
4437
+ return { container: key.slice(0, cut), slot: Number(key.slice(cut + 1)), type: d.type, object: d.object };
4438
+ }),
4439
+ dispose() {
4440
+ discardObject3D(group);
4441
+ drawn.clear();
4442
+ carriers.clear();
4443
+ }
4444
+ };
4445
+ }
4446
+
4271
4447
  // src/wires.ts
4272
4448
  var WIRE_COLORS = { conduit: 13618628, cable: 2762532, drop: 1381653, cord: 1776413 };
4273
4449
  var WIRE_RADIUS = 6e-3;