Skip to content

⬅️ Back to Table of Contents

📄 BufferGeometryLoader.js

📊 Analysis Summary

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

📚 Table of Contents

🛠️ File Location:

📂 src/loaders/BufferGeometryLoader.js

📦 Imports

Name Source
Sphere ../math/Sphere.js
BufferAttribute ../core/BufferAttribute.js
BufferGeometry ../core/BufferGeometry.js
FileLoader ./FileLoader.js
Loader ./Loader.js
InstancedBufferGeometry ../core/InstancedBufferGeometry.js
InstancedBufferAttribute ../core/InstancedBufferAttribute.js
InterleavedBufferAttribute ../core/InterleavedBufferAttribute.js
InterleavedBuffer ../core/InterleavedBuffer.js
getTypedArray ../utils.js

Variables & Constants

Name Type Kind Value Exported
scope this let/var this
loader FileLoader let/var new FileLoader( scope.manager )
interleavedBufferMap {} let/var {}
arrayBufferMap {} let/var {}
interleavedBuffers any let/var json.interleavedBuffers
interleavedBuffer any let/var interleavedBuffers[ uuid ]
ib InterleavedBuffer let/var new InterleavedBuffer( array, interleavedBuffer.stride )
arrayBuffers any let/var json.arrayBuffers
arrayBuffer any let/var arrayBuffers[ uuid ]
ab any let/var new Uint32Array( arrayBuffer ).buffer
geometry BufferGeometry let/var json.isInstancedBufferGeometry ? new InstancedBufferGeometry() : new BufferGe...
index any let/var json.data.index
attributes any let/var json.data.attributes
attribute any let/var attributes[ key ]
bufferAttribute any let/var *not shown*
bufferAttributeConstr typeof BufferAttribute \| typeof Inst... let/var attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttri...
morphAttributes any let/var json.data.morphAttributes
attributeArray any let/var morphAttributes[ key ]
array any[] let/var []
attribute any let/var attributeArray[ i ]
bufferAttribute any let/var *not shown*
morphTargetsRelative any let/var json.data.morphTargetsRelative
groups any let/var json.data.groups \|\| json.data.drawcalls \|\| json.data.offsets
group any let/var groups[ i ]
boundingSphere any let/var json.data.boundingSphere

Functions

BufferGeometryLoader.load(url: string, onLoad: (arg0: BufferGeometry) => any, onProgress: onProgressCallback, onError: onErrorCallback): void

JSDoc:

/**
     * Starts loading from the given URL and pass the loaded geometry to the `onLoad()` callback.
     *
     * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
     * @param {function(BufferGeometry)} 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.
     */

Parameters:

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

Returns: void

Calls:

  • loader.setPath
  • loader.setRequestHeader
  • loader.setWithCredentials
  • loader.load
  • onLoad
  • scope.parse
  • JSON.parse
  • onError
  • console.error
  • scope.manager.itemError
Code
load( url, onLoad, onProgress, onError ) {

        const scope = this;

        const loader = new FileLoader( scope.manager );
        loader.setPath( scope.path );
        loader.setRequestHeader( scope.requestHeader );
        loader.setWithCredentials( scope.withCredentials );
        loader.load( url, function ( text ) {

            try {

                onLoad( scope.parse( JSON.parse( text ) ) );

            } catch ( e ) {

                if ( onError ) {

                    onError( e );

                } else {

                    console.error( e );

                }

                scope.manager.itemError( url );

            }

        }, onProgress, onError );

    }

BufferGeometryLoader.parse(json: any): BufferGeometry

JSDoc:

/**
     * Parses the given JSON object and returns a geometry.
     *
     * @param {Object} json - The serialized geometry.
     * @return {BufferGeometry} The parsed geometry.
     */

Parameters:

  • json any

Returns: BufferGeometry

Calls:

  • getArrayBuffer
  • getTypedArray (from ../utils.js)
  • geometry.setIndex
  • getInterleavedBuffer
  • bufferAttribute.setUsage
  • geometry.setAttribute
  • array.push
  • geometry.addGroup
  • new Sphere().fromJSON
Code
parse( json ) {

        const interleavedBufferMap = {};
        const arrayBufferMap = {};

        function getInterleavedBuffer( json, uuid ) {

            if ( interleavedBufferMap[ uuid ] !== undefined ) return interleavedBufferMap[ uuid ];

            const interleavedBuffers = json.interleavedBuffers;
            const interleavedBuffer = interleavedBuffers[ uuid ];

            const buffer = getArrayBuffer( json, interleavedBuffer.buffer );

            const array = getTypedArray( interleavedBuffer.type, buffer );
            const ib = new InterleavedBuffer( array, interleavedBuffer.stride );
            ib.uuid = interleavedBuffer.uuid;

            interleavedBufferMap[ uuid ] = ib;

            return ib;

        }

        function getArrayBuffer( json, uuid ) {

            if ( arrayBufferMap[ uuid ] !== undefined ) return arrayBufferMap[ uuid ];

            const arrayBuffers = json.arrayBuffers;
            const arrayBuffer = arrayBuffers[ uuid ];

            const ab = new Uint32Array( arrayBuffer ).buffer;

            arrayBufferMap[ uuid ] = ab;

            return ab;

        }

        const geometry = json.isInstancedBufferGeometry ? new InstancedBufferGeometry() : new BufferGeometry();

        const index = json.data.index;

        if ( index !== undefined ) {

            const typedArray = getTypedArray( index.type, index.array );
            geometry.setIndex( new BufferAttribute( typedArray, 1 ) );

        }

        const attributes = json.data.attributes;

        for ( const key in attributes ) {

            const attribute = attributes[ key ];
            let bufferAttribute;

            if ( attribute.isInterleavedBufferAttribute ) {

                const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data );
                bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized );

            } else {

                const typedArray = getTypedArray( attribute.type, attribute.array );
                const bufferAttributeConstr = attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute;
                bufferAttribute = new bufferAttributeConstr( typedArray, attribute.itemSize, attribute.normalized );

            }

            if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
            if ( attribute.usage !== undefined ) bufferAttribute.setUsage( attribute.usage );

            geometry.setAttribute( key, bufferAttribute );

        }

        const morphAttributes = json.data.morphAttributes;

        if ( morphAttributes ) {

            for ( const key in morphAttributes ) {

                const attributeArray = morphAttributes[ key ];

                const array = [];

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

                    const attribute = attributeArray[ i ];
                    let bufferAttribute;

                    if ( attribute.isInterleavedBufferAttribute ) {

                        const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data );
                        bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized );

                    } else {

                        const typedArray = getTypedArray( attribute.type, attribute.array );
                        bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );

                    }

                    if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
                    array.push( bufferAttribute );

                }

                geometry.morphAttributes[ key ] = array;

            }

        }

        const morphTargetsRelative = json.data.morphTargetsRelative;

        if ( morphTargetsRelative ) {

            geometry.morphTargetsRelative = true;

        }

        const groups = json.data.groups || json.data.drawcalls || json.data.offsets;

        if ( groups !== undefined ) {

            for ( let i = 0, n = groups.length; i !== n; ++ i ) {

                const group = groups[ i ];

                geometry.addGroup( group.start, group.count, group.materialIndex );

            }

        }

        const boundingSphere = json.data.boundingSphere;

        if ( boundingSphere !== undefined ) {

            geometry.boundingSphere = new Sphere().fromJSON( boundingSphere );

        }

        if ( json.name ) geometry.name = json.name;
        if ( json.userData ) geometry.userData = json.userData;

        return geometry;

    }

getInterleavedBuffer(json: any, uuid: any): any

Parameters:

  • json any
  • uuid any

Returns: any

Calls:

  • getArrayBuffer
  • getTypedArray (from ../utils.js)
Code
function getInterleavedBuffer( json, uuid ) {

            if ( interleavedBufferMap[ uuid ] !== undefined ) return interleavedBufferMap[ uuid ];

            const interleavedBuffers = json.interleavedBuffers;
            const interleavedBuffer = interleavedBuffers[ uuid ];

            const buffer = getArrayBuffer( json, interleavedBuffer.buffer );

            const array = getTypedArray( interleavedBuffer.type, buffer );
            const ib = new InterleavedBuffer( array, interleavedBuffer.stride );
            ib.uuid = interleavedBuffer.uuid;

            interleavedBufferMap[ uuid ] = ib;

            return ib;

        }

getArrayBuffer(json: any, uuid: any): any

Parameters:

  • json any
  • uuid any

Returns: any

Code
function getArrayBuffer( json, uuid ) {

            if ( arrayBufferMap[ uuid ] !== undefined ) return arrayBufferMap[ uuid ];

            const arrayBuffers = json.arrayBuffers;
            const arrayBuffer = arrayBuffers[ uuid ];

            const ab = new Uint32Array( arrayBuffer ).buffer;

            arrayBufferMap[ uuid ] = ab;

            return ab;

        }

Classes

BufferGeometryLoader

Class Code
class BufferGeometryLoader extends Loader {

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

        super( manager );

    }

    /**
     * Starts loading from the given URL and pass the loaded geometry to the `onLoad()` callback.
     *
     * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
     * @param {function(BufferGeometry)} 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.
     */
    load( url, onLoad, onProgress, onError ) {

        const scope = this;

        const loader = new FileLoader( scope.manager );
        loader.setPath( scope.path );
        loader.setRequestHeader( scope.requestHeader );
        loader.setWithCredentials( scope.withCredentials );
        loader.load( url, function ( text ) {

            try {

                onLoad( scope.parse( JSON.parse( text ) ) );

            } catch ( e ) {

                if ( onError ) {

                    onError( e );

                } else {

                    console.error( e );

                }

                scope.manager.itemError( url );

            }

        }, onProgress, onError );

    }

    /**
     * Parses the given JSON object and returns a geometry.
     *
     * @param {Object} json - The serialized geometry.
     * @return {BufferGeometry} The parsed geometry.
     */
    parse( json ) {

        const interleavedBufferMap = {};
        const arrayBufferMap = {};

        function getInterleavedBuffer( json, uuid ) {

            if ( interleavedBufferMap[ uuid ] !== undefined ) return interleavedBufferMap[ uuid ];

            const interleavedBuffers = json.interleavedBuffers;
            const interleavedBuffer = interleavedBuffers[ uuid ];

            const buffer = getArrayBuffer( json, interleavedBuffer.buffer );

            const array = getTypedArray( interleavedBuffer.type, buffer );
            const ib = new InterleavedBuffer( array, interleavedBuffer.stride );
            ib.uuid = interleavedBuffer.uuid;

            interleavedBufferMap[ uuid ] = ib;

            return ib;

        }

        function getArrayBuffer( json, uuid ) {

            if ( arrayBufferMap[ uuid ] !== undefined ) return arrayBufferMap[ uuid ];

            const arrayBuffers = json.arrayBuffers;
            const arrayBuffer = arrayBuffers[ uuid ];

            const ab = new Uint32Array( arrayBuffer ).buffer;

            arrayBufferMap[ uuid ] = ab;

            return ab;

        }

        const geometry = json.isInstancedBufferGeometry ? new InstancedBufferGeometry() : new BufferGeometry();

        const index = json.data.index;

        if ( index !== undefined ) {

            const typedArray = getTypedArray( index.type, index.array );
            geometry.setIndex( new BufferAttribute( typedArray, 1 ) );

        }

        const attributes = json.data.attributes;

        for ( const key in attributes ) {

            const attribute = attributes[ key ];
            let bufferAttribute;

            if ( attribute.isInterleavedBufferAttribute ) {

                const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data );
                bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized );

            } else {

                const typedArray = getTypedArray( attribute.type, attribute.array );
                const bufferAttributeConstr = attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute;
                bufferAttribute = new bufferAttributeConstr( typedArray, attribute.itemSize, attribute.normalized );

            }

            if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
            if ( attribute.usage !== undefined ) bufferAttribute.setUsage( attribute.usage );

            geometry.setAttribute( key, bufferAttribute );

        }

        const morphAttributes = json.data.morphAttributes;

        if ( morphAttributes ) {

            for ( const key in morphAttributes ) {

                const attributeArray = morphAttributes[ key ];

                const array = [];

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

                    const attribute = attributeArray[ i ];
                    let bufferAttribute;

                    if ( attribute.isInterleavedBufferAttribute ) {

                        const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data );
                        bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized );

                    } else {

                        const typedArray = getTypedArray( attribute.type, attribute.array );
                        bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );

                    }

                    if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
                    array.push( bufferAttribute );

                }

                geometry.morphAttributes[ key ] = array;

            }

        }

        const morphTargetsRelative = json.data.morphTargetsRelative;

        if ( morphTargetsRelative ) {

            geometry.morphTargetsRelative = true;

        }

        const groups = json.data.groups || json.data.drawcalls || json.data.offsets;

        if ( groups !== undefined ) {

            for ( let i = 0, n = groups.length; i !== n; ++ i ) {

                const group = groups[ i ];

                geometry.addGroup( group.start, group.count, group.materialIndex );

            }

        }

        const boundingSphere = json.data.boundingSphere;

        if ( boundingSphere !== undefined ) {

            geometry.boundingSphere = new Sphere().fromJSON( boundingSphere );

        }

        if ( json.name ) geometry.name = json.name;
        if ( json.userData ) geometry.userData = json.userData;

        return geometry;

    }

}

Methods

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

        const scope = this;

        const loader = new FileLoader( scope.manager );
        loader.setPath( scope.path );
        loader.setRequestHeader( scope.requestHeader );
        loader.setWithCredentials( scope.withCredentials );
        loader.load( url, function ( text ) {

            try {

                onLoad( scope.parse( JSON.parse( text ) ) );

            } catch ( e ) {

                if ( onError ) {

                    onError( e );

                } else {

                    console.error( e );

                }

                scope.manager.itemError( url );

            }

        }, onProgress, onError );

    }
parse(json: any): BufferGeometry
Code
parse( json ) {

        const interleavedBufferMap = {};
        const arrayBufferMap = {};

        function getInterleavedBuffer( json, uuid ) {

            if ( interleavedBufferMap[ uuid ] !== undefined ) return interleavedBufferMap[ uuid ];

            const interleavedBuffers = json.interleavedBuffers;
            const interleavedBuffer = interleavedBuffers[ uuid ];

            const buffer = getArrayBuffer( json, interleavedBuffer.buffer );

            const array = getTypedArray( interleavedBuffer.type, buffer );
            const ib = new InterleavedBuffer( array, interleavedBuffer.stride );
            ib.uuid = interleavedBuffer.uuid;

            interleavedBufferMap[ uuid ] = ib;

            return ib;

        }

        function getArrayBuffer( json, uuid ) {

            if ( arrayBufferMap[ uuid ] !== undefined ) return arrayBufferMap[ uuid ];

            const arrayBuffers = json.arrayBuffers;
            const arrayBuffer = arrayBuffers[ uuid ];

            const ab = new Uint32Array( arrayBuffer ).buffer;

            arrayBufferMap[ uuid ] = ab;

            return ab;

        }

        const geometry = json.isInstancedBufferGeometry ? new InstancedBufferGeometry() : new BufferGeometry();

        const index = json.data.index;

        if ( index !== undefined ) {

            const typedArray = getTypedArray( index.type, index.array );
            geometry.setIndex( new BufferAttribute( typedArray, 1 ) );

        }

        const attributes = json.data.attributes;

        for ( const key in attributes ) {

            const attribute = attributes[ key ];
            let bufferAttribute;

            if ( attribute.isInterleavedBufferAttribute ) {

                const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data );
                bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized );

            } else {

                const typedArray = getTypedArray( attribute.type, attribute.array );
                const bufferAttributeConstr = attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute;
                bufferAttribute = new bufferAttributeConstr( typedArray, attribute.itemSize, attribute.normalized );

            }

            if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
            if ( attribute.usage !== undefined ) bufferAttribute.setUsage( attribute.usage );

            geometry.setAttribute( key, bufferAttribute );

        }

        const morphAttributes = json.data.morphAttributes;

        if ( morphAttributes ) {

            for ( const key in morphAttributes ) {

                const attributeArray = morphAttributes[ key ];

                const array = [];

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

                    const attribute = attributeArray[ i ];
                    let bufferAttribute;

                    if ( attribute.isInterleavedBufferAttribute ) {

                        const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data );
                        bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized );

                    } else {

                        const typedArray = getTypedArray( attribute.type, attribute.array );
                        bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );

                    }

                    if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
                    array.push( bufferAttribute );

                }

                geometry.morphAttributes[ key ] = array;

            }

        }

        const morphTargetsRelative = json.data.morphTargetsRelative;

        if ( morphTargetsRelative ) {

            geometry.morphTargetsRelative = true;

        }

        const groups = json.data.groups || json.data.drawcalls || json.data.offsets;

        if ( groups !== undefined ) {

            for ( let i = 0, n = groups.length; i !== n; ++ i ) {

                const group = groups[ i ];

                geometry.addGroup( group.start, group.count, group.materialIndex );

            }

        }

        const boundingSphere = json.data.boundingSphere;

        if ( boundingSphere !== undefined ) {

            geometry.boundingSphere = new Sphere().fromJSON( boundingSphere );

        }

        if ( json.name ) geometry.name = json.name;
        if ( json.userData ) geometry.userData = json.userData;

        return geometry;

    }