CmdSetMaterialMap.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. */
  4. /**
  5. * @param object THREE.Object3D
  6. * @param mapName string
  7. * @param newMap THREE.Texture
  8. * @constructor
  9. */
  10. CmdSetMaterialMap = function ( object, mapName, newMap ) {
  11. Cmd.call( this );
  12. this.type = 'CmdSetMaterialMap';
  13. this.name = 'Set Material.' + mapName;
  14. this.object = object;
  15. this.mapName = mapName;
  16. this.oldMap = ( object !== undefined ) ? object.material[ mapName ] : undefined;
  17. this.newMap = newMap;
  18. };
  19. CmdSetMaterialMap.prototype = {
  20. execute: function () {
  21. this.object.material[ this.mapName ] = this.newMap;
  22. this.object.material.needsUpdate = true;
  23. this.editor.signals.materialChanged.dispatch( this.object.material );
  24. },
  25. undo: function () {
  26. this.object.material[ this.mapName ] = this.oldMap;
  27. this.object.material.needsUpdate = true;
  28. this.editor.signals.materialChanged.dispatch( this.object.material );
  29. },
  30. toJSON: function () {
  31. var output = Cmd.prototype.toJSON.call( this );
  32. output.objectUuid = this.object.uuid;
  33. output.mapName = this.mapName;
  34. output.newMap = serializeMap( this.newMap );
  35. output.oldMap = serializeMap( this.oldMap );
  36. return output;
  37. // serializes a map (THREE.Texture)
  38. function serializeMap ( map ) {
  39. if ( map === null || map === undefined ) return null;
  40. var meta = {
  41. geometries: {},
  42. materials: {},
  43. textures: {},
  44. images: {}
  45. };
  46. var json = map.toJSON( meta );
  47. var images = extractFromCache( meta.images );
  48. if ( images.length > 0 ) json.images = images;
  49. json.sourceFile = map.sourceFile;
  50. return json;
  51. }
  52. // Note: The function 'extractFromCache' is copied from Object3D.toJSON()
  53. // extract data from the cache hash
  54. // remove metadata on each item
  55. // and return as array
  56. function extractFromCache ( cache ) {
  57. var values = [];
  58. for ( var key in cache ) {
  59. var data = cache[ key ];
  60. delete data.metadata;
  61. values.push( data );
  62. }
  63. return values;
  64. }
  65. },
  66. fromJSON: function ( json ) {
  67. Cmd.prototype.fromJSON.call( this, json );
  68. this.object = this.editor.objectByUuid( json.objectUuid );
  69. this.mapName = json.mapName;
  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. };