@adatechnology/game-shapes-three 0.1.1 → 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/README.md CHANGED
@@ -38,8 +38,13 @@ const geometry = toGeometry(THREE, buildPrimitive({ kind: 'torus', radius: 0.3,
38
38
  | `toObject3D(THREE, buffers, { materials, materialFor, shadows, copy, name })` | `{ object, near, parts, meshes, instanced, points, materials }`; meshes are named `static:<material>` / `part:<id>` with `userData.material`, `userData.part` and `userData.ranges`; levels after the first are static (parts baked at rest), and `cullDistance` ends the `LOD` |
39
39
  | `createMaterials(THREE, hints, ids)` | `MeshStandardMaterial` per id from the schema's material hints (vertex colours on) |
40
40
  | `mergeByMaterial(THREE, root, space)` | Every visible mesh under `root` baked into `space` and joined per material (non-indexed), on game-shapes' merge; the same bytes `game-props3d`'s version gave |
41
+ | `releaseObject3D(roots, { keep })` | Frees the GPU side of trees taken out of the scene for good: geometries, materials, textures (maps and shader uniforms) and skeletons, except what a tree in `keep` (the scene) still uses or what is marked `userData.shared`. Returns how many it freed |
42
+ | `discardObject3D(objects, { keep })` | `removeFromParent()` plus `releaseObject3D` for one object or a list (one walk of the scene), keeping what the rest of its scene draws: the one call for something that leaves the game |
43
+ | `markShared(...resources)` | Marks a palette, kit or catalog resource as shared on purpose, so the release never frees it; same as `userData.shared = true` without touching the resource (digests and goldens stay) |
41
44
 
42
45
  ## Português
43
46
 
44
47
  Desenho three.js das formas do `game-shapes`: geometria com o tipo e o protótipo da classe do three, malha por lote, grupos de peça, instâncias e
45
48
  LOD; o `shapeGeometries(THREE)` é o `T` que o código portado dos times recebe para continuar escrevendo `new T.CylinderGeometry(...)`.
49
+
50
+ Tudo o que sai da cena para sempre passa por `discardObject3D` ou `releaseObject3D` (regra de memória do kit, `CLAUDE.md` seção 7): o three.js só libera a GPU no `dispose()`.
package/dist/index.d.ts CHANGED
@@ -66,4 +66,47 @@ 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
- export { SHAPE_GEOMETRY_TYPES, type ShapeGeometriesOptions, type ShapeObject, type Three, type ToGeometryOptions, type ToObjectOptions, createMaterials, mergeByMaterial, shapeGeometries, toGeometry, toObject3D };
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
+
80
+ /** What `releaseObject3D` freed. */
81
+ interface ReleaseReport {
82
+ geometries: number;
83
+ materials: number;
84
+ textures: number;
85
+ }
86
+ interface ReleaseOptions {
87
+ /**
88
+ * Trees that stay drawn (usually the scene): a geometry, material or texture any of them still uses is kept, so a resource shared by a clone that is
89
+ * still on screen (a catalogue model, a material of the kit's palette) is not freed under it.
90
+ */
91
+ keep?: THREE_NS.Object3D | readonly THREE_NS.Object3D[];
92
+ }
93
+ /**
94
+ * Marks geometries, materials or textures as shared on purpose (a palette, a model kit, a catalog): `releaseObject3D` and `discardObject3D` never free
95
+ * them. Same as `userData.shared = true`, without touching the resource (its bytes and userData stay as they were, for digests and goldens).
96
+ */
97
+ declare function markShared<T extends object>(...resources: T[]): T[];
98
+ /**
99
+ * 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
100
+ * 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
101
+ * 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
102
+ * later is safe: three.js uploads it again on the next draw.
103
+ */
104
+ declare function releaseObject3D(roots: THREE_NS.Object3D | readonly THREE_NS.Object3D[], options?: ReleaseOptions): ReleaseReport;
105
+ /**
106
+ * Take `objects` out of their trees and free what only they used (`releaseObject3D`, keeping whatever the rest of those trees, usually the scene,
107
+ * still draws). The one call for pieces, fragments, helpers or bodies that leave the game for good. A list costs one walk of each scene, so pass
108
+ * the whole list instead of calling it once per object.
109
+ */
110
+ declare function discardObject3D(objects: THREE_NS.Object3D | readonly THREE_NS.Object3D[], options?: ReleaseOptions): ReleaseReport;
111
+
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,8 +22,13 @@ 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,
28
+ discardObject3D: () => discardObject3D,
29
+ markShared: () => markShared,
26
30
  mergeByMaterial: () => mergeByMaterial,
31
+ releaseObject3D: () => releaseObject3D,
27
32
  shapeGeometries: () => shapeGeometries,
28
33
  toGeometry: () => toGeometry,
29
34
  toObject3D: () => toObject3D
@@ -70,7 +75,7 @@ var GameShapesThree = (() => {
70
75
  return geometry;
71
76
  }
72
77
 
73
- // ../game-shapes/dist/chunk-R4RY23RH.js
78
+ // ../game-shapes/dist/chunk-IFXQIDSA.js
74
79
  function colorHex(color2, fallback = 16777215) {
75
80
  if (color2 === void 0) return fallback;
76
81
  if (typeof color2 === "number") return Math.floor(color2);
@@ -1793,6 +1798,131 @@ var GameShapesThree = (() => {
1793
1798
  return mesh2;
1794
1799
  });
1795
1800
  }
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
+
1837
+ // src/release.ts
1838
+ var isTexture = (v) => !!v && typeof v === "object" && v.isTexture === true;
1839
+ function texturesOf(material, out) {
1840
+ for (const value of Object.values(material)) if (isTexture(value)) out.add(value);
1841
+ const uniforms = material.uniforms;
1842
+ if (uniforms) {
1843
+ for (const u of Object.values(uniforms)) if (isTexture(u?.value)) out.add(u.value);
1844
+ }
1845
+ }
1846
+ function collect(roots) {
1847
+ const geometries = /* @__PURE__ */ new Set();
1848
+ const materials = /* @__PURE__ */ new Set();
1849
+ const textures = /* @__PURE__ */ new Set();
1850
+ const skeletons = /* @__PURE__ */ new Set();
1851
+ for (const root of roots) {
1852
+ root.traverse((object2) => {
1853
+ const o = object2;
1854
+ if (o.geometry && typeof o.geometry.dispose === "function") geometries.add(o.geometry);
1855
+ if (o.skeleton && typeof o.skeleton.dispose === "function") skeletons.add(o.skeleton);
1856
+ const list2 = Array.isArray(o.material) ? o.material : o.material ? [o.material] : [];
1857
+ for (const m of list2) {
1858
+ if (!m || typeof m.dispose !== "function") continue;
1859
+ materials.add(m);
1860
+ texturesOf(m, textures);
1861
+ }
1862
+ });
1863
+ }
1864
+ return { geometries, materials, textures, skeletons };
1865
+ }
1866
+ var marked = /* @__PURE__ */ new WeakSet();
1867
+ function markShared(...resources) {
1868
+ for (const resource of resources) marked.add(resource);
1869
+ return resources;
1870
+ }
1871
+ var shared = (r) => r.userData?.shared === true || marked.has(r);
1872
+ function releaseObject3D(roots, options = {}) {
1873
+ const list2 = Array.isArray(roots) ? roots : [roots];
1874
+ const report = { geometries: 0, materials: 0, textures: 0 };
1875
+ if (list2.length === 0) return report;
1876
+ const found = collect(list2);
1877
+ const keepRoots = options.keep === void 0 ? [] : Array.isArray(options.keep) ? options.keep : [options.keep];
1878
+ const releasing = new Set(list2);
1879
+ for (const keep of keepRoots) {
1880
+ const walk = (o) => {
1881
+ if (releasing.has(o)) return;
1882
+ const owner = o;
1883
+ if (owner.geometry) found.geometries.delete(owner.geometry);
1884
+ if (owner.skeleton) found.skeletons.delete(owner.skeleton);
1885
+ const mats = Array.isArray(owner.material) ? owner.material : owner.material ? [owner.material] : [];
1886
+ for (const m of mats) {
1887
+ if (!m) continue;
1888
+ found.materials.delete(m);
1889
+ const t = /* @__PURE__ */ new Set();
1890
+ texturesOf(m, t);
1891
+ for (const x of t) found.textures.delete(x);
1892
+ }
1893
+ for (const c of o.children) walk(c);
1894
+ };
1895
+ walk(keep);
1896
+ }
1897
+ for (const g of found.geometries) {
1898
+ if (shared(g)) continue;
1899
+ g.dispose();
1900
+ report.geometries++;
1901
+ }
1902
+ for (const m of found.materials) {
1903
+ if (shared(m)) continue;
1904
+ m.dispose();
1905
+ report.materials++;
1906
+ }
1907
+ for (const t of found.textures) {
1908
+ if (shared(t)) continue;
1909
+ t.dispose();
1910
+ report.textures++;
1911
+ }
1912
+ for (const s of found.skeletons) s.dispose();
1913
+ return report;
1914
+ }
1915
+ function discardObject3D(objects, options = {}) {
1916
+ const list2 = Array.isArray(objects) ? objects : [objects];
1917
+ const keep = options.keep === void 0 ? [] : Array.isArray(options.keep) ? [...options.keep] : [options.keep];
1918
+ for (const object2 of list2) {
1919
+ let top = object2;
1920
+ while (top.parent) top = top.parent;
1921
+ object2.removeFromParent();
1922
+ if (top !== object2 && !keep.includes(top)) keep.push(top);
1923
+ }
1924
+ return releaseObject3D(list2, { keep });
1925
+ }
1796
1926
  return __toCommonJS(src_exports);
1797
1927
  })();
1798
1928
  //# sourceMappingURL=index.global.js.map