LoadingManager.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../../" />
  6. <script src="list.js"></script>
  7. <script src="page.js"></script>
  8. <link type="text/css" rel="stylesheet" href="page.css" />
  9. </head>
  10. <body>
  11. <h1>[name]</h1>
  12. <p class="desc">
  13. Handles and keeps track of loaded and pending data. A default global instance of this class
  14. is created and used by loaders if not supplied manually - see [page:DefaultLoadingManager].<br /><br />
  15. In general that should be sufficient, however there are times when it can be useful to have seperate loaders -
  16. for example if you want to show seperate loading bars for objects and textures.
  17. </p>
  18. <h2>Example</h2>
  19. <p>
  20. [example:webgl_loader_fbx WebGL / loader / fbx]<br />
  21. [example:webgl_loader_obj WebGL / loader / obj]<br />
  22. [example:webgl_materials_reflectivity WebGL / materials / reflectivity]<br />
  23. [example:webgl_postprocessing_outline WebGL / postprocesing / outline]<br />
  24. [example:webgl_terrain_dynamic WebGL / terrain / dynamic]
  25. </p>
  26. <p>
  27. This example shows how to use LoadingManager to track the progress of
  28. [page:OBJLoader].
  29. </p>
  30. <code>
  31. var manager = new THREE.LoadingManager();
  32. manager.onStart = function ( url, itemsLoaded, itemsTotal ) {
  33. console.log( 'Started loading file: ' + url + '.\nLoaded ' + itemsLoaded + ' of ' + itemsTotal + ' files.' );
  34. };
  35. manager.onLoad = function ( ) {
  36. console.log( 'Loading complete!');
  37. };
  38. manager.onProgress = function ( url, itemsLoaded, itemsTotal ) {
  39. console.log( 'Loading file: ' + url + '.\nLoaded ' + itemsLoaded + ' of ' + itemsTotal + ' files.' );
  40. };
  41. manager.onError = function ( url ) {
  42. console.log( 'There was an error loading ' + url );
  43. };
  44. var loader = new THREE.OBJLoader( manager );
  45. loader.load( 'file.obj', function ( object ) {
  46. //
  47. } );
  48. </code>
  49. <p>
  50. In addition to observing progress, a LoadingManager can be used to
  51. override resource URLs during loading. This may be helpful for assets
  52. coming from drag-and-drop events, WebSockets, WebRTC, or other APIs. An
  53. example showing how to load an in-memory model using Blob URLs is below.
  54. </p>
  55. <code>
  56. // Blob or File objects created when dragging files into the webpage.
  57. var blobs = {'fish.gltf': blob1, 'diffuse.png': blob2, 'normal.png': blob3};
  58. var manager = new THREE.LoadingManager();
  59. // Initialize loading manager with URL callback.
  60. var objectURLs = [];
  61. manager.setURLModifier( ( url ) => {
  62. url = URL.createObjectURL( blobs[ url ] );
  63. objectURLs.push( url );
  64. return url;
  65. } );
  66. // Load as usual, then revoke the blob URLs.
  67. var loader = new THREE.GLTFLoader( manager );
  68. loader.load( 'fish.gltf', (gltf) => {
  69. scene.add( gltf.scene );
  70. objectURLs.forEach( ( url ) => URL.revokeObjectURL( url ) );
  71. });
  72. </code>
  73. <h2>Constructor</h2>
  74. <h3>[name]( [param:Function onLoad], [param:Function onProgress], [param:Function onError] )</h3>
  75. <p>
  76. [page:Function onLoad] — (optional) this function will be called when all loaders are done.<br />
  77. [page:Function onProgress] — (optional) this function will be called when an item is complete.<br />
  78. [page:Function onError] — (optional) this function will be called a loader encounters errors. <br />
  79. Creates a new [name].
  80. </p>
  81. <h2>Properties</h2>
  82. <h3>[property:Function onStart]</h3>
  83. <p>
  84. This function will be called when loading starts.
  85. The arguments are:<br />
  86. [page:String url] — The url of the item just loaded.<br />
  87. [page:Integer itemsLoaded] — the number of items already loaded so far.<br />
  88. [page:Iteger itemsTotal] — the total amount of items to be loaded.<br /><br />
  89. By default this is undefined.
  90. </p>
  91. <h3>[property:Function onLoad]</h3>
  92. <p>
  93. This function will be called when all loading is completed. By default this is undefined,
  94. unless passed in the constructor.
  95. </p>
  96. <h3>[property:Function onProgress]</h3>
  97. <p>
  98. This function will be called when an item is complete.
  99. The arguments are:<br />
  100. [page:String url] — The url of the item just loaded.<br />
  101. [page:Integer itemsLoaded] — the number of items already loaded so far.<br />
  102. [page:Iteger itemsTotal] — the total amount of items to be loaded.<br /><br />
  103. By default this is undefined, unless passed in the constructor.
  104. </p>
  105. <h3>[property:Function onError]</h3>
  106. <p>
  107. This function will be called when any item errors, with the argument:<br />
  108. [page:String url] — The url of the item that errored.<br /><br />
  109. By default this is undefined, unless passed in the constructor.
  110. </p>
  111. <h2>Methods</h2>
  112. <h3>[method:LoadingManager addHandler]( [param:Object regex], [param:Loader loader] )</h3>
  113. <p>
  114. [page:Object regex] — A regular expression.<br />
  115. [page:Loader loader] — The loader.
  116. <p>
  117. Registers a loader with the given regular expression. Can be used to define what loader should be used in
  118. order to load specific files. A typical use case is to overwrite the default loader for textures.
  119. </p>
  120. <code>
  121. // add handler for TGA textures
  122. manager.addHandler( /\.tga$/i, new TGALoader() );
  123. </code>
  124. <h3>[method:null getHandler]( [param:String file] )</h3>
  125. <p>
  126. [page:String file] — The file path.
  127. <p>
  128. Can be used to retrieve the registered loader for the given file path.
  129. </p>
  130. <h3>[method:LoadingManager removeHandler]( [param:Object regex] )</h3>
  131. <p>
  132. [page:Object regex] — A regular expression.
  133. <p>
  134. Removes the loader for the given regular expression.
  135. </p>
  136. <h3>[method:String resolveURL]( [param:String url] )</h3>
  137. <p>
  138. [page:String url] — the url to load<br /><br />
  139. Given a URL, uses the URL modifier callback (if any) and returns a resolved URL. If no
  140. URL modifier is set, returns the original URL.
  141. </p>
  142. <h3>[method:null setURLModifier]( [param:Function callback] )</h3>
  143. <p>
  144. [page:Function callback] — URL modifier callback. Called with [page:String url] argument, and
  145. must return [page:String resolvedURL].<br /><br />
  146. If provided, the callback will be passed each resource URL before a request is sent. The
  147. callback may return the original URL, or a new URL to override loading behavior. This
  148. behavior can be used to load assets from .ZIP files, drag-and-drop APIs, and Data URIs.
  149. </p>
  150. <br /><br />
  151. <p>
  152. <em>Note: The following methods are designed to be called internally by loaders. You shouldn't call
  153. them directly.</em>
  154. </p>
  155. <h3>[method:null itemStart]( [param:String url] )</h3>
  156. <p>
  157. [page:String url] — the url to load<br /><br />
  158. This should be called by any loader using the manager when the loader starts loading an url.
  159. </p>
  160. <h3>[method:null itemEnd]( [param:String url] )</h3>
  161. <p>
  162. [page:String url] — the loaded url<br /><br />
  163. This should be called by any loader using the manager when the loader ended loading an url.
  164. </p>
  165. <h3>[method:null itemError]( [param:String url] )</h3>
  166. <p>
  167. [page:String url] — the loaded url<br /><br />
  168. This should be called by any loader using the manager when the loader errors loading an url.
  169. </p>
  170. <h2>Source</h2>
  171. [link:https://github.com/mrdoob/three.js/blob/master/src/loaders/LoadingManager.js src/loaders/LoadingManager.js]
  172. </body>
  173. </html>