SetMaterialCommand.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. * Developed as part of a project at University of Applied Sciences and Arts Northwestern Switzerland (www.fhnw.ch)
  4. */
  5. import { Command } from '../Command.js';
  6. import { ObjectLoader } from '../../../build/three.module.js';
  7. /**
  8. * @param editor Editor
  9. * @param object THREE.Object3D
  10. * @param newMaterial THREE.Material
  11. * @constructor
  12. */
  13. var SetMaterialCommand = function ( editor, object, newMaterial, materialSlot ) {
  14. Command.call( this, editor );
  15. this.type = 'SetMaterialCommand';
  16. this.name = 'New Material';
  17. this.object = object;
  18. this.materialSlot = materialSlot;
  19. this.oldMaterial = this.editor.getObjectMaterial( object, materialSlot );
  20. this.newMaterial = newMaterial;
  21. };
  22. SetMaterialCommand.prototype = {
  23. execute: function () {
  24. this.editor.setObjectMaterial( this.object, this.materialSlot, this.newMaterial );
  25. this.editor.signals.materialChanged.dispatch( this.newMaterial );
  26. },
  27. undo: function () {
  28. this.editor.setObjectMaterial( this.object, this.materialSlot, this.oldMaterial );
  29. this.editor.signals.materialChanged.dispatch( this.oldMaterial );
  30. },
  31. toJSON: function () {
  32. var output = Command.prototype.toJSON.call( this );
  33. output.objectUuid = this.object.uuid;
  34. output.oldMaterial = this.oldMaterial.toJSON();
  35. output.newMaterial = this.newMaterial.toJSON();
  36. return output;
  37. },
  38. fromJSON: function ( json ) {
  39. Command.prototype.fromJSON.call( this, json );
  40. this.object = this.editor.objectByUuid( json.objectUuid );
  41. this.oldMaterial = parseMaterial( json.oldMaterial );
  42. this.newMaterial = parseMaterial( json.newMaterial );
  43. function parseMaterial( json ) {
  44. var loader = new ObjectLoader();
  45. var images = loader.parseImages( json.images );
  46. var textures = loader.parseTextures( json.textures, images );
  47. var materials = loader.parseMaterials( [ json ], textures );
  48. return materials[ json.uuid ];
  49. }
  50. }
  51. };
  52. export { SetMaterialCommand };