SetMaterialMapCommand.js 2.8 KB

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