add persistent count for the number of model users to unload only when

uses drops to 0
This commit is contained in:
tresabhi committed 2026-07-05 13:44:04 -05:00
1 parent cdbb7647f0
commit f2f6d4ff49
1 file changed
+10
+10
View File
@@ -3,9 +3,19 @@ import { useEffect } from "react";
import { Cache, Group, Mesh, MeshStandardMaterial, Object3D } from "three";
import { GLTFLoader, type GLTF } from "three-stdlib";
const users: Record<string, number> = {};
export function useDispose(gltf: GLTF & ObjectMap, path: string) {
useEffect(() => {
if (path in users) {
users[path]++;
} else {
users[path] = 1;
}
return () => {
if (--users[path] > 0) return;
gltf.scene.traverse((object) => {
if (object instanceof Mesh) {
if (object.material instanceof MeshStandardMaterial) {