SetMaterialMapCommand.js 3.1 KB

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