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