SetGeometryCommand.js 2.0 KB

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