HDRCubeTextureLoader.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {
  2. CubeTexture,
  3. DataTexture,
  4. FileLoader,
  5. FloatType,
  6. HalfFloatType,
  7. LinearEncoding,
  8. LinearFilter,
  9. Loader,
  10. NearestFilter,
  11. RGBAFormat,
  12. RGBEEncoding,
  13. RGBFormat,
  14. UnsignedByteType
  15. } from '../../../build/three.module.js';
  16. import { RGBELoader } from '../loaders/RGBELoader.js';
  17. class HDRCubeTextureLoader extends Loader {
  18. constructor( manager ) {
  19. super( manager );
  20. this.hdrLoader = new RGBELoader();
  21. this.type = HalfFloatType;
  22. }
  23. load( urls, onLoad, onProgress, onError ) {
  24. if ( ! Array.isArray( urls ) ) {
  25. console.warn( 'THREE.HDRCubeTextureLoader signature has changed. Use .setDataType() instead.' );
  26. this.setDataType( urls );
  27. urls = onLoad;
  28. onLoad = onProgress;
  29. onProgress = onError;
  30. onError = arguments[ 4 ];
  31. }
  32. const texture = new CubeTexture();
  33. texture.type = this.type;
  34. switch ( texture.type ) {
  35. case UnsignedByteType:
  36. texture.encoding = RGBEEncoding;
  37. texture.format = RGBAFormat;
  38. texture.minFilter = NearestFilter;
  39. texture.magFilter = NearestFilter;
  40. texture.generateMipmaps = false;
  41. break;
  42. case FloatType:
  43. texture.encoding = LinearEncoding;
  44. texture.format = RGBFormat;
  45. texture.minFilter = LinearFilter;
  46. texture.magFilter = LinearFilter;
  47. texture.generateMipmaps = false;
  48. break;
  49. case HalfFloatType:
  50. texture.encoding = LinearEncoding;
  51. texture.format = RGBFormat;
  52. texture.minFilter = LinearFilter;
  53. texture.magFilter = LinearFilter;
  54. texture.generateMipmaps = false;
  55. break;
  56. }
  57. const scope = this;
  58. let loaded = 0;
  59. function loadHDRData( i, onLoad, onProgress, onError ) {
  60. new FileLoader( scope.manager )
  61. .setPath( scope.path )
  62. .setResponseType( 'arraybuffer' )
  63. .setWithCredentials( scope.withCredentials )
  64. .load( urls[ i ], function ( buffer ) {
  65. loaded ++;
  66. const texData = scope.hdrLoader.parse( buffer );
  67. if ( ! texData ) return;
  68. if ( texData.data !== undefined ) {
  69. const dataTexture = new DataTexture( texData.data, texData.width, texData.height );
  70. dataTexture.type = texture.type;
  71. dataTexture.encoding = texture.encoding;
  72. dataTexture.format = texture.format;
  73. dataTexture.minFilter = texture.minFilter;
  74. dataTexture.magFilter = texture.magFilter;
  75. dataTexture.generateMipmaps = texture.generateMipmaps;
  76. texture.images[ i ] = dataTexture;
  77. }
  78. if ( loaded === 6 ) {
  79. texture.needsUpdate = true;
  80. if ( onLoad ) onLoad( texture );
  81. }
  82. }, onProgress, onError );
  83. }
  84. for ( let i = 0; i < urls.length; i ++ ) {
  85. loadHDRData( i, onLoad, onProgress, onError );
  86. }
  87. return texture;
  88. }
  89. setDataType( value ) {
  90. this.type = value;
  91. this.hdrLoader.setDataType( value );
  92. return this;
  93. }
  94. }
  95. export { HDRCubeTextureLoader };