SetMaterialCommand.js 1.6 KB

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