SetMaterialVectorCommand.js 2.0 KB

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