SetGeometryCommand.js 1.8 KB

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