SetGeometryCommand.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { Command } from '../Command.js';
  2. import * as THREE from '../../../build/three.module.js';
  3. /**
  4. * @param editor Editor
  5. * @param object THREE.Object3D
  6. * @param newGeometry THREE.Geometry
  7. * @constructor
  8. */
  9. function SetGeometryCommand( editor, object, newGeometry ) {
  10. Command.call( this, editor );
  11. this.type = 'SetGeometryCommand';
  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. SetGeometryCommand.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. },
  36. toJSON: function () {
  37. var output = Command.prototype.toJSON.call( this );
  38. output.objectUuid = this.object.uuid;
  39. output.oldGeometry = this.object.geometry.toJSON();
  40. output.newGeometry = this.newGeometry.toJSON();
  41. return output;
  42. },
  43. fromJSON: function ( json ) {
  44. Command.prototype.fromJSON.call( this, 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. var loader = new THREE.ObjectLoader();
  50. return loader.parseGeometries( [ data ] )[ data.uuid ];
  51. }
  52. }
  53. };
  54. export { SetGeometryCommand };