Skip to content

Commit 6787a9e

Browse files
fix(Environment): dispose on unmount
1 parent e3c5e6c commit 6787a9e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/core/Environment.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export function EnvironmentCube({
100100
}: EnvironmentProps) {
101101
const texture = useEnvironment(rest)
102102
const defaultScene = useThree((state) => state.scene)
103+
103104
React.useLayoutEffect(() => {
104105
return setEnvProps(background, scene, defaultScene, texture, {
105106
backgroundBlurriness: blur ?? backgroundBlurriness,
@@ -109,6 +110,13 @@ export function EnvironmentCube({
109110
environmentRotation,
110111
})
111112
})
113+
114+
React.useEffect(() => {
115+
return () => {
116+
texture.dispose()
117+
}
118+
}, [texture])
119+
112120
return null
113121
}
114122

@@ -142,6 +150,12 @@ export function EnvironmentPortal({
142150
return fbo
143151
}, [resolution])
144152

153+
React.useEffect(() => {
154+
return () => {
155+
fbo.dispose()
156+
}
157+
}, [fbo])
158+
145159
React.useLayoutEffect(() => {
146160
if (frames === 1) {
147161
const autoClear = gl.autoClear
@@ -202,6 +216,12 @@ function EnvironmentGround(props: EnvironmentProps) {
202216

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

219+
React.useEffect(() => {
220+
return () => {
221+
textureDefault.dispose()
222+
}
223+
}, [textureDefault])
224+
205225
const args = React.useMemo<[CubeTexture | Texture]>(() => [texture], [texture])
206226
const height = (props.ground as any)?.height
207227
const radius = (props.ground as any)?.radius

0 commit comments

Comments
 (0)