Properly Dispose Objects on Deletion (#664)

Properly deletes objects by disposing of their textures, materials,
geometry from memory.
This commit is contained in:
Miles Ryan authored and GitHub committed 2024-08-19 11:22:30 -04:00
1 parent 73cadb5e9f
commit 0a12da9a5f
1 file changed
+57 -25
+57 -25
View File
@@ -230,30 +230,62 @@ function removeObject3D(object3D) {
*/
deleteObject(uuid: string) {
const obj = this.scene.get_object_by_uuid(uuid);
this.removeTransformControls();
if (obj?.name === this.editor.camera_name) {
return;
}
if (obj) {
this.scene.scene.remove(obj);
}
this.editor.timeline.deleteObject(uuid);
Queue.publish({
queueName: QueueNames.FROM_ENGINE,
action: fromEngineActions.DELETE_OBJECT,
data: {
version: 1,
type: AssetType.OBJECT,
media_id: "",
object_uuid: uuid,
name: "",
} as MediaItem,
});
this.editor.selected = undefined;
this.editor.publishSelect();
hideObjectPanel();
this.editor.timeline.deleteObject(uuid);
deleteObject(uuid: string) {
const obj = this.scene.get_object_by_uuid(uuid);
this.removeTransformControls();
if (obj?.name === this.editor.camera_name) {
return;
}
if (obj) {
// Finally remove the object from the scene
this.scene.scene.remove(obj);
obj.traverse(child => {
(child as THREE.Mesh)?.geometry?.dispose()
if (Array.isArray((child as THREE.Mesh).texture)) {
(child as THREE.Mesh).texture.forEach(mat => mat.dispose());
} else if ((child as THREE.Mesh).texture) {
(child as THREE.Mesh).texture.dispose();
}
if (Array.isArray((child as THREE.Mesh).material)) {
(child as THREE.Mesh).material.forEach(mat => mat.dispose());
} else if ((child as THREE.Mesh).material) {
(child as THREE.Mesh).material.dispose();
}
})
if (Array.isArray((obj as THREE.Mesh).texture)) {
(obj as THREE.Mesh).texture.forEach(mat => mat.dispose());
} else if ((obj as THREE.Mesh).texture) {
(obj as THREE.Mesh).texture.dispose();
}
if (Array.isArray((obj as THREE.Mesh).material)) {
(obj as THREE.Mesh).material.forEach(mat => mat.dispose());
} else if ((obj as THREE.Mesh).material) {
(obj as THREE.Mesh).material.dispose();
}
if((obj as THREE.Mesh).geometry){
(obj as THREE.Mesh).geometry.dispose()
}
}
this.editor.timeline.deleteObject(uuid);
Queue.publish({
queueName: QueueNames.FROM_ENGINE,
action: fromEngineActions.DELETE_OBJECT,
data: {
version: 1,
type: AssetType.OBJECT,
media_id: "",
object_uuid: uuid,
name: "",
} as MediaItem,
});
this.editor.selected = undefined;
this.editor.publishSelect();
hideObjectPanel();
this.editor.timeline.deleteObject(uuid);
}
}