LoadingManager.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>LoadingManager - Three.js Docs</title>
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <script src="../scripts/highlight.min.js"></script>
  8. <link type="text/css" rel="stylesheet" href="../styles/highlight-three.css">
  9. <link type="text/css" rel="stylesheet" href="../styles/page.css">
  10. </head>
  11. <body>
  12. <h1 translate="no">LoadingManager</h1>
  13. <section>
  14. <header>
  15. <div class="class-description"><p>Handles and keeps track of loaded and pending data. A default global
  16. instance of this class is created and used by loaders if not supplied
  17. manually.</p>
  18. <p>In general that should be sufficient, however there are times when it can
  19. be useful to have separate loaders - for example if you want to show
  20. separate loading bars for objects and textures.</p></div>
  21. <h2>Code Example</h2>
  22. <div translate="no"><pre><code class="language-js">const manager = new THREE.LoadingManager();
  23. manager.onLoad = () => console.log( 'Loading complete!' );
  24. const loader1 = new OBJLoader( manager );
  25. const loader2 = new ColladaLoader( manager );
  26. </code></pre></div>
  27. </header>
  28. <article>
  29. <div class="container-overview">
  30. <h2>Constructor</h2>
  31. <h3 class="name name-method" id="LoadingManager" translate="no">new <a href="#LoadingManager">LoadingManager</a><span class="signature">( onLoad : <span class="param-type">function</span>, onProgress : <span class="param-type">function</span>, onError : <span class="param-type">function</span> )</span> </h3>
  32. <div class="method">
  33. <div class="description">
  34. <p>Constructs a new loading manager.</p>
  35. </div>
  36. <table class="params">
  37. <tbody>
  38. <tr>
  39. <td class="name"><code>onLoad</code></td>
  40. <td class="description last"><p>Executes when all items have been loaded.</p></td>
  41. </tr>
  42. <tr>
  43. <td class="name"><code>onProgress</code></td>
  44. <td class="description last"><p>Executes when single items have been loaded.</p></td>
  45. </tr>
  46. <tr>
  47. <td class="name"><code>onError</code></td>
  48. <td class="description last"><p>Executes when an error occurs.</p></td>
  49. </tr>
  50. </tbody>
  51. </table>
  52. </div>
  53. </div>
  54. <h2 class="subsection-title">Properties</h2>
  55. <div class="member">
  56. <h3 class="name" id="abortController" translate="no">.<a href="#abortController">abortController</a><span class="type-signature"> : AbortController</span> </h3>
  57. <div class="description">
  58. <p>Used for aborting ongoing requests in loaders using this manager.</p>
  59. </div>
  60. </div>
  61. <div class="member">
  62. <h3 class="name" id="onError" translate="no">.<a href="#onError">onError</a><span class="type-signature"> : function | undefined</span> </h3>
  63. <div class="description">
  64. <p>Executes when an error occurs.<br/>Default is <code>undefined</code>.</p>
  65. </div>
  66. </div>
  67. <div class="member">
  68. <h3 class="name" id="onLoad" translate="no">.<a href="#onLoad">onLoad</a><span class="type-signature"> : function | undefined</span> </h3>
  69. <div class="description">
  70. <p>Executes when all items have been loaded.<br/>Default is <code>undefined</code>.</p>
  71. </div>
  72. </div>
  73. <div class="member">
  74. <h3 class="name" id="onProgress" translate="no">.<a href="#onProgress">onProgress</a><span class="type-signature"> : function | undefined</span> </h3>
  75. <div class="description">
  76. <p>Executes when single items have been loaded.<br/>Default is <code>undefined</code>.</p>
  77. </div>
  78. </div>
  79. <div class="member">
  80. <h3 class="name" id="onStart" translate="no">.<a href="#onStart">onStart</a><span class="type-signature"> : function | undefined</span> </h3>
  81. <div class="description">
  82. <p>Executes when an item starts loading.<br/>Default is <code>undefined</code>.</p>
  83. </div>
  84. </div>
  85. <h2 class="subsection-title">Methods</h2>
  86. <h3 class="name name-method" id="abort" translate="no">.<a href="#abort">abort</a><span class="signature">()</span><span class="type-signature"> : <a href="LoadingManager.html">LoadingManager</a></span> </h3>
  87. <div class="method">
  88. <div class="description">
  89. <p>Can be used to abort ongoing loading requests in loaders using this manager.
  90. The abort only works if the loaders implement <a href="Loader.html#abort">Loader#abort</a> and <code>AbortSignal.any()</code>
  91. is supported in the browser.</p>
  92. </div>
  93. <dl class="details">
  94. <dt class="tag-returns"><strong>Returns:</strong> A reference to this loading manager.</dt>
  95. </dl>
  96. </div>
  97. <h3 class="name name-method" id="addHandler" translate="no">.<a href="#addHandler">addHandler</a><span class="signature">( regex : <span class="param-type">string</span>, loader : <span class="param-type">Loader</span> )</span><span class="type-signature"> : <a href="LoadingManager.html">LoadingManager</a></span> </h3>
  98. <div class="method">
  99. <div class="description">
  100. <p>Registers a loader with the given regular expression. Can be used to
  101. define what loader should be used in order to load specific files. A
  102. typical use case is to overwrite the default loader for textures.</p>
  103. <pre><code class="language-js">// add handler for TGA textures
  104. manager.addHandler( /\.tga$/i, new TGALoader() );
  105. </code></pre>
  106. </div>
  107. <table class="params">
  108. <tbody>
  109. <tr>
  110. <td class="name"><code>regex</code></td>
  111. <td class="description last"><p>A regular expression.</p></td>
  112. </tr>
  113. <tr>
  114. <td class="name"><code>loader</code></td>
  115. <td class="description last"><p>A loader that should handle matched cases.</p></td>
  116. </tr>
  117. </tbody>
  118. </table>
  119. <dl class="details">
  120. <dt class="tag-returns"><strong>Returns:</strong> A reference to this loading manager.</dt>
  121. </dl>
  122. </div>
  123. <h3 class="name name-method" id="getHandler" translate="no">.<a href="#getHandler">getHandler</a><span class="signature">( file : <span class="param-type">string</span> )</span><span class="type-signature"> : <a href="Loader.html">Loader</a></span> </h3>
  124. <div class="method">
  125. <div class="description">
  126. <p>Can be used to retrieve the registered loader for the given file path.</p>
  127. </div>
  128. <table class="params">
  129. <tbody>
  130. <tr>
  131. <td class="name"><code>file</code></td>
  132. <td class="description last"><p>The file path.</p></td>
  133. </tr>
  134. </tbody>
  135. </table>
  136. <dl class="details">
  137. <dt class="tag-returns"><strong>Returns:</strong> The registered loader. Returns <code>null</code> if no loader was found.</dt>
  138. </dl>
  139. </div>
  140. <h3 class="name name-method" id="itemEnd" translate="no">.<a href="#itemEnd">itemEnd</a><span class="signature">( url : <span class="param-type">string</span> )</span> </h3>
  141. <div class="method">
  142. <div class="description">
  143. <p>This should be called by any loader using the manager when the loader
  144. ended loading an item.</p>
  145. </div>
  146. <table class="params">
  147. <tbody>
  148. <tr>
  149. <td class="name"><code>url</code></td>
  150. <td class="description last"><p>The URL of the loaded item.</p></td>
  151. </tr>
  152. </tbody>
  153. </table>
  154. </div>
  155. <h3 class="name name-method" id="itemError" translate="no">.<a href="#itemError">itemError</a><span class="signature">( url : <span class="param-type">string</span> )</span> </h3>
  156. <div class="method">
  157. <div class="description">
  158. <p>This should be called by any loader using the manager when the loader
  159. encounters an error when loading an item.</p>
  160. </div>
  161. <table class="params">
  162. <tbody>
  163. <tr>
  164. <td class="name"><code>url</code></td>
  165. <td class="description last"><p>The URL of the item that produces an error.</p></td>
  166. </tr>
  167. </tbody>
  168. </table>
  169. </div>
  170. <h3 class="name name-method" id="itemStart" translate="no">.<a href="#itemStart">itemStart</a><span class="signature">( url : <span class="param-type">string</span> )</span> </h3>
  171. <div class="method">
  172. <div class="description">
  173. <p>This should be called by any loader using the manager when the loader
  174. starts loading an item.</p>
  175. </div>
  176. <table class="params">
  177. <tbody>
  178. <tr>
  179. <td class="name"><code>url</code></td>
  180. <td class="description last"><p>The URL to load.</p></td>
  181. </tr>
  182. </tbody>
  183. </table>
  184. </div>
  185. <h3 class="name name-method" id="removeHandler" translate="no">.<a href="#removeHandler">removeHandler</a><span class="signature">( regex : <span class="param-type">string</span> )</span><span class="type-signature"> : <a href="LoadingManager.html">LoadingManager</a></span> </h3>
  186. <div class="method">
  187. <div class="description">
  188. <p>Removes the loader for the given regular expression.</p>
  189. </div>
  190. <table class="params">
  191. <tbody>
  192. <tr>
  193. <td class="name"><code>regex</code></td>
  194. <td class="description last"><p>A regular expression.</p></td>
  195. </tr>
  196. </tbody>
  197. </table>
  198. <dl class="details">
  199. <dt class="tag-returns"><strong>Returns:</strong> A reference to this loading manager.</dt>
  200. </dl>
  201. </div>
  202. <h3 class="name name-method" id="resolveURL" translate="no">.<a href="#resolveURL">resolveURL</a><span class="signature">( url : <span class="param-type">string</span> )</span><span class="type-signature"> : string</span> </h3>
  203. <div class="method">
  204. <div class="description">
  205. <p>Given a URL, uses the URL modifier callback (if any) and returns a
  206. resolved URL. If no URL modifier is set, returns the original URL.</p>
  207. </div>
  208. <table class="params">
  209. <tbody>
  210. <tr>
  211. <td class="name"><code>url</code></td>
  212. <td class="description last"><p>The URL to load.</p></td>
  213. </tr>
  214. </tbody>
  215. </table>
  216. <dl class="details">
  217. <dt class="tag-returns"><strong>Returns:</strong> The resolved URL.</dt>
  218. </dl>
  219. </div>
  220. <h3 class="name name-method" id="setURLModifier" translate="no">.<a href="#setURLModifier">setURLModifier</a><span class="signature">( transform : <span class="param-type">function</span> )</span><span class="type-signature"> : <a href="LoadingManager.html">LoadingManager</a></span> </h3>
  221. <div class="method">
  222. <div class="description">
  223. <p>If provided, the callback will be passed each resource URL before a
  224. request is sent. The callback may return the original URL, or a new URL to
  225. override loading behavior. This behavior can be used to load assets from
  226. .ZIP files, drag-and-drop APIs, and Data URIs.</p>
  227. <pre><code class="language-js">const blobs = {'fish.gltf': blob1, 'diffuse.png': blob2, 'normal.png': blob3};
  228. const manager = new THREE.LoadingManager();
  229. // Initialize loading manager with URL callback.
  230. const objectURLs = [];
  231. manager.setURLModifier( ( url ) => {
  232. url = URL.createObjectURL( blobs[ url ] );
  233. objectURLs.push( url );
  234. return url;
  235. } );
  236. // Load as usual, then revoke the blob URLs.
  237. const loader = new GLTFLoader( manager );
  238. loader.load( 'fish.gltf', (gltf) => {
  239. scene.add( gltf.scene );
  240. objectURLs.forEach( ( url ) => URL.revokeObjectURL( url ) );
  241. } );
  242. </code></pre>
  243. </div>
  244. <table class="params">
  245. <tbody>
  246. <tr>
  247. <td class="name"><code>transform</code></td>
  248. <td class="description last"><p>URL modifier callback. Called with an URL and must return a resolved URL.</p></td>
  249. </tr>
  250. </tbody>
  251. </table>
  252. <dl class="details">
  253. <dt class="tag-returns"><strong>Returns:</strong> A reference to this loading manager.</dt>
  254. </dl>
  255. </div>
  256. <h2 class="subsection-title">Source</h2>
  257. <p>
  258. <a href="https://github.com/mrdoob/three.js/blob/master/src/loaders/LoadingManager.js" target="_blank" rel="noopener" translate="no">src/loaders/LoadingManager.js</a>
  259. </p>
  260. </article>
  261. </section>
  262. <script src="../scripts/linenumber.js"></script>
  263. <script src="../scripts/page.js"></script>
  264. </body>
  265. </html>
粤ICP备19079148号