SetGeometryCommand.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { Command } from '../Command.js';
  2. import { ObjectLoader } from 'three';
  3. /**
  4. * @param editor Editor
  5. * @param object THREE.Object3D
  6. * @param newGeometry THREE.Geometry
  7. * @constructor
  8. */
  9. class SetGeometryCommand extends Command {
  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 };