HDRCubeMapLoader.js 5.6 KB

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