SetMaterialValueCommand.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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, attributeName, newValue, materialSlot ) {
  11. super( editor );
  12. this.type = 'SetMaterialValueCommand';
  13. this.name = `Set Material.${attributeName}`;
  14. this.updatable = true;
  15. this.object = object;
  16. this.materialSlot = materialSlot;
  17. this.material = this.editor.getObjectMaterial( object, materialSlot );
  18. this.oldValue = ( this.material !== undefined ) ? this.material[ attributeName ] : undefined;
  19. this.newValue = newValue;
  20. this.attributeName = attributeName;
  21. }
  22. execute() {
  23. this.material[ this.attributeName ] = this.newValue;
  24. this.material.needsUpdate = true;
  25. this.editor.signals.objectChanged.dispatch( this.object );
  26. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  27. }
  28. undo() {
  29. this.material[ this.attributeName ] = this.oldValue;
  30. this.material.needsUpdate = true;
  31. this.editor.signals.objectChanged.dispatch( this.object );
  32. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  33. }
  34. update( cmd ) {
  35. this.newValue = cmd.newValue;
  36. }
  37. toJSON() {
  38. const output = super.toJSON( this );
  39. output.objectUuid = this.object.uuid;
  40. output.attributeName = this.attributeName;
  41. output.oldValue = this.oldValue;
  42. output.newValue = this.newValue;
  43. return output;
  44. }
  45. fromJSON( json ) {
  46. super.fromJSON( json );
  47. this.attributeName = json.attributeName;
  48. this.oldValue = json.oldValue;
  49. this.newValue = json.newValue;
  50. this.object = this.editor.objectByUuid( json.objectUuid );
  51. }
  52. }
  53. export { SetMaterialValueCommand };