SetMaterialValueCommand.js 1.8 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. function SetMaterialValueCommand( editor, object, attributeName, newValue, materialSlot ) {
  10. Command.call( this, editor );
  11. this.type = 'SetMaterialValueCommand';
  12. this.name = 'Set Material.' + attributeName;
  13. this.updatable = true;
  14. this.object = object;
  15. this.material = this.editor.getObjectMaterial( object, materialSlot );
  16. this.oldValue = ( this.material !== undefined ) ? this.material[ attributeName ] : undefined;
  17. this.newValue = newValue;
  18. this.attributeName = attributeName;
  19. }
  20. SetMaterialValueCommand.prototype = {
  21. execute: function () {
  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: function () {
  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: function ( cmd ) {
  34. this.newValue = cmd.newValue;
  35. },
  36. toJSON: function () {
  37. var output = Command.prototype.toJSON.call( 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: function ( json ) {
  45. Command.prototype.fromJSON.call( this, 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 };