CmdAddObject.js 2.0 KB

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