SetMaterialMapCommand.js 2.7 KB

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