try to fix memory leak
This commit is contained in:
parent
a3bcd19c24
commit
6ecc292d92
2 changed files with 426 additions and 542 deletions
|
@ -24,6 +24,9 @@ class ThreeViewer extends React.PureComponent<Props> {
|
||||||
|
|
||||||
const { theme } = this.props;
|
const { theme } = this.props;
|
||||||
|
|
||||||
|
this.scene = null;
|
||||||
|
this.mesh = null;
|
||||||
|
|
||||||
// Main container
|
// Main container
|
||||||
this.viewer = React.createRef();
|
this.viewer = React.createRef();
|
||||||
|
|
||||||
|
@ -75,7 +78,29 @@ class ThreeViewer extends React.PureComponent<Props> {
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
|
// Remove event listeners
|
||||||
window.removeEventListener('resize', this.handleResize, false);
|
window.removeEventListener('resize', this.handleResize, false);
|
||||||
|
|
||||||
|
// Free memory
|
||||||
|
if (this.mesh) {
|
||||||
|
// Clean up group
|
||||||
|
this.scene.remove(this.mesh);
|
||||||
|
if (this.mesh.geometry) this.mesh.geometry.dispose();
|
||||||
|
if (this.mesh.material) this.mesh.material.dispose();
|
||||||
|
// Clean up group items
|
||||||
|
this.mesh.traverse(child => {
|
||||||
|
if (child instanceof THREE.Mesh) {
|
||||||
|
if (child.geometry) child.geometry.dispose();
|
||||||
|
if (child.material) child.material.dispose();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// It's unclear if we need this:
|
||||||
|
// https://github.com/mrdoob/three.js/issues/12447
|
||||||
|
this.mesh = null;
|
||||||
|
this.renderer.renderLists.dispose();
|
||||||
|
this.renderer.dispose();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
transformGroup(group) {
|
transformGroup(group) {
|
||||||
|
@ -281,20 +306,24 @@ class ThreeViewer extends React.PureComponent<Props> {
|
||||||
|
|
||||||
// Create model material
|
// Create model material
|
||||||
this.material = new THREE.MeshPhongMaterial({
|
this.material = new THREE.MeshPhongMaterial({
|
||||||
opacity: 1,
|
// opacity: 1,
|
||||||
transparent: true,
|
// transparent: true,
|
||||||
// depthWrite: true,
|
depthWrite: true,
|
||||||
vertexColors: THREE.FaceColors,
|
vertexColors: THREE.FaceColors,
|
||||||
// Positive value pushes polygon further away
|
// Positive value pushes polygon further away
|
||||||
// polygonOffsetFactor: 1,
|
// polygonOffsetFactor: 1,
|
||||||
// polygonOffsetUnits: 1,
|
// polygonOffsetUnits: 1,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set material color
|
// Set material color
|
||||||
this.material.color.set(this.materialColors.green);
|
this.material.color.set(this.materialColors.green);
|
||||||
|
|
||||||
// Load file and render mesh
|
// Load file and render mesh
|
||||||
this.startLoader();
|
this.startLoader();
|
||||||
|
|
||||||
|
// Append canvas
|
||||||
|
viewer.appendChild(canvas);
|
||||||
|
|
||||||
const updateScene = () => {
|
const updateScene = () => {
|
||||||
requestAnimationFrame(updateScene);
|
requestAnimationFrame(updateScene);
|
||||||
this.controls.update();
|
this.controls.update();
|
||||||
|
@ -302,19 +331,18 @@ class ThreeViewer extends React.PureComponent<Props> {
|
||||||
};
|
};
|
||||||
|
|
||||||
updateScene();
|
updateScene();
|
||||||
// Append canvas
|
|
||||||
viewer.appendChild(canvas);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { error, isReady, isLoading } = this.state;
|
const { error, isReady, isLoading } = this.state;
|
||||||
const loadingMessage = 'Loading 3D model.';
|
const errorMessage = error;
|
||||||
|
const loadingMessage = __('Loading 3D model.');
|
||||||
const showViewer = isReady && !error;
|
const showViewer = isReady && !error;
|
||||||
const showLoading = isLoading && !error;
|
const showLoading = isLoading && !error;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
{error && <LoadingScreen status={error} spinner={false} />}
|
{error && <LoadingScreen status={errorMessage} spinner={false} />}
|
||||||
{showLoading && <LoadingScreen status={loadingMessage} spinner />}
|
{showLoading && <LoadingScreen status={loadingMessage} spinner />}
|
||||||
<div
|
<div
|
||||||
style={{ opacity: showViewer ? 1 : 0 }}
|
style={{ opacity: showViewer ? 1 : 0 }}
|
||||||
|
|
Loading…
Add table
Reference in a new issue