2
0

LoadingManager.html 7.0 KB

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