CmdSetMaterialMap.js 2.4 KB

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