Skip to content

⬅️ Back to Table of Contents

📄 DataTextureLoader.js

📊 Analysis Summary

Metric Count
🔧 Functions 1
🧱 Classes 1
📦 Imports 6
📊 Variables & Constants 4

📚 Table of Contents

🛠️ File Location:

📂 src/loaders/DataTextureLoader.js

📦 Imports

Name Source
LinearFilter ../constants.js
LinearMipmapLinearFilter ../constants.js
ClampToEdgeWrapping ../constants.js
FileLoader ./FileLoader.js
DataTexture ../textures/DataTexture.js
Loader ./Loader.js

Variables & Constants

Name Type Kind Value Exported
scope this let/var this
texture DataTexture let/var new DataTexture()
loader FileLoader let/var new FileLoader( this.manager )
texData any let/var *not shown*

Functions

DataTextureLoader.load(url: string, onLoad: (arg0: DataTexture) => any, onProgress: onProgressCallback, onError: onErrorCallback): DataTexture

JSDoc:

/**
     * 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.
     */

Parameters:

  • url string
  • onLoad (arg0: DataTexture) => any
  • onProgress onProgressCallback
  • onError onErrorCallback

Returns: DataTexture

Calls:

  • loader.setResponseType
  • loader.setRequestHeader
  • loader.setPath
  • loader.setWithCredentials
  • loader.load
  • scope.parse
  • onError
  • console.error
  • onLoad
Code
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;

    }

Classes

DataTextureLoader

Class Code
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;

    }

}

Methods

load(url: string, onLoad: (arg0: DataTexture) => any, onProgress: onProgressCallback, onError: onErrorCallback): DataTexture
Code
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;

    }