Skip to content

Commit

Permalink
Cleaned object dispose for 3d scene
Browse files Browse the repository at this point in the history
  • Loading branch information
danilosalvati committed Oct 24, 2016
1 parent 5907ac2 commit ab78a73
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 87 deletions.
86 changes: 4 additions & 82 deletions src/components/viewer3d/scene-creator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Three from 'three';
import createGrid from './grid-creator';
import convert from 'convert-units';
import {disposeObject} from './three-memory-cleaner';

export function parseData(sceneData, editingActions, catalog) {

Expand Down Expand Up @@ -261,33 +262,7 @@ function replaceLine(layer, oldLineObject, newLineData, editingActions, planData

planData.plan.remove(oldLineObject);

oldLineObject.traverse(function (child) {
if (child instanceof Three.Mesh || child instanceof Three.BoxHelper) {

child.geometry.dispose();

let material = child.material;

if (material instanceof Three.MultiMaterial) {
material.materials.forEach(childMaterial => {
let childTexture = childMaterial.map;
if (childTexture) {
childTexture.dispose();
}

childMaterial.dispose();
});
} else {
let texture = child.material.map;
// Save memory for the renderer
if (texture) {
texture.dispose();
}
material.dispose();
}
}
});

disposeObject(oldLineObject);
oldLineObject = null;

planData.plan.add(newLineObject);
Expand Down Expand Up @@ -343,33 +318,7 @@ function replaceArea(layer, oldAreaObject, newAreaData, editingActions, planData
planData.grid.position.z += oldCenter[2];

planData.plan.remove(oldAreaObject);

oldAreaObject.traverse(function (child) {
if (child instanceof Three.Mesh || child instanceof Three.BoxHelper) {

child.geometry.dispose();

let material = child.material;

if (material instanceof Three.MultiMaterial) {
material.materials.forEach(childMaterial => {
let childTexture = childMaterial.map;
if (childTexture) {
childTexture.dispose();
}

childMaterial.dispose();
});
} else {
let texture = child.material.map;
// Save memory for the renderer
if (texture) {
texture.dispose();
}
material.dispose();
}
}
});
disposeObject(oldAreaObject);

planData.plan.add(newAreaObject);

Expand All @@ -392,7 +341,6 @@ function replaceArea(layer, oldAreaObject, newAreaData, editingActions, planData
return newAreaObject;
}

planData.plan.remove(oldAreaObject);
return new Three.Object3D();

}
Expand Down Expand Up @@ -435,33 +383,7 @@ function createItem(layer, item, editingActions, sceneGraph, catalog, plan, scen
function replaceItem(layer, oldItemObject, newItemData, editingActions, planData, catalog, scene) {

planData.plan.remove(oldItemObject);

oldItemObject.traverse(function (child) {
if (child instanceof Three.Mesh || child instanceof Three.BoxHelper) {

child.geometry.dispose();

let material = child.material;

if (material instanceof Three.MultiMaterial) {
material.materials.forEach(childMaterial => {
let childTexture = childMaterial.map;
if (childTexture) {
childTexture.dispose();
}

childMaterial.dispose();
});
} else {
let texture = child.material.map;
// Save memory for the renderer
if (texture) {
texture.dispose();
}
material.dispose();
}
}
});
disposeObject(oldItemObject);

let item3DPromise = catalog.getElement(newItemData.type).render3D(newItemData, layer, scene);

Expand Down
11 changes: 6 additions & 5 deletions src/components/viewer3d/three-memory-cleaner.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Three from 'three';

function disposeGeometry(geometry) {
geometry.dispose();
}
Expand Down Expand Up @@ -28,24 +30,23 @@ function disposeMaterial(material) {
material.dispose();
}

function disposeMesh(scene3D, mesh) {
function disposeMesh(mesh) {
if (!(mesh instanceof Three.Mesh || mesh instanceof Three.BoxHelper)) {
return;
}
scene3D.remove(mesh);
disposeGeometry(mesh.geometry);
disposeMultimaterial(mesh.material);
disposeMaterial(mesh.material);
}

export function disposeScene(scene3D) {
scene3D.traverse(child => {
disposeMesh(scene3D, child);
disposeMesh(child);
});
}

export function disposeObject(scene3D, object) {
export function disposeObject(object) {
object.traverse(child => {
disposeMesh(scene3D, child);
disposeMesh(child);
});
}

0 comments on commit ab78a73

Please sign in to comment.