HDRCubeTextureLoader.js 5.5 KB

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