CmdSetGeometry.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. */
  4. /**
  5. * @param object THREE.Object3D
  6. * @param newGeometry THREE.Geometry
  7. * @constructor
  8. */
  9. CmdSetGeometry = function ( object, newGeometry ) {
  10. Cmd.call( this );
  11. this.type = 'CmdSetGeometry';
  12. this.name = 'Set Geometry';
  13. this.updatable = true;
  14. this.object = object;
  15. this.oldGeometry = ( object !== undefined ) ? object.geometry : undefined;
  16. this.newGeometry = newGeometry;
  17. };
  18. CmdSetGeometry.prototype = {
  19. execute: function () {
  20. this.object.geometry.dispose();
  21. this.object.geometry = this.newGeometry;
  22. this.object.geometry.computeBoundingSphere();
  23. this.editor.signals.geometryChanged.dispatch( this.object );
  24. this.editor.signals.sceneGraphChanged.dispatch();
  25. },
  26. undo: function () {
  27. this.object.geometry.dispose();
  28. this.object.geometry = this.oldGeometry;
  29. this.object.geometry.computeBoundingSphere();
  30. this.editor.signals.geometryChanged.dispatch( this.object );
  31. this.editor.signals.sceneGraphChanged.dispatch();
  32. },
  33. update: function ( cmd ) {
  34. this.newGeometry = cmd.newGeometry;
  35. this.newGeometryJSON = cmd.newGeometry.toJSON();
  36. },
  37. toJSON: function () {
  38. var output = Cmd.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. Cmd.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. };