SetGeometryCommand.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 editor Editor
  7. * @param object THREE.Object3D
  8. * @param newGeometry THREE.Geometry
  9. * @constructor
  10. */
  11. var SetGeometryCommand = function ( editor, object, newGeometry ) {
  12. Command.call( this, editor );
  13. this.type = 'SetGeometryCommand';
  14. this.name = 'Set Geometry';
  15. this.updatable = true;
  16. this.object = object;
  17. this.oldGeometry = ( object !== undefined ) ? object.geometry : undefined;
  18. this.newGeometry = newGeometry;
  19. };
  20. SetGeometryCommand.prototype = {
  21. execute: function () {
  22. this.object.geometry.dispose();
  23. this.object.geometry = this.newGeometry;
  24. this.object.geometry.computeBoundingSphere();
  25. this.editor.signals.geometryChanged.dispatch( this.object );
  26. this.editor.signals.sceneGraphChanged.dispatch();
  27. },
  28. undo: function () {
  29. this.object.geometry.dispose();
  30. this.object.geometry = this.oldGeometry;
  31. this.object.geometry.computeBoundingSphere();
  32. this.editor.signals.geometryChanged.dispatch( this.object );
  33. this.editor.signals.sceneGraphChanged.dispatch();
  34. },
  35. update: function ( cmd ) {
  36. this.newGeometry = cmd.newGeometry;
  37. },
  38. toJSON: function () {
  39. var output = Command.prototype.toJSON.call( this );
  40. output.objectUuid = this.object.uuid;
  41. output.oldGeometry = this.object.geometry.toJSON();
  42. output.newGeometry = this.newGeometry.toJSON();
  43. return output;
  44. },
  45. fromJSON: function ( json ) {
  46. Command.prototype.fromJSON.call( this, json );
  47. this.object = this.editor.objectByUuid( json.objectUuid );
  48. this.oldGeometry = parseGeometry( json.oldGeometry );
  49. this.newGeometry = parseGeometry( json.newGeometry );
  50. function parseGeometry ( data ) {
  51. var loader = new THREE.ObjectLoader();
  52. return loader.parseGeometries( [ data ] )[ data.uuid ];
  53. }
  54. }
  55. };