CmdSetMaterialMap.js 2.6 KB

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