CmdSetMaterialMap.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Created by Daniel on 21.07.15.
  3. */
  4. CmdSetMaterialMap = function ( object, mapName, newMap ) {
  5. Cmd.call( this );
  6. this.type = 'CmdSetMaterialMap';
  7. this.object = object;
  8. this.mapName = mapName;
  9. this.oldMap = object !== undefined ? object.material[ mapName ] : undefined;
  10. this.newMap = newMap;
  11. this.objectUuid = object !== undefined ? object.uuid : undefined;
  12. /*
  13. if ( object !== undefined ) {
  14. meta = {
  15. geometries: {},
  16. materials: {},
  17. textures: {},
  18. images: {}
  19. };
  20. this.newMapJSON = newMap.toJSON( meta );
  21. this.newMapJSON.images = [ newMap.image.toJSON( meta ) ];
  22. if ( object.material[ mapName ] !== null ) {
  23. this.oldMapJSON = object.material[ mapName ].toJSON( meta );
  24. this.oldMapJSON.textures = [ object.material[ mapName ].texture.toJSON( meta ) ];
  25. }
  26. }
  27. */
  28. };
  29. CmdSetMaterialMap.prototype = {
  30. execute: function () {
  31. this.object.material[ this.mapName ] = this.newMap;
  32. this.object.material.needsUpdate = true;
  33. this.editor.signals.materialChanged.dispatch( this.object.material );
  34. },
  35. undo: function () {
  36. this.object.material[ this.mapName ] = this.oldMap;
  37. this.object.material.needsUpdate = true;
  38. this.editor.signals.materialChanged.dispatch( this.object.material );
  39. },
  40. toJSON: function () {
  41. var output = Cmd.prototype.toJSON.call( this );
  42. output.objectUuid = this.objectUuid;
  43. output.mapName = this.mapName;
  44. output.oldMap = this.oldMapJSON;
  45. output.newMap = this.newMapJSON;
  46. return output;
  47. },
  48. fromJSON: function ( json ) {
  49. Cmd.prototype.fromJSON.call( this, json );
  50. this.objectUuid = json.objectUuid;
  51. this.mapName = json.mapName;
  52. this.object = this.editor.objectByUuid( json.objectUuid );
  53. this.oldMap = this.parseTexture( json.oldMap );
  54. this.newMap = this.parseTexture( json.newMap );
  55. this.oldMapJSON = json.oldMap;
  56. this.newMapJSON = json.newMap;
  57. },
  58. parseTexture: function ( data, images ) {
  59. var loader = new THREE.ObjectLoader();
  60. return loader.parseTextures( [ data ] )[ data.uuid ];
  61. }
  62. };