CmdAddObject.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Created by Daniel on 20.07.15.
  3. */
  4. CmdAddObject = function ( object ) {
  5. Cmd.call( this );
  6. this.type = 'CmdAddObject';
  7. this.object = object;
  8. if ( object !== undefined ) {
  9. this.name = 'Add Object: ' + object.name;
  10. }
  11. };
  12. CmdAddObject.prototype = {
  13. execute: function () {
  14. this.editor.addObject( this.object );
  15. },
  16. undo: function () {
  17. this.editor.removeObject( this.object );
  18. this.editor.deselect();
  19. },
  20. toJSON: function () {
  21. var output = Cmd.prototype.toJSON.call( this );
  22. this.object.updateMatrixWorld( true );
  23. meta = {
  24. geometries: {},
  25. materials: {},
  26. textures: {},
  27. images: {}
  28. };
  29. var json = this.object.toJSON( meta );
  30. var geometries = extractFromCache( meta.geometries );
  31. var materials = extractFromCache( meta.materials );
  32. var textures = extractFromCache( meta.textures );
  33. var images = extractFromCache( meta.images );
  34. if ( geometries.length > 0 ) json.geometries = geometries;
  35. if ( materials.length > 0 ) json.materials = materials;
  36. if ( textures.length > 0 ) json.textures = textures;
  37. if ( images.length > 0 ) json.images = images;
  38. output.object = json;
  39. return output;
  40. // Note: The function 'extractFromCache' is copied from Object3D.toJSON()
  41. // extract data from the cache hash
  42. // remove metadata on each item
  43. // and return as array
  44. function extractFromCache ( cache ) {
  45. var values = [];
  46. for ( var key in cache ) {
  47. var data = cache[ key ];
  48. delete data.metadata;
  49. values.push( data );
  50. }
  51. return values;
  52. }
  53. },
  54. fromJSON: function ( json ) {
  55. Cmd.prototype.fromJSON.call( this, json );
  56. this.object = this.editor.objectByUuid( json.object.object.uuid );
  57. if ( this.object === undefined ) {
  58. var loader = new THREE.ObjectLoader();
  59. this.object = loader.parse( json.object );
  60. }
  61. }
  62. };