2
0

SetMaterialValueCommand.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. * Developed as part of a project at University of Applied Sciences and Arts Northwestern Switzerland (www.fhnw.ch)
  4. */
  5. import { Command } from '../Command.js';
  6. /**
  7. * @param editor Editor
  8. * @param object THREE.Object3D
  9. * @param attributeName string
  10. * @param newValue number, string, boolean or object
  11. * @constructor
  12. */
  13. var SetMaterialValueCommand = function ( editor, object, attributeName, newValue, materialSlot ) {
  14. Command.call( this, editor );
  15. this.type = 'SetMaterialValueCommand';
  16. this.name = 'Set Material.' + attributeName;
  17. this.updatable = true;
  18. this.object = object;
  19. this.material = this.editor.getObjectMaterial( object, materialSlot );
  20. this.oldValue = ( this.material !== undefined ) ? this.material[ attributeName ] : undefined;
  21. this.newValue = newValue;
  22. this.attributeName = attributeName;
  23. };
  24. SetMaterialValueCommand.prototype = {
  25. execute: function () {
  26. this.material[ this.attributeName ] = this.newValue;
  27. this.material.needsUpdate = true;
  28. this.editor.signals.objectChanged.dispatch( this.object );
  29. this.editor.signals.materialChanged.dispatch( this.material );
  30. },
  31. undo: function () {
  32. this.material[ this.attributeName ] = this.oldValue;
  33. this.material.needsUpdate = true;
  34. this.editor.signals.objectChanged.dispatch( this.object );
  35. this.editor.signals.materialChanged.dispatch( this.material );
  36. },
  37. update: function ( cmd ) {
  38. this.newValue = cmd.newValue;
  39. },
  40. toJSON: function () {
  41. var output = Command.prototype.toJSON.call( this );
  42. output.objectUuid = this.object.uuid;
  43. output.attributeName = this.attributeName;
  44. output.oldValue = this.oldValue;
  45. output.newValue = this.newValue;
  46. return output;
  47. },
  48. fromJSON: function ( json ) {
  49. Command.prototype.fromJSON.call( this, 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. }
  55. };
  56. export { SetMaterialValueCommand };