1
0

SetMaterialCommand.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { Command } from '../Command.js';
  2. import { ObjectLoader } from 'three';
  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 = null, newMaterial = null, materialSlot = - 1 ) {
  11. super( editor );
  12. this.type = 'SetMaterialCommand';
  13. this.name = editor.strings.getKey( 'command/SetMaterial' );
  14. this.object = object;
  15. this.materialSlot = materialSlot;
  16. this.oldMaterial = ( object !== null ) ? editor.getObjectMaterial( object, materialSlot ) : null;
  17. this.newMaterial = newMaterial;
  18. }
  19. execute() {
  20. this.editor.setObjectMaterial( this.object, this.materialSlot, this.newMaterial );
  21. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  22. }
  23. undo() {
  24. this.editor.setObjectMaterial( this.object, this.materialSlot, this.oldMaterial );
  25. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  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. output.materialSlot = this.materialSlot;
  33. return output;
  34. }
  35. fromJSON( json ) {
  36. super.fromJSON( json );
  37. this.object = this.editor.objectByUuid( json.objectUuid );
  38. this.oldMaterial = parseMaterial( json.oldMaterial );
  39. this.newMaterial = parseMaterial( json.newMaterial );
  40. this.materialSlot = json.materialSlot;
  41. function parseMaterial( json ) {
  42. const loader = new ObjectLoader();
  43. const images = loader.parseImages( json.images );
  44. const textures = loader.parseTextures( json.textures, images );
  45. const materials = loader.parseMaterials( [ json ], textures );
  46. return materials[ json.uuid ];
  47. }
  48. }
  49. }
  50. export { SetMaterialCommand };