CmdAddObject.js 1.8 KB

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