SetMaterialRangeCommand.js 2.3 KB

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