/** * @author dforrer / https://github.com/dforrer */ /** * @param object THREE.Object3D * @constructor */ CmdRemoveObject = function ( object ) { Cmd.call( this ); this.type = 'CmdRemoveObject'; this.name = 'Remove Object'; this.object = object; this.parent = ( object !== undefined ) ? object.parent : undefined; if ( this.parent !== undefined ) { this.index = this.parent.children.indexOf( this.object ); } }; CmdRemoveObject.prototype = { execute: function () { var scope = this.editor; this.object.traverse( function ( child ) { scope.removeHelper( child ); } ); this.parent.remove( this.object ); this.editor.signals.objectRemoved.dispatch( this.object ); this.editor.signals.sceneGraphChanged.dispatch(); }, undo: function () { var scope = this.editor; this.object.traverse( function ( child ) { if ( child.geometry !== undefined ) scope.addGeometry( child.geometry ); if ( child.material !== undefined ) scope.addMaterial( child.material ); scope.addHelper( child ); } ); this.parent.children.splice( this.index, 0, this.object ); this.object.parent = this.parent; this.editor.signals.objectAdded.dispatch( this.object ); this.editor.signals.sceneGraphChanged.dispatch(); }, toJSON: function () { var output = Cmd.prototype.toJSON.call( this ); this.object.updateMatrixWorld( true ); var meta = { geometries: {}, materials: {}, textures: {}, images: {} }; var json = this.object.toJSON( meta ); var geometries = extractFromCache( meta.geometries ); var materials = extractFromCache( meta.materials ); var textures = extractFromCache( meta.textures ); var images = extractFromCache( meta.images ); if ( geometries.length > 0 ) json.geometries = geometries; if ( materials.length > 0 ) json.materials = materials; if ( textures.length > 0 ) json.textures = textures; if ( images.length > 0 ) json.images = images; output.object = json; output.index = this.index; output.parentUuid = this.parent.uuid; return output; // Note: The function 'extractFromCache' is copied from Object3D.toJSON() // extract data from the cache hash // remove metadata on each item // and return as array function extractFromCache ( cache ) { var values = []; for ( var key in cache ) { var data = cache[ key ]; delete data.metadata; values.push( data ); } return values; } }, fromJSON: function ( json ) { Cmd.prototype.fromJSON.call( this, json ); this.parent = this.editor.objectByUuid( json.parentUuid ); if ( this.parent === undefined ) { this.parent = this.editor.scene; } this.index = json.index; this.object = this.editor.objectByUuid( json.object.object.uuid ); if ( this.object === undefined ) { var loader = new THREE.ObjectLoader(); this.object = loader.parse( json.object ); } } };