DataTextureLoader.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { LinearFilter, LinearMipmapLinearFilter, ClampToEdgeWrapping } from '../constants.js';
  2. import { FileLoader } from './FileLoader.js';
  3. import { DataTexture } from '../textures/DataTexture.js';
  4. import { Loader } from './Loader.js';
  5. /**
  6. * Abstract base class for loading binary texture formats RGBE, EXR or TGA.
  7. * Textures are internally loaded via {@link FileLoader}.
  8. *
  9. * Derived classes have to implement the `parse()` method which holds the parsing
  10. * for the respective format.
  11. *
  12. * @abstract
  13. * @augments Loader
  14. */
  15. class DataTextureLoader extends Loader {
  16. /**
  17. * Constructs a new data texture loader.
  18. *
  19. * @param {LoadingManager} [manager] - The loading manager.
  20. */
  21. constructor( manager ) {
  22. super( manager );
  23. }
  24. /**
  25. * Starts loading from the given URL and passes the loaded data texture
  26. * to the `onLoad()` callback. The method also returns a new texture object which can
  27. * directly be used for material creation. If you do it this way, the texture
  28. * may pop up in your scene once the respective loading process is finished.
  29. *
  30. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  31. * @param {function(DataTexture)} onLoad - Executed when the loading process has been finished.
  32. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  33. * @param {onErrorCallback} onError - Executed when errors occur.
  34. * @return {DataTexture} The data texture.
  35. */
  36. load( url, onLoad, onProgress, onError ) {
  37. const scope = this;
  38. const texture = new DataTexture();
  39. const loader = new FileLoader( this.manager );
  40. loader.setResponseType( 'arraybuffer' );
  41. loader.setRequestHeader( this.requestHeader );
  42. loader.setPath( this.path );
  43. loader.setWithCredentials( scope.withCredentials );
  44. loader.load( url, function ( buffer ) {
  45. let texData;
  46. try {
  47. texData = scope.parse( buffer );
  48. } catch ( error ) {
  49. if ( onError !== undefined ) {
  50. onError( error );
  51. } else {
  52. console.error( error );
  53. return;
  54. }
  55. }
  56. if ( texData.image !== undefined ) {
  57. texture.image = texData.image;
  58. } else if ( texData.data !== undefined ) {
  59. texture.image.width = texData.width;
  60. texture.image.height = texData.height;
  61. texture.image.data = texData.data;
  62. }
  63. texture.wrapS = texData.wrapS !== undefined ? texData.wrapS : ClampToEdgeWrapping;
  64. texture.wrapT = texData.wrapT !== undefined ? texData.wrapT : ClampToEdgeWrapping;
  65. texture.magFilter = texData.magFilter !== undefined ? texData.magFilter : LinearFilter;
  66. texture.minFilter = texData.minFilter !== undefined ? texData.minFilter : LinearFilter;
  67. texture.anisotropy = texData.anisotropy !== undefined ? texData.anisotropy : 1;
  68. if ( texData.colorSpace !== undefined ) {
  69. texture.colorSpace = texData.colorSpace;
  70. }
  71. if ( texData.flipY !== undefined ) {
  72. texture.flipY = texData.flipY;
  73. }
  74. if ( texData.format !== undefined ) {
  75. texture.format = texData.format;
  76. }
  77. if ( texData.type !== undefined ) {
  78. texture.type = texData.type;
  79. }
  80. if ( texData.mipmaps !== undefined ) {
  81. texture.mipmaps = texData.mipmaps;
  82. texture.minFilter = LinearMipmapLinearFilter; // presumably...
  83. }
  84. if ( texData.mipmapCount === 1 ) {
  85. texture.minFilter = LinearFilter;
  86. }
  87. if ( texData.generateMipmaps !== undefined ) {
  88. texture.generateMipmaps = texData.generateMipmaps;
  89. }
  90. texture.needsUpdate = true;
  91. if ( onLoad ) onLoad( texture, texData );
  92. }, onProgress, onError );
  93. return texture;
  94. }
  95. }
  96. export { DataTextureLoader };
粤ICP备19079148号