| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- import { LinearFilter, LinearMipmapLinearFilter, ClampToEdgeWrapping } from '../constants.js';
- import { FileLoader } from './FileLoader.js';
- import { DataTexture } from '../textures/DataTexture.js';
- import { Loader } from './Loader.js';
- /**
- * Abstract base class for loading binary texture formats RGBE, EXR or TGA.
- * Textures are internally loaded via {@link FileLoader}.
- *
- * Derived classes have to implement the `parse()` method which holds the parsing
- * for the respective format.
- *
- * @abstract
- * @augments Loader
- */
- class DataTextureLoader extends Loader {
- /**
- * Constructs a new data texture loader.
- *
- * @param {LoadingManager} [manager] - The loading manager.
- */
- constructor( manager ) {
- super( manager );
- }
- /**
- * Starts loading from the given URL and passes the loaded data texture
- * to the `onLoad()` callback. The method also returns a new texture object which can
- * directly be used for material creation. If you do it this way, the texture
- * may pop up in your scene once the respective loading process is finished.
- *
- * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
- * @param {function(DataTexture)} onLoad - Executed when the loading process has been finished.
- * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
- * @param {onErrorCallback} onError - Executed when errors occur.
- * @return {DataTexture} The data texture.
- */
- load( url, onLoad, onProgress, onError ) {
- const scope = this;
- const texture = new DataTexture();
- const loader = new FileLoader( this.manager );
- loader.setResponseType( 'arraybuffer' );
- loader.setRequestHeader( this.requestHeader );
- loader.setPath( this.path );
- loader.setWithCredentials( scope.withCredentials );
- loader.load( url, function ( buffer ) {
- let texData;
- try {
- texData = scope.parse( buffer );
- } catch ( error ) {
- if ( onError !== undefined ) {
- onError( error );
- } else {
- console.error( error );
- return;
- }
- }
- if ( texData.image !== undefined ) {
- texture.image = texData.image;
- } else if ( texData.data !== undefined ) {
- texture.image.width = texData.width;
- texture.image.height = texData.height;
- texture.image.data = texData.data;
- }
- texture.wrapS = texData.wrapS !== undefined ? texData.wrapS : ClampToEdgeWrapping;
- texture.wrapT = texData.wrapT !== undefined ? texData.wrapT : ClampToEdgeWrapping;
- texture.magFilter = texData.magFilter !== undefined ? texData.magFilter : LinearFilter;
- texture.minFilter = texData.minFilter !== undefined ? texData.minFilter : LinearFilter;
- texture.anisotropy = texData.anisotropy !== undefined ? texData.anisotropy : 1;
- if ( texData.colorSpace !== undefined ) {
- texture.colorSpace = texData.colorSpace;
- }
- if ( texData.flipY !== undefined ) {
- texture.flipY = texData.flipY;
- }
- if ( texData.format !== undefined ) {
- texture.format = texData.format;
- }
- if ( texData.type !== undefined ) {
- texture.type = texData.type;
- }
- if ( texData.mipmaps !== undefined ) {
- texture.mipmaps = texData.mipmaps;
- texture.minFilter = LinearMipmapLinearFilter; // presumably...
- }
- if ( texData.mipmapCount === 1 ) {
- texture.minFilter = LinearFilter;
- }
- if ( texData.generateMipmaps !== undefined ) {
- texture.generateMipmaps = texData.generateMipmaps;
- }
- texture.needsUpdate = true;
- if ( onLoad ) onLoad( texture, texData );
- }, onProgress, onError );
- return texture;
- }
- }
- export { DataTextureLoader };
|