SetMaterialColorCommand.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { Command } from '../Command.js';
  2. class SetMaterialColorCommand extends Command {
  3. /**
  4. * @param {Editor} editor
  5. * @param {THREE.Object3D|null} [object=null]
  6. * @param {string} attributeName
  7. * @param {?number} [newValue=null] Integer representing a hex color value
  8. * @param {number} [materialSlot=-1]
  9. * @constructor
  10. */
  11. constructor( editor, object = null, attributeName = '', newValue = null, materialSlot = - 1 ) {
  12. super( editor );
  13. this.type = 'SetMaterialColorCommand';
  14. this.name = editor.strings.getKey( 'command/SetMaterialColor' ) + ': ' + attributeName;
  15. this.updatable = true;
  16. this.object = object;
  17. this.materialSlot = materialSlot;
  18. const material = ( object !== null ) ? editor.getObjectMaterial( object, materialSlot ) : null;
  19. this.oldValue = ( material !== null ) ? material[ attributeName ].getHex() : null;
  20. this.newValue = newValue;
  21. this.attributeName = attributeName;
  22. }
  23. execute() {
  24. const material = this.editor.getObjectMaterial( this.object, this.materialSlot );
  25. material[ this.attributeName ].setHex( this.newValue );
  26. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  27. }
  28. undo() {
  29. const material = this.editor.getObjectMaterial( this.object, this.materialSlot );
  30. material[ this.attributeName ].setHex( this.oldValue );
  31. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  32. }
  33. update( cmd ) {
  34. this.newValue = cmd.newValue;
  35. }
  36. toJSON() {
  37. const output = super.toJSON( this );
  38. output.objectUuid = this.object.uuid;
  39. output.attributeName = this.attributeName;
  40. output.oldValue = this.oldValue;
  41. output.newValue = this.newValue;
  42. output.materialSlot = this.materialSlot;
  43. return output;
  44. }
  45. fromJSON( json ) {
  46. super.fromJSON( json );
  47. this.object = this.editor.objectByUuid( json.objectUuid );
  48. this.attributeName = json.attributeName;
  49. this.oldValue = json.oldValue;
  50. this.newValue = json.newValue;
  51. this.materialSlot = json.materialSlot;
  52. }
  53. }
  54. export { SetMaterialColorCommand };
粤ICP备19079148号