Loader.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { DefaultLoadingManager } from './LoadingManager.js';
  2. /**
  3. * Abstract base class for loaders.
  4. *
  5. * @abstract
  6. */
  7. class Loader {
  8. /**
  9. * Constructs a new loader.
  10. *
  11. * @param {LoadingManager} [manager] - The loading manager.
  12. */
  13. constructor( manager ) {
  14. /**
  15. * The loading manager.
  16. *
  17. * @type {LoadingManager}
  18. * @default DefaultLoadingManager
  19. */
  20. this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
  21. /**
  22. * The crossOrigin string to implement CORS for loading the url from a
  23. * different domain that allows CORS.
  24. *
  25. * @type {string}
  26. * @default 'anonymous'
  27. */
  28. this.crossOrigin = 'anonymous';
  29. /**
  30. * Whether the XMLHttpRequest uses credentials.
  31. *
  32. * @type {boolean}
  33. * @default false
  34. */
  35. this.withCredentials = false;
  36. /**
  37. * The base path from which the asset will be loaded.
  38. *
  39. * @type {string}
  40. */
  41. this.path = '';
  42. /**
  43. * The base path from which additional resources like textures will be loaded.
  44. *
  45. * @type {string}
  46. */
  47. this.resourcePath = '';
  48. /**
  49. * The [request header](https://developer.mozilla.org/en-US/docs/Glossary/Request_header)
  50. * used in HTTP request.
  51. *
  52. * @type {Object<string, any>}
  53. */
  54. this.requestHeader = {};
  55. if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) {
  56. __THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'observe', { detail: this } ) );
  57. }
  58. }
  59. /**
  60. * This method needs to be implemented by all concrete loaders. It holds the
  61. * logic for loading assets from the backend.
  62. *
  63. * @abstract
  64. * @param {string} url - The path/URL of the file to be loaded.
  65. * @param {Function} onLoad - Executed when the loading process has been finished.
  66. * @param {onProgressCallback} [onProgress] - Executed while the loading is in progress.
  67. * @param {onErrorCallback} [onError] - Executed when errors occur.
  68. */
  69. load( /* url, onLoad, onProgress, onError */ ) {}
  70. /**
  71. * A async version of {@link Loader#load}.
  72. *
  73. * @param {string} url - The path/URL of the file to be loaded.
  74. * @param {onProgressCallback} [onProgress] - Executed while the loading is in progress.
  75. * @return {Promise} A Promise that resolves when the asset has been loaded.
  76. */
  77. loadAsync( url, onProgress ) {
  78. const scope = this;
  79. return new Promise( function ( resolve, reject ) {
  80. scope.load( url, resolve, onProgress, reject );
  81. } );
  82. }
  83. /**
  84. * This method needs to be implemented by all concrete loaders. It holds the
  85. * logic for parsing the asset into three.js entities.
  86. *
  87. * @abstract
  88. * @param {any} data - The data to parse.
  89. */
  90. parse( /* data */ ) {}
  91. /**
  92. * Sets the `crossOrigin` String to implement CORS for loading the URL
  93. * from a different domain that allows CORS.
  94. *
  95. * @param {string} crossOrigin - The `crossOrigin` value.
  96. * @return {Loader} A reference to this instance.
  97. */
  98. setCrossOrigin( crossOrigin ) {
  99. this.crossOrigin = crossOrigin;
  100. return this;
  101. }
  102. /**
  103. * Whether the XMLHttpRequest uses credentials such as cookies, authorization
  104. * headers or TLS client certificates, see [XMLHttpRequest.withCredentials](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials).
  105. *
  106. * Note: This setting has no effect if you are loading files locally or from the same domain.
  107. *
  108. * @param {boolean} value - The `withCredentials` value.
  109. * @return {Loader} A reference to this instance.
  110. */
  111. setWithCredentials( value ) {
  112. this.withCredentials = value;
  113. return this;
  114. }
  115. /**
  116. * Sets the base path for the asset.
  117. *
  118. * @param {string} path - The base path.
  119. * @return {Loader} A reference to this instance.
  120. */
  121. setPath( path ) {
  122. this.path = path;
  123. return this;
  124. }
  125. /**
  126. * Sets the base path for dependent resources like textures.
  127. *
  128. * @param {string} resourcePath - The resource path.
  129. * @return {Loader} A reference to this instance.
  130. */
  131. setResourcePath( resourcePath ) {
  132. this.resourcePath = resourcePath;
  133. return this;
  134. }
  135. /**
  136. * Sets the given request header.
  137. *
  138. * @param {Object} requestHeader - A [request header](https://developer.mozilla.org/en-US/docs/Glossary/Request_header)
  139. * for configuring the HTTP request.
  140. * @return {Loader} A reference to this instance.
  141. */
  142. setRequestHeader( requestHeader ) {
  143. this.requestHeader = requestHeader;
  144. return this;
  145. }
  146. /**
  147. * This method can be implemented in loaders for aborting ongoing requests.
  148. *
  149. * @abstract
  150. * @return {Loader} A reference to this instance.
  151. */
  152. abort() {
  153. return this;
  154. }
  155. }
  156. /**
  157. * Callback for onProgress in loaders.
  158. *
  159. * @callback onProgressCallback
  160. * @param {ProgressEvent} event - An instance of `ProgressEvent` that represents the current loading status.
  161. */
  162. /**
  163. * Callback for onError in loaders.
  164. *
  165. * @callback onErrorCallback
  166. * @param {Error} error - The error which occurred during the loading process.
  167. */
  168. /**
  169. * The default material name that is used by loaders
  170. * when creating materials for loaded 3D objects.
  171. *
  172. * Note: Not all loaders might honor this setting.
  173. *
  174. * @static
  175. * @type {string}
  176. * @default '__DEFAULT'
  177. */
  178. Loader.DEFAULT_MATERIAL_NAME = '__DEFAULT';
  179. export { Loader };
粤ICP备19079148号