SetMaterialMapCommand.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. * Developed as part of a project at University of Applied Sciences and Arts Northwestern Switzerland (www.fhnw.ch)
  4. */
  5. /**
  6. * @param object THREE.Object3D
  7. * @param mapName string
  8. * @param newMap THREE.Texture
  9. * @constructor
  10. */
  11. var SetMaterialMapCommand = function ( object, mapName, newMap ) {
  12. Command.call( this );
  13. this.type = 'SetMaterialMapCommand';
  14. this.name = 'Set Material.' + mapName;
  15. this.object = object;
  16. this.mapName = mapName;
  17. this.oldMap = ( object !== undefined ) ? object.material[ mapName ] : undefined;
  18. this.newMap = newMap;
  19. };
  20. SetMaterialMapCommand.prototype = {
  21. execute: function () {
  22. this.object.material[ this.mapName ] = this.newMap;
  23. this.object.material.needsUpdate = true;
  24. this.editor.signals.materialChanged.dispatch( this.object.material );
  25. },
  26. undo: function () {
  27. this.object.material[ this.mapName ] = this.oldMap;
  28. this.object.material.needsUpdate = true;
  29. this.editor.signals.materialChanged.dispatch( this.object.material );
  30. },
  31. toJSON: function () {
  32. var output = Command.prototype.toJSON.call( this );
  33. output.objectUuid = this.object.uuid;
  34. output.mapName = this.mapName;
  35. output.newMap = serializeMap( this.newMap );
  36. output.oldMap = serializeMap( this.oldMap );
  37. return output;
  38. // serializes a map (THREE.Texture)
  39. function serializeMap ( map ) {
  40. if ( map === null || map === undefined ) return null;
  41. var meta = {
  42. geometries: {},
  43. materials: {},
  44. textures: {},
  45. images: {}
  46. };
  47. var json = map.toJSON( meta );
  48. var images = extractFromCache( meta.images );
  49. if ( images.length > 0 ) json.images = images;
  50. json.sourceFile = map.sourceFile;
  51. return json;
  52. }
  53. // Note: The function 'extractFromCache' is copied from Object3D.toJSON()
  54. // extract data from the cache hash
  55. // remove metadata on each item
  56. // and return as array
  57. function extractFromCache ( cache ) {
  58. var values = [];
  59. for ( var key in cache ) {
  60. var data = cache[ key ];
  61. delete data.metadata;
  62. values.push( data );
  63. }
  64. return values;
  65. }
  66. },
  67. fromJSON: function ( json ) {
  68. Command.prototype.fromJSON.call( this, json );
  69. this.object = this.editor.objectByUuid( json.objectUuid );
  70. this.mapName = json.mapName;
  71. this.oldMap = parseTexture( json.oldMap );
  72. this.newMap = parseTexture( json.newMap );
  73. function parseTexture ( json ) {
  74. var map = null;
  75. if ( json !== null ) {
  76. var loader = new THREE.ObjectLoader();
  77. var images = loader.parseImages( json.images );
  78. var textures = loader.parseTextures( [ json ], images );
  79. map = textures[ json.uuid ];
  80. map.sourceFile = json.sourceFile;
  81. }
  82. return map;
  83. }
  84. }
  85. };