SetMaterialRangeCommand.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 = null, attributeName = '', newMinValue = - Infinity, newMaxValue = Infinity, materialSlot = - 1 ) {
  12. super( editor );
  13. this.type = 'SetMaterialRangeCommand';
  14. this.name = editor.strings.getKey( 'command/SetMaterialRange' ) + ': ' + attributeName;
  15. this.updatable = true;
  16. this.object = object;
  17. this.materialSlot = materialSlot;
  18. const material = ( object !== null ) ? editor.getObjectMaterial( object, materialSlot ) : null;
  19. this.oldRange = ( material !== null && material[ attributeName ] !== undefined ) ? [ ...this.material[ attributeName ] ] : null;
  20. this.newRange = [ newMinValue, newMaxValue ];
  21. this.attributeName = attributeName;
  22. }
  23. execute() {
  24. const material = this.editor.getObjectMaterial( this.object, this.materialSlot );
  25. material[ this.attributeName ] = [ ...this.newRange ];
  26. material.needsUpdate = true;
  27. this.editor.signals.objectChanged.dispatch( this.object );
  28. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  29. }
  30. undo() {
  31. const material = this.editor.getObjectMaterial( this.object, this.materialSlot );
  32. material[ this.attributeName ] = [ ...this.oldRange ];
  33. material.needsUpdate = true;
  34. this.editor.signals.objectChanged.dispatch( this.object );
  35. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  36. }
  37. update( cmd ) {
  38. this.newRange = [ ...cmd.newRange ];
  39. }
  40. toJSON() {
  41. const output = super.toJSON( this );
  42. output.objectUuid = this.object.uuid;
  43. output.attributeName = this.attributeName;
  44. output.oldRange = [ ...this.oldRange ];
  45. output.newRange = [ ...this.newRange ];
  46. output.materialSlot = this.materialSlot;
  47. return output;
  48. }
  49. fromJSON( json ) {
  50. super.fromJSON( json );
  51. this.attributeName = json.attributeName;
  52. this.oldRange = [ ...json.oldRange ];
  53. this.newRange = [ ...json.newRange ];
  54. this.object = this.editor.objectByUuid( json.objectUuid );
  55. this.materialSlot = json.materialSlot;
  56. }
  57. }
  58. export { SetMaterialRangeCommand };