@adatechnology/game-interactables-three 0.5.1 → 0.6.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/README.md CHANGED
@@ -54,7 +54,7 @@ ones this api made with its own templates before the move (`tests/exact-golden.t
54
54
 
55
55
  - Implements `box`, `cylinder` (shapes `cyl`, `cyl6`, `cone`, `sphere`), `leaf`, `plane` (with atlas `uv`), `light` and `part`.
56
56
  - Static primitives are baked (vertex colors) into **one `BufferGeometry` per material**: the draw calls of the static body are the number
57
- of materials in use (`plain`, `metal`, `wood`, `glass`, `fabric`, `plant`, `water`, `emissive`, `atlas_picture`, `atlas_floor`).
57
+ of materials in use (`plain`, `metal`, `wood`, `glass`, `fabric`, `foam`, `plant`, `water`, `emissive`, `atlas_picture`, `atlas_floor`).
58
58
  - `part(id, pivot, ry, draw)` routes everything drawn inside `draw` into a group of its own, merged per material as well. The group origin is
59
59
  the pivot and it is turned by `ry`, so a hinge or slide axis is expressed in the part frame. Names are `part:<id>` and `userData.part = id`
60
60
  on the group **and** its meshes, so `raycaster.intersectObject(group, true)[0].object.userData.part` gives the part that was hit.
@@ -62,7 +62,7 @@ ones this api made with its own templates before the move (`tests/exact-golden.t
62
62
  - `materials` maps keys to your materials; missing keys get defaults with `vertexColors` (`MeshStandardMaterial` for `quality: 'high'`,
63
63
  `MeshLambertMaterial` for `'game'`, which also uses fewer segments). Atlas materials are yours to provide (a `CanvasTexture` of the atlas).
64
64
  - `api.light` creates a `PointLight` inside the right group; `finish()` also returns them as `lights`.
65
- - **Draw calls in `game` quality.** `plain`, `metal`, `wood` and `fabric` are one shared Lambert material, so they merge into one mesh per
65
+ - **Draw calls in `game` quality.** `plain`, `metal`, `wood`, `fabric` and `foam` are one shared Lambert material, so they merge into one mesh per
66
66
  target. `finish(spec)` (pass the spec the pack returned; option `mergeParts`, default on in `game` quality) also merges the parts of one
67
67
  `PartSpec.group` that share a motion (blind slats: a per-vertex pivot in the vertex shader, one set of uniforms, shadows follow) and the
68
68
  `cloth` soft volumes (clothes on a line: one mesh, a per-vertex cloth index and anchor, each cloth keeps its own swing). `parts[id]` then
@@ -156,6 +156,12 @@ point is `sag` meters below the chord.
156
156
  points or more, named by its id, in the colour of its kind (`WIRE_COLORS`, the houses' colours), radius `WIRE_RADIUS` when the wire gives none.
157
157
  Add them to the object's group; a house folds its wires into its own batches instead.
158
158
 
159
+ `createSpanWire(THREE, { span, radius, material, ground?, onSnap? })` draws a wire strung between two points the host moves (the drop from a pole to a house,
160
+ `spec.wires[].span`): `update(from, to)` lays the tube along the catenary of its strung length between where its ends are now (rewritten in place, nothing
161
+ when neither end moved more than a millimetre), lying on `ground` where it would hang lower. Pulled past its length it stretches as its material does
162
+ (`solveSpan` of game-interactables) and, past the material's strength, snaps once at the end that pulls the harder (`onSnap`); what is left hangs from the
163
+ other end down to the ground and along it. `dispose()` takes it out and frees its geometry.
164
+
159
165
  ## Notes
160
166
 
161
167
  - `poseOf` converts the pose math of the interactables package (same defaults) to a quaternion, so the rendered part always matches the engine.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ActiveEmitter, ObjectSpec, Motion, World, ItemStack, ObjectControlsState } from '@adatechnology/game-interactables';
1
+ import { ActiveEmitter, ObjectSpec, Motion, World, ItemStack, 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';
@@ -24,7 +24,7 @@ type ThreeLib = typeof ThreeNS;
24
24
 
25
25
  type Vec3 = [number, number, number];
26
26
  /** Material keys the mesh api understands (same as `PropsApi` materials). */
27
- type MaterialKey = 'plain' | 'metal' | 'wood' | 'glass' | 'fabric' | 'plant' | 'water' | 'emissive' | 'atlas_picture' | 'atlas_floor';
27
+ type MaterialKey = 'plain' | 'metal' | 'wood' | 'glass' | 'fabric' | 'foam' | 'plant' | 'water' | 'emissive' | 'atlas_picture' | 'atlas_floor';
28
28
  declare const MATERIAL_KEYS: readonly MaterialKey[];
29
29
  type Quality = 'high' | 'game';
30
30
  /**
@@ -165,7 +165,7 @@ interface MeshApi {
165
165
  /** Materials in use (defaults merged with the given ones). */
166
166
  materials: Record<MaterialKey, ThreeNS.Material>;
167
167
  }
168
- /** Default materials: vertex colors everywhere (in `game` quality plain, metal, wood and fabric are one shared Lambert material), glass and water transparent, emissive unlit. */
168
+ /** Default materials: vertex colors everywhere (in `game` quality plain, metal, wood, fabric and foam are one shared Lambert material), glass and water transparent, emissive unlit. */
169
169
  declare function createDefaultMaterials(THREE: ThreeLib, quality?: Quality): Record<MaterialKey, ThreeNS.Material>;
170
170
  /**
171
171
  * Mesh api for content packs. Every primitive is baked (vertex colors) into one `BufferGeometry` per material and per target
@@ -467,6 +467,32 @@ declare const WIRE_RADIUS = 0.006;
467
467
  /** One tube per wire with two points or more, named by the wire's id, in the object's own space. */
468
468
  declare function wireMeshes(THREE: ThreeLib, wires: ObjectSpec['wires']): ThreeNS.Mesh[];
469
469
 
470
+ type SpanWireOptions = {
471
+ readonly span: Span;
472
+ /** Radius the wire is drawn with, metres. */
473
+ readonly radius: number;
474
+ readonly material: ThreeNS.Material;
475
+ /** Points along the wire (default 16). */
476
+ readonly segments?: number;
477
+ /** Sides of the tube (default 5). */
478
+ readonly radialSegments?: number;
479
+ /** Height of the ground under a point of the frame the ends are given in. */
480
+ readonly ground?: (x: number, z: number) => number;
481
+ /** Called once, when the wire snaps. */
482
+ readonly onSnap?: (end: 'from' | 'to', shape: SpanShape) => void;
483
+ };
484
+ type SpanWire = {
485
+ /** The tube, in the frame the ends are given in (add it to that frame's object). */
486
+ readonly mesh: ThreeNS.Mesh;
487
+ /** The end it snapped at, null while it holds. */
488
+ readonly snapped: 'from' | 'to' | null;
489
+ /** Draws the wire between where its ends are now; returns the shape it took, or null when neither end moved. */
490
+ update(from: Vec3, to: Vec3): SpanShape | null;
491
+ dispose(): void;
492
+ };
493
+ /** A wire between two points that hangs as its span does and snaps past its material's strength. */
494
+ declare function createSpanWire(THREE: ThreeLib, options: SpanWireOptions): SpanWire;
495
+
470
496
  /**
471
497
  * What an object's controls show on its drawing (`createObjectControls` of game-interactables): the picture of a screen that is on, on its channel; what is in each
472
498
  * bowl, to its level; what a body left in a basin. Everything is drawn from the spec's data (`DeviceSpec.display`, `BowlSpec`, `BasinSpec.soils`), as children of
@@ -487,4 +513,4 @@ interface ControlsView {
487
513
  }
488
514
  declare function createControlsView(THREE: ThreeLib, options: ControlsViewOptions): ControlsView;
489
515
 
490
- 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 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, createThreeAdapter, mergeParts, patchSwayShader, poseOf, rememberRest, standOutCoincidentBoxes, swayMaterial, syncObject, wireMeshes };
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 };
@@ -35,6 +35,7 @@ var GameInteractablesThree = (() => {
35
35
  createMeshApi: () => createMeshApi,
36
36
  createMirrors: () => createMirrors,
37
37
  createScreen: () => createScreen,
38
+ createSpanWire: () => createSpanWire,
38
39
  createThreeAdapter: () => createThreeAdapter,
39
40
  mergeParts: () => mergeParts,
40
41
  patchSwayShader: () => patchSwayShader,
@@ -3331,7 +3332,7 @@ void main() {
3331
3332
  }
3332
3333
 
3333
3334
  // src/types.ts
3334
- var MATERIAL_KEYS = ["plain", "metal", "wood", "glass", "fabric", "plant", "water", "emissive", "atlas_picture", "atlas_floor"];
3335
+ var MATERIAL_KEYS = ["plain", "metal", "wood", "glass", "fabric", "foam", "plant", "water", "emissive", "atlas_picture", "atlas_floor"];
3335
3336
 
3336
3337
  // ../game-interactables/dist/index.js
3337
3338
  var DEFAULT_AXIS = [0, 1, 0];
@@ -3360,6 +3361,85 @@ void main() {
3360
3361
  }
3361
3362
  var pose = new Float64Array(7);
3362
3363
  var pose2 = new Float64Array(7);
3364
+ var GRAVITY = 9.81;
3365
+ var SHAPE_STEPS = 60;
3366
+ var VERTICAL_SPAN = 1e-4;
3367
+ function shapeOf(ratio) {
3368
+ let low = 1e-9;
3369
+ let high = 1;
3370
+ while (Math.sinh(high) / high < ratio && high < 700) high *= 2;
3371
+ for (let i = 0; i < SHAPE_STEPS; i++) {
3372
+ const mid = (low + high) / 2;
3373
+ if (Math.sinh(mid) / mid < ratio) low = mid;
3374
+ else high = mid;
3375
+ }
3376
+ return (low + high) / 2;
3377
+ }
3378
+ function spanStrength(span) {
3379
+ return span.material.ultimateMPa * 1e6 * span.section;
3380
+ }
3381
+ function straight(params, count, weight, chord) {
3382
+ const { span, from, to } = params;
3383
+ const pull = span.material.elasticGPa * 1e9 * span.section * ((chord - span.length) / span.length);
3384
+ const rise = to[1] - from[1];
3385
+ const points = Array.from({ length: count }, (_, i) => {
3386
+ const t = i / (count - 1);
3387
+ return [from[0] + (to[0] - from[0]) * t, from[1] + rise * t, from[2] + (to[2] - from[2]) * t];
3388
+ });
3389
+ return { points, pullFrom: pull + weight * (span.length - rise) / 2, pullTo: pull + weight * (span.length + rise) / 2 };
3390
+ }
3391
+ function loop(params, count, weight) {
3392
+ const { span, from, to } = params;
3393
+ const bottom = (from[1] + to[1] - span.length) / 2;
3394
+ const down = from[1] - bottom;
3395
+ const points = Array.from({ length: count }, (_, i) => {
3396
+ const s = i / (count - 1) * span.length;
3397
+ return s <= down ? [from[0], from[1] - s, from[2]] : [to[0], bottom + (s - down), to[2]];
3398
+ });
3399
+ return { points, pullFrom: weight * down, pullTo: weight * (to[1] - bottom) };
3400
+ }
3401
+ function hanging(params, count, weight, across) {
3402
+ const { span, from, to } = params;
3403
+ const rise = to[1] - from[1];
3404
+ const c = across / (2 * shapeOf(Math.sqrt(span.length * span.length - rise * rise) / across));
3405
+ const x0 = across / 2 - c * Math.atanh(rise / span.length);
3406
+ const base = Math.cosh(-x0 / c);
3407
+ const points = Array.from({ length: count }, (_, i) => {
3408
+ const t = i / (count - 1);
3409
+ return [from[0] + (to[0] - from[0]) * t, from[1] + c * (Math.cosh((across * t - x0) / c) - base), from[2] + (to[2] - from[2]) * t];
3410
+ });
3411
+ return { points, pullFrom: weight * c * base, pullTo: weight * c * Math.cosh((across - x0) / c) };
3412
+ }
3413
+ function solveSpan(params) {
3414
+ const { span, from, to } = params;
3415
+ const count = Math.max(2, Math.floor(params.segments ?? 16));
3416
+ const weight = span.material.density * span.section * GRAVITY;
3417
+ const across = Math.hypot(to[0] - from[0], to[2] - from[2]);
3418
+ const chord = Math.hypot(across, to[1] - from[1]);
3419
+ const taut = chord >= span.length;
3420
+ const shape = taut ? straight(params, count, weight, chord) : across < VERTICAL_SPAN ? loop(params, count, weight) : hanging(params, count, weight, across);
3421
+ const ground = params.ground;
3422
+ if (ground) for (const p of shape.points) p[1] = Math.max(p[1], ground(p[0], p[2]) + (params.clearance ?? 0));
3423
+ const tension = Math.max(shape.pullFrom, shape.pullTo);
3424
+ const snaps = tension > spanStrength(span) ? shape.pullFrom >= shape.pullTo ? "from" : "to" : null;
3425
+ return { points: shape.points, tension, taut, snaps };
3426
+ }
3427
+ function hangTail(params) {
3428
+ const { anchor, length: length2 } = params;
3429
+ const count = Math.max(2, Math.floor(params.segments ?? 16));
3430
+ const clearance = params.clearance ?? 0;
3431
+ const floorAt = (x, z) => params.ground ? params.ground(x, z) + clearance : -Infinity;
3432
+ const drop = Math.max(0, Math.min(length2, anchor[1] - floorAt(anchor[0], anchor[2])));
3433
+ const toward = params.toward ?? [1, 0, 0];
3434
+ const flat = Math.hypot(toward[0], toward[2]) || 1;
3435
+ return Array.from({ length: count }, (_, i) => {
3436
+ const s = i / (count - 1) * length2;
3437
+ if (s <= drop) return [anchor[0], anchor[1] - s, anchor[2]];
3438
+ const x = anchor[0] + toward[0] / flat * (s - drop);
3439
+ const z = anchor[2] + toward[2] / flat * (s - drop);
3440
+ return [x, Math.max(anchor[1] - drop, floorAt(x, z)), z];
3441
+ });
3442
+ }
3363
3443
 
3364
3444
  // src/pose.ts
3365
3445
  function poseOf2(motion, t) {
@@ -3979,6 +4059,7 @@ ${QROT}${shader.vertexShader}`;
3979
4059
  wood: solid ?? std({ roughness: 0.8 }),
3980
4060
  glass: std({ roughness: 0.05, transparent: true, opacity: 0.35, depthWrite: false }),
3981
4061
  fabric: solid ?? std({ roughness: 1 }),
4062
+ foam: solid ?? std({ roughness: 1 }),
3982
4063
  plant: std({ roughness: 0.9, side: THREE.DoubleSide }),
3983
4064
  water: std({ roughness: 0.1, transparent: true, opacity: 0.7, depthWrite: false }),
3984
4065
  emissive: new THREE.MeshBasicMaterial({ vertexColors: true }),
@@ -4575,6 +4656,98 @@ uniform float swayAmplitude;
4575
4656
  return meshes;
4576
4657
  }
4577
4658
 
4659
+ // src/span-wire.ts
4660
+ var MOVED = 1e-3;
4661
+ function tubeGeometry(THREE, rings, sides) {
4662
+ const geometry = new THREE.BufferGeometry();
4663
+ geometry.setAttribute("position", new THREE.BufferAttribute(new Float32Array(rings * sides * 3), 3));
4664
+ geometry.setAttribute("normal", new THREE.BufferAttribute(new Float32Array(rings * sides * 3), 3));
4665
+ const index = [];
4666
+ for (let r = 0; r + 1 < rings; r++) {
4667
+ for (let s = 0; s < sides; s++) {
4668
+ const a = r * sides + s;
4669
+ const b = r * sides + (s + 1) % sides;
4670
+ index.push(a, a + sides, b, b, a + sides, b + sides);
4671
+ }
4672
+ }
4673
+ geometry.setIndex(index);
4674
+ return geometry;
4675
+ }
4676
+ function frameAt(points, i) {
4677
+ const before = points[Math.max(0, i - 1)];
4678
+ const after = points[Math.min(points.length - 1, i + 1)];
4679
+ const length = Math.hypot(after[0] - before[0], after[1] - before[1], after[2] - before[2]) || 1;
4680
+ const t = [(after[0] - before[0]) / length, (after[1] - before[1]) / length, (after[2] - before[2]) / length];
4681
+ const upright = Math.abs(t[1]) > 0.9;
4682
+ const raw = upright ? [0, t[2], -t[1]] : [-t[2], 0, t[0]];
4683
+ const nl = Math.hypot(raw[0], raw[1], raw[2]) || 1;
4684
+ const n = [raw[0] / nl, raw[1] / nl, raw[2] / nl];
4685
+ return { n, b: [t[1] * n[2] - t[2] * n[1], t[2] * n[0] - t[0] * n[2], t[0] * n[1] - t[1] * n[0]] };
4686
+ }
4687
+ function writeTube(geometry, points, radius, sides) {
4688
+ const position = geometry.getAttribute("position");
4689
+ const normal = geometry.getAttribute("normal");
4690
+ for (let i = 0; i < points.length; i++) {
4691
+ const { n, b } = frameAt(points, i);
4692
+ const p = points[i];
4693
+ for (let s = 0; s < sides; s++) {
4694
+ const angle = s / sides * Math.PI * 2;
4695
+ const ox = n[0] * Math.cos(angle) + b[0] * Math.sin(angle);
4696
+ const oy = n[1] * Math.cos(angle) + b[1] * Math.sin(angle);
4697
+ const oz = n[2] * Math.cos(angle) + b[2] * Math.sin(angle);
4698
+ position.setXYZ(i * sides + s, p[0] + ox * radius, p[1] + oy * radius, p[2] + oz * radius);
4699
+ normal.setXYZ(i * sides + s, ox, oy, oz);
4700
+ }
4701
+ }
4702
+ position.needsUpdate = true;
4703
+ normal.needsUpdate = true;
4704
+ geometry.computeBoundingSphere();
4705
+ geometry.computeBoundingBox();
4706
+ }
4707
+ var movedFrom = (last, now) => !last || Math.hypot(now[0] - last[0], now[1] - last[1], now[2] - last[2]) > MOVED;
4708
+ function createSpanWire(THREE, options) {
4709
+ const segments = Math.max(2, Math.floor(options.segments ?? 16));
4710
+ const sides = Math.max(3, Math.floor(options.radialSegments ?? 5));
4711
+ const geometry = tubeGeometry(THREE, segments, sides);
4712
+ const mesh2 = new THREE.Mesh(geometry, options.material);
4713
+ mesh2.name = "span-wire";
4714
+ mesh2.castShadow = true;
4715
+ const ground = options.ground ? { ground: options.ground } : {};
4716
+ const clearance = options.radius;
4717
+ let snapped = null;
4718
+ let lastFrom = null;
4719
+ let lastTo = null;
4720
+ return {
4721
+ mesh: mesh2,
4722
+ get snapped() {
4723
+ return snapped;
4724
+ },
4725
+ update(from, to) {
4726
+ if (!movedFrom(lastFrom, from) && !movedFrom(lastTo, to)) return null;
4727
+ lastFrom = [from[0], from[1], from[2]];
4728
+ lastTo = [to[0], to[1], to[2]];
4729
+ const shape = solveSpan({ span: options.span, from, to, segments, clearance, ...ground });
4730
+ if (!snapped && shape.snaps) {
4731
+ snapped = shape.snaps;
4732
+ options.onSnap?.(shape.snaps, shape);
4733
+ }
4734
+ if (!snapped) {
4735
+ writeTube(geometry, shape.points, options.radius, sides);
4736
+ return shape;
4737
+ }
4738
+ const anchor = snapped === "from" ? to : from;
4739
+ const other = snapped === "from" ? from : to;
4740
+ const toward = [other[0] - anchor[0], 0, other[2] - anchor[2]];
4741
+ writeTube(geometry, hangTail({ anchor, length: options.span.length, toward, segments, clearance, ...ground }), options.radius, sides);
4742
+ return shape;
4743
+ },
4744
+ dispose() {
4745
+ mesh2.removeFromParent();
4746
+ geometry.dispose();
4747
+ }
4748
+ };
4749
+ }
4750
+
4578
4751
  // src/controls-view.ts
4579
4752
  var FOOD = 8018480;
4580
4753
  var WATER = 6262704;