fix: add disposal on unmount in SkyModel and SimpleModel
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { useMemo } from "react";
|
import { useEffect, useMemo } from "react";
|
||||||
import { useLoggedGLTF } from "@/hooks/three/useLoggedGLTF";
|
import { useLoggedGLTF } from "@/hooks/three/useLoggedGLTF";
|
||||||
import type { ModelTransformProps, Vector3Tuple } from "@/types/three/three";
|
import type { ModelTransformProps, Vector3Tuple } from "@/types/three/three";
|
||||||
|
import { disposeObject3D } from "@/utils/three/dispose";
|
||||||
|
|
||||||
export interface SimpleModelConfig extends ModelTransformProps {
|
export interface SimpleModelConfig extends ModelTransformProps {
|
||||||
modelPath: string;
|
modelPath: string;
|
||||||
@@ -29,6 +30,12 @@ export function SimpleModel({
|
|||||||
});
|
});
|
||||||
const model = useMemo(() => scene.clone(true), [scene]);
|
const model = useMemo(() => scene.clone(true), [scene]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
disposeObject3D(model);
|
||||||
|
};
|
||||||
|
}, [model]);
|
||||||
|
|
||||||
const parsedScale =
|
const parsedScale =
|
||||||
typeof scale === "number" ? ([scale, scale, scale] as Vector3Tuple) : scale;
|
typeof scale === "number" ? ([scale, scale, scale] as Vector3Tuple) : scale;
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { useFrame, useThree } from "@react-three/fiber";
|
import { useFrame, useThree } from "@react-three/fiber";
|
||||||
import { useGLTF } from "@react-three/drei";
|
import { useGLTF } from "@react-three/drei";
|
||||||
import { Component, useMemo, useRef, type ReactNode } from "react";
|
import { Component, useEffect, useMemo, useRef, type ReactNode } from "react";
|
||||||
import * as THREE from "three";
|
import * as THREE from "three";
|
||||||
import { useLoggedGLTF } from "@/hooks/three/useLoggedGLTF";
|
import { useLoggedGLTF } from "@/hooks/three/useLoggedGLTF";
|
||||||
|
import { disposeObject3D } from "@/utils/three/dispose";
|
||||||
|
|
||||||
interface SkyModelProps {
|
interface SkyModelProps {
|
||||||
modelPath: string;
|
modelPath: string;
|
||||||
@@ -80,6 +81,12 @@ function SkyModelContent({
|
|||||||
});
|
});
|
||||||
const model = useMemo(() => createSkyModel(scene), [scene]);
|
const model = useMemo(() => createSkyModel(scene), [scene]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
disposeObject3D(model);
|
||||||
|
};
|
||||||
|
}, [model]);
|
||||||
|
|
||||||
useFrame(() => {
|
useFrame(() => {
|
||||||
groupRef.current?.position.copy(camera.position);
|
groupRef.current?.position.copy(camera.position);
|
||||||
});
|
});
|
||||||
@@ -122,5 +129,5 @@ function createSkyMaterial<T extends THREE.Material>(material: T): T {
|
|||||||
return skyMaterial as T;
|
return skyMaterial as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
useGLTF.preload("/models/skybox/skybox.gltf");
|
useGLTF.preload("/models/skybox/model.gltf");
|
||||||
useGLTF.preload(LEGACY_SKY_MODEL_PATH);
|
useGLTF.preload(LEGACY_SKY_MODEL_PATH);
|
||||||
|
|||||||
Reference in New Issue
Block a user