HDRCubeTextureLoader.js 2.7 KB

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