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