SetMaterialValueCommand.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { Command } from '../Command.js';
  2. class SetMaterialValueCommand extends Command {
  3. /**
  4. * @param {Editor} editor
  5. * @param {THREE.Object3D|null} [object=null]
  6. * @param {string} [attributeName='']
  7. * @param {number|string|boolean|Object|null} [newValue=null]
  8. * @param {number} [materialSlot=-1]
  9. * @constructor
  10. */
  11. constructor( editor, object = null, attributeName = '', newValue = null, materialSlot = - 1 ) {
  12. super( editor );
  13. this.type = 'SetMaterialValueCommand';
  14. this.name = editor.strings.getKey( 'command/SetMaterialValue' ) + ': ' + 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 ] : 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 ] = this.newValue;
  26. material.needsUpdate = true;
  27. this.editor.signals.objectChanged.dispatch( this.object );
  28. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  29. }
  30. undo() {
  31. const material = this.editor.getObjectMaterial( this.object, this.materialSlot );
  32. material[ this.attributeName ] = this.oldValue;
  33. material.needsUpdate = true;
  34. this.editor.signals.objectChanged.dispatch( this.object );
  35. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  36. }
  37. update( cmd ) {
  38. this.newValue = cmd.newValue;
  39. }
  40. toJSON() {
  41. const output = super.toJSON( this );
  42. output.objectUuid = this.object.uuid;
  43. output.attributeName = this.attributeName;
  44. output.oldValue = this.oldValue;
  45. output.newValue = this.newValue;
  46. output.materialSlot = this.materialSlot;
  47. return output;
  48. }
  49. fromJSON( json ) {
  50. super.fromJSON( 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. this.materialSlot = json.materialSlot;
  56. }
  57. }
  58. export { SetMaterialValueCommand };
粤ICP备19079148号