ImageUtils.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. THREE.ImageUtils = {
  6. crossOrigin: 'anonymous',
  7. loadTexture: function ( url, mapping, onLoad, onError ) {
  8. var image = new Image();
  9. var texture = new THREE.Texture( image, mapping );
  10. var loader = new THREE.ImageLoader();
  11. loader.addEventListener( 'load', function ( event ) {
  12. texture.image = event.content;
  13. texture.needsUpdate = true;
  14. if ( onLoad ) onLoad( texture );
  15. } );
  16. loader.addEventListener( 'error', function ( event ) {
  17. if ( onError ) onError( event.message );
  18. } );
  19. loader.crossOrigin = this.crossOrigin;
  20. loader.load( url, image );
  21. texture.sourceFile = url;
  22. return texture;
  23. },
  24. loadCompressedTexture: function ( url, mapping, onLoad, onError ) {
  25. var texture = new THREE.CompressedTexture();
  26. texture.mapping = mapping;
  27. var request = new XMLHttpRequest();
  28. request.onload = function () {
  29. var buffer = request.response;
  30. var dds = THREE.ImageUtils.parseDDS( buffer, true );
  31. texture.format = dds.format;
  32. texture.mipmaps = dds.mipmaps;
  33. texture.image.width = dds.width;
  34. texture.image.height = dds.height;
  35. // gl.generateMipmap fails for compressed textures
  36. // mipmaps must be embedded in the DDS file
  37. // or texture filters must not use mipmapping
  38. texture.generateMipmaps = false;
  39. texture.needsUpdate = true;
  40. if ( onLoad ) onLoad( texture );
  41. }
  42. request.onerror = onError;
  43. request.open( 'GET', url, true );
  44. request.responseType = "arraybuffer";
  45. request.send( null );
  46. return texture;
  47. },
  48. loadTextureCube: function ( array, mapping, onLoad, onError ) {
  49. var images = [];
  50. images.loadCount = 0;
  51. var texture = new THREE.Texture();
  52. texture.image = images;
  53. if ( mapping !== undefined ) texture.mapping = mapping;
  54. // no flipping needed for cube textures
  55. texture.flipY = false;
  56. for ( var i = 0, il = array.length; i < il; ++ i ) {
  57. var cubeImage = new Image();
  58. images[ i ] = cubeImage;
  59. cubeImage.onload = function () {
  60. images.loadCount += 1;
  61. if ( images.loadCount === 6 ) {
  62. texture.needsUpdate = true;
  63. if ( onLoad ) onLoad( texture );
  64. }
  65. };
  66. cubeImage.onerror = onError;
  67. cubeImage.crossOrigin = this.crossOrigin;
  68. cubeImage.src = array[ i ];
  69. }
  70. return texture;
  71. },
  72. loadCompressedTextureCube: function ( array, mapping, onLoad, onError ) {
  73. var images = [];
  74. images.loadCount = 0;
  75. var texture = new THREE.CompressedTexture();
  76. texture.image = images;
  77. if ( mapping !== undefined ) texture.mapping = mapping;
  78. // no flipping for cube textures
  79. // (also flipping doesn't work for compressed textures )
  80. texture.flipY = false;
  81. // can't generate mipmaps for compressed textures
  82. // mips must be embedded in DDS files
  83. texture.generateMipmaps = false;
  84. var generateCubeFaceCallback = function ( rq, img ) {
  85. return function () {
  86. var buffer = rq.response;
  87. var dds = THREE.ImageUtils.parseDDS( buffer, true );
  88. img.format = dds.format;
  89. img.mipmaps = dds.mipmaps;
  90. img.width = dds.width;
  91. img.height = dds.height;
  92. images.loadCount += 1;
  93. if ( images.loadCount === 6 ) {
  94. texture.format = dds.format;
  95. texture.needsUpdate = true;
  96. if ( onLoad ) onLoad( texture );
  97. }
  98. }
  99. }
  100. // compressed cubemap textures as 6 separate DDS files
  101. if ( array instanceof Array ) {
  102. for ( var i = 0, il = array.length; i < il; ++ i ) {
  103. var cubeImage = {};
  104. images[ i ] = cubeImage;
  105. var request = new XMLHttpRequest();
  106. request.onload = generateCubeFaceCallback( request, cubeImage );
  107. request.onerror = onError;
  108. var url = array[ i ];
  109. request.open( 'GET', url, true );
  110. request.responseType = "arraybuffer";
  111. request.send( null );
  112. }
  113. // compressed cubemap texture stored in a single DDS file
  114. } else {
  115. var url = array;
  116. var request = new XMLHttpRequest();
  117. request.onload = function( ) {
  118. var buffer = request.response;
  119. var dds = THREE.ImageUtils.parseDDS( buffer, true );
  120. if ( dds.isCubemap ) {
  121. var faces = dds.mipmaps.length / dds.mipmapCount;
  122. for ( var f = 0; f < faces; f ++ ) {
  123. images[ f ] = { mipmaps : [] };
  124. for ( var i = 0; i < dds.mipmapCount; i ++ ) {
  125. images[ f ].mipmaps.push( dds.mipmaps[ f * dds.mipmapCount + i ] );
  126. images[ f ].format = dds.format;
  127. images[ f ].width = dds.width;
  128. images[ f ].height = dds.height;
  129. }
  130. }
  131. texture.format = dds.format;
  132. texture.needsUpdate = true;
  133. if ( onLoad ) onLoad( texture );
  134. }
  135. }
  136. request.onerror = onError;
  137. request.open( 'GET', url, true );
  138. request.responseType = "arraybuffer";
  139. request.send( null );
  140. }
  141. return texture;
  142. },
  143. parseDDS: function ( buffer, loadMipmaps ) {
  144. var dds = { mipmaps: [], width: 0, height: 0, format: null, mipmapCount: 1 };
  145. // Adapted from @toji's DDS utils
  146. // https://github.com/toji/webgl-texture-utils/blob/master/texture-util/dds.js
  147. // All values and structures referenced from:
  148. // http://msdn.microsoft.com/en-us/library/bb943991.aspx/
  149. var DDS_MAGIC = 0x20534444;
  150. var DDSD_CAPS = 0x1,
  151. DDSD_HEIGHT = 0x2,
  152. DDSD_WIDTH = 0x4,
  153. DDSD_PITCH = 0x8,
  154. DDSD_PIXELFORMAT = 0x1000,
  155. DDSD_MIPMAPCOUNT = 0x20000,
  156. DDSD_LINEARSIZE = 0x80000,
  157. DDSD_DEPTH = 0x800000;
  158. var DDSCAPS_COMPLEX = 0x8,
  159. DDSCAPS_MIPMAP = 0x400000,
  160. DDSCAPS_TEXTURE = 0x1000;
  161. var DDSCAPS2_CUBEMAP = 0x200,
  162. DDSCAPS2_CUBEMAP_POSITIVEX = 0x400,
  163. DDSCAPS2_CUBEMAP_NEGATIVEX = 0x800,
  164. DDSCAPS2_CUBEMAP_POSITIVEY = 0x1000,
  165. DDSCAPS2_CUBEMAP_NEGATIVEY = 0x2000,
  166. DDSCAPS2_CUBEMAP_POSITIVEZ = 0x4000,
  167. DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x8000,
  168. DDSCAPS2_VOLUME = 0x200000;
  169. var DDPF_ALPHAPIXELS = 0x1,
  170. DDPF_ALPHA = 0x2,
  171. DDPF_FOURCC = 0x4,
  172. DDPF_RGB = 0x40,
  173. DDPF_YUV = 0x200,
  174. DDPF_LUMINANCE = 0x20000;
  175. function fourCCToInt32( value ) {
  176. return value.charCodeAt(0) +
  177. (value.charCodeAt(1) << 8) +
  178. (value.charCodeAt(2) << 16) +
  179. (value.charCodeAt(3) << 24);
  180. }
  181. function int32ToFourCC( value ) {
  182. return String.fromCharCode(
  183. value & 0xff,
  184. (value >> 8) & 0xff,
  185. (value >> 16) & 0xff,
  186. (value >> 24) & 0xff
  187. );
  188. }
  189. var FOURCC_DXT1 = fourCCToInt32("DXT1");
  190. var FOURCC_DXT3 = fourCCToInt32("DXT3");
  191. var FOURCC_DXT5 = fourCCToInt32("DXT5");
  192. var headerLengthInt = 31; // The header length in 32 bit ints
  193. // Offsets into the header array
  194. var off_magic = 0;
  195. var off_size = 1;
  196. var off_flags = 2;
  197. var off_height = 3;
  198. var off_width = 4;
  199. var off_mipmapCount = 7;
  200. var off_pfFlags = 20;
  201. var off_pfFourCC = 21;
  202. var off_caps = 27;
  203. var off_caps2 = 28;
  204. var off_caps3 = 29;
  205. var off_caps4 = 30;
  206. // Parse header
  207. var header = new Int32Array( buffer, 0, headerLengthInt );
  208. if ( header[ off_magic ] !== DDS_MAGIC ) {
  209. console.error( "ImageUtils.parseDDS(): Invalid magic number in DDS header" );
  210. return dds;
  211. }
  212. if ( ! header[ off_pfFlags ] & DDPF_FOURCC ) {
  213. console.error( "ImageUtils.parseDDS(): Unsupported format, must contain a FourCC code" );
  214. return dds;
  215. }
  216. var blockBytes;
  217. var fourCC = header[ off_pfFourCC ];
  218. switch ( fourCC ) {
  219. case FOURCC_DXT1:
  220. blockBytes = 8;
  221. dds.format = THREE.RGB_S3TC_DXT1_Format;
  222. break;
  223. case FOURCC_DXT3:
  224. blockBytes = 16;
  225. dds.format = THREE.RGBA_S3TC_DXT3_Format;
  226. break;
  227. case FOURCC_DXT5:
  228. blockBytes = 16;
  229. dds.format = THREE.RGBA_S3TC_DXT5_Format;
  230. break;
  231. default:
  232. console.error( "ImageUtils.parseDDS(): Unsupported FourCC code: ", int32ToFourCC( fourCC ) );
  233. return dds;
  234. }
  235. dds.mipmapCount = 1;
  236. if ( header[ off_flags ] & DDSD_MIPMAPCOUNT && loadMipmaps !== false ) {
  237. dds.mipmapCount = Math.max( 1, header[ off_mipmapCount ] );
  238. }
  239. //TODO: Verify that all faces of the cubemap are present with DDSCAPS2_CUBEMAP_POSITIVEX, etc.
  240. dds.isCubemap = header[ off_caps2 ] & DDSCAPS2_CUBEMAP ? true : false;
  241. dds.width = header[ off_width ];
  242. dds.height = header[ off_height ];
  243. var dataOffset = header[ off_size ] + 4;
  244. // Extract mipmaps buffers
  245. var width = dds.width;
  246. var height = dds.height;
  247. var faces = dds.isCubemap ? 6 : 1;
  248. for ( var face = 0; face < faces; face ++ ) {
  249. for ( var i = 0; i < dds.mipmapCount; i ++ ) {
  250. var dataLength = Math.max( 4, width ) / 4 * Math.max( 4, height ) / 4 * blockBytes;
  251. var byteArray = new Uint8Array( buffer, dataOffset, dataLength );
  252. var mipmap = { "data": byteArray, "width": width, "height": height };
  253. dds.mipmaps.push( mipmap );
  254. dataOffset += dataLength;
  255. width = Math.max( width * 0.5, 1 );
  256. height = Math.max( height * 0.5, 1 );
  257. }
  258. width = dds.width;
  259. height = dds.height;
  260. }
  261. return dds;
  262. },
  263. getNormalMap: function ( image, depth ) {
  264. // Adapted from http://www.paulbrunt.co.uk/lab/heightnormal/
  265. var cross = function ( a, b ) {
  266. return [ a[ 1 ] * b[ 2 ] - a[ 2 ] * b[ 1 ], a[ 2 ] * b[ 0 ] - a[ 0 ] * b[ 2 ], a[ 0 ] * b[ 1 ] - a[ 1 ] * b[ 0 ] ];
  267. }
  268. var subtract = function ( a, b ) {
  269. return [ a[ 0 ] - b[ 0 ], a[ 1 ] - b[ 1 ], a[ 2 ] - b[ 2 ] ];
  270. }
  271. var normalize = function ( a ) {
  272. var l = Math.sqrt( a[ 0 ] * a[ 0 ] + a[ 1 ] * a[ 1 ] + a[ 2 ] * a[ 2 ] );
  273. return [ a[ 0 ] / l, a[ 1 ] / l, a[ 2 ] / l ];
  274. }
  275. depth = depth | 1;
  276. var width = image.width;
  277. var height = image.height;
  278. var canvas = document.createElement( 'canvas' );
  279. canvas.width = width;
  280. canvas.height = height;
  281. var context = canvas.getContext( '2d' );
  282. context.drawImage( image, 0, 0 );
  283. var data = context.getImageData( 0, 0, width, height ).data;
  284. var imageData = context.createImageData( width, height );
  285. var output = imageData.data;
  286. for ( var x = 0; x < width; x ++ ) {
  287. for ( var y = 0; y < height; y ++ ) {
  288. var ly = y - 1 < 0 ? 0 : y - 1;
  289. var uy = y + 1 > height - 1 ? height - 1 : y + 1;
  290. var lx = x - 1 < 0 ? 0 : x - 1;
  291. var ux = x + 1 > width - 1 ? width - 1 : x + 1;
  292. var points = [];
  293. var origin = [ 0, 0, data[ ( y * width + x ) * 4 ] / 255 * depth ];
  294. points.push( [ - 1, 0, data[ ( y * width + lx ) * 4 ] / 255 * depth ] );
  295. points.push( [ - 1, - 1, data[ ( ly * width + lx ) * 4 ] / 255 * depth ] );
  296. points.push( [ 0, - 1, data[ ( ly * width + x ) * 4 ] / 255 * depth ] );
  297. points.push( [ 1, - 1, data[ ( ly * width + ux ) * 4 ] / 255 * depth ] );
  298. points.push( [ 1, 0, data[ ( y * width + ux ) * 4 ] / 255 * depth ] );
  299. points.push( [ 1, 1, data[ ( uy * width + ux ) * 4 ] / 255 * depth ] );
  300. points.push( [ 0, 1, data[ ( uy * width + x ) * 4 ] / 255 * depth ] );
  301. points.push( [ - 1, 1, data[ ( uy * width + lx ) * 4 ] / 255 * depth ] );
  302. var normals = [];
  303. var num_points = points.length;
  304. for ( var i = 0; i < num_points; i ++ ) {
  305. var v1 = points[ i ];
  306. var v2 = points[ ( i + 1 ) % num_points ];
  307. v1 = subtract( v1, origin );
  308. v2 = subtract( v2, origin );
  309. normals.push( normalize( cross( v1, v2 ) ) );
  310. }
  311. var normal = [ 0, 0, 0 ];
  312. for ( var i = 0; i < normals.length; i ++ ) {
  313. normal[ 0 ] += normals[ i ][ 0 ];
  314. normal[ 1 ] += normals[ i ][ 1 ];
  315. normal[ 2 ] += normals[ i ][ 2 ];
  316. }
  317. normal[ 0 ] /= normals.length;
  318. normal[ 1 ] /= normals.length;
  319. normal[ 2 ] /= normals.length;
  320. var idx = ( y * width + x ) * 4;
  321. output[ idx ] = ( ( normal[ 0 ] + 1.0 ) / 2.0 * 255 ) | 0;
  322. output[ idx + 1 ] = ( ( normal[ 1 ] + 1.0 ) / 2.0 * 255 ) | 0;
  323. output[ idx + 2 ] = ( normal[ 2 ] * 255 ) | 0;
  324. output[ idx + 3 ] = 255;
  325. }
  326. }
  327. context.putImageData( imageData, 0, 0 );
  328. return canvas;
  329. },
  330. generateDataTexture: function ( width, height, color ) {
  331. var size = width * height;
  332. var data = new Uint8Array( 3 * size );
  333. var r = Math.floor( color.r * 255 );
  334. var g = Math.floor( color.g * 255 );
  335. var b = Math.floor( color.b * 255 );
  336. for ( var i = 0; i < size; i ++ ) {
  337. data[ i * 3 ] = r;
  338. data[ i * 3 + 1 ] = g;
  339. data[ i * 3 + 2 ] = b;
  340. }
  341. var texture = new THREE.DataTexture( data, width, height, THREE.RGBFormat );
  342. texture.needsUpdate = true;
  343. return texture;
  344. }
  345. };
粤ICP备19079148号