Files
vibemake/js/assembly-memory.js
T

24 lines
1.0 KiB
JavaScript

// Limit decoding concurrency on phones without altering the uploaded assets.
export function constrainedAssemblyDevice() {
return !!globalThis.matchMedia?.('(pointer: coarse)').matches
|| (globalThis.navigator?.deviceMemory > 0 && navigator.deviceMemory <= 4);
}
export function disposeAssemblyObjects(roots) {
const resources = new Set(), images = new Set();
for (const root of [roots].flat().filter(Boolean)) root.traverse((object) => {
if (object.geometry) resources.add(object.geometry);
for (const material of [object.material].flat().filter(Boolean)) {
resources.add(material);
for (const value of Object.values(material)) if (value?.isTexture) {
resources.add(value);
if (value.source?.data) images.add(value.source.data);
}
}
});
for (const resource of resources) resource.dispose();
// Texture.dispose() only frees WebGL storage; Safari's decoded ImageBitmap
// backing memory needs an explicit close as well.
for (const image of images) image.close?.();
}