2
0

SetMaterialRangeCommand.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.materialSlot = materialSlot;
  18. this.material = this.editor.getObjectMaterial( object, materialSlot );
  19. this.oldRange = ( this.material !== undefined && this.material[ attributeName ] !== undefined ) ? [ ...this.material[ attributeName ] ] : undefined;
  20. this.newRange = [ newMinValue, newMaxValue ];
  21. this.attributeName = attributeName;
  22. }
  23. execute() {
  24. this.material[ this.attributeName ] = [ ...this.newRange ];
  25. this.material.needsUpdate = true;
  26. this.editor.signals.objectChanged.dispatch( this.object );
  27. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  28. }
  29. undo() {
  30. this.material[ this.attributeName ] = [ ...this.oldRange ];
  31. this.material.needsUpdate = true;
  32. this.editor.signals.objectChanged.dispatch( this.object );
  33. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  34. }
  35. update( cmd ) {
  36. this.newRange = [ ...cmd.newRange ];
  37. }
  38. toJSON() {
  39. const output = super.toJSON( this );
  40. output.objectUuid = this.object.uuid;
  41. output.attributeName = this.attributeName;
  42. output.oldRange = [ ...this.oldRange ];
  43. output.newRange = [ ...this.newRange ];
  44. return output;
  45. }
  46. fromJSON( json ) {
  47. super.fromJSON( json );
  48. this.attributeName = json.attributeName;
  49. this.oldRange = [ ...json.oldRange ];
  50. this.newRange = [ ...json.newRange ];
  51. this.object = this.editor.objectByUuid( json.objectUuid );
  52. }
  53. }
  54. export { SetMaterialRangeCommand };