SetMaterialCommand.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { Command } from '../Command.js';
  2. import * as THREE from '../../../build/three.module.js';
  3. /**
  4. * @param editor Editor
  5. * @param object THREE.Object3D
  6. * @param newMaterial THREE.Material
  7. * @constructor
  8. */
  9. function SetMaterialCommand( editor, object, newMaterial, materialSlot ) {
  10. Command.call( this, editor );
  11. this.type = 'SetMaterialCommand';
  12. this.name = 'New Material';
  13. this.object = object;
  14. this.materialSlot = materialSlot;
  15. this.oldMaterial = this.editor.getObjectMaterial( object, materialSlot );
  16. this.newMaterial = newMaterial;
  17. }
  18. SetMaterialCommand.prototype = {
  19. execute: function () {
  20. this.editor.setObjectMaterial( this.object, this.materialSlot, this.newMaterial );
  21. this.editor.signals.materialChanged.dispatch( this.newMaterial );
  22. },
  23. undo: function () {
  24. this.editor.setObjectMaterial( this.object, this.materialSlot, this.oldMaterial );
  25. this.editor.signals.materialChanged.dispatch( this.oldMaterial );
  26. },
  27. toJSON: function () {
  28. var output = Command.prototype.toJSON.call( 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: function ( json ) {
  35. Command.prototype.fromJSON.call( this, 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. var loader = new THREE.ObjectLoader();
  41. var images = loader.parseImages( json.images );
  42. var textures = loader.parseTextures( json.textures, images );
  43. var materials = loader.parseMaterials( [ json ], textures );
  44. return materials[ json.uuid ];
  45. }
  46. }
  47. };
  48. export { SetMaterialCommand };