Skip to content

⬅️ Back to Table of Contents

📄 CompressedTextureLoader.js

📊 Analysis Summary

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

📚 Table of Contents

🛠️ File Location:

📂 src/loaders/CompressedTextureLoader.js

📦 Imports

Name Source
LinearFilter ../constants.js
FileLoader ./FileLoader.js
CompressedTexture ../textures/CompressedTexture.js
Loader ./Loader.js

Variables & Constants

Name Type Kind Value Exported
scope this let/var this
images any[] let/var []
texture CompressedTexture let/var new CompressedTexture()
loader FileLoader let/var new FileLoader( this.manager )
loaded number let/var 0
faces number let/var texDatas.mipmaps.length / texDatas.mipmapCount

Functions

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

JSDoc:

/**
     * Starts loading from the given URL and passes the loaded compressed 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(CompressedTexture)} 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 {CompressedTexture} The compressed texture.
     */

Parameters:

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

Returns: CompressedTexture

Calls:

  • loader.setPath
  • loader.setResponseType
  • loader.setRequestHeader
  • loader.setWithCredentials
  • loader.load
  • scope.parse
  • onLoad
  • Array.isArray
  • loadTexture
  • images[ f ].mipmaps.push

Internal Comments:

// compressed cubemap texture stored in a single DDS file (x4)

Code
load( url, onLoad, onProgress, onError ) {

        const scope = this;

        const images = [];

        const texture = new CompressedTexture();

        const loader = new FileLoader( this.manager );
        loader.setPath( this.path );
        loader.setResponseType( 'arraybuffer' );
        loader.setRequestHeader( this.requestHeader );
        loader.setWithCredentials( scope.withCredentials );

        let loaded = 0;

        function loadTexture( i ) {

            loader.load( url[ i ], function ( buffer ) {

                const texDatas = scope.parse( buffer, true );

                images[ i ] = {
                    width: texDatas.width,
                    height: texDatas.height,
                    format: texDatas.format,
                    mipmaps: texDatas.mipmaps
                };

                loaded += 1;

                if ( loaded === 6 ) {

                    if ( texDatas.mipmapCount === 1 ) texture.minFilter = LinearFilter;

                    texture.image = images;
                    texture.format = texDatas.format;
                    texture.needsUpdate = true;

                    if ( onLoad ) onLoad( texture );

                }

            }, onProgress, onError );

        }

        if ( Array.isArray( url ) ) {

            for ( let i = 0, il = url.length; i < il; ++ i ) {

                loadTexture( i );

            }

        } else {

            // compressed cubemap texture stored in a single DDS file

            loader.load( url, function ( buffer ) {

                const texDatas = scope.parse( buffer, true );

                if ( texDatas.isCubemap ) {

                    const faces = texDatas.mipmaps.length / texDatas.mipmapCount;

                    for ( let f = 0; f < faces; f ++ ) {

                        images[ f ] = { mipmaps: [] };

                        for ( let i = 0; i < texDatas.mipmapCount; i ++ ) {

                            images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] );
                            images[ f ].format = texDatas.format;
                            images[ f ].width = texDatas.width;
                            images[ f ].height = texDatas.height;

                        }

                    }

                    texture.image = images;

                } else {

                    texture.image.width = texDatas.width;
                    texture.image.height = texDatas.height;
                    texture.mipmaps = texDatas.mipmaps;

                }

                if ( texDatas.mipmapCount === 1 ) {

                    texture.minFilter = LinearFilter;

                }

                texture.format = texDatas.format;
                texture.needsUpdate = true;

                if ( onLoad ) onLoad( texture );

            }, onProgress, onError );

        }

        return texture;

    }

loadTexture(i: any): void

Parameters:

  • i any

Returns: void

Calls:

  • loader.load
  • scope.parse
  • onLoad
Code
function loadTexture( i ) {

            loader.load( url[ i ], function ( buffer ) {

                const texDatas = scope.parse( buffer, true );

                images[ i ] = {
                    width: texDatas.width,
                    height: texDatas.height,
                    format: texDatas.format,
                    mipmaps: texDatas.mipmaps
                };

                loaded += 1;

                if ( loaded === 6 ) {

                    if ( texDatas.mipmapCount === 1 ) texture.minFilter = LinearFilter;

                    texture.image = images;
                    texture.format = texDatas.format;
                    texture.needsUpdate = true;

                    if ( onLoad ) onLoad( texture );

                }

            }, onProgress, onError );

        }

Classes

CompressedTextureLoader

Class Code
class CompressedTextureLoader extends Loader {

    /**
     * Constructs a new compressed texture loader.
     *
     * @param {LoadingManager} [manager] - The loading manager.
     */
    constructor( manager ) {

        super( manager );

    }

    /**
     * Starts loading from the given URL and passes the loaded compressed 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(CompressedTexture)} 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 {CompressedTexture} The compressed texture.
     */
    load( url, onLoad, onProgress, onError ) {

        const scope = this;

        const images = [];

        const texture = new CompressedTexture();

        const loader = new FileLoader( this.manager );
        loader.setPath( this.path );
        loader.setResponseType( 'arraybuffer' );
        loader.setRequestHeader( this.requestHeader );
        loader.setWithCredentials( scope.withCredentials );

        let loaded = 0;

        function loadTexture( i ) {

            loader.load( url[ i ], function ( buffer ) {

                const texDatas = scope.parse( buffer, true );

                images[ i ] = {
                    width: texDatas.width,
                    height: texDatas.height,
                    format: texDatas.format,
                    mipmaps: texDatas.mipmaps
                };

                loaded += 1;

                if ( loaded === 6 ) {

                    if ( texDatas.mipmapCount === 1 ) texture.minFilter = LinearFilter;

                    texture.image = images;
                    texture.format = texDatas.format;
                    texture.needsUpdate = true;

                    if ( onLoad ) onLoad( texture );

                }

            }, onProgress, onError );

        }

        if ( Array.isArray( url ) ) {

            for ( let i = 0, il = url.length; i < il; ++ i ) {

                loadTexture( i );

            }

        } else {

            // compressed cubemap texture stored in a single DDS file

            loader.load( url, function ( buffer ) {

                const texDatas = scope.parse( buffer, true );

                if ( texDatas.isCubemap ) {

                    const faces = texDatas.mipmaps.length / texDatas.mipmapCount;

                    for ( let f = 0; f < faces; f ++ ) {

                        images[ f ] = { mipmaps: [] };

                        for ( let i = 0; i < texDatas.mipmapCount; i ++ ) {

                            images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] );
                            images[ f ].format = texDatas.format;
                            images[ f ].width = texDatas.width;
                            images[ f ].height = texDatas.height;

                        }

                    }

                    texture.image = images;

                } else {

                    texture.image.width = texDatas.width;
                    texture.image.height = texDatas.height;
                    texture.mipmaps = texDatas.mipmaps;

                }

                if ( texDatas.mipmapCount === 1 ) {

                    texture.minFilter = LinearFilter;

                }

                texture.format = texDatas.format;
                texture.needsUpdate = true;

                if ( onLoad ) onLoad( texture );

            }, onProgress, onError );

        }

        return texture;

    }

}

Methods

load(url: string, onLoad: (arg0: CompressedTexture) => any, onProgress: onProgressCallback, onError: onErrorCallback): CompressedTexture
Code
load( url, onLoad, onProgress, onError ) {

        const scope = this;

        const images = [];

        const texture = new CompressedTexture();

        const loader = new FileLoader( this.manager );
        loader.setPath( this.path );
        loader.setResponseType( 'arraybuffer' );
        loader.setRequestHeader( this.requestHeader );
        loader.setWithCredentials( scope.withCredentials );

        let loaded = 0;

        function loadTexture( i ) {

            loader.load( url[ i ], function ( buffer ) {

                const texDatas = scope.parse( buffer, true );

                images[ i ] = {
                    width: texDatas.width,
                    height: texDatas.height,
                    format: texDatas.format,
                    mipmaps: texDatas.mipmaps
                };

                loaded += 1;

                if ( loaded === 6 ) {

                    if ( texDatas.mipmapCount === 1 ) texture.minFilter = LinearFilter;

                    texture.image = images;
                    texture.format = texDatas.format;
                    texture.needsUpdate = true;

                    if ( onLoad ) onLoad( texture );

                }

            }, onProgress, onError );

        }

        if ( Array.isArray( url ) ) {

            for ( let i = 0, il = url.length; i < il; ++ i ) {

                loadTexture( i );

            }

        } else {

            // compressed cubemap texture stored in a single DDS file

            loader.load( url, function ( buffer ) {

                const texDatas = scope.parse( buffer, true );

                if ( texDatas.isCubemap ) {

                    const faces = texDatas.mipmaps.length / texDatas.mipmapCount;

                    for ( let f = 0; f < faces; f ++ ) {

                        images[ f ] = { mipmaps: [] };

                        for ( let i = 0; i < texDatas.mipmapCount; i ++ ) {

                            images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] );
                            images[ f ].format = texDatas.format;
                            images[ f ].width = texDatas.width;
                            images[ f ].height = texDatas.height;

                        }

                    }

                    texture.image = images;

                } else {

                    texture.image.width = texDatas.width;
                    texture.image.height = texDatas.height;
                    texture.mipmaps = texDatas.mipmaps;

                }

                if ( texDatas.mipmapCount === 1 ) {

                    texture.minFilter = LinearFilter;

                }

                texture.format = texDatas.format;
                texture.needsUpdate = true;

                if ( onLoad ) onLoad( texture );

            }, onProgress, onError );

        }

        return texture;

    }