CmdRemoveObject.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. * Developed as part of a project at University of Applied Sciences and Arts Northwestern Switzerland (www.fhnw.ch)
  4. */
  5. /**
  6. * @param object THREE.Object3D
  7. * @constructor
  8. */
  9. CmdRemoveObject = function ( object ) {
  10. Cmd.call( this );
  11. this.type = 'CmdRemoveObject';
  12. this.name = 'Remove Object';
  13. this.object = object;
  14. this.parent = ( object !== undefined ) ? object.parent : undefined;
  15. if ( this.parent !== undefined ) {
  16. this.index = this.parent.children.indexOf( this.object );
  17. }
  18. };
  19. CmdRemoveObject.prototype = {
  20. execute: function () {
  21. var scope = this.editor;
  22. this.object.traverse( function ( child ) {
  23. scope.removeHelper( child );
  24. } );
  25. this.parent.remove( this.object );
  26. this.editor.signals.objectRemoved.dispatch( this.object );
  27. this.editor.signals.sceneGraphChanged.dispatch();
  28. },
  29. undo: function () {
  30. var scope = this.editor;
  31. this.object.traverse( function ( child ) {
  32. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  33. if ( child.material !== undefined ) scope.addMaterial( child.material );
  34. scope.addHelper( child );
  35. } );
  36. this.parent.children.splice( this.index, 0, this.object );
  37. this.object.parent = this.parent;
  38. this.editor.signals.objectAdded.dispatch( this.object );
  39. this.editor.signals.sceneGraphChanged.dispatch();
  40. },
  41. toJSON: function () {
  42. var output = Cmd.prototype.toJSON.call( this );
  43. this.object.updateMatrixWorld( true );
  44. var meta = {
  45. geometries: {},
  46. materials: {},
  47. textures: {},
  48. images: {}
  49. };
  50. var json = this.object.toJSON( meta );
  51. var geometries = extractFromCache( meta.geometries );
  52. var materials = extractFromCache( meta.materials );
  53. var textures = extractFromCache( meta.textures );
  54. var images = extractFromCache( meta.images );
  55. if ( geometries.length > 0 ) json.geometries = geometries;
  56. if ( materials.length > 0 ) json.materials = materials;
  57. if ( textures.length > 0 ) json.textures = textures;
  58. if ( images.length > 0 ) json.images = images;
  59. output.object = json;
  60. output.index = this.index;
  61. output.parentUuid = this.parent.uuid;
  62. return output;
  63. // Note: The function 'extractFromCache' is copied from Object3D.toJSON()
  64. // extract data from the cache hash
  65. // remove metadata on each item
  66. // and return as array
  67. function extractFromCache ( cache ) {
  68. var values = [];
  69. for ( var key in cache ) {
  70. var data = cache[ key ];
  71. delete data.metadata;
  72. values.push( data );
  73. }
  74. return values;
  75. }
  76. },
  77. fromJSON: function ( json ) {
  78. Cmd.prototype.fromJSON.call( this, json );
  79. this.parent = this.editor.objectByUuid( json.parentUuid );
  80. if ( this.parent === undefined ) {
  81. this.parent = this.editor.scene;
  82. }
  83. this.index = json.index;
  84. this.object = this.editor.objectByUuid( json.object.object.uuid );
  85. if ( this.object === undefined ) {
  86. var loader = new THREE.ObjectLoader();
  87. this.object = loader.parse( json.object );
  88. }
  89. }
  90. };