CmdSetMaterialMap.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. }
  27. this.oldMapJSON = null;
  28. if ( object !== undefined && object.material[ mapName ] !== null ) {
  29. var meta = {
  30. geometries: {},
  31. materials: {},
  32. textures: {},
  33. images: {}
  34. };
  35. this.oldMapJSON = this.oldMap.toJSON( meta );
  36. this.oldMapJSON.images = [];
  37. this.oldMapJSON.images.push( {
  38. uuid: this.oldMap.image.uuid,
  39. url: this.getDataURL( this.oldMap.image )
  40. } );
  41. }
  42. };
  43. CmdSetMaterialMap.prototype = {
  44. execute: function () {
  45. this.object.material[ this.mapName ] = this.newMap;
  46. this.object.material.needsUpdate = true;
  47. this.editor.signals.materialChanged.dispatch( this.object.material );
  48. },
  49. undo: function () {
  50. this.object.material[ this.mapName ] = this.oldMap;
  51. this.object.material.needsUpdate = true;
  52. this.editor.signals.materialChanged.dispatch( this.object.material );
  53. },
  54. toJSON: function () {
  55. var output = Cmd.prototype.toJSON.call( this );
  56. output.objectUuid = this.objectUuid;
  57. output.mapName = this.mapName;
  58. output.oldMap = this.oldMapJSON;
  59. output.newMap = this.newMapJSON;
  60. return output;
  61. },
  62. fromJSON: function ( json ) {
  63. Cmd.prototype.fromJSON.call( this, json );
  64. this.objectUuid = json.objectUuid;
  65. this.mapName = json.mapName;
  66. this.object = this.editor.objectByUuid( json.objectUuid );
  67. this.oldMapJSON = json.oldMap;
  68. this.newMapJSON = json.newMap;
  69. this.oldMap = this.parseTexture( json.oldMap );
  70. this.newMap = this.parseTexture( json.newMap );
  71. },
  72. parseTexture: function ( json ) {
  73. var map = null;
  74. if ( json !== null ) {
  75. var loader = new THREE.ObjectLoader();
  76. var images = loader.parseImages( json.images );
  77. var textures = loader.parseTextures( [ json ], images );
  78. map = textures[ json.uuid ];
  79. }
  80. return map;
  81. },
  82. getDataURL: function ( image ) {
  83. var canvas;
  84. if ( image.toDataURL !== undefined ) {
  85. canvas = image;
  86. } else {
  87. canvas = document.createElement( 'canvas' );
  88. canvas.width = image.width;
  89. canvas.height = image.height;
  90. canvas.getContext( '2d' ).drawImage( image, 0, 0, image.width, image.height );
  91. }
  92. if ( canvas.width > 2048 || canvas.height > 2048 ) {
  93. return canvas.toDataURL( 'image/jpeg', 0.6 );
  94. } else {
  95. return canvas.toDataURL( 'image/png' );
  96. }
  97. }
  98. };