mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 18:57:03 +02:00
clean up
This commit is contained in:
1 parent
100c7391fb
commit
2f745a16d1
1 file changed
+34
-51
@@ -1,62 +1,45 @@
|
||||
import { useLoader } from "@react-three/fiber";
|
||||
import { useLoader, type ObjectMap } from "@react-three/fiber";
|
||||
import { useEffect } from "react";
|
||||
import { Cache } from "three";
|
||||
import { GLTFLoader } from "three-stdlib";
|
||||
import { Cache, Group, Mesh, MeshStandardMaterial, Object3D } from "three";
|
||||
import { GLTFLoader, type GLTF } from "three-stdlib";
|
||||
|
||||
export function useDispose(gltf: any, path: string) {
|
||||
export function useDispose(gltf: GLTF & ObjectMap, path: string) {
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
const disposedGeometries = new Set<string>();
|
||||
const disposedMaterials = new Set<string>();
|
||||
const disposedTextures = new Set<string>();
|
||||
gltf.scene.traverse((object) => {
|
||||
if (object instanceof Mesh) {
|
||||
if (object.material instanceof MeshStandardMaterial) {
|
||||
const maps = [
|
||||
object.material.map,
|
||||
object.material.aoMap,
|
||||
object.material.envMap,
|
||||
object.material.bumpMap,
|
||||
object.material.alphaMap,
|
||||
object.material.lightMap,
|
||||
object.material.normalMap,
|
||||
object.material.emissiveMap,
|
||||
object.material.roughnessMap,
|
||||
object.material.metalnessMap,
|
||||
object.material.displacementMap,
|
||||
];
|
||||
|
||||
gltf.scene.traverse((obj: any) => {
|
||||
if (obj.geometry) {
|
||||
const geometry = obj.geometry;
|
||||
for (const map of maps) {
|
||||
if (!map) continue;
|
||||
|
||||
if (!disposedGeometries.has(geometry.uuid)) {
|
||||
disposedGeometries.add(geometry.uuid);
|
||||
geometry.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
if (obj.material) {
|
||||
const materials = Array.isArray(obj.material)
|
||||
? obj.material
|
||||
: [obj.material];
|
||||
|
||||
for (const material of materials) {
|
||||
if (!disposedMaterials.has(material.uuid)) {
|
||||
disposedMaterials.add(material.uuid);
|
||||
|
||||
for (const key in material) {
|
||||
const value = material[key];
|
||||
|
||||
if (
|
||||
value &&
|
||||
value.isTexture &&
|
||||
!disposedTextures.has(value.uuid)
|
||||
) {
|
||||
disposedTextures.add(value.uuid);
|
||||
}
|
||||
}
|
||||
|
||||
for (const key in material) {
|
||||
const value = material[key];
|
||||
|
||||
if (value && value.isTexture) {
|
||||
value.source.data.close();
|
||||
value.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
material.dispose();
|
||||
map.source.data.close();
|
||||
map.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (obj.skeleton) {
|
||||
obj.skeleton.dispose?.();
|
||||
object.material.dispose();
|
||||
} else {
|
||||
throw new TypeError(
|
||||
`Unexpected material type: ${object.material.type}`,
|
||||
);
|
||||
}
|
||||
} else if (object instanceof Group || object instanceof Object3D) {
|
||||
// traverse will get children
|
||||
} else {
|
||||
throw new TypeError(`Unexpected object type: ${object}`);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user