ThreeJS Managers

DefaultLoadingManager

When no custom manager is specified, most loaders use a global instance of the LoadingManager.

This will suffice for most purposes, but there may be times when you want separate loading managers for textures and models, for example.

Code Example

You can optionally configure the manager's onStart, onLoad, onProgress, and onError functions. These will then be applied to all loaders that use the DefaultLoadingManager.

These are not to be confused with the similarly named functions of individual loaders, as they are intended to display information about the overall status of loading rather than dealing with the data that has been loaded.

THREE.DefaultLoadingManager.onStart = function ( url, itemsLoaded, itemsTotal ) {
	
	console.log( 'Started loading file: ' + url + '.\nLoaded ' + itemsLoaded + ' of ' + itemsTotal + ' files.' );


};


THREE.DefaultLoadingManager.onLoad = function ( ) {


	console.log( 'Loading Complete!');


};




THREE.DefaultLoadingManager.onProgress = function ( url, itemsLoaded, itemsTotal ) {


	console.log( 'Loading file: ' + url + '.\nLoaded ' + itemsLoaded + ' of ' + itemsTotal + ' files.' );


};


THREE.DefaultLoadingManager.onError = function ( url ) {


	console.log( 'There was an error loading ' + url );


};

LoadingManager

Handles and monitors loaded and pending data. If this class is not explicitly supplied, a default global instance is created and used by loaders - see DefaultLoadingManager.

In general, that should suffice, but there are times when having separate loaders can be useful, such as when you want to show separate loading bars for objects and textures.

Code Example

This example shows how to use LoadingManager to track the progress of OBJLoader.

const manager = new THREE.LoadingManager();
manager.onStart = function ( url, itemsLoaded, itemsTotal ) {


	console.log( 'Started loading file: ' + url + '.\nLoaded ' + itemsLoaded + ' of ' + itemsTotal + ' files.' );


};


manager.onLoad = function ( ) {


	console.log( 'Loading complete!');


};




manager.onProgress = function ( url, itemsLoaded, itemsTotal ) {


	console.log( 'Loading file: ' + url + '.\nLoaded ' + itemsLoaded + ' of ' + itemsTotal + ' files.' );


};


manager.onError = function ( url ) {


	console.log( 'There was an error loading ' + url );


};


const loader = new THREE.OBJLoader( manager );
loader.load( 'file.obj', function ( object ) {


	//


} );

Constructor

LoadingManager( onLoad : Function, onProgress : Function, onError : Function )

onLoad — (optional) this function will be called when all loaders are done.

onProgress — (optional) this function will be called when an item is complete.

onError — (optional) this function will be called a loader encounters errors.

Creates a new LoadingManager.

Properties

.onStart : Function

This function will be called when loading starts. The arguments are:

url — The url of the item just loaded.

itemsLoaded — the number of items already loaded so far.

itemsTotal — the total amount of items to be loaded.

By default this is undefined.

.onLoad : Function

This function will be called when all loading is completed. By default this is undefined, unless passed in the constructor.

.onProgress : Function

This function will be called when an item is complete. The arguments are:

url — The url of the item just loaded.

itemsLoaded — the number of items already loaded so far.

itemsTotal — the total amount of items to be loaded.

By default this is undefined, unless passed in the constructor.

.onError : Function

This function will be called when any item errors, with the argument:

url — The url of the item that errored.

By default this is undefined, unless passed in the constructor.

Methods

.addHandler ( regex : Object, loader : Loader ) : this

regex — A regular expression.

loader — The loader.

Registers a loader with the given regular expression. Can be used to define what loader should be used in order to load specific files. A typical use case is to overwrite the default loader for textures.

// add handler for TGA textures
manager.addHandler( /\.tga$/i, new TGALoader() );

.getHandler ( file : String ) : Loader

file — The file path.

Can be used to retrieve the registered loader for the given file path.

.removeHandler ( regex : Object ) : this

regex — A regular expression.

Removes the loader for the given regular expression.

.resolveURL ( url : String ) : String

url — the url to load

Given a URL, uses the URL modifier callback (if any) and returns a resolved URL. If no URL modifier is set, returns the original URL.

.setURLModifier ( callback : Function ) : this

callback — URL modifier callback. Called with url argument, and must return resolvedURL.

If provided, the callback will be passed each resource URL before a request is sent. The callback may return the original URL, or a new URL to override loading behavior. This behavior can be used to load assets from .ZIP files, drag-and-drop APIs, and Data URIs.

Note: The following methods are designed to be called internally by loaders. You shouldn't call them directly.

.itemStart ( url : String ) : undefined

url — the url to load

This should be called by any loader using the manager when the loader starts loading an url.

.itemEnd ( url : String ) : undefined

url — the loaded url

This should be called by any loader using the manager when the loader ended loading an url.

.itemError ( url : String ) : undefined

url — the loaded url

This should be called by any loader using the manager when the loader errors loading an url.


Related Topics

ThreeJS AnimationClip

An AnimationClip is a group of keyframe tracks that may be reused to depict an animation. The "Animation System" page in the "Next Steps" part of the handbook provides an overview...

2 minutes read.

ThreeJS Helper 1

ArrowHelper A 3D arrow object for visualizing directions. Code Example const dir = new THREE.Vector3( 1, 2, 0 ); //normalize the direction vector (convert to vector of length 1) dir.normalize(); const origin = new THREE.Vector3( 0,...

5 minutes read.

ThreeJS BufferGeometry

A mesh, line, or point geometry representation. Included within buffers are vertex positions, face indices, normals, colours, UVs, and custom attributes, lowering the cost of passing all of this data...

5 minutes read.

ThreeJS Material - Mesh

MeshBasicMaterial A material for drawing geometries in a simple shaded (flat or wireframe) way. This material is not affected by lights. Constructor MeshBasicMaterial( parameters : Object ) parameters - (optional) an object with one or...

24 minutes read.

ThreeJS AnimationAction

AnimationActions are used to schedule the playback of animations saved in AnimationClips. Note that the majority of AnimationAction's methods can be chained together. The "Animation System" page in the "Next Steps" part...

6 minutes read.

ThreeJS Curve

An abstract base class for creating a Curve object with interpolation methods. CurvePath is an array of Curves. Constructor Curve() This constructor creates a new Curve. Properties .arcLengthDivisions : Integer This value determines the amount of...

13 minutes read.

ThreeJS Lights

Light Abstract base class for lights - all other light types inherit the properties and methods described here. Constructor Light( color : Integer, intensity : Float ) color - (optional) hexadecimal color of the...

10 minutes read.

ThreeJS Object3D

This is the base class for most three.js objects, and it provides a set of properties and methods for manipulating objects in 3D space. It should be noted that this can...

9 minutes read.

ThreeJS Animation Systems

You may animate many aspects of your models with the three.js animation system, including the bones of a skinned and rigged model, morph targets, multiple material properties (colours, opacity, booleans),...

3 minutes read.

ThreeJS Loaders 2

ImageBitmapLoader A loader that converts an image to an ImageBitmap. An ImageBitmap provides an asynchronous and resource-efficient path for preparing textures for WebGL rendering. ImageBitmapLoader, unlike FileLoader, does not prevent multiple concurrent...

8 minutes read.

ThreeJS Lines and Texts

Drawing Lines Let's say you want to draw a line or a circle, not a wireframe Mesh. First, we need to set up the renderer, scene, and camera. Here is the...

3 minutes read.

ThreeJS Renderers

WebGLMultipleRenderTargets A special render target that enables a fragment shader to write to several textures. This approach is useful for advanced rendering techniques like post-processing or deferred rendering. Heads up: WebGLMultipleRenderTargets...

15 minutes read.

ThreeJS Installation

You can use npm and modern build tools to install three.js, or you can get started quickly with static hosting or a CDN. Installing from npm is the best option...

4 minutes read.

ThreeJS Scenes

Fog This class contains the parameters that define linear fog, i.e., that grows linearly denser with the distance. Constructor Fog( color : Integer, near : Float, far : Float ) The color parameter is...

3 minutes read.

ThreeJS Math

Box2 Represents an axis-aligned bounding box (AABB) in 2D space. Constructor Box2( min : Vector2, max : Vector2 ) min - (optional) Vector2 representing the lower (x, y) boundary of the box. Default is...

48 minutes read.

ThreeJS Loader 1

AnimationLoader Class for loading AnimationClips in JSON format. This uses the FileLoader internally for loading files. Code Example // instantiate a loader const loader = new THREE.AnimationLoader(); // load a resource loader.load( // resource URL 'animations/animation.js', // onLoad callback function...

8 minutes read.

ThreeJS Materials 3

RawShaderMaterial This class works just like ShaderMaterial, except that definitions of built-in uniforms and attributes are not automatically prepended to the GLSL shader code. Code Example const material = new THREE.RawShaderMaterial( {    ...

9 minutes read.

ThreeJS Uniform

Uniforms are global GLSL variables. They are passed to shader programs. Code Example When declaring a uniform of a ShaderMaterial, it is declared by value or by object. uniforms: { time: { value: 1.0...

2 minutes read.

ThreeJS Keyframe Tracks and Animation

A KeyframeTrack is a timed series of keyframes that are made up of lists of times and related values and are used to animate an object's unique property. BooleanKeyframeTrack A Track of...

3 minutes read.

ThreeJS Interpolant

CubicInterpolant Code Example const interpolant = new THREE.CubicInterpolant(new Float32Array( 2 ),new Float32Array( 2 ),1,new Float32Array( 1 ));interpolant.evaluate( 0.5 ); Constructor CubicInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) parameterPositions -- array of positions sampleValues -- array of samples sampleSize...

2 minutes read.