2
0

LoadingManager.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 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 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_postprocessing_outline WebGL / postprocessing / outline]
  71. </p>
  72. <h2>Constructor</h2>
  73. <h3>
  74. [name]( [param:Function onLoad], [param:Function onProgress],
  75. [param:Function onError] )
  76. </h3>
  77. <p>
  78. [page:Function onLoad] — (optional) this function will be called when all
  79. loaders are done.<br />
  80. [page:Function onProgress] — (optional) this function will be called when
  81. an item is complete.<br />
  82. [page:Function onError] — (optional) this function will be called a loader
  83. encounters errors. <br />
  84. Creates a new [name].
  85. </p>
  86. <h2>Properties</h2>
  87. <h3>[property:Function onStart]</h3>
  88. <p>
  89. This function will be called when loading starts. The arguments are:<br />
  90. [page:String url] — The url of the item just loaded.<br />
  91. [page:Integer itemsLoaded] — the number of items already loaded so far.<br />
  92. [page:Integer itemsTotal] — the total amount of items to be loaded.<br /><br />
  93. By default this is undefined.
  94. </p>
  95. <h3>[property:Function onLoad]</h3>
  96. <p>
  97. This function will be called when all loading is completed. By default
  98. this is undefined, unless passed in the constructor.
  99. </p>
  100. <h3>[property:Function onProgress]</h3>
  101. <p>
  102. This function will be called when an item is complete. The arguments
  103. are:<br />
  104. [page:String url] — The url of the item just loaded.<br />
  105. [page:Integer itemsLoaded] — the number of items already loaded so far.<br />
  106. [page:Integer itemsTotal] — the total amount of items to be loaded.<br /><br />
  107. By default this is undefined, unless passed in the constructor.
  108. </p>
  109. <h3>[property:Function onError]</h3>
  110. <p>
  111. This function will be called when any item errors, with the argument:<br />
  112. [page:String url] — The url of the item that errored.<br /><br />
  113. By default this is undefined, unless passed in the constructor.
  114. </p>
  115. <h2>Methods</h2>
  116. <h3>
  117. [method:this addHandler]( [param:Object regex], [param:Loader loader] )
  118. </h3>
  119. <p>
  120. [page:Object regex] — A regular expression.<br />
  121. [page:Loader loader] — The loader.
  122. </p>
  123. <p>
  124. Registers a loader with the given regular expression. Can be used to
  125. define what loader should be used in order to load specific files. A
  126. typical use case is to overwrite the default loader for textures.
  127. </p>
  128. <code>
  129. // add handler for TGA textures
  130. manager.addHandler( /\.tga$/i, new TGALoader() );
  131. </code>
  132. <h3>[method:Loader getHandler]( [param:String file] )</h3>
  133. <p>[page:String file] — The file path.</p>
  134. <p>
  135. Can be used to retrieve the registered loader for the given file path.
  136. </p>
  137. <h3>[method:this removeHandler]( [param:Object regex] )</h3>
  138. <p>[page:Object regex] — A regular expression.</p>
  139. <p>Removes the loader for the given regular expression.</p>
  140. <h3>[method:String resolveURL]( [param:String url] )</h3>
  141. <p>
  142. [page:String url] — the url to load<br /><br />
  143. Given a URL, uses the URL modifier callback (if any) and returns a
  144. resolved URL. If no URL modifier is set, returns the original URL.
  145. </p>
  146. <h3>[method:this setURLModifier]( [param:Function callback] )</h3>
  147. <p>
  148. [page:Function callback] — URL modifier callback. Called with [page:String url] argument,
  149. and must return [page:String resolvedURL].<br /><br />
  150. If provided, the callback will be passed each resource URL before a
  151. request is sent. The callback may return the original URL, or a new URL to
  152. override loading behavior. This behavior can be used to load assets from
  153. .ZIP files, drag-and-drop APIs, and Data URIs.
  154. </p>
  155. <br />
  156. <p>
  157. <em>
  158. Note: The following methods are designed to be called internally by
  159. loaders. You shouldn't call them directly.
  160. </em>
  161. </p>
  162. <h3>[method:undefined itemStart]( [param:String url] )</h3>
  163. <p>
  164. [page:String url] — the url to load<br /><br />
  165. This should be called by any loader using the manager when the loader
  166. starts loading an url.
  167. </p>
  168. <h3>[method:undefined itemEnd]( [param:String url] )</h3>
  169. <p>
  170. [page:String url] — the loaded url<br /><br />
  171. This should be called by any loader using the manager when the loader
  172. ended loading an url.
  173. </p>
  174. <h3>[method:undefined itemError]( [param:String url] )</h3>
  175. <p>
  176. [page:String url] — the loaded url<br /><br />
  177. This should be called by any loader using the manager when the loader
  178. errors loading an url.
  179. </p>
  180. <h2>Source</h2>
  181. <p>
  182. [link:https://github.com/mrdoob/three.js/blob/master/src/loaders/LoadingManager.js src/loaders/LoadingManager.js]
  183. </p>
  184. </body>
  185. </html>