SetMaterialCommand.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 , slot) {
  11. Command.call( this );
  12. this.type = 'SetMaterialCommand';
  13. this.name = 'New Material';
  14. this.object = object;
  15. this.slot = slot;
  16. var material = this.editor.getObjectMaterial( this.object, this.slot );
  17. this.oldMaterial = material;
  18. this.newMaterial = newMaterial;
  19. };
  20. SetMaterialCommand.prototype = {
  21. execute: function () {
  22. this.editor.setObjectMaterial( this.object, this.slot, this.newMaterial );
  23. this.editor.signals.materialChanged.dispatch( this.newMaterial );
  24. },
  25. undo: function () {
  26. this.editor.setObjectMaterial( this.object, this.slot, 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. };