CmdSetMaterialMap.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.object = object;
  8. this.mapName = mapName;
  9. this.oldMap = object !== undefined ? object.material[ mapName ] : undefined;
  10. this.newMap = newMap;
  11. this.objectUuid = object !== undefined ? object.uuid : undefined;
  12. this.newMapJSON = null;
  13. if ( newMap !== undefined && newMap !== null ) {
  14. var meta = {
  15. geometries: {},
  16. materials: {},
  17. textures: {},
  18. images: {}
  19. };
  20. this.newMapJSON = newMap.toJSON( meta );
  21. this.newMapJSON.images = [];
  22. this.newMapJSON.images.push( {
  23. uuid: newMap.image.uuid,
  24. url: this.getDataURL( newMap.image )
  25. } );
  26. this.newMapJSON.sourceFile = newMap.sourceFile;
  27. }
  28. this.oldMapJSON = null;
  29. if ( object !== undefined && object.material[ mapName ] !== null ) {
  30. var meta = {
  31. geometries: {},
  32. materials: {},
  33. textures: {},
  34. images: {}
  35. };
  36. this.oldMapJSON = this.oldMap.toJSON( meta );
  37. this.oldMapJSON.images = [];
  38. this.oldMapJSON.images.push( {
  39. uuid: this.oldMap.image.uuid,
  40. url: this.getDataURL( this.oldMap.image )
  41. } );
  42. this.oldMapJSON.sourceFile = this.oldMap.sourceFile;
  43. }
  44. };
  45. CmdSetMaterialMap.prototype = {
  46. execute: function () {
  47. this.object.material[ this.mapName ] = this.newMap;
  48. this.object.material.needsUpdate = true;
  49. this.editor.signals.materialChanged.dispatch( this.object.material );
  50. },
  51. undo: function () {
  52. this.object.material[ this.mapName ] = this.oldMap;
  53. this.object.material.needsUpdate = true;
  54. this.editor.signals.materialChanged.dispatch( this.object.material );
  55. },
  56. toJSON: function () {
  57. var output = Cmd.prototype.toJSON.call( this );
  58. output.objectUuid = this.objectUuid;
  59. output.mapName = this.mapName;
  60. output.oldMap = this.oldMapJSON;
  61. output.newMap = this.newMapJSON;
  62. return output;
  63. },
  64. fromJSON: function ( json ) {
  65. Cmd.prototype.fromJSON.call( this, json );
  66. this.objectUuid = json.objectUuid;
  67. this.mapName = json.mapName;
  68. this.object = this.editor.objectByUuid( json.objectUuid );
  69. this.oldMapJSON = json.oldMap;
  70. this.newMapJSON = json.newMap;
  71. this.oldMap = this.parseTexture( json.oldMap );
  72. this.newMap = this.parseTexture( json.newMap );
  73. },
  74. parseTexture: function ( 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. getDataURL: function ( image ) {
  86. var canvas;
  87. if ( image.toDataURL !== undefined ) {
  88. canvas = image;
  89. } else {
  90. canvas = document.createElement( 'canvas' );
  91. canvas.width = image.width;
  92. canvas.height = image.height;
  93. canvas.getContext( '2d' ).drawImage( image, 0, 0, image.width, image.height );
  94. }
  95. if ( canvas.width > 2048 || canvas.height > 2048 ) {
  96. return canvas.toDataURL( 'image/jpeg', 0.6 );
  97. } else {
  98. return canvas.toDataURL( 'image/png' );
  99. }
  100. }
  101. };