HDRCubeTextureLoader.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import {
  2. CubeTexture,
  3. DataTexture,
  4. FileLoader,
  5. FloatType,
  6. HalfFloatType,
  7. LinearFilter,
  8. LinearSRGBColorSpace,
  9. Loader
  10. } from 'three';
  11. import { RGBELoader } from '../loaders/RGBELoader.js';
  12. /**
  13. * A loader for loading HDR cube textures.
  14. *
  15. * ```js
  16. * const loader = new HDRCubeTextureLoader();
  17. * loader.setPath( 'textures/cube/pisaHDR/' );
  18. * const cubeTexture = await loader.loadAsync( [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ] );
  19. *
  20. * scene.background = cubeTexture;
  21. * scene.environment = cubeTexture;
  22. * ```
  23. *
  24. * @augments Loader
  25. */
  26. class HDRCubeTextureLoader extends Loader {
  27. /**
  28. * Constructs a new HDR cube texture loader.
  29. *
  30. * @param {LoadingManager} [manager] - The loading manager.
  31. */
  32. constructor( manager ) {
  33. super( manager );
  34. /**
  35. * The internal HDR loader that loads the
  36. * individual textures for each cube face.
  37. *
  38. * @type {RGBELoader}
  39. */
  40. this.hdrLoader = new RGBELoader();
  41. /**
  42. * The texture type.
  43. *
  44. * @type {(HalfFloatType|FloatType)}
  45. * @default HalfFloatType
  46. */
  47. this.type = HalfFloatType;
  48. }
  49. /**
  50. * Starts loading from the given URLs and passes the loaded HDR cube texture
  51. * to the `onLoad()` callback.
  52. *
  53. * @param {Array<string>} urls - The paths/URLs of the files to be loaded. This can also be a data URIs.
  54. * @param {function(CubeTexture)} onLoad - Executed when the loading process has been finished.
  55. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  56. * @param {onErrorCallback} onError - Executed when errors occur.
  57. * @return {CubeTexture} The HDR cube texture.
  58. */
  59. load( urls, onLoad, onProgress, onError ) {
  60. const texture = new CubeTexture();
  61. texture.type = this.type;
  62. switch ( texture.type ) {
  63. case FloatType:
  64. texture.colorSpace = LinearSRGBColorSpace;
  65. texture.minFilter = LinearFilter;
  66. texture.magFilter = LinearFilter;
  67. texture.generateMipmaps = false;
  68. break;
  69. case HalfFloatType:
  70. texture.colorSpace = LinearSRGBColorSpace;
  71. texture.minFilter = LinearFilter;
  72. texture.magFilter = LinearFilter;
  73. texture.generateMipmaps = false;
  74. break;
  75. }
  76. const scope = this;
  77. let loaded = 0;
  78. function loadHDRData( i, onLoad, onProgress, onError ) {
  79. new FileLoader( scope.manager )
  80. .setPath( scope.path )
  81. .setResponseType( 'arraybuffer' )
  82. .setWithCredentials( scope.withCredentials )
  83. .load( urls[ i ], function ( buffer ) {
  84. loaded ++;
  85. const texData = scope.hdrLoader.parse( buffer );
  86. if ( ! texData ) return;
  87. if ( texData.data !== undefined ) {
  88. const dataTexture = new DataTexture( texData.data, texData.width, texData.height );
  89. dataTexture.type = texture.type;
  90. dataTexture.colorSpace = texture.colorSpace;
  91. dataTexture.format = texture.format;
  92. dataTexture.minFilter = texture.minFilter;
  93. dataTexture.magFilter = texture.magFilter;
  94. dataTexture.generateMipmaps = texture.generateMipmaps;
  95. texture.images[ i ] = dataTexture;
  96. }
  97. if ( loaded === 6 ) {
  98. texture.needsUpdate = true;
  99. if ( onLoad ) onLoad( texture );
  100. }
  101. }, onProgress, onError );
  102. }
  103. for ( let i = 0; i < urls.length; i ++ ) {
  104. loadHDRData( i, onLoad, onProgress, onError );
  105. }
  106. return texture;
  107. }
  108. /**
  109. * Sets the texture type.
  110. *
  111. * @param {(HalfFloatType|FloatType)} value - The texture type to set.
  112. * @return {RGBELoader} A reference to this loader.
  113. */
  114. setDataType( value ) {
  115. this.type = value;
  116. this.hdrLoader.setDataType( value );
  117. return this;
  118. }
  119. }
  120. export { HDRCubeTextureLoader };
粤ICP备19079148号