CmdAddObject.js 1.8 KB

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