CmdAddObject.js 1.8 KB

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