LoadingManager.html 7.1 KB

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