Skip to content

Commit

Permalink
fix(Environment): dispose on unmount (#2297)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett authored Jan 11, 2025
1 parent e3c5e6c commit 715a9f5
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/core/Environment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export function EnvironmentCube({
}: EnvironmentProps) {
const texture = useEnvironment(rest)
const defaultScene = useThree((state) => state.scene)

React.useLayoutEffect(() => {
return setEnvProps(background, scene, defaultScene, texture, {
backgroundBlurriness: blur ?? backgroundBlurriness,
Expand All @@ -109,6 +110,13 @@ export function EnvironmentCube({
environmentRotation,
})
})

React.useEffect(() => {
return () => {
texture.dispose()
}
}, [texture])

return null
}

Expand Down Expand Up @@ -142,6 +150,12 @@ export function EnvironmentPortal({
return fbo
}, [resolution])

React.useEffect(() => {
return () => {
fbo.dispose()
}
}, [fbo])

React.useLayoutEffect(() => {
if (frames === 1) {
const autoClear = gl.autoClear
Expand Down Expand Up @@ -202,6 +216,12 @@ function EnvironmentGround(props: EnvironmentProps) {

React.useMemo(() => extend({ GroundProjectedEnvImpl }), [])

React.useEffect(() => {
return () => {
textureDefault.dispose()
}
}, [textureDefault])

const args = React.useMemo<[CubeTexture | Texture]>(() => [texture], [texture])
const height = (props.ground as any)?.height
const radius = (props.ground as any)?.radius
Expand Down

0 comments on commit 715a9f5

Please sign in to comment.