I am using the Environment component for my scene as the background for my game. I have a customization studio where the user can change the background of their game. This is done by keeping the user settings in state and a URL representing the background chosen. When the user wants to change the background, they click a button that updates the URL part of the state which causes the Environment to reload with the new image. Since the Environment is handling the loading of the image it is also managing and caching its resources. The issue occurs when you change the background a few times which causes the memory to overflow. In my case three backgrounds is usually my consistent limit and I can interchange between them after I've loaded them but loading a fourth causes the crash.
Error Codes
THREE.WebGLProgram: Shader Error 0 - VALIDATE_STATUS false
Material Name: CubemapFromEquirect
Material Type: ShaderMaterial
Program Info Log:
console.error @ GamePageIndex.1718885885000.js:227
GamePageIndex.1718885885000.js:168 WebGL: INVALID_OPERATION: useProgram: program not valid
x1 @ GamePageIndex.1718885885000.js:168
localhost/:1 WebGL: CONTEXT_LOST_WEBGL: loseContext: context lost
GamePageIndex.1718885885000.js:227 THREE.WebGLRenderer: Context Lost.
There needs to be a prop for Environment that can control the disposal of the loaded textures. I believe the default should be that on change it disposes of the last texture and then loads the new one. The texture created from environment is very resource heavy. Therefore, users could still override this behavior by setting the prop and cache the other loaded environments. Without the default users having to know about this prop to avoid potential memory overflow.
three
version: 0.165.0@react-three/fiber
version: 8.16.8@react-three/drei
version: 9.107.0node
(bun)
version: 9.107.0(ornpm
) (bun) version: 1.1.15yarn
Problem description:
I am using the Environment component for my scene as the background for my game. I have a customization studio where the user can change the background of their game. This is done by keeping the user settings in state and a URL representing the background chosen. When the user wants to change the background, they click a button that updates the URL part of the state which causes the Environment to reload with the new image. Since the Environment is handling the loading of the image it is also managing and caching its resources. The issue occurs when you change the background a few times which causes the memory to overflow. In my case three backgrounds is usually my consistent limit and I can interchange between them after I've loaded them but loading a fourth causes the crash.
Error Codes
Video
https://github.com/pmndrs/drei/assets/140863288/aea7981b-bd4b-4146-a4c4-c0f676049602
Relevant code:
Suggested solution:
There needs to be a prop for Environment that can control the disposal of the loaded textures. I believe the default should be that on change it disposes of the last texture and then loads the new one. The texture created from environment is very resource heavy. Therefore, users could still override this behavior by setting the prop and cache the other loaded environments. Without the default users having to know about this prop to avoid potential memory overflow.