SetMaterialValueCommand.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { Command } from '../Command.js';
  2. /**
  3. * @param editor Editor
  4. * @param object THREE.Object3D
  5. * @param attributeName string
  6. * @param newValue number, string, boolean or object
  7. * @constructor
  8. */
  9. class SetMaterialValueCommand extends Command {
  10. constructor( editor, object = null, attributeName = '', newValue = null, materialSlot = - 1 ) {
  11. super( editor );
  12. this.type = 'SetMaterialValueCommand';
  13. this.name = editor.strings.getKey( 'command/SetMaterialValue' ) + ': ' + attributeName;
  14. this.updatable = true;
  15. this.object = object;
  16. this.materialSlot = materialSlot;
  17. const material = ( object !== null ) ? editor.getObjectMaterial( object, materialSlot ) : null;
  18. this.oldValue = ( material !== null ) ? material[ attributeName ] : null;
  19. this.newValue = newValue;
  20. this.attributeName = attributeName;
  21. }
  22. execute() {
  23. const material = this.editor.getObjectMaterial( this.object, this.materialSlot );
  24. material[ this.attributeName ] = this.newValue;
  25. 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. const material = this.editor.getObjectMaterial( this.object, this.materialSlot );
  31. material[ this.attributeName ] = this.oldValue;
  32. material.needsUpdate = true;
  33. this.editor.signals.objectChanged.dispatch( this.object );
  34. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  35. }
  36. update( cmd ) {
  37. this.newValue = cmd.newValue;
  38. }
  39. toJSON() {
  40. const output = super.toJSON( this );
  41. output.objectUuid = this.object.uuid;
  42. output.attributeName = this.attributeName;
  43. output.oldValue = this.oldValue;
  44. output.newValue = this.newValue;
  45. output.materialSlot = this.materialSlot;
  46. return output;
  47. }
  48. fromJSON( json ) {
  49. super.fromJSON( json );
  50. this.attributeName = json.attributeName;
  51. this.oldValue = json.oldValue;
  52. this.newValue = json.newValue;
  53. this.object = this.editor.objectByUuid( json.objectUuid );
  54. this.materialSlot = json.materialSlot;
  55. }
  56. }
  57. export { SetMaterialValueCommand };