HDRCubeTextureLoader.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @author Prashant Sharma / spidersharma03
  3. * @author Ben Houston / http://clara.io / bhouston
  4. */
  5. THREE.HDRCubeTextureLoader = function ( manager ) {
  6. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  7. // override in sub classes
  8. this.hdrLoader = new THREE.RGBELoader();
  9. };
  10. THREE.HDRCubeTextureLoader.prototype.load = function ( type, urls, onLoad, onProgress, onError ) {
  11. var RGBEByteToRGBFloat = function ( sourceArray, sourceOffset, destArray, destOffset ) {
  12. var e = sourceArray[ sourceOffset + 3 ];
  13. var scale = Math.pow( 2.0, e - 128.0 ) / 255.0;
  14. destArray[ destOffset + 0 ] = sourceArray[ sourceOffset + 0 ] * scale;
  15. destArray[ destOffset + 1 ] = sourceArray[ sourceOffset + 1 ] * scale;
  16. destArray[ destOffset + 2 ] = sourceArray[ sourceOffset + 2 ] * scale;
  17. };
  18. var RGBEByteToRGBHalf = ( function () {
  19. // Source: http://gamedev.stackexchange.com/questions/17326/conversion-of-a-number-from-single-precision-floating-point-representation-to-a/17410#17410
  20. var floatView = new Float32Array( 1 );
  21. var int32View = new Int32Array( floatView.buffer );
  22. /* This method is faster than the OpenEXR implementation (very often
  23. * used, eg. in Ogre), with the additional benefit of rounding, inspired
  24. * by James Tursa?s half-precision code. */
  25. function toHalf( val ) {
  26. floatView[ 0 ] = val;
  27. var x = int32View[ 0 ];
  28. var bits = ( x >> 16 ) & 0x8000; /* Get the sign */
  29. var m = ( x >> 12 ) & 0x07ff; /* Keep one extra bit for rounding */
  30. var e = ( x >> 23 ) & 0xff; /* Using int is faster here */
  31. /* If zero, or denormal, or exponent underflows too much for a denormal
  32. * half, return signed zero. */
  33. if ( e < 103 ) return bits;
  34. /* If NaN, return NaN. If Inf or exponent overflow, return Inf. */
  35. if ( e > 142 ) {
  36. bits |= 0x7c00;
  37. /* If exponent was 0xff and one mantissa bit was set, it means NaN,
  38. * not Inf, so make sure we set one mantissa bit too. */
  39. bits |= ( ( e == 255 ) ? 0 : 1 ) && ( x & 0x007fffff );
  40. return bits;
  41. }
  42. /* If exponent underflows but not too much, return a denormal */
  43. if ( e < 113 ) {
  44. m |= 0x0800;
  45. /* Extra rounding may overflow and set mantissa to 0 and exponent
  46. * to 1, which is OK. */
  47. bits |= ( m >> ( 114 - e ) ) + ( ( m >> ( 113 - e ) ) & 1 );
  48. return bits;
  49. }
  50. bits |= ( ( e - 112 ) << 10 ) | ( m >> 1 );
  51. /* Extra rounding. An overflow will set mantissa to 0 and increment
  52. * the exponent, which is OK. */
  53. bits += m & 1;
  54. return bits;
  55. }
  56. return function ( sourceArray, sourceOffset, destArray, destOffset ) {
  57. var e = sourceArray[ sourceOffset + 3 ];
  58. var scale = Math.pow( 2.0, e - 128.0 ) / 255.0;
  59. destArray[ destOffset + 0 ] = toHalf( sourceArray[ sourceOffset + 0 ] * scale );
  60. destArray[ destOffset + 1 ] = toHalf( sourceArray[ sourceOffset + 1 ] * scale );
  61. destArray[ destOffset + 2 ] = toHalf( sourceArray[ sourceOffset + 2 ] * scale );
  62. };
  63. } )();
  64. //
  65. var texture = new THREE.CubeTexture();
  66. texture.type = type;
  67. texture.encoding = ( type === THREE.UnsignedByteType ) ? THREE.RGBEEncoding : THREE.LinearEncoding;
  68. texture.format = ( type === THREE.UnsignedByteType ) ? THREE.RGBAFormat : THREE.RGBFormat;
  69. texture.minFilter = ( texture.encoding === THREE.RGBEEncoding ) ? THREE.NearestFilter : THREE.LinearFilter;
  70. texture.magFilter = ( texture.encoding === THREE.RGBEEncoding ) ? THREE.NearestFilter : THREE.LinearFilter;
  71. texture.generateMipmaps = ( texture.encoding !== THREE.RGBEEncoding );
  72. texture.anisotropy = 0;
  73. var scope = this.hdrLoader;
  74. var loaded = 0;
  75. function loadHDRData( i, onLoad, onProgress, onError ) {
  76. var loader = new THREE.FileLoader( this.manager );
  77. loader.setResponseType( 'arraybuffer' );
  78. loader.load( urls[ i ], function ( buffer ) {
  79. loaded ++;
  80. var texData = scope._parser( buffer );
  81. if ( ! texData ) return;
  82. if ( type === THREE.FloatType ) {
  83. var numElements = ( texData.data.length / 4 ) * 3;
  84. var floatdata = new Float32Array( numElements );
  85. for ( var j = 0; j < numElements; j ++ ) {
  86. RGBEByteToRGBFloat( texData.data, j * 4, floatdata, j * 3 );
  87. }
  88. texData.data = floatdata;
  89. } else if ( type === THREE.HalfFloatType ) {
  90. var numElements = ( texData.data.length / 4 ) * 3;
  91. var halfdata = new Uint16Array( numElements );
  92. for ( var j = 0; j < numElements; j ++ ) {
  93. RGBEByteToRGBHalf( texData.data, j * 4, halfdata, j * 3 );
  94. }
  95. texData.data = halfdata;
  96. }
  97. if ( undefined !== texData.image ) {
  98. texture[ i ].images = texData.image;
  99. } else if ( undefined !== texData.data ) {
  100. var dataTexture = new THREE.DataTexture( texData.data, texData.width, texData.height );
  101. dataTexture.format = texture.format;
  102. dataTexture.type = texture.type;
  103. dataTexture.encoding = texture.encoding;
  104. dataTexture.minFilter = texture.minFilter;
  105. dataTexture.magFilter = texture.magFilter;
  106. dataTexture.generateMipmaps = texture.generateMipmaps;
  107. texture.images[ i ] = dataTexture;
  108. }
  109. if ( loaded === 6 ) {
  110. texture.needsUpdate = true;
  111. if ( onLoad ) onLoad( texture );
  112. }
  113. }, onProgress, onError );
  114. }
  115. for ( var i = 0; i < urls.length; i ++ ) {
  116. loadHDRData( i, onLoad, onProgress, onError );
  117. }
  118. return texture;
  119. };