LoadingManager

Handles and keeps track of loaded and pending data. A default global instance of this class is created and used by loaders if not supplied manually. In general that should be sufficient, however there are times when it can be useful to have separate loaders - for example if you want to show separate loading bars for objects and textures.

const manager = new LoadingManager(
  () => console.log('All items loaded!'),
  (url, itemsLoaded, itemsTotal) => {
    console.log(`Loaded ${itemsLoaded} of ${itemsTotal} items`);
  },
  url => console.error(`Error loading ${url}`)
);

Constructor

new LoadingManager(onLoadopt, onProgressopt, onErroropt)

Constructs a new loading manager.

Parameters:
NameTypeAttributesDescription
onLoadfunction<optional>

Executes when all items have been loaded.

onProgressfunction<optional>

Executes when single items have been loaded.

onErrorfunction<optional>

Executes when an error occurs.

Members

onError :function|undefined

Executes when an error occurs.

Type:
  • function | undefined
Default Value
  • undefined

onLoad :function|undefined

Executes when all items have been loaded.

Type:
  • function | undefined
Default Value
  • undefined

onProgress :function|undefined

Executes when single items have been loaded.

Type:
  • function | undefined
Default Value
  • undefined

onStart :function|undefined

Executes when an item starts loading.

Type:
  • function | undefined
Default Value
  • undefined

Methods

itemEnd(url)

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

Parameters:
NameTypeDescription
urlstring

The URL of the loaded item.

itemError(url)

This should be called by any loader using the manager when the loader encounters an error when loading an item.

Parameters:
NameTypeDescription
urlstring

The URL of the item that produces an error.

itemStart(url)

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

Parameters:
NameTypeDescription
urlstring

The URL to load.

resolveURL(url) → {string}

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.

Parameters:
NameTypeDescription
urlstring

The URL to load.

Returns:

The resolved URL.

Type: 
string

setURLModifier(transform) → {LoadingManager}

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.

Parameters:
NameTypeDescription
transformfunction

URL modifier callback. Called with an URL and must return a resolved URL.

Returns:

A reference to this loading manager.

Type: 
LoadingManager
Example
const blobs = { 'fish.gltf': blob1, 'diffuse.png': blob2, 'normal.png': blob3 };

const manager = new LoadingManager();

// Initialize loading manager with URL callback.
const objectURLs = [];
manager.setURLModifier(url => {
	 url = URL.createObjectURL(blobs[url]);
	 objectURLs.push(url);
	 return url;
});

// Load as usual, then revoke the blob URLs.
const loader = new GLTFLoader(manager);
loader.load('fish.gltf', gltf => {
	 scene.add(gltf.scene);
	 objectURLs.forEach(url => URL.revokeObjectURL(url));
});