SetMaterialCommand.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. /**
  6. * @param object THREE.Object3D
  7. * @param newMaterial THREE.Material
  8. * @constructor
  9. */
  10. var SetMaterialCommand = function ( object, newMaterial, materialSlot ) {
  11. Command.call( this );
  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. SetMaterialCommand.prototype = {
  20. execute: function () {
  21. this.editor.setObjectMaterial( this.object, this.materialSlot, this.newMaterial );
  22. this.editor.signals.materialChanged.dispatch( this.newMaterial );
  23. },
  24. undo: function () {
  25. this.editor.setObjectMaterial( this.object, this.materialSlot, this.oldMaterial );
  26. this.editor.signals.materialChanged.dispatch( this.oldMaterial );
  27. },
  28. toJSON: function () {
  29. var output = Command.prototype.toJSON.call( this );
  30. output.objectUuid = this.object.uuid;
  31. output.oldMaterial = this.oldMaterial.toJSON();
  32. output.newMaterial = this.newMaterial.toJSON();
  33. return output;
  34. },
  35. fromJSON: function ( json ) {
  36. Command.prototype.fromJSON.call( this, json );
  37. this.object = this.editor.objectByUuid( json.objectUuid );
  38. this.oldMaterial = parseMaterial( json.oldMaterial );
  39. this.newMaterial = parseMaterial( json.newMaterial );
  40. function parseMaterial ( json ) {
  41. var loader = new THREE.ObjectLoader();
  42. var images = loader.parseImages( json.images );
  43. var textures = loader.parseTextures( json.textures, images );
  44. var materials = loader.parseMaterials( [ json ], textures );
  45. return materials[ json.uuid ];
  46. }
  47. }
  48. };