@adatechnology/game-shapes-three 0.4.2 → 0.5.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
@@ -77,6 +77,28 @@ type ThreeLib = typeof THREE_NS;
77
77
  declare function catenary(a: Vec3, b: Vec3, sag: number, n?: number): Vec3[];
78
78
  /** A thin tube through `points` (Catmull-Rom smoothed) for wires, cords and conduits. */
79
79
  declare function createCord(THREE: ThreeLib, points: readonly Vec3[], radius: number, material: THREE_NS.Material, radialSegments?: number): THREE_NS.Mesh;
80
+ /** A point as three.js and plain objects give it. */
81
+ interface PointLike {
82
+ readonly x: number;
83
+ readonly y: number;
84
+ readonly z: number;
85
+ }
86
+ /** A cord that hangs again between two moving ends every frame (a hose from a pack to the hand): one geometry, rewritten in place. */
87
+ interface HangingCord {
88
+ readonly mesh: THREE_NS.Mesh;
89
+ /** Hangs the cord from `a` to `b` with `sag` (m) below the straight line, rewriting its geometry in place: no allocation. */
90
+ hang(a: PointLike, b: PointLike, sag: number): void;
91
+ }
92
+ /**
93
+ * A tube of `segments` lengths around a catenary between two ends that move (`createCord` draws one that stays). The vertices and normals are written in place on every `hang`, the rings
94
+ * carried along the cord by parallel transport, so nothing is made or freed per frame.
95
+ */
96
+ declare function createHangingCord(THREE: ThreeLib, options: {
97
+ readonly segments: number;
98
+ readonly radius: number;
99
+ readonly material: THREE_NS.Material;
100
+ readonly radialSegments?: number;
101
+ }): HangingCord;
80
102
 
81
103
  /** What `releaseObject3D` freed. */
82
104
  interface ReleaseReport {
@@ -97,7 +119,8 @@ interface ReleaseOptions {
97
119
  */
98
120
  declare function markShared<T extends object>(...resources: T[]): T[];
99
121
  /**
100
- * Free the GPU side of trees taken out of the scene: their geometries, materials, textures (maps and shader uniforms) and skeletons. three.js keeps a removed
122
+ * Free the GPU side of trees taken out of the scene: their geometries, materials, textures (maps and shader uniforms), skeletons and the per-instance buffers of
123
+ * their instanced meshes. three.js keeps a removed
101
124
  * mesh's buffers, programs and textures on the GPU (and the JS objects in its caches) until `dispose()`, so every object a game removes without this stays
102
125
  * in memory for good. A resource marked `userData.shared = true`, or still used by a tree in `keep`, is left alone. Disposing a resource that is used again
103
126
  * later is safe: three.js uploads it again on the next draw.
@@ -162,4 +185,4 @@ interface StandOutReport {
162
185
  */
163
186
  declare function standOutCoincident(root: THREE_NS.Object3D, options?: StandOutOptions): StandOutReport;
164
187
 
165
- export { type CoincidentFacesOfOptions, type MeshBatches, type ReleaseOptions, type ReleaseReport, SHAPE_GEOMETRY_TYPES, type ShapeGeometriesOptions, type ShapeObject, type StandOutOptions, type StandOutReport, type Three, type ToGeometryOptions, type ToObjectOptions, catenary, coincidentBatchesOf, coincidentFacesOf, createCord, createMaterials, discardObject3D, isMesh, markPositionsWritten, markShared, mergeByMaterial, meshBatchesOf, rangesWrittenSince, releaseObject3D, shapeGeometries, standOutCoincident, toGeometry, toObject3D };
188
+ export { type CoincidentFacesOfOptions, type HangingCord, type MeshBatches, type PointLike, type ReleaseOptions, type ReleaseReport, SHAPE_GEOMETRY_TYPES, type ShapeGeometriesOptions, type ShapeObject, type StandOutOptions, type StandOutReport, type Three, type ToGeometryOptions, type ToObjectOptions, catenary, coincidentBatchesOf, coincidentFacesOf, createCord, createHangingCord, createMaterials, discardObject3D, isMesh, markPositionsWritten, markShared, mergeByMaterial, meshBatchesOf, rangesWrittenSince, releaseObject3D, shapeGeometries, standOutCoincident, toGeometry, toObject3D };
@@ -27,6 +27,7 @@ var GameShapesThree = (() => {
27
27
  coincidentBatchesOf: () => coincidentBatchesOf,
28
28
  coincidentFacesOf: () => coincidentFacesOf,
29
29
  createCord: () => createCord,
30
+ createHangingCord: () => createHangingCord,
30
31
  createMaterials: () => createMaterials,
31
32
  depthResolution: () => depthResolution,
32
33
  describeCoincidentFaces: () => describeCoincidentFaces,
@@ -2083,28 +2084,29 @@ var GameShapesThree = (() => {
2083
2084
  // src/cord.ts
2084
2085
  function catenary(a, b, sag, n = 12) {
2085
2086
  const count = Math.max(2, Math.floor(n));
2086
- 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]);
2087
- const depth = Math.max(0, sag);
2087
+ const { span, c } = catenaryOf(a[0], a[1], a[2], b[0], b[1], b[2], sag);
2088
2088
  const out = [];
2089
- let shape = () => 0;
2090
- if (depth > 1e-6 && span > 1e-6) {
2091
- let lo = 1e-4;
2092
- let hi = span * 1e3;
2093
- for (let i = 0; i < 80; i++) {
2094
- const c2 = (lo + hi) / 2;
2095
- const d = c2 * (Math.cosh(span / (2 * c2)) - 1);
2096
- if (d > depth) lo = c2;
2097
- else hi = c2;
2098
- }
2099
- const c = (lo + hi) / 2;
2100
- shape = (s) => c * (Math.cosh((s - 0.5) * span / c) - Math.cosh(span / (2 * c)));
2101
- }
2102
2089
  for (let i = 0; i < count; i++) {
2103
2090
  const s = i / (count - 1);
2104
- 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]);
2091
+ out.push([a[0] + (b[0] - a[0]) * s, a[1] + (b[1] - a[1]) * s + catenaryDrop(span, c, s), a[2] + (b[2] - a[2]) * s]);
2105
2092
  }
2106
2093
  return out;
2107
2094
  }
2095
+ var catenaryOf = (ax, ay, az, bx, by, bz, sag) => {
2096
+ const span = Math.hypot(bx - ax, bz - az) || Math.hypot(bx - ax, by - ay, bz - az);
2097
+ const depth = Math.max(0, sag);
2098
+ if (depth <= 1e-6 || span <= 1e-6) return { span, c: 0 };
2099
+ let lo = 1e-4;
2100
+ let hi = span * 1e3;
2101
+ for (let i = 0; i < 80; i++) {
2102
+ const c = (lo + hi) / 2;
2103
+ const d = c * (Math.cosh(span / (2 * c)) - 1);
2104
+ if (d > depth) lo = c;
2105
+ else hi = c;
2106
+ }
2107
+ return { span, c: (lo + hi) / 2 };
2108
+ };
2109
+ var catenaryDrop = (span, c, s) => c > 0 ? c * (Math.cosh((s - 0.5) * span / c) - Math.cosh(span / (2 * c))) : 0;
2108
2110
  function createCord(THREE, points, radius, material, radialSegments = 5) {
2109
2111
  if (points.length < 2) throw new Error("createCord needs at least two points");
2110
2112
  const curve = new THREE.CatmullRomCurve3(points.map((p) => new THREE.Vector3(p[0], p[1], p[2])), false, "centripetal");
@@ -2114,6 +2116,96 @@ var GameShapesThree = (() => {
2114
2116
  mesh2.castShadow = true;
2115
2117
  return mesh2;
2116
2118
  }
2119
+ function createHangingCord(THREE, options) {
2120
+ const segments = Math.max(1, Math.floor(options.segments));
2121
+ const radial = Math.max(3, Math.floor(options.radialSegments ?? 5));
2122
+ const ring2 = radial + 1;
2123
+ const positions = new Float32Array((segments + 1) * ring2 * 3);
2124
+ const normals = new Float32Array((segments + 1) * ring2 * 3);
2125
+ const index = [];
2126
+ for (let i = 0; i < segments; i++) {
2127
+ for (let j = 0; j < radial; j++) {
2128
+ const a = i * ring2 + j;
2129
+ const b = (i + 1) * ring2 + j;
2130
+ index.push(a, b, a + 1, b, b + 1, a + 1);
2131
+ }
2132
+ }
2133
+ const geometry = new THREE.BufferGeometry();
2134
+ const positionAttribute = new THREE.BufferAttribute(positions, 3);
2135
+ const normalAttribute = new THREE.BufferAttribute(normals, 3);
2136
+ geometry.setAttribute("position", positionAttribute);
2137
+ geometry.setAttribute("normal", normalAttribute);
2138
+ geometry.setIndex(index);
2139
+ const sphere2 = new THREE.Sphere();
2140
+ geometry.boundingSphere = sphere2;
2141
+ const mesh2 = new THREE.Mesh(geometry, options.material);
2142
+ mesh2.name = "cord";
2143
+ mesh2.castShadow = true;
2144
+ const centre = new Float64Array((segments + 1) * 3);
2145
+ const tangent = new THREE.Vector3();
2146
+ const normal = new THREE.Vector3();
2147
+ const binormal = new THREE.Vector3();
2148
+ const previous = new THREE.Vector3();
2149
+ const low = new THREE.Vector3();
2150
+ const high = new THREE.Vector3();
2151
+ const tangentAt = (i) => {
2152
+ const from = Math.max(0, i - 1);
2153
+ const to = Math.min(segments, i + 1);
2154
+ tangent.set(centre[to * 3] - centre[from * 3], centre[to * 3 + 1] - centre[from * 3 + 1], centre[to * 3 + 2] - centre[from * 3 + 2]).normalize();
2155
+ };
2156
+ return {
2157
+ mesh: mesh2,
2158
+ hang(a, b, sag) {
2159
+ const { span, c } = catenaryOf(a.x, a.y, a.z, b.x, b.y, b.z, sag);
2160
+ for (let i = 0; i <= segments; i++) {
2161
+ const s = i / segments;
2162
+ centre[i * 3] = a.x + (b.x - a.x) * s;
2163
+ centre[i * 3 + 1] = a.y + (b.y - a.y) * s + catenaryDrop(span, c, s);
2164
+ centre[i * 3 + 2] = a.z + (b.z - a.z) * s;
2165
+ }
2166
+ low.set(Infinity, Infinity, Infinity);
2167
+ high.set(-Infinity, -Infinity, -Infinity);
2168
+ for (let i = 0; i <= segments; i++) {
2169
+ tangentAt(i);
2170
+ if (i === 0) {
2171
+ const ax = Math.abs(tangent.x);
2172
+ const ay = Math.abs(tangent.y);
2173
+ const az = Math.abs(tangent.z);
2174
+ normal.set(ax <= ay && ax <= az ? 1 : 0, ay < ax && ay <= az ? 1 : 0, az < ax && az < ay ? 1 : 0);
2175
+ normal.sub(binormal.copy(tangent).multiplyScalar(normal.dot(tangent))).normalize();
2176
+ } else {
2177
+ normal.copy(previous).sub(binormal.copy(tangent).multiplyScalar(previous.dot(tangent))).normalize();
2178
+ }
2179
+ previous.copy(normal);
2180
+ binormal.crossVectors(tangent, normal);
2181
+ const px = centre[i * 3];
2182
+ const py = centre[i * 3 + 1];
2183
+ const pz = centre[i * 3 + 2];
2184
+ for (let j = 0; j <= radial; j++) {
2185
+ const angle = j / radial * Math.PI * 2;
2186
+ const cos = Math.cos(angle);
2187
+ const sin = -Math.sin(angle);
2188
+ const nx = cos * normal.x + sin * binormal.x;
2189
+ const ny = cos * normal.y + sin * binormal.y;
2190
+ const nz = cos * normal.z + sin * binormal.z;
2191
+ const at = (i * ring2 + j) * 3;
2192
+ normals[at] = nx;
2193
+ normals[at + 1] = ny;
2194
+ normals[at + 2] = nz;
2195
+ positions[at] = px + options.radius * nx;
2196
+ positions[at + 1] = py + options.radius * ny;
2197
+ positions[at + 2] = pz + options.radius * nz;
2198
+ }
2199
+ low.set(Math.min(low.x, px), Math.min(low.y, py), Math.min(low.z, pz));
2200
+ high.set(Math.max(high.x, px), Math.max(high.y, py), Math.max(high.z, pz));
2201
+ }
2202
+ positionAttribute.needsUpdate = true;
2203
+ normalAttribute.needsUpdate = true;
2204
+ sphere2.center.addVectors(low, high).multiplyScalar(0.5);
2205
+ sphere2.radius = sphere2.center.distanceTo(high) + options.radius;
2206
+ }
2207
+ };
2208
+ }
2117
2209
 
2118
2210
  // src/release.ts
2119
2211
  var isTexture = (v) => !!v && typeof v === "object" && v.isTexture === true;
@@ -2124,13 +2216,16 @@ var GameShapesThree = (() => {
2124
2216
  for (const u of Object.values(uniforms)) if (isTexture(u?.value)) out.add(u.value);
2125
2217
  }
2126
2218
  }
2219
+ var isInstanced = (o) => o.isInstancedMesh === true;
2127
2220
  function collect(roots) {
2128
2221
  const geometries = /* @__PURE__ */ new Set();
2129
2222
  const materials = /* @__PURE__ */ new Set();
2130
2223
  const textures = /* @__PURE__ */ new Set();
2131
2224
  const skeletons = /* @__PURE__ */ new Set();
2225
+ const instanced = /* @__PURE__ */ new Set();
2132
2226
  for (const root of roots) {
2133
2227
  root.traverse((object2) => {
2228
+ if (isInstanced(object2)) instanced.add(object2);
2134
2229
  const o = object2;
2135
2230
  if (o.geometry && typeof o.geometry.dispose === "function") geometries.add(o.geometry);
2136
2231
  if (o.skeleton && typeof o.skeleton.dispose === "function") skeletons.add(o.skeleton);
@@ -2142,7 +2237,7 @@ var GameShapesThree = (() => {
2142
2237
  }
2143
2238
  });
2144
2239
  }
2145
- return { geometries, materials, textures, skeletons };
2240
+ return { geometries, materials, textures, skeletons, instanced };
2146
2241
  }
2147
2242
  var marked = /* @__PURE__ */ new WeakSet();
2148
2243
  function markShared(...resources) {
@@ -2191,6 +2286,7 @@ var GameShapesThree = (() => {
2191
2286
  report.textures++;
2192
2287
  }
2193
2288
  for (const s of found.skeletons) s.dispose();
2289
+ for (const mesh2 of found.instanced) mesh2.dispose();
2194
2290
  return report;
2195
2291
  }
2196
2292
  function discardObject3D(objects, options = {}) {