SetMaterialMapCommand.js 2.9 KB

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