SetMaterialValueCommand.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. /**
  6. * @param object THREE.Object3D
  7. * @param attributeName string
  8. * @param newValue number, string, boolean or object
  9. * @constructor
  10. */
  11. var SetMaterialValueCommand = function ( object, attributeName, newValue, materialSlot ) {
  12. Command.call( this );
  13. this.type = 'SetMaterialValueCommand';
  14. this.name = 'Set Material.' + attributeName;
  15. this.updatable = true;
  16. this.object = object;
  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. SetMaterialValueCommand.prototype = {
  23. execute: function () {
  24. this.material[ this.attributeName ] = this.newValue;
  25. this.material.needsUpdate = true;
  26. this.editor.signals.objectChanged.dispatch( this.object );
  27. this.editor.signals.materialChanged.dispatch( this.material );
  28. },
  29. undo: function () {
  30. this.material[ this.attributeName ] = this.oldValue;
  31. this.material.needsUpdate = true;
  32. this.editor.signals.objectChanged.dispatch( this.object );
  33. this.editor.signals.materialChanged.dispatch( this.material );
  34. },
  35. update: function ( cmd ) {
  36. this.newValue = cmd.newValue;
  37. },
  38. toJSON: function () {
  39. var output = Command.prototype.toJSON.call( this );
  40. output.objectUuid = this.object.uuid;
  41. output.attributeName = this.attributeName;
  42. output.oldValue = this.oldValue;
  43. output.newValue = this.newValue;
  44. return output;
  45. },
  46. fromJSON: function ( json ) {
  47. Command.prototype.fromJSON.call( this, json );
  48. this.attributeName = json.attributeName;
  49. this.oldValue = json.oldValue;
  50. this.newValue = json.newValue;
  51. this.object = this.editor.objectByUuid( json.objectUuid );
  52. }
  53. };