SetGeometryCommand.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { Command } from '../Command.js';
  2. import { ObjectLoader } from 'three';
  3. class SetGeometryCommand extends Command {
  4. /**
  5. * @param {Editor} editor
  6. * @param {THREE.Object3D|null} [object=null]
  7. * @param {THREE.Geometry|null} [newGeometry=null]
  8. * @constructor
  9. */
  10. constructor( editor, object = null, newGeometry = null ) {
  11. super( editor );
  12. this.type = 'SetGeometryCommand';
  13. this.name = editor.strings.getKey( 'command/SetGeometry' );
  14. this.updatable = true;
  15. this.object = object;
  16. this.oldGeometry = ( object !== null ) ? object.geometry : null;
  17. this.newGeometry = newGeometry;
  18. }
  19. execute() {
  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() {
  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( cmd ) {
  34. this.newGeometry = cmd.newGeometry;
  35. }
  36. toJSON() {
  37. const output = super.toJSON( this );
  38. output.objectUuid = this.object.uuid;
  39. output.oldGeometry = this.oldGeometry.toJSON();
  40. output.newGeometry = this.newGeometry.toJSON();
  41. return output;
  42. }
  43. fromJSON( json ) {
  44. super.fromJSON( json );
  45. this.object = this.editor.objectByUuid( json.objectUuid );
  46. this.oldGeometry = parseGeometry( json.oldGeometry );
  47. this.newGeometry = parseGeometry( json.newGeometry );
  48. function parseGeometry( data ) {
  49. const loader = new ObjectLoader();
  50. return loader.parseGeometries( [ data ] )[ data.uuid ];
  51. }
  52. }
  53. }
  54. export { SetGeometryCommand };
粤ICP备19079148号