SetMaterialCommand.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Command } from '../Command.js';
  2. import { ObjectLoader } from 'three';
  3. class SetMaterialCommand extends Command {
  4. /**
  5. * @param {Editor} editor
  6. * @param {THREE.Object3D|null} object
  7. * @param {THREE.Material|null} newMaterial
  8. * @param {number} [materialSlot=-1]
  9. * @constructor
  10. */
  11. constructor( editor, object = null, newMaterial = null, materialSlot = - 1 ) {
  12. super( editor );
  13. this.type = 'SetMaterialCommand';
  14. this.name = editor.strings.getKey( 'command/SetMaterial' );
  15. this.object = object;
  16. this.materialSlot = materialSlot;
  17. this.oldMaterial = ( object !== null ) ? editor.getObjectMaterial( object, materialSlot ) : null;
  18. this.newMaterial = newMaterial;
  19. }
  20. execute() {
  21. this.editor.setObjectMaterial( this.object, this.materialSlot, this.newMaterial );
  22. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  23. }
  24. undo() {
  25. this.editor.setObjectMaterial( this.object, this.materialSlot, this.oldMaterial );
  26. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  27. }
  28. toJSON() {
  29. const output = super.toJSON( this );
  30. output.objectUuid = this.object.uuid;
  31. output.oldMaterial = this.oldMaterial.toJSON();
  32. output.newMaterial = this.newMaterial.toJSON();
  33. output.materialSlot = this.materialSlot;
  34. return output;
  35. }
  36. fromJSON( json ) {
  37. super.fromJSON( json );
  38. this.object = this.editor.objectByUuid( json.objectUuid );
  39. this.oldMaterial = parseMaterial( json.oldMaterial );
  40. this.newMaterial = parseMaterial( json.newMaterial );
  41. this.materialSlot = json.materialSlot;
  42. function parseMaterial( json ) {
  43. const loader = new ObjectLoader();
  44. const images = loader.parseImages( json.images );
  45. const textures = loader.parseTextures( json.textures, images );
  46. const materials = loader.parseMaterials( [ json ], textures );
  47. return materials[ json.uuid ];
  48. }
  49. }
  50. }
  51. export { SetMaterialCommand };
粤ICP备19079148号