SetMaterialCommand.js 1.7 KB

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