CmdSetMaterialMap.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. this.newMapJSON = serializeMap( this.newMap );
  13. this.oldMapJSON = serializeMap( this.oldMap );
  14. // serializes a map (THREE.Texture)
  15. function serializeMap ( map ) {
  16. if ( map === null || map === undefined ) return null;
  17. var meta = {
  18. geometries: {},
  19. materials: {},
  20. textures: {},
  21. images: {}
  22. };
  23. var json = map.toJSON( meta );
  24. var images = extractFromCache( meta.images );
  25. if ( images.length > 0 ) json.images = images;
  26. json.sourceFile = map.sourceFile;
  27. return json;
  28. }
  29. // Note: The function 'extractFromCache' is copied from Object3D.toJSON()
  30. // extract data from the cache hash
  31. // remove metadata on each item
  32. // and return as array
  33. function extractFromCache ( cache ) {
  34. var values = [];
  35. for ( var key in cache ) {
  36. var data = cache[ key ];
  37. delete data.metadata;
  38. values.push( data );
  39. }
  40. return values;
  41. }
  42. };
  43. CmdSetMaterialMap.prototype = {
  44. execute: function () {
  45. this.object.material[ this.mapName ] = this.newMap;
  46. this.object.material.needsUpdate = true;
  47. this.editor.signals.materialChanged.dispatch( this.object.material );
  48. },
  49. undo: function () {
  50. this.object.material[ this.mapName ] = this.oldMap;
  51. this.object.material.needsUpdate = true;
  52. this.editor.signals.materialChanged.dispatch( this.object.material );
  53. },
  54. toJSON: function () {
  55. var output = Cmd.prototype.toJSON.call( this );
  56. output.objectUuid = this.objectUuid;
  57. output.mapName = this.mapName;
  58. output.oldMap = this.oldMapJSON;
  59. output.newMap = this.newMapJSON;
  60. return output;
  61. },
  62. fromJSON: function ( json ) {
  63. Cmd.prototype.fromJSON.call( this, json );
  64. this.objectUuid = json.objectUuid;
  65. this.mapName = json.mapName;
  66. this.object = this.editor.objectByUuid( json.objectUuid );
  67. this.oldMapJSON = json.oldMap;
  68. this.newMapJSON = json.newMap;
  69. this.oldMap = parseTexture( json.oldMap );
  70. this.newMap = parseTexture( json.newMap );
  71. function parseTexture ( json ) {
  72. var map = null;
  73. if ( json !== null ) {
  74. var loader = new THREE.ObjectLoader();
  75. var images = loader.parseImages( json.images );
  76. var textures = loader.parseTextures( [ json ], images );
  77. map = textures[ json.uuid ];
  78. map.sourceFile = json.sourceFile;
  79. }
  80. return map;
  81. }
  82. }
  83. };