SetMaterialRangeCommand.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { Command } from '../Command.js';
  2. /**
  3. * @param editor Editor
  4. * @param object THREE.Object3D
  5. * @param attributeName string
  6. * @param newMinValue number
  7. * @param newMaxValue number
  8. * @constructor
  9. */
  10. class SetMaterialRangeCommand extends Command {
  11. constructor( editor, object, attributeName, newMinValue, newMaxValue, materialSlot ) {
  12. super( editor );
  13. this.type = 'SetMaterialRangeCommand';
  14. this.name = `Set Material.${attributeName}`;
  15. this.updatable = true;
  16. this.object = object;
  17. this.material = this.editor.getObjectMaterial( object, materialSlot );
  18. this.oldRange = ( this.material !== undefined && this.material[ attributeName ] !== undefined ) ? [ ...this.material[ attributeName ] ] : undefined;
  19. this.newRange = [ newMinValue, newMaxValue ];
  20. this.attributeName = attributeName;
  21. }
  22. execute() {
  23. this.material[ this.attributeName ] = [ ...this.newRange ];
  24. this.material.needsUpdate = true;
  25. this.editor.signals.objectChanged.dispatch( this.object );
  26. this.editor.signals.materialChanged.dispatch( this.material );
  27. }
  28. undo() {
  29. this.material[ this.attributeName ] = [ ...this.oldRange ];
  30. this.material.needsUpdate = true;
  31. this.editor.signals.objectChanged.dispatch( this.object );
  32. this.editor.signals.materialChanged.dispatch( this.material );
  33. }
  34. update( cmd ) {
  35. this.newRange = [ ...cmd.newRange ];
  36. }
  37. toJSON() {
  38. const output = super.toJSON( this );
  39. output.objectUuid = this.object.uuid;
  40. output.attributeName = this.attributeName;
  41. output.oldRange = [ ...this.oldRange ];
  42. output.newRange = [ ...this.newRange ];
  43. return output;
  44. }
  45. fromJSON( json ) {
  46. super.fromJSON( json );
  47. this.attributeName = json.attributeName;
  48. this.oldRange = [ ...json.oldRange ];
  49. this.newRange = [ ...json.newRange ];
  50. this.object = this.editor.objectByUuid( json.objectUuid );
  51. }
  52. }
  53. export { SetMaterialRangeCommand };