@adatechnology/game-shapes-three 0.1.0 → 0.2.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 +5 -0
- package/dist/index.d.ts +33 -1
- package/dist/index.global.js +96 -0
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +96 -0
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
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,36 @@ 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
|
-
|
|
69
|
+
/** What `releaseObject3D` freed. */
|
|
70
|
+
interface ReleaseReport {
|
|
71
|
+
geometries: number;
|
|
72
|
+
materials: number;
|
|
73
|
+
textures: number;
|
|
74
|
+
}
|
|
75
|
+
interface ReleaseOptions {
|
|
76
|
+
/**
|
|
77
|
+
* 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
|
|
78
|
+
* still on screen (a catalogue model, a material of the kit's palette) is not freed under it.
|
|
79
|
+
*/
|
|
80
|
+
keep?: THREE_NS.Object3D | readonly THREE_NS.Object3D[];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Marks geometries, materials or textures as shared on purpose (a palette, a model kit, a catalog): `releaseObject3D` and `discardObject3D` never free
|
|
84
|
+
* them. Same as `userData.shared = true`, without touching the resource (its bytes and userData stay as they were, for digests and goldens).
|
|
85
|
+
*/
|
|
86
|
+
declare function markShared<T extends object>(...resources: T[]): T[];
|
|
87
|
+
/**
|
|
88
|
+
* 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
|
|
89
|
+
* 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
|
|
90
|
+
* 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
|
|
91
|
+
* later is safe: three.js uploads it again on the next draw.
|
|
92
|
+
*/
|
|
93
|
+
declare function releaseObject3D(roots: THREE_NS.Object3D | readonly THREE_NS.Object3D[], options?: ReleaseOptions): ReleaseReport;
|
|
94
|
+
/**
|
|
95
|
+
* Take `objects` out of their trees and free what only they used (`releaseObject3D`, keeping whatever the rest of those trees, usually the scene,
|
|
96
|
+
* 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
|
|
97
|
+
* the whole list instead of calling it once per object.
|
|
98
|
+
*/
|
|
99
|
+
declare function discardObject3D(objects: THREE_NS.Object3D | readonly THREE_NS.Object3D[], options?: ReleaseOptions): ReleaseReport;
|
|
100
|
+
|
|
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 };
|
package/dist/index.global.js
CHANGED
|
@@ -23,7 +23,10 @@ var GameShapesThree = (() => {
|
|
|
23
23
|
__export(src_exports, {
|
|
24
24
|
SHAPE_GEOMETRY_TYPES: () => SHAPE_GEOMETRY_TYPES,
|
|
25
25
|
createMaterials: () => createMaterials,
|
|
26
|
+
discardObject3D: () => discardObject3D,
|
|
27
|
+
markShared: () => markShared,
|
|
26
28
|
mergeByMaterial: () => mergeByMaterial,
|
|
29
|
+
releaseObject3D: () => releaseObject3D,
|
|
27
30
|
shapeGeometries: () => shapeGeometries,
|
|
28
31
|
toGeometry: () => toGeometry,
|
|
29
32
|
toObject3D: () => toObject3D
|
|
@@ -1582,7 +1585,9 @@ var GameShapesThree = (() => {
|
|
|
1582
1585
|
return extrude(outline, options.depth, options.steps, bevel);
|
|
1583
1586
|
}
|
|
1584
1587
|
var namespaces = /* @__PURE__ */ new WeakMap();
|
|
1588
|
+
var shims = /* @__PURE__ */ new WeakSet();
|
|
1585
1589
|
function shapeGeometries(THREE, options = {}) {
|
|
1590
|
+
if (shims.has(THREE)) return THREE;
|
|
1586
1591
|
const cache = options.cache !== false;
|
|
1587
1592
|
const key = cache ? "cache" : "nocache";
|
|
1588
1593
|
let byKey = namespaces.get(THREE);
|
|
@@ -1629,6 +1634,7 @@ var GameShapesThree = (() => {
|
|
|
1629
1634
|
}
|
|
1630
1635
|
const result = namespace;
|
|
1631
1636
|
byKey.set(key, result);
|
|
1637
|
+
shims.add(result);
|
|
1632
1638
|
return result;
|
|
1633
1639
|
}
|
|
1634
1640
|
|
|
@@ -1790,6 +1796,96 @@ var GameShapesThree = (() => {
|
|
|
1790
1796
|
return mesh2;
|
|
1791
1797
|
});
|
|
1792
1798
|
}
|
|
1799
|
+
|
|
1800
|
+
// src/release.ts
|
|
1801
|
+
var isTexture = (v) => !!v && typeof v === "object" && v.isTexture === true;
|
|
1802
|
+
function texturesOf(material, out) {
|
|
1803
|
+
for (const value of Object.values(material)) if (isTexture(value)) out.add(value);
|
|
1804
|
+
const uniforms = material.uniforms;
|
|
1805
|
+
if (uniforms) {
|
|
1806
|
+
for (const u of Object.values(uniforms)) if (isTexture(u?.value)) out.add(u.value);
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
function collect(roots) {
|
|
1810
|
+
const geometries = /* @__PURE__ */ new Set();
|
|
1811
|
+
const materials = /* @__PURE__ */ new Set();
|
|
1812
|
+
const textures = /* @__PURE__ */ new Set();
|
|
1813
|
+
const skeletons = /* @__PURE__ */ new Set();
|
|
1814
|
+
for (const root of roots) {
|
|
1815
|
+
root.traverse((object2) => {
|
|
1816
|
+
const o = object2;
|
|
1817
|
+
if (o.geometry && typeof o.geometry.dispose === "function") geometries.add(o.geometry);
|
|
1818
|
+
if (o.skeleton && typeof o.skeleton.dispose === "function") skeletons.add(o.skeleton);
|
|
1819
|
+
const list2 = Array.isArray(o.material) ? o.material : o.material ? [o.material] : [];
|
|
1820
|
+
for (const m of list2) {
|
|
1821
|
+
if (!m || typeof m.dispose !== "function") continue;
|
|
1822
|
+
materials.add(m);
|
|
1823
|
+
texturesOf(m, textures);
|
|
1824
|
+
}
|
|
1825
|
+
});
|
|
1826
|
+
}
|
|
1827
|
+
return { geometries, materials, textures, skeletons };
|
|
1828
|
+
}
|
|
1829
|
+
var marked = /* @__PURE__ */ new WeakSet();
|
|
1830
|
+
function markShared(...resources) {
|
|
1831
|
+
for (const resource of resources) marked.add(resource);
|
|
1832
|
+
return resources;
|
|
1833
|
+
}
|
|
1834
|
+
var shared = (r) => r.userData?.shared === true || marked.has(r);
|
|
1835
|
+
function releaseObject3D(roots, options = {}) {
|
|
1836
|
+
const list2 = Array.isArray(roots) ? roots : [roots];
|
|
1837
|
+
const report = { geometries: 0, materials: 0, textures: 0 };
|
|
1838
|
+
if (list2.length === 0) return report;
|
|
1839
|
+
const found = collect(list2);
|
|
1840
|
+
const keepRoots = options.keep === void 0 ? [] : Array.isArray(options.keep) ? options.keep : [options.keep];
|
|
1841
|
+
const releasing = new Set(list2);
|
|
1842
|
+
for (const keep of keepRoots) {
|
|
1843
|
+
const walk = (o) => {
|
|
1844
|
+
if (releasing.has(o)) return;
|
|
1845
|
+
const owner = o;
|
|
1846
|
+
if (owner.geometry) found.geometries.delete(owner.geometry);
|
|
1847
|
+
if (owner.skeleton) found.skeletons.delete(owner.skeleton);
|
|
1848
|
+
const mats = Array.isArray(owner.material) ? owner.material : owner.material ? [owner.material] : [];
|
|
1849
|
+
for (const m of mats) {
|
|
1850
|
+
if (!m) continue;
|
|
1851
|
+
found.materials.delete(m);
|
|
1852
|
+
const t = /* @__PURE__ */ new Set();
|
|
1853
|
+
texturesOf(m, t);
|
|
1854
|
+
for (const x of t) found.textures.delete(x);
|
|
1855
|
+
}
|
|
1856
|
+
for (const c of o.children) walk(c);
|
|
1857
|
+
};
|
|
1858
|
+
walk(keep);
|
|
1859
|
+
}
|
|
1860
|
+
for (const g of found.geometries) {
|
|
1861
|
+
if (shared(g)) continue;
|
|
1862
|
+
g.dispose();
|
|
1863
|
+
report.geometries++;
|
|
1864
|
+
}
|
|
1865
|
+
for (const m of found.materials) {
|
|
1866
|
+
if (shared(m)) continue;
|
|
1867
|
+
m.dispose();
|
|
1868
|
+
report.materials++;
|
|
1869
|
+
}
|
|
1870
|
+
for (const t of found.textures) {
|
|
1871
|
+
if (shared(t)) continue;
|
|
1872
|
+
t.dispose();
|
|
1873
|
+
report.textures++;
|
|
1874
|
+
}
|
|
1875
|
+
for (const s of found.skeletons) s.dispose();
|
|
1876
|
+
return report;
|
|
1877
|
+
}
|
|
1878
|
+
function discardObject3D(objects, options = {}) {
|
|
1879
|
+
const list2 = Array.isArray(objects) ? objects : [objects];
|
|
1880
|
+
const keep = options.keep === void 0 ? [] : Array.isArray(options.keep) ? [...options.keep] : [options.keep];
|
|
1881
|
+
for (const object2 of list2) {
|
|
1882
|
+
let top = object2;
|
|
1883
|
+
while (top.parent) top = top.parent;
|
|
1884
|
+
object2.removeFromParent();
|
|
1885
|
+
if (top !== object2 && !keep.includes(top)) keep.push(top);
|
|
1886
|
+
}
|
|
1887
|
+
return releaseObject3D(list2, { keep });
|
|
1888
|
+
}
|
|
1793
1889
|
return __toCommonJS(src_exports);
|
|
1794
1890
|
})();
|
|
1795
1891
|
//# sourceMappingURL=index.global.js.map
|