In order to save memory in my application I've decided to dispose of any 3D models that escape the view of the camera. To do this I used a method which detects if the camera is able to see the position of the model (using model.position). The below code is within my animate loop.
const frustum = new THREE.Frustum();
const matrix = new THREE.Matrix4().multiplyMatrices(test.camera.projectionMatrix, test.camera.matrixWorldInverse);
frustum.setFromProjectionMatrix(matrix);
let earthContainerPos = { ...earthContainer.position }; //spread syntax removes reference to container position
earthContainerPos.z += 10; //to ensure its only disposed once completely out of view (since .position is the center of the model)
if (!frustum.containsPoint(earthContainerPos)) { //check if container in view of camera
console.log('Out of view')
earthContainer.dispose(); //dispose of the model - I thought .dispose would work since its being rendered by a webGL renderer (from the docs) but it didnt
};
The method works for detecting when my model is out of view, but for some reason the dispose method for my container (which holds 2 3D Models) is not working. Do I have to dispose of everything or is there a more dynamic method of disposal?
Model Details:
- GLTF models
- 2 models in the container