智慧水务管理系统 - 精河县供水工程综合管理平台

ResourceCache.js 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. import Check from "../Core/Check.js";
  2. import Frozen from "../Core/Frozen.js";
  3. import defined from "../Core/defined.js";
  4. import DeveloperError from "../Core/DeveloperError.js";
  5. import BufferLoader from "./BufferLoader.js";
  6. import GltfBufferViewLoader from "./GltfBufferViewLoader.js";
  7. import GltfDracoLoader from "./GltfDracoLoader.js";
  8. import GltfImageLoader from "./GltfImageLoader.js";
  9. import GltfIndexBufferLoader from "./GltfIndexBufferLoader.js";
  10. import GltfJsonLoader from "./GltfJsonLoader.js";
  11. import GltfTextureLoader from "./GltfTextureLoader.js";
  12. import GltfVertexBufferLoader from "./GltfVertexBufferLoader.js";
  13. import GltfSpzLoader from "./GltfSpzLoader.js";
  14. import MetadataSchemaLoader from "./MetadataSchemaLoader.js";
  15. import ResourceCacheKey from "./ResourceCacheKey.js";
  16. import ResourceCacheStatistics from "./ResourceCacheStatistics.js";
  17. /**
  18. * Cache for resources shared across 3D Tiles and glTF.
  19. *
  20. * @namespace ResourceCache
  21. *
  22. * @private
  23. */
  24. function ResourceCache() {}
  25. ResourceCache.cacheEntries = {};
  26. // Statistics about binary data stored in the resource cache
  27. ResourceCache.statistics = new ResourceCacheStatistics();
  28. /**
  29. * A reference-counted cache entry.
  30. *
  31. * @param {ResourceLoader} resourceLoader The resource.
  32. *
  33. * @alias CacheEntry
  34. * @constructor
  35. *
  36. * @private
  37. */
  38. function CacheEntry(resourceLoader) {
  39. this.referenceCount = 1;
  40. this.resourceLoader = resourceLoader;
  41. // For unit testing only
  42. this._statisticsPromise = undefined;
  43. }
  44. /**
  45. * Gets a resource from the cache. If the resource exists its reference count is
  46. * incremented. Otherwise, if no resource loader exists, undefined is returned.
  47. *
  48. * @param {string} cacheKey The cache key of the resource.
  49. *
  50. * @returns {ResourceLoader|undefined} The resource.
  51. * @private
  52. */
  53. ResourceCache.get = function (cacheKey) {
  54. //>>includeStart('debug', pragmas.debug);
  55. Check.typeOf.string("cacheKey", cacheKey);
  56. //>>includeEnd('debug');
  57. const cacheEntry = ResourceCache.cacheEntries[cacheKey];
  58. if (defined(cacheEntry)) {
  59. ++cacheEntry.referenceCount;
  60. return cacheEntry.resourceLoader;
  61. }
  62. return undefined;
  63. };
  64. /**
  65. * Adds it to the cache.
  66. *
  67. * @param {ResourceLoader} resourceLoader The resource.
  68. * @returns {ResourceLoader} The resource.
  69. *
  70. * @exception {DeveloperError} Resource with this cacheKey is already in the cache
  71. * @private
  72. */
  73. ResourceCache.add = function (resourceLoader) {
  74. //>>includeStart('debug', pragmas.debug);
  75. Check.typeOf.object("resourceLoader", resourceLoader);
  76. //>>includeEnd('debug');
  77. const cacheKey = resourceLoader.cacheKey;
  78. //>>includeStart('debug', pragmas.debug);
  79. Check.typeOf.string("options.resourceLoader.cacheKey", cacheKey);
  80. if (defined(ResourceCache.cacheEntries[cacheKey])) {
  81. throw new DeveloperError(
  82. `Resource with this cacheKey is already in the cache: ${cacheKey}`,
  83. );
  84. }
  85. //>>includeEnd('debug');
  86. ResourceCache.cacheEntries[cacheKey] = new CacheEntry(resourceLoader);
  87. return resourceLoader;
  88. };
  89. /**
  90. * Unloads a resource from the cache. When the reference count hits zero the
  91. * resource is destroyed.
  92. *
  93. * @param {ResourceLoader} resourceLoader The resource.
  94. *
  95. * @exception {DeveloperError} Resource is not in the cache.
  96. * @exception {DeveloperError} Cannot unload resource that has no references.
  97. * @private
  98. */
  99. ResourceCache.unload = function (resourceLoader) {
  100. //>>includeStart('debug', pragmas.debug);
  101. Check.typeOf.object("resourceLoader", resourceLoader);
  102. //>>includeEnd('debug');
  103. const cacheKey = resourceLoader.cacheKey;
  104. const cacheEntry = ResourceCache.cacheEntries[cacheKey];
  105. //>>includeStart('debug', pragmas.debug);
  106. if (!defined(cacheEntry)) {
  107. throw new DeveloperError(`Resource is not in the cache: ${cacheKey}`);
  108. }
  109. //>>includeEnd('debug');
  110. --cacheEntry.referenceCount;
  111. if (cacheEntry.referenceCount === 0) {
  112. ResourceCache.statistics.removeLoader(resourceLoader);
  113. resourceLoader.destroy();
  114. delete ResourceCache.cacheEntries[cacheKey];
  115. }
  116. };
  117. /**
  118. * Gets an existing schema loader from the cache, or creates a new loader if one does not already exist.
  119. *
  120. * @param {object} options Object with the following properties:
  121. * @param {object} [options.schema] An object that explicitly defines a schema JSON. Mutually exclusive with options.resource.
  122. * @param {Resource} [options.resource] The {@link Resource} pointing to the schema JSON. Mutually exclusive with options.schema.
  123. *
  124. * @returns {MetadataSchemaLoader} The cached schema resource.
  125. *
  126. * @exception {DeveloperError} One of options.schema and options.resource must be defined.
  127. * @private
  128. */
  129. ResourceCache.getSchemaLoader = function (options) {
  130. options = options ?? Frozen.EMPTY_OBJECT;
  131. const { schema, resource } = options;
  132. //>>includeStart('debug', pragmas.debug);
  133. if (defined(schema) === defined(resource)) {
  134. throw new DeveloperError(
  135. "One of options.schema and options.resource must be defined.",
  136. );
  137. }
  138. //>>includeEnd('debug');
  139. const cacheKey = ResourceCacheKey.getSchemaCacheKey({
  140. schema: schema,
  141. resource: resource,
  142. });
  143. let schemaLoader = ResourceCache.get(cacheKey);
  144. if (defined(schemaLoader)) {
  145. return schemaLoader;
  146. }
  147. schemaLoader = new MetadataSchemaLoader({
  148. schema: schema,
  149. resource: resource,
  150. cacheKey: cacheKey,
  151. });
  152. return ResourceCache.add(schemaLoader);
  153. };
  154. /**
  155. * Gets an existing embedded buffer loader from the cache, or creates a new loader if one does not already exist.
  156. *
  157. * @param {object} options Object with the following properties:
  158. * @param {Resource} options.parentResource The {@link Resource} containing the embedded buffer.
  159. * @param {number} options.bufferId A unique identifier of the embedded buffer within the parent resource.
  160. * @param {Uint8Array} [options.typedArray] The typed array containing the embedded buffer contents.
  161. *
  162. * @returns {BufferLoader} The cached buffer loader.
  163. * @private
  164. */
  165. ResourceCache.getEmbeddedBufferLoader = function (options) {
  166. options = options ?? Frozen.EMPTY_OBJECT;
  167. const { parentResource, bufferId, typedArray } = options;
  168. //>>includeStart('debug', pragmas.debug);
  169. Check.typeOf.object("options.parentResource", parentResource);
  170. Check.typeOf.number("options.bufferId", bufferId);
  171. //>>includeEnd('debug');
  172. const cacheKey = ResourceCacheKey.getEmbeddedBufferCacheKey({
  173. parentResource: parentResource,
  174. bufferId: bufferId,
  175. });
  176. let bufferLoader = ResourceCache.get(cacheKey);
  177. if (defined(bufferLoader)) {
  178. return bufferLoader;
  179. }
  180. //>>includeStart('debug', pragmas.debug);
  181. Check.typeOf.object("options.typedArray", typedArray);
  182. //>>includeEnd('debug');
  183. bufferLoader = new BufferLoader({
  184. typedArray: typedArray,
  185. cacheKey: cacheKey,
  186. });
  187. return ResourceCache.add(bufferLoader);
  188. };
  189. /**
  190. * Gets an existing external buffer from loader the cache, or creates a new loader if one does not already exist.
  191. *
  192. * @param {object} options Object with the following properties:
  193. * @param {Resource} options.resource The {@link Resource} pointing to the external buffer.
  194. *
  195. * @returns {BufferLoader} The cached buffer loader.
  196. * @private
  197. */
  198. ResourceCache.getExternalBufferLoader = function (options) {
  199. options = options ?? Frozen.EMPTY_OBJECT;
  200. const { resource } = options;
  201. //>>includeStart('debug', pragmas.debug);
  202. Check.typeOf.object("options.resource", resource);
  203. //>>includeEnd('debug');
  204. const cacheKey = ResourceCacheKey.getExternalBufferCacheKey({
  205. resource: resource,
  206. });
  207. let bufferLoader = ResourceCache.get(cacheKey);
  208. if (defined(bufferLoader)) {
  209. return bufferLoader;
  210. }
  211. bufferLoader = new BufferLoader({
  212. resource: resource,
  213. cacheKey: cacheKey,
  214. });
  215. return ResourceCache.add(bufferLoader);
  216. };
  217. /**
  218. * Gets an existing glTF JSON loader from the cache, or creates a new loader if one does not already exist.
  219. *
  220. * @param {object} options Object with the following properties:
  221. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
  222. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
  223. * @param {Uint8Array} [options.typedArray] The typed array containing the glTF contents.
  224. * @param {object} [options.gltfJson] The parsed glTF JSON contents.
  225. *
  226. * @returns {GltfJsonLoader} The cached glTF JSON loader.
  227. * @private
  228. */
  229. ResourceCache.getGltfJsonLoader = function (options) {
  230. options = options ?? Frozen.EMPTY_OBJECT;
  231. const { gltfResource, baseResource, typedArray, gltfJson } = options;
  232. //>>includeStart('debug', pragmas.debug);
  233. Check.typeOf.object("options.gltfResource", gltfResource);
  234. Check.typeOf.object("options.baseResource", baseResource);
  235. //>>includeEnd('debug');
  236. const cacheKey = ResourceCacheKey.getGltfCacheKey({
  237. gltfResource: gltfResource,
  238. });
  239. let gltfJsonLoader = ResourceCache.get(cacheKey);
  240. if (defined(gltfJsonLoader)) {
  241. return gltfJsonLoader;
  242. }
  243. gltfJsonLoader = new GltfJsonLoader({
  244. resourceCache: ResourceCache,
  245. gltfResource: gltfResource,
  246. baseResource: baseResource,
  247. typedArray: typedArray,
  248. gltfJson: gltfJson,
  249. cacheKey: cacheKey,
  250. });
  251. return ResourceCache.add(gltfJsonLoader);
  252. };
  253. /**
  254. * Gets an existing glTF buffer view from the cache, or creates a new loader if one does not already exist.
  255. *
  256. * @param {object} options Object with the following properties:
  257. * @param {object} options.gltf The glTF JSON.
  258. * @param {number} options.bufferViewId The bufferView ID.
  259. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
  260. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
  261. *
  262. * @returns {GltfBufferViewLoader} The cached buffer view loader.
  263. * @private
  264. */
  265. ResourceCache.getBufferViewLoader = function (options) {
  266. options = options ?? Frozen.EMPTY_OBJECT;
  267. const { gltf, bufferViewId, gltfResource, baseResource } = options;
  268. //>>includeStart('debug', pragmas.debug);
  269. Check.typeOf.object("options.gltf", gltf);
  270. Check.typeOf.number("options.bufferViewId", bufferViewId);
  271. Check.typeOf.object("options.gltfResource", gltfResource);
  272. Check.typeOf.object("options.baseResource", baseResource);
  273. //>>includeEnd('debug');
  274. const cacheKey = ResourceCacheKey.getBufferViewCacheKey({
  275. gltf: gltf,
  276. bufferViewId: bufferViewId,
  277. gltfResource: gltfResource,
  278. baseResource: baseResource,
  279. });
  280. let bufferViewLoader = ResourceCache.get(cacheKey);
  281. if (defined(bufferViewLoader)) {
  282. return bufferViewLoader;
  283. }
  284. bufferViewLoader = new GltfBufferViewLoader({
  285. resourceCache: ResourceCache,
  286. gltf: gltf,
  287. bufferViewId: bufferViewId,
  288. gltfResource: gltfResource,
  289. baseResource: baseResource,
  290. cacheKey: cacheKey,
  291. });
  292. return ResourceCache.add(bufferViewLoader);
  293. };
  294. /**
  295. * Gets an existing Draco data from the cache, or creates a new loader if one does not already exist.
  296. *
  297. * @param {object} options Object with the following properties:
  298. * @param {object} options.gltf The glTF JSON.
  299. * @param {object} options.primitive The primitive containing the Draco extension.
  300. * @param {object} options.draco The Draco extension object.
  301. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
  302. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
  303. *
  304. * @returns {GltfDracoLoader} The cached Draco loader.
  305. * @private
  306. */
  307. ResourceCache.getDracoLoader = function (options) {
  308. options = options ?? Frozen.EMPTY_OBJECT;
  309. const { gltf, primitive, draco, gltfResource, baseResource } = options;
  310. //>>includeStart('debug', pragmas.debug);
  311. Check.typeOf.object("options.gltf", gltf);
  312. Check.typeOf.object("options.primitive", primitive);
  313. Check.typeOf.object("options.draco", draco);
  314. Check.typeOf.object("options.gltfResource", gltfResource);
  315. Check.typeOf.object("options.baseResource", baseResource);
  316. //>>includeEnd('debug');
  317. const cacheKey = ResourceCacheKey.getDracoCacheKey({
  318. gltf: gltf,
  319. draco: draco,
  320. gltfResource: gltfResource,
  321. baseResource: baseResource,
  322. });
  323. let dracoLoader = ResourceCache.get(cacheKey);
  324. if (defined(dracoLoader)) {
  325. return dracoLoader;
  326. }
  327. dracoLoader = new GltfDracoLoader({
  328. resourceCache: ResourceCache,
  329. gltf: gltf,
  330. primitive: primitive,
  331. draco: draco,
  332. gltfResource: gltfResource,
  333. baseResource: baseResource,
  334. cacheKey: cacheKey,
  335. });
  336. return ResourceCache.add(dracoLoader);
  337. };
  338. /**
  339. * Gets an existing SPZ loader from the cache, or creates a new loader if one does not already exist.
  340. * This loader is used to decode SPZ (Splat Point Cloud) data in glTF.
  341. *
  342. * @param {object} options Object with the following properties:
  343. * @param {object} options.gltf The glTF JSON.
  344. * @param {object} options.primitive The primitive containing the SPZ extension.
  345. * @param {object} options.spz The SPZ extension object.
  346. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
  347. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
  348. *
  349. * @return {GltfSpzLoader} The cached SPZ loader.
  350. @private
  351. * */
  352. ResourceCache.getSpzLoader = function (options) {
  353. options = options ?? Frozen.EMPTY_OBJECT;
  354. const { gltf, primitive, spz, gltfResource, baseResource } = options;
  355. //>>includeStart('debug', pragmas.debug);
  356. Check.typeOf.object("options.gltf", gltf);
  357. Check.typeOf.object("options.primitive", primitive);
  358. Check.typeOf.object("options.spz", spz);
  359. Check.typeOf.object("options.gltfResource", gltfResource);
  360. Check.typeOf.object("options.baseResource", baseResource);
  361. //>>includeEnd('debug');
  362. const cacheKey = ResourceCacheKey.getSpzCacheKey({
  363. gltf: gltf,
  364. primitive: primitive,
  365. gltfResource: gltfResource,
  366. baseResource: baseResource,
  367. });
  368. let spzLoader = ResourceCache.get(cacheKey);
  369. if (defined(spzLoader)) {
  370. return spzLoader;
  371. }
  372. spzLoader = new GltfSpzLoader({
  373. resourceCache: ResourceCache,
  374. gltf: gltf,
  375. primitive: primitive,
  376. spz: spz,
  377. gltfResource: gltfResource,
  378. baseResource: baseResource,
  379. cacheKey: cacheKey,
  380. });
  381. return ResourceCache.add(spzLoader);
  382. };
  383. /**
  384. * Gets an existing glTF vertex buffer from the cache, or creates a new loader if one does not already exist.
  385. *
  386. * @param {object} options Object with the following properties:
  387. * @param {object} options.gltf The glTF JSON.
  388. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
  389. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
  390. * @param {FrameState} options.frameState The frame state.
  391. * @param {number} [options.bufferViewId] The bufferView ID corresponding to the vertex buffer.
  392. * @param {object} [options.primitive] The primitive containing the Draco extension.
  393. * @param {object} [options.draco] The Draco extension object.
  394. * @param {string} [options.attributeSemantic] The attribute semantic, e.g. POSITION or NORMAL.
  395. * @param {number} [options.accessorId] The accessor ID.
  396. * @param {boolean} [options.asynchronous=true] Determines if WebGL resource creation will be spread out over several frames or block until all WebGL resources are created.
  397. * @param {boolean} [options.dequantize=false] Determines whether or not the vertex buffer will be dequantized on the CPU.
  398. * @param {boolean} [options.loadBuffer=false] Load vertex buffer as a GPU vertex buffer.
  399. * @param {boolean} [options.loadTypedArray=false] Load vertex buffer as a typed array.
  400. * @exception {DeveloperError} One of options.bufferViewId and options.draco must be defined.
  401. * @exception {DeveloperError} When options.draco is defined options.attributeSemantic must also be defined.
  402. * @exception {DeveloperError} When options.draco is defined options.accessorId must also be defined.
  403. *
  404. * @returns {GltfVertexBufferLoader} The cached vertex buffer loader.
  405. * @private
  406. */
  407. ResourceCache.getVertexBufferLoader = function (options) {
  408. options = options ?? Frozen.EMPTY_OBJECT;
  409. const {
  410. gltf,
  411. gltfResource,
  412. baseResource,
  413. frameState,
  414. bufferViewId,
  415. primitive,
  416. draco,
  417. spz,
  418. attributeSemantic,
  419. accessorId,
  420. asynchronous = true,
  421. dequantize = false,
  422. loadBuffer = false,
  423. loadTypedArray = false,
  424. } = options;
  425. //>>includeStart('debug', pragmas.debug);
  426. Check.typeOf.object("options.gltf", gltf);
  427. Check.typeOf.object("options.gltfResource", gltfResource);
  428. Check.typeOf.object("options.baseResource", baseResource);
  429. Check.typeOf.object("options.frameState", frameState);
  430. if (!loadBuffer && !loadTypedArray) {
  431. throw new DeveloperError(
  432. "At least one of loadBuffer and loadTypedArray must be true.",
  433. );
  434. }
  435. const hasBufferViewId = defined(bufferViewId);
  436. const hasPrimitive = defined(primitive);
  437. const hasDraco = hasDracoCompression(draco, attributeSemantic);
  438. const hasAttributeSemantic = defined(attributeSemantic);
  439. const hasAccessorId = defined(accessorId);
  440. const hasSpz = defined(spz);
  441. if (hasBufferViewId === (hasDraco !== hasSpz)) {
  442. throw new DeveloperError(
  443. "One of options.bufferViewId, options.draco, or options.spz must be defined.",
  444. );
  445. }
  446. if (hasDraco && !hasAttributeSemantic) {
  447. throw new DeveloperError(
  448. "When options.draco is defined options.attributeSemantic must also be defined.",
  449. );
  450. }
  451. if (hasDraco && !hasAccessorId) {
  452. throw new DeveloperError(
  453. "When options.draco is defined options.hasAccessorId must also be defined.",
  454. );
  455. }
  456. if (hasDraco && !hasPrimitive) {
  457. throw new DeveloperError(
  458. "When options.draco is defined options.primitive must also be defined.",
  459. );
  460. }
  461. if (hasDraco) {
  462. Check.typeOf.object("options.draco", draco);
  463. Check.typeOf.string("options.attributeSemantic", attributeSemantic);
  464. Check.typeOf.number("options.accessorId", accessorId);
  465. }
  466. //>>includeEnd('debug');
  467. const cacheKey = ResourceCacheKey.getVertexBufferCacheKey({
  468. gltf: gltf,
  469. gltfResource: gltfResource,
  470. baseResource: baseResource,
  471. frameState: frameState,
  472. bufferViewId: bufferViewId,
  473. draco: draco,
  474. spz: spz,
  475. attributeSemantic: attributeSemantic,
  476. dequantize: dequantize,
  477. loadBuffer: loadBuffer,
  478. loadTypedArray: loadTypedArray,
  479. });
  480. let vertexBufferLoader = ResourceCache.get(cacheKey);
  481. if (defined(vertexBufferLoader)) {
  482. return vertexBufferLoader;
  483. }
  484. vertexBufferLoader = new GltfVertexBufferLoader({
  485. resourceCache: ResourceCache,
  486. gltf: gltf,
  487. gltfResource: gltfResource,
  488. baseResource: baseResource,
  489. bufferViewId: bufferViewId,
  490. primitive: primitive,
  491. draco: draco,
  492. spz: spz,
  493. attributeSemantic: attributeSemantic,
  494. accessorId: accessorId,
  495. cacheKey: cacheKey,
  496. asynchronous: asynchronous,
  497. dequantize: dequantize,
  498. loadBuffer: loadBuffer,
  499. loadTypedArray: loadTypedArray,
  500. });
  501. return ResourceCache.add(vertexBufferLoader);
  502. };
  503. function hasDracoCompression(draco, semantic) {
  504. return (
  505. defined(draco) &&
  506. defined(draco.attributes) &&
  507. defined(draco.attributes[semantic])
  508. );
  509. }
  510. /**
  511. * Gets an existing glTF index buffer from the cache, or creates a new loader if one does not already exist.
  512. *
  513. * @param {object} options Object with the following properties:
  514. * @param {object} options.gltf The glTF JSON.
  515. * @param {number} options.accessorId The accessor ID corresponding to the index buffer.
  516. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
  517. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
  518. * @param {FrameState} options.frameState The frame state.
  519. * @param {object} [options.primitive] The primitive containing the Draco extension.
  520. * @param {object} [options.draco] The Draco extension object.
  521. * @param {boolean} [options.asynchronous=true] Determines if WebGL resource creation will be spread out over several frames or block until all WebGL resources are created.
  522. * @param {boolean} [options.loadBuffer=false] Load index buffer as a GPU index buffer.
  523. * @param {boolean} [options.loadTypedArray=false] Load index buffer as a typed array.
  524. * @returns {GltfIndexBufferLoader} The cached index buffer loader.
  525. * @private
  526. */
  527. ResourceCache.getIndexBufferLoader = function (options) {
  528. options = options ?? Frozen.EMPTY_OBJECT;
  529. const {
  530. gltf,
  531. accessorId,
  532. gltfResource,
  533. baseResource,
  534. frameState,
  535. primitive,
  536. draco,
  537. asynchronous = true,
  538. loadBuffer = false,
  539. loadTypedArray = false,
  540. } = options;
  541. //>>includeStart('debug', pragmas.debug);
  542. Check.typeOf.object("options.gltf", gltf);
  543. Check.typeOf.number("options.accessorId", accessorId);
  544. Check.typeOf.object("options.gltfResource", gltfResource);
  545. Check.typeOf.object("options.baseResource", baseResource);
  546. Check.typeOf.object("options.frameState", frameState);
  547. if (!loadBuffer && !loadTypedArray) {
  548. throw new DeveloperError(
  549. "At least one of loadBuffer and loadTypedArray must be true.",
  550. );
  551. }
  552. //>>includeEnd('debug');
  553. const cacheKey = ResourceCacheKey.getIndexBufferCacheKey({
  554. gltf: gltf,
  555. accessorId: accessorId,
  556. gltfResource: gltfResource,
  557. baseResource: baseResource,
  558. frameState: frameState,
  559. draco: draco,
  560. loadBuffer: loadBuffer,
  561. loadTypedArray: loadTypedArray,
  562. });
  563. let indexBufferLoader = ResourceCache.get(cacheKey);
  564. if (defined(indexBufferLoader)) {
  565. return indexBufferLoader;
  566. }
  567. indexBufferLoader = new GltfIndexBufferLoader({
  568. resourceCache: ResourceCache,
  569. gltf: gltf,
  570. accessorId: accessorId,
  571. gltfResource: gltfResource,
  572. baseResource: baseResource,
  573. primitive: primitive,
  574. draco: draco,
  575. cacheKey: cacheKey,
  576. asynchronous: asynchronous,
  577. loadBuffer: loadBuffer,
  578. loadTypedArray: loadTypedArray,
  579. });
  580. return ResourceCache.add(indexBufferLoader);
  581. };
  582. /**
  583. * Gets an existing glTF image from the cache, or creates a new loader if one does not already exist.
  584. *
  585. * @param {object} options Object with the following properties:
  586. * @param {object} options.gltf The glTF JSON.
  587. * @param {number} options.imageId The image ID.
  588. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
  589. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
  590. *
  591. * @returns {GltfImageLoader} The cached image loader.
  592. * @private
  593. */
  594. ResourceCache.getImageLoader = function (options) {
  595. options = options ?? Frozen.EMPTY_OBJECT;
  596. const { gltf, imageId, gltfResource, baseResource } = options;
  597. //>>includeStart('debug', pragmas.debug);
  598. Check.typeOf.object("options.gltf", gltf);
  599. Check.typeOf.number("options.imageId", imageId);
  600. Check.typeOf.object("options.gltfResource", gltfResource);
  601. Check.typeOf.object("options.baseResource", baseResource);
  602. //>>includeEnd('debug');
  603. const cacheKey = ResourceCacheKey.getImageCacheKey({
  604. gltf: gltf,
  605. imageId: imageId,
  606. gltfResource: gltfResource,
  607. baseResource: baseResource,
  608. });
  609. let imageLoader = ResourceCache.get(cacheKey);
  610. if (defined(imageLoader)) {
  611. return imageLoader;
  612. }
  613. imageLoader = new GltfImageLoader({
  614. resourceCache: ResourceCache,
  615. gltf: gltf,
  616. imageId: imageId,
  617. gltfResource: gltfResource,
  618. baseResource: baseResource,
  619. cacheKey: cacheKey,
  620. });
  621. return ResourceCache.add(imageLoader);
  622. };
  623. /**
  624. * Gets an existing glTF texture from the cache, or creates a new loader if one does not already exist.
  625. *
  626. * @param {object} options Object with the following properties:
  627. * @param {object} options.gltf The glTF JSON.
  628. * @param {object} options.textureInfo The texture info object.
  629. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
  630. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
  631. * @param {SupportedImageFormats} options.supportedImageFormats The supported image formats.
  632. * @param {FrameState} options.frameState The frame state.
  633. * @param {boolean} [options.asynchronous=true] Determines if WebGL resource creation will be spread out over several frames or block until all WebGL resources are created.
  634. *
  635. * @returns {GltfTextureLoader} The cached texture loader.
  636. * @private
  637. */
  638. ResourceCache.getTextureLoader = function (options) {
  639. options = options ?? Frozen.EMPTY_OBJECT;
  640. const {
  641. gltf,
  642. textureInfo,
  643. gltfResource,
  644. baseResource,
  645. supportedImageFormats,
  646. frameState,
  647. asynchronous = true,
  648. } = options;
  649. //>>includeStart('debug', pragmas.debug);
  650. Check.typeOf.object("options.gltf", gltf);
  651. Check.typeOf.object("options.textureInfo", textureInfo);
  652. Check.typeOf.object("options.gltfResource", gltfResource);
  653. Check.typeOf.object("options.baseResource", baseResource);
  654. Check.typeOf.object("options.supportedImageFormats", supportedImageFormats);
  655. Check.typeOf.object("options.frameState", frameState);
  656. //>>includeEnd('debug');
  657. const cacheKey = ResourceCacheKey.getTextureCacheKey({
  658. gltf: gltf,
  659. textureInfo: textureInfo,
  660. gltfResource: gltfResource,
  661. baseResource: baseResource,
  662. supportedImageFormats: supportedImageFormats,
  663. frameState: frameState,
  664. });
  665. let textureLoader = ResourceCache.get(cacheKey);
  666. if (defined(textureLoader)) {
  667. return textureLoader;
  668. }
  669. textureLoader = new GltfTextureLoader({
  670. resourceCache: ResourceCache,
  671. gltf: gltf,
  672. textureInfo: textureInfo,
  673. gltfResource: gltfResource,
  674. baseResource: baseResource,
  675. supportedImageFormats: supportedImageFormats,
  676. cacheKey: cacheKey,
  677. asynchronous: asynchronous,
  678. });
  679. return ResourceCache.add(textureLoader);
  680. };
  681. /**
  682. * Unload everything from the cache. This is used for unit testing.
  683. *
  684. * @private
  685. */
  686. ResourceCache.clearForSpecs = function () {
  687. // Unload in the order below. This prevents an unload function from unloading
  688. // a resource that has already been unloaded.
  689. const precedence = [
  690. GltfVertexBufferLoader,
  691. GltfIndexBufferLoader,
  692. GltfDracoLoader,
  693. GltfTextureLoader,
  694. GltfImageLoader,
  695. GltfBufferViewLoader,
  696. BufferLoader,
  697. MetadataSchemaLoader,
  698. GltfJsonLoader,
  699. ];
  700. let cacheKey;
  701. const cacheEntries = ResourceCache.cacheEntries;
  702. const cacheEntriesSorted = [];
  703. for (cacheKey in cacheEntries) {
  704. if (cacheEntries.hasOwnProperty(cacheKey)) {
  705. cacheEntriesSorted.push(cacheEntries[cacheKey]);
  706. }
  707. }
  708. cacheEntriesSorted.sort(function (a, b) {
  709. const indexA = precedence.indexOf(a.resourceLoader.constructor);
  710. const indexB = precedence.indexOf(b.resourceLoader.constructor);
  711. return indexA - indexB;
  712. });
  713. const cacheEntriesLength = cacheEntriesSorted.length;
  714. for (let i = 0; i < cacheEntriesLength; ++i) {
  715. const cacheEntry = cacheEntriesSorted[i];
  716. cacheKey = cacheEntry.resourceLoader.cacheKey;
  717. if (defined(cacheEntries[cacheKey])) {
  718. cacheEntry.resourceLoader.destroy();
  719. delete cacheEntries[cacheKey];
  720. }
  721. }
  722. ResourceCache.statistics.clear();
  723. };
  724. export default ResourceCache;