SetMaterialValueCommand.js 1.7 KB

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