HDRCubeTextureLoader.js 2.1 KB

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