2
0

SetMaterialMapCommand.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { Command } from '../Command.js';
  2. import { ObjectLoader } 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. class SetMaterialMapCommand extends Command {
  11. constructor( editor, object, mapName, newMap, materialSlot ) {
  12. super( editor );
  13. this.type = 'SetMaterialMapCommand';
  14. this.name = `Set Material.${mapName}`;
  15. this.object = object;
  16. this.material = this.editor.getObjectMaterial( object, materialSlot );
  17. this.oldMap = ( object !== undefined ) ? this.material[ mapName ] : undefined;
  18. this.newMap = newMap;
  19. this.mapName = mapName;
  20. }
  21. execute() {
  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() {
  28. this.material[ this.mapName ] = this.oldMap;
  29. this.material.needsUpdate = true;
  30. this.editor.signals.materialChanged.dispatch( this.material );
  31. }
  32. toJSON() {
  33. const output = super.toJSON( 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. const meta = {
  43. geometries: {},
  44. materials: {},
  45. textures: {},
  46. images: {}
  47. };
  48. const json = map.toJSON( meta );
  49. const 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. const values = [];
  60. for ( const key in cache ) {
  61. const data = cache[ key ];
  62. delete data.metadata;
  63. values.push( data );
  64. }
  65. return values;
  66. }
  67. }
  68. fromJSON( json ) {
  69. super.fromJSON( 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. let map = null;
  76. if ( json !== null ) {
  77. const loader = new ObjectLoader();
  78. const images = loader.parseImages( json.images );
  79. const 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 };