HDRCubeTextureLoader.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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;
  74. var loaded = 0;
  75. function loadHDRData( i, onLoad, onProgress, onError ) {
  76. var loader = new THREE.FileLoader( scope.manager );
  77. loader.setPath( scope.path );
  78. loader.setResponseType( 'arraybuffer' );
  79. loader.load( urls[ i ], function ( buffer ) {
  80. loaded ++;
  81. var texData = scope.hdrLoader._parser( buffer );
  82. if ( ! texData ) return;
  83. if ( type === THREE.FloatType ) {
  84. var numElements = ( texData.data.length / 4 ) * 3;
  85. var floatdata = new Float32Array( numElements );
  86. for ( var j = 0; j < numElements; j ++ ) {
  87. RGBEByteToRGBFloat( texData.data, j * 4, floatdata, j * 3 );
  88. }
  89. texData.data = floatdata;
  90. } else if ( type === THREE.HalfFloatType ) {
  91. var numElements = ( texData.data.length / 4 ) * 3;
  92. var halfdata = new Uint16Array( numElements );
  93. for ( var j = 0; j < numElements; j ++ ) {
  94. RGBEByteToRGBHalf( texData.data, j * 4, halfdata, j * 3 );
  95. }
  96. texData.data = halfdata;
  97. }
  98. if ( texData.image !== undefined ) {
  99. texture[ i ].images = texData.image;
  100. } else if ( texData.data !== undefined ) {
  101. var dataTexture = new THREE.DataTexture( texData.data, texData.width, texData.height );
  102. dataTexture.format = texture.format;
  103. dataTexture.type = texture.type;
  104. dataTexture.encoding = texture.encoding;
  105. dataTexture.minFilter = texture.minFilter;
  106. dataTexture.magFilter = texture.magFilter;
  107. dataTexture.generateMipmaps = texture.generateMipmaps;
  108. texture.images[ i ] = dataTexture;
  109. }
  110. if ( loaded === 6 ) {
  111. texture.needsUpdate = true;
  112. if ( onLoad ) onLoad( texture );
  113. }
  114. }, onProgress, onError );
  115. }
  116. for ( var i = 0; i < urls.length; i ++ ) {
  117. loadHDRData( i, onLoad, onProgress, onError );
  118. }
  119. return texture;
  120. };
  121. THREE.HDRCubeTextureLoader.prototype.setPath = function ( value ) {
  122. this.path = value;
  123. return this;
  124. };