@adatechnology/game-shapes-three 0.2.0 → 0.3.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/dist/index.d.ts CHANGED
@@ -66,6 +66,17 @@ declare function toObject3D(THREE: Three, buffers: ShapeBuffers, options?: ToObj
66
66
  */
67
67
  declare function mergeByMaterial(THREE: Three, root: THREE_NS.Object3D, space: THREE_NS.Object3D): THREE_NS.Mesh[];
68
68
 
69
+ type Vec3 = [number, number, number];
70
+ type ThreeLib = typeof THREE_NS;
71
+ /**
72
+ * Points of a hanging cord between `a` and `b` (n points including both ends, n >= 2). The deepest point sits `sag` meters below
73
+ * the straight line between the ends, on a real catenary (y = a cosh(x/a)); when the ends differ in height the curve is
74
+ * measured against the straight line between them.
75
+ */
76
+ declare function catenary(a: Vec3, b: Vec3, sag: number, n?: number): Vec3[];
77
+ /** A thin tube through `points` (Catmull-Rom smoothed) for wires, cords and conduits. */
78
+ declare function createCord(THREE: ThreeLib, points: readonly Vec3[], radius: number, material: THREE_NS.Material, radialSegments?: number): THREE_NS.Mesh;
79
+
69
80
  /** What `releaseObject3D` freed. */
70
81
  interface ReleaseReport {
71
82
  geometries: number;
@@ -98,4 +109,4 @@ declare function releaseObject3D(roots: THREE_NS.Object3D | readonly THREE_NS.Ob
98
109
  */
99
110
  declare function discardObject3D(objects: THREE_NS.Object3D | readonly THREE_NS.Object3D[], options?: ReleaseOptions): ReleaseReport;
100
111
 
101
- export { type ReleaseOptions, type ReleaseReport, SHAPE_GEOMETRY_TYPES, type ShapeGeometriesOptions, type ShapeObject, type Three, type ToGeometryOptions, type ToObjectOptions, createMaterials, discardObject3D, markShared, mergeByMaterial, releaseObject3D, shapeGeometries, toGeometry, toObject3D };
112
+ export { type ReleaseOptions, type ReleaseReport, SHAPE_GEOMETRY_TYPES, type ShapeGeometriesOptions, type ShapeObject, type Three, type ToGeometryOptions, type ToObjectOptions, catenary, createCord, createMaterials, discardObject3D, markShared, mergeByMaterial, releaseObject3D, shapeGeometries, toGeometry, toObject3D };
@@ -22,6 +22,8 @@ var GameShapesThree = (() => {
22
22
  var src_exports = {};
23
23
  __export(src_exports, {
24
24
  SHAPE_GEOMETRY_TYPES: () => SHAPE_GEOMETRY_TYPES,
25
+ catenary: () => catenary,
26
+ createCord: () => createCord,
25
27
  createMaterials: () => createMaterials,
26
28
  discardObject3D: () => discardObject3D,
27
29
  markShared: () => markShared,
@@ -73,7 +75,7 @@ var GameShapesThree = (() => {
73
75
  return geometry;
74
76
  }
75
77
 
76
- // ../game-shapes/dist/chunk-R4RY23RH.js
78
+ // ../game-shapes/dist/chunk-M5N2SHNL.js
77
79
  function colorHex(color2, fallback = 16777215) {
78
80
  if (color2 === void 0) return fallback;
79
81
  if (typeof color2 === "number") return Math.floor(color2);
@@ -1363,8 +1365,8 @@ var GameShapesThree = (() => {
1363
1365
  groups: []
1364
1366
  };
1365
1367
  }
1366
- function make(kind, p) {
1367
- switch (kind) {
1368
+ function make(p) {
1369
+ switch (p.kind) {
1368
1370
  case "box": {
1369
1371
  const size = p.size ?? [1, 1, 1];
1370
1372
  const segments = p.segments ?? [1, 1, 1];
@@ -1393,7 +1395,7 @@ var GameShapesThree = (() => {
1393
1395
  case "mesh":
1394
1396
  return mesh(p);
1395
1397
  default:
1396
- throw new Error(`game-shapes: unknown primitive kind "${String(kind)}"`);
1398
+ throw new Error(`game-shapes: unknown primitive kind "${String(p.kind)}"`);
1397
1399
  }
1398
1400
  }
1399
1401
  var templates = /* @__PURE__ */ new Map();
@@ -1401,7 +1403,7 @@ var GameShapesThree = (() => {
1401
1403
  var stats = { hits: 0, misses: 0 };
1402
1404
  function buildPrimitive(node, options = {}) {
1403
1405
  const parameters = primitiveParameters(node, options);
1404
- if (node.kind === "mesh" || templateLimit === 0) return make(node.kind, parameters);
1406
+ if (node.kind === "mesh" || templateLimit === 0) return make({ ...node, ...parameters });
1405
1407
  const key = `${node.kind}:${canonicalJson(parameters)}`;
1406
1408
  const cached = templates.get(key);
1407
1409
  if (cached) {
@@ -1411,7 +1413,7 @@ var GameShapesThree = (() => {
1411
1413
  return cached;
1412
1414
  }
1413
1415
  stats.misses++;
1414
- const built = make(node.kind, parameters);
1416
+ const built = make({ ...node, ...parameters });
1415
1417
  templates.set(key, built);
1416
1418
  if (templates.size > templateLimit) templates.delete(templates.keys().next().value);
1417
1419
  return built;
@@ -1797,6 +1799,41 @@ var GameShapesThree = (() => {
1797
1799
  });
1798
1800
  }
1799
1801
 
1802
+ // src/cord.ts
1803
+ function catenary(a, b, sag, n = 12) {
1804
+ const count = Math.max(2, Math.floor(n));
1805
+ const span = Math.hypot(b[0] - a[0], b[2] - a[2]) || Math.hypot(b[0] - a[0], b[1] - a[1], b[2] - a[2]);
1806
+ const depth = Math.max(0, sag);
1807
+ const out = [];
1808
+ let shape = () => 0;
1809
+ if (depth > 1e-6 && span > 1e-6) {
1810
+ let lo = 1e-4;
1811
+ let hi = span * 1e3;
1812
+ for (let i = 0; i < 80; i++) {
1813
+ const c2 = (lo + hi) / 2;
1814
+ const d = c2 * (Math.cosh(span / (2 * c2)) - 1);
1815
+ if (d > depth) lo = c2;
1816
+ else hi = c2;
1817
+ }
1818
+ const c = (lo + hi) / 2;
1819
+ shape = (s) => c * (Math.cosh((s - 0.5) * span / c) - Math.cosh(span / (2 * c)));
1820
+ }
1821
+ for (let i = 0; i < count; i++) {
1822
+ const s = i / (count - 1);
1823
+ out.push([a[0] + (b[0] - a[0]) * s, a[1] + (b[1] - a[1]) * s + shape(s), a[2] + (b[2] - a[2]) * s]);
1824
+ }
1825
+ return out;
1826
+ }
1827
+ function createCord(THREE, points, radius, material, radialSegments = 5) {
1828
+ if (points.length < 2) throw new Error("createCord needs at least two points");
1829
+ const curve = new THREE.CatmullRomCurve3(points.map((p) => new THREE.Vector3(p[0], p[1], p[2])), false, "centripetal");
1830
+ const segments = Math.max(4, (points.length - 1) * 4);
1831
+ const mesh2 = new THREE.Mesh(new THREE.TubeGeometry(curve, segments, radius, radialSegments, false), material);
1832
+ mesh2.name = "cord";
1833
+ mesh2.castShadow = true;
1834
+ return mesh2;
1835
+ }
1836
+
1800
1837
  // src/release.ts
1801
1838
  var isTexture = (v) => !!v && typeof v === "object" && v.isTexture === true;
1802
1839
  function texturesOf(material, out) {