SetMaterialValueCommand.js 1.9 KB

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