CmdSetMaterialMap.js 2.8 KB

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