@gglib/content 0.6.2 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/Readme.md +11 -7
- package/dist/bundles/content.esm.js +1050 -0
- package/dist/bundles/content.esm.js.map +1 -0
- package/dist/content/src/AssetContainer.d.ts +18 -0
- package/dist/content/src/AssetContainer.d.ts.map +1 -0
- package/dist/content/src/AssetContainer.js +30 -0
- package/dist/content/src/AssetContainer.js.map +1 -0
- package/dist/content/src/AssetLoaderRegistry.d.ts +21 -0
- package/dist/content/src/AssetLoaderRegistry.d.ts.map +1 -0
- package/dist/content/src/AssetLoaderRegistry.js +28 -0
- package/dist/content/src/AssetLoaderRegistry.js.map +1 -0
- package/dist/content/src/AssetType.d.ts +13 -0
- package/dist/content/src/AssetType.d.ts.map +1 -0
- package/dist/content/src/AssetType.js +6 -0
- package/dist/content/src/AssetType.js.map +1 -0
- package/dist/content/src/AsyncExecutor.d.ts +63 -0
- package/dist/content/src/AsyncExecutor.d.ts.map +1 -0
- package/dist/content/src/AsyncExecutor.js +166 -0
- package/dist/content/src/AsyncExecutor.js.map +1 -0
- package/dist/content/src/ContentLoader.d.ts +266 -0
- package/dist/content/src/ContentLoader.d.ts.map +1 -0
- package/dist/content/src/ContentLoader.js +341 -0
- package/dist/content/src/ContentLoader.js.map +1 -0
- package/dist/content/src/ContentType.d.ts.map +1 -1
- package/dist/content/src/ContentType.js +6 -10
- package/dist/content/src/ContentType.js.map +1 -1
- package/dist/content/src/HttpClient.d.ts +48 -0
- package/dist/content/src/HttpClient.d.ts.map +1 -0
- package/dist/content/src/HttpClient.js +139 -0
- package/dist/content/src/HttpClient.js.map +1 -0
- package/dist/content/src/MaterialRegistry.d.ts +14 -0
- package/dist/content/src/MaterialRegistry.d.ts.map +1 -0
- package/dist/content/src/MaterialRegistry.js +24 -0
- package/dist/content/src/MaterialRegistry.js.map +1 -0
- package/dist/content/src/ResourceGraph.d.ts +54 -0
- package/dist/content/src/ResourceGraph.d.ts.map +1 -0
- package/dist/content/src/ResourceGraph.js +195 -0
- package/dist/content/src/ResourceGraph.js.map +1 -0
- package/dist/content/src/TextureLoader.d.ts +14 -0
- package/dist/content/src/TextureLoader.d.ts.map +1 -0
- package/dist/content/src/TextureLoader.js +70 -0
- package/dist/content/src/TextureLoader.js.map +1 -0
- package/dist/content/src/index.d.ts +8 -7
- package/dist/content/src/index.d.ts.map +1 -1
- package/dist/content/src/index.js +8 -7
- package/dist/content/src/index.js.map +1 -1
- package/dist/content/tsconfig.build.tsbuildinfo +1 -0
- package/dist/content/tsconfig.spec.tsbuildinfo +1 -0
- package/dist/stats.html +211 -170
- package/package.json +19 -8
- package/dist/bundles/content.umd.js +0 -713
- package/dist/bundles/content.umd.js.map +0 -1
- package/dist/content/src/ContentManager.d.ts +0 -153
- package/dist/content/src/ContentManager.d.ts.map +0 -1
- package/dist/content/src/ContentManager.js +0 -361
- package/dist/content/src/ContentManager.js.map +0 -1
- package/dist/content/src/ContentType.spec.d.ts +0 -2
- package/dist/content/src/ContentType.spec.d.ts.map +0 -1
- package/dist/content/src/ContentType.spec.js +0 -27
- package/dist/content/src/ContentType.spec.js.map +0 -1
- package/dist/content/src/Data.d.ts +0 -37
- package/dist/content/src/Data.d.ts.map +0 -1
- package/dist/content/src/Data.js +0 -34
- package/dist/content/src/Data.js.map +0 -1
- package/dist/content/src/Loader.d.ts +0 -69
- package/dist/content/src/Loader.d.ts.map +0 -1
- package/dist/content/src/Loader.js +0 -16
- package/dist/content/src/Loader.js.map +0 -1
- package/dist/content/src/Pipeline.d.ts +0 -38
- package/dist/content/src/Pipeline.d.ts.map +0 -1
- package/dist/content/src/Pipeline.js +0 -182
- package/dist/content/src/Pipeline.js.map +0 -1
- package/dist/content/src/Pipeline.spec.d.ts +0 -2
- package/dist/content/src/Pipeline.spec.d.ts.map +0 -1
- package/dist/content/src/Pipeline.spec.js +0 -121
- package/dist/content/src/Pipeline.spec.js.map +0 -1
- package/dist/content/src/PipelineContext.d.ts +0 -40
- package/dist/content/src/PipelineContext.d.ts.map +0 -1
- package/dist/content/src/PipelineContext.js +0 -2
- package/dist/content/src/PipelineContext.js.map +0 -1
- package/dist/content/src/symbols.d.ts +0 -2
- package/dist/content/src/symbols.d.ts.map +0 -1
- package/dist/content/src/symbols.js +0 -2
- package/dist/content/src/symbols.js.map +0 -1
- package/dist/content/src/utils.d.ts +0 -17
- package/dist/content/src/utils.d.ts.map +0 -1
- package/dist/content/src/utils.js +0 -42
- package/dist/content/src/utils.js.map +0 -1
- package/dist/content/tsconfig.tsbuildinfo +0 -2773
|
@@ -0,0 +1,1050 @@
|
|
|
1
|
+
import { Mesh, Material, BasicMaterial, isAquirableTextureOptions, isOwnable, createImageBitmapOptions, createTextureSource, Texture } from '@gglib/graphics';
|
|
2
|
+
import { Model } from '@gglib/model';
|
|
3
|
+
import { mergeUri, extname } from '@gglib/utils';
|
|
4
|
+
|
|
5
|
+
/******************************************************************************
|
|
6
|
+
Copyright (c) Microsoft Corporation.
|
|
7
|
+
|
|
8
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
9
|
+
purpose with or without fee is hereby granted.
|
|
10
|
+
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
12
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
13
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
14
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
15
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
16
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
17
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
18
|
+
***************************************************************************** */
|
|
19
|
+
|
|
20
|
+
function __rest(s, e) {
|
|
21
|
+
var t = {};
|
|
22
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
23
|
+
t[p] = s[p];
|
|
24
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
25
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
26
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
27
|
+
t[p[i]] = s[p[i]];
|
|
28
|
+
}
|
|
29
|
+
return t;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
33
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
34
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
35
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
36
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
37
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
38
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
43
|
+
var e = new Error(message);
|
|
44
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const AssetType = {
|
|
48
|
+
Model: 'model',
|
|
49
|
+
Texture: 'texture',
|
|
50
|
+
Material: 'material',
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* An asset container that wraps one or more textures. This is a common case for texture only loaders, and allows them to implement
|
|
55
|
+
* the AssetLoader interface without having to create a custom container class for each loader.
|
|
56
|
+
*/
|
|
57
|
+
class TextureAssetContainer {
|
|
58
|
+
constructor(textures) {
|
|
59
|
+
this.textures = textures;
|
|
60
|
+
}
|
|
61
|
+
count(asset) {
|
|
62
|
+
if (asset === AssetType.Texture) {
|
|
63
|
+
return this.textures.length;
|
|
64
|
+
}
|
|
65
|
+
return 0;
|
|
66
|
+
}
|
|
67
|
+
load(asset, index, context) {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
if (asset !== AssetType.Texture) {
|
|
70
|
+
throw new Error(`AssetType not supported by this container: ${asset}`);
|
|
71
|
+
}
|
|
72
|
+
const texture = this.textures[index];
|
|
73
|
+
if (!texture) {
|
|
74
|
+
throw new Error(`Texture index ${index} out of bounds for container with 1 texture`);
|
|
75
|
+
}
|
|
76
|
+
return texture;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
class AssetLoaderRegistry {
|
|
82
|
+
constructor() {
|
|
83
|
+
this.loaders = [];
|
|
84
|
+
}
|
|
85
|
+
register(spec) {
|
|
86
|
+
if ('match' in spec) {
|
|
87
|
+
this.loaders.push(spec);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
this.loaders.push({
|
|
91
|
+
match: (type) => spec.extensions.includes(type) || spec.mimeTypes.includes(type),
|
|
92
|
+
create: spec.create,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
find(type) {
|
|
97
|
+
if (!type) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
for (const loader of this.loaders) {
|
|
101
|
+
if (loader.match(type)) {
|
|
102
|
+
return loader.create;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const noopController = new AbortController();
|
|
110
|
+
const noopSignal = noopController.signal;
|
|
111
|
+
/**
|
|
112
|
+
* Naive executor with no concurrency limits.
|
|
113
|
+
* Tasks are executed immediately and in parallel.
|
|
114
|
+
*/
|
|
115
|
+
class NaiveAsyncExecutor {
|
|
116
|
+
/**
|
|
117
|
+
* Executes a task immediately.
|
|
118
|
+
*/
|
|
119
|
+
run(task, signal) {
|
|
120
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
121
|
+
if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
|
|
122
|
+
throw abortError(signal);
|
|
123
|
+
}
|
|
124
|
+
return task(signal !== null && signal !== void 0 ? signal : noopSignal);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Executes all tasks in parallel using Promise.all.
|
|
129
|
+
*/
|
|
130
|
+
runAll(tasks, signal) {
|
|
131
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
|
|
133
|
+
throw abortError(signal);
|
|
134
|
+
}
|
|
135
|
+
return Promise.all(tasks.map((task) => {
|
|
136
|
+
return task(signal !== null && signal !== void 0 ? signal : noopSignal);
|
|
137
|
+
}));
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function abortError(signal) {
|
|
142
|
+
if (signal.reason !== undefined) {
|
|
143
|
+
return signal.reason;
|
|
144
|
+
}
|
|
145
|
+
return new Error('Aborted');
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Executes asynchronous tasks with bounded concurrency.
|
|
149
|
+
* Ensures that no more than a fixed number of tasks run in parallel.
|
|
150
|
+
*/
|
|
151
|
+
class BoundedAsyncExecutor {
|
|
152
|
+
constructor(maxConcurrent) {
|
|
153
|
+
this.activeCount = 0;
|
|
154
|
+
this.pendingCount = 0;
|
|
155
|
+
this.queue = [];
|
|
156
|
+
if (maxConcurrent <= 0) {
|
|
157
|
+
throw new Error('AsyncLimiter: maxConcurrent must be > 0');
|
|
158
|
+
}
|
|
159
|
+
this.maxConcurrent = maxConcurrent;
|
|
160
|
+
}
|
|
161
|
+
get active() {
|
|
162
|
+
return this.activeCount;
|
|
163
|
+
}
|
|
164
|
+
get pending() {
|
|
165
|
+
return this.pendingCount;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Executes a single task subject to the concurrency limit.
|
|
169
|
+
* If a signal is provided and already aborted, the task is not started.
|
|
170
|
+
*/
|
|
171
|
+
run(task, signal) {
|
|
172
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
173
|
+
if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
|
|
174
|
+
throw abortError(signal);
|
|
175
|
+
}
|
|
176
|
+
if (this.activeCount >= this.maxConcurrent) {
|
|
177
|
+
yield this.enqueue(signal);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
this.activeCount++;
|
|
181
|
+
}
|
|
182
|
+
if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
|
|
183
|
+
throw abortError(signal);
|
|
184
|
+
}
|
|
185
|
+
// create a linked controller so we always pass a signal
|
|
186
|
+
const controller = new AbortController();
|
|
187
|
+
const abortHandler = () => {
|
|
188
|
+
controller.abort(signal === null || signal === void 0 ? void 0 : signal.reason);
|
|
189
|
+
};
|
|
190
|
+
if (signal) {
|
|
191
|
+
signal.addEventListener('abort', abortHandler, { once: true });
|
|
192
|
+
}
|
|
193
|
+
try {
|
|
194
|
+
const result = yield task(controller.signal);
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
200
|
+
finally {
|
|
201
|
+
if (signal) {
|
|
202
|
+
signal.removeEventListener('abort', abortHandler);
|
|
203
|
+
}
|
|
204
|
+
this.activeCount--;
|
|
205
|
+
this.dequeue();
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Executes multiple tasks with bounded concurrency.
|
|
211
|
+
* Each task is scheduled through the shared limiter,
|
|
212
|
+
* ensuring global concurrency constraints are respected.
|
|
213
|
+
*/
|
|
214
|
+
runAll(tasks, signal) {
|
|
215
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
216
|
+
var _a;
|
|
217
|
+
if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
|
|
218
|
+
throw (_a = signal.reason) !== null && _a !== void 0 ? _a : new Error('Aborted');
|
|
219
|
+
}
|
|
220
|
+
return Promise.all(tasks.map((task) => {
|
|
221
|
+
return this.run(task, signal !== null && signal !== void 0 ? signal : noopSignal);
|
|
222
|
+
}));
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
enqueue(signal) {
|
|
226
|
+
this.pendingCount++;
|
|
227
|
+
return new Promise((resolve, reject) => {
|
|
228
|
+
const entry = {
|
|
229
|
+
resolve: () => {
|
|
230
|
+
this.pendingCount--;
|
|
231
|
+
cleanupEntry(entry);
|
|
232
|
+
resolve();
|
|
233
|
+
},
|
|
234
|
+
reject: (err) => {
|
|
235
|
+
this.pendingCount--;
|
|
236
|
+
cleanupEntry(entry);
|
|
237
|
+
reject(err);
|
|
238
|
+
},
|
|
239
|
+
signal,
|
|
240
|
+
};
|
|
241
|
+
if (signal) {
|
|
242
|
+
const onAbort = () => {
|
|
243
|
+
entry.reject(abortError(signal));
|
|
244
|
+
};
|
|
245
|
+
entry.onAbort = onAbort;
|
|
246
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
247
|
+
}
|
|
248
|
+
this.queue.push(entry);
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
dequeue() {
|
|
252
|
+
var _a;
|
|
253
|
+
while (this.queue.length > 0 && this.activeCount < this.maxConcurrent) {
|
|
254
|
+
const entry = this.queue.shift();
|
|
255
|
+
if (!entry) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
// if aborted while waiting, skip
|
|
259
|
+
if ((_a = entry.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
|
|
260
|
+
entry.reject(abortError(entry.signal));
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
entry.resolve();
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
function cleanupEntry(entry) {
|
|
269
|
+
if (entry.signal && entry.onAbort) {
|
|
270
|
+
entry.signal.removeEventListener('abort', entry.onAbort);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
class HttpClient {
|
|
275
|
+
constructor() {
|
|
276
|
+
/**
|
|
277
|
+
* Default headers to be sent with each request
|
|
278
|
+
*/
|
|
279
|
+
this.headers = {};
|
|
280
|
+
this.disposed = false;
|
|
281
|
+
}
|
|
282
|
+
dispose() {
|
|
283
|
+
this.disposed = true;
|
|
284
|
+
}
|
|
285
|
+
fetch(url, options) {
|
|
286
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
287
|
+
var _a;
|
|
288
|
+
const requestInit = this.createRequestInit(options);
|
|
289
|
+
const request = new Request(url, requestInit);
|
|
290
|
+
const response = yield this.request(request);
|
|
291
|
+
if (!this.isSuccess(response)) {
|
|
292
|
+
throw new Error(`HTTP request failed: ${response.status} ${response.statusText}`);
|
|
293
|
+
}
|
|
294
|
+
const contentType = response.headers.get('Content-Type') || '';
|
|
295
|
+
const content = yield this.read(response, options === null || options === void 0 ? void 0 : options.signal);
|
|
296
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
|
|
297
|
+
throw new Error('HTTP request was aborted');
|
|
298
|
+
}
|
|
299
|
+
const body = this.decode(options.responseType, content, contentType);
|
|
300
|
+
return {
|
|
301
|
+
body: body,
|
|
302
|
+
status: response.status,
|
|
303
|
+
statusText: response.statusText,
|
|
304
|
+
headers: response.headers,
|
|
305
|
+
contentType: contentType,
|
|
306
|
+
content: content,
|
|
307
|
+
};
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
request(request) {
|
|
311
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
312
|
+
if (!this.cacheName) {
|
|
313
|
+
return this.sendRequest(request);
|
|
314
|
+
}
|
|
315
|
+
const cache = yield caches.open(this.cacheName);
|
|
316
|
+
const cachedResponse = yield cache.match(request, this.cacheOptions);
|
|
317
|
+
if (cachedResponse) {
|
|
318
|
+
return cachedResponse;
|
|
319
|
+
}
|
|
320
|
+
const response = yield this.sendRequest(request);
|
|
321
|
+
if (this.isSuccess(response)) {
|
|
322
|
+
yield cache.put(request, response.clone());
|
|
323
|
+
}
|
|
324
|
+
return response;
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
sendRequest(request) {
|
|
328
|
+
return fetch(request);
|
|
329
|
+
}
|
|
330
|
+
isSuccess(response) {
|
|
331
|
+
return response.status >= 200 && response.status < 300;
|
|
332
|
+
}
|
|
333
|
+
createRequestInit(options) {
|
|
334
|
+
const headers = {};
|
|
335
|
+
if (this.headers) {
|
|
336
|
+
for (const key in this.headers) {
|
|
337
|
+
headers[key] = this.headers[key];
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (options.headers) {
|
|
341
|
+
for (const key in options.headers) {
|
|
342
|
+
headers[key] = options.headers[key];
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (!('Accept' in headers)) {
|
|
346
|
+
headers['Accept'] = `application/json, text/plain, */*`;
|
|
347
|
+
}
|
|
348
|
+
return {
|
|
349
|
+
method: options.method || 'GET',
|
|
350
|
+
headers: headers,
|
|
351
|
+
credentials: options.withCredentials ? 'include' : undefined,
|
|
352
|
+
signal: options.signal,
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
read(response, signal) {
|
|
356
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
357
|
+
if (!response.body) {
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
const chunks = [];
|
|
361
|
+
const reader = response.body.getReader();
|
|
362
|
+
let receivedLength = 0;
|
|
363
|
+
let canceled = false;
|
|
364
|
+
let done = false;
|
|
365
|
+
while (!done && !canceled) {
|
|
366
|
+
if (this.disposed || (signal === null || signal === void 0 ? void 0 : signal.aborted)) {
|
|
367
|
+
yield reader.cancel();
|
|
368
|
+
canceled = true;
|
|
369
|
+
break;
|
|
370
|
+
}
|
|
371
|
+
const chunk = yield reader.read();
|
|
372
|
+
if (chunk.done) {
|
|
373
|
+
done = true;
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
chunks.push(chunk.value);
|
|
377
|
+
receivedLength += chunk.value.length;
|
|
378
|
+
// TODO: report progress
|
|
379
|
+
}
|
|
380
|
+
return concatChunks(chunks, receivedLength);
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
decode(type, content, contentType) {
|
|
384
|
+
switch (type) {
|
|
385
|
+
case 'json': {
|
|
386
|
+
const text = new TextDecoder().decode(content);
|
|
387
|
+
return text === '' ? null : JSON.parse(text);
|
|
388
|
+
}
|
|
389
|
+
case 'text':
|
|
390
|
+
return new TextDecoder().decode(content);
|
|
391
|
+
case 'blob':
|
|
392
|
+
return new Blob([content], { type: contentType });
|
|
393
|
+
case 'arraybuffer':
|
|
394
|
+
return content.buffer;
|
|
395
|
+
}
|
|
396
|
+
return null;
|
|
397
|
+
}
|
|
398
|
+
installCache(name) {
|
|
399
|
+
caches.open(name);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
function concatChunks(chunks, totalLength) {
|
|
403
|
+
const chunksAll = new Uint8Array(totalLength);
|
|
404
|
+
let position = 0;
|
|
405
|
+
for (const chunk of chunks) {
|
|
406
|
+
chunksAll.set(chunk, position);
|
|
407
|
+
position += chunk.length;
|
|
408
|
+
}
|
|
409
|
+
return chunksAll;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* The default {@link AssetCacheKey}: the url, and the asset index if it is not 0
|
|
414
|
+
*/
|
|
415
|
+
const defaultAssetCacheKey = (_type, url, options) => {
|
|
416
|
+
return (options === null || options === void 0 ? void 0 : options.index) ? `${url}#${options.index}` : url;
|
|
417
|
+
};
|
|
418
|
+
/**
|
|
419
|
+
* The default {@link UriResolver}: resolves `assetUri` like a browser resolves a link,
|
|
420
|
+
* with `parentUri` as the document URL and `baseUri` as its `<base href>`
|
|
421
|
+
*/
|
|
422
|
+
const defaultUriResolver = (assetUri, { parentUri, baseUri }) => {
|
|
423
|
+
let base = parentUri || '';
|
|
424
|
+
if (baseUri) {
|
|
425
|
+
const isDirectory = baseUri.endsWith('/');
|
|
426
|
+
base = mergeUri(base, baseUri);
|
|
427
|
+
// mergeUri drops the trailing slash, but it decides which directory the base points to
|
|
428
|
+
if (isDirectory && !base.endsWith('/')) {
|
|
429
|
+
base += '/';
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return mergeUri(base, assetUri);
|
|
433
|
+
};
|
|
434
|
+
class ContentLoader {
|
|
435
|
+
/**
|
|
436
|
+
* Registers a loader type
|
|
437
|
+
*/
|
|
438
|
+
static registerLoader(descriptor) {
|
|
439
|
+
this.loaders.register(descriptor);
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Registers a creator function for all content loaders
|
|
443
|
+
*/
|
|
444
|
+
static registerCreator(type, factory) {
|
|
445
|
+
this.creators.set(type, factory);
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Registers a loader type
|
|
449
|
+
*/
|
|
450
|
+
registerLoader(descriptor) {
|
|
451
|
+
this.loaders.register(descriptor);
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Registeres a creator function
|
|
455
|
+
*/
|
|
456
|
+
registerCreator(type, factory) {
|
|
457
|
+
this.creators.set(type, factory);
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Resolves an asset URI to the URL that is loaded
|
|
461
|
+
*/
|
|
462
|
+
resolveUri(assetUri, options) {
|
|
463
|
+
return this.uriResolver(assetUri, options || {});
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Number of cached containers and asset instances
|
|
467
|
+
*/
|
|
468
|
+
get cacheStats() {
|
|
469
|
+
let assets = 0;
|
|
470
|
+
for (const cache of this.assetCache.values()) {
|
|
471
|
+
assets += cache.size;
|
|
472
|
+
}
|
|
473
|
+
return { containers: this.containerCache.size, assets };
|
|
474
|
+
}
|
|
475
|
+
constructor(device, options) {
|
|
476
|
+
/**
|
|
477
|
+
* Asset creator registry that takes precedence over the static {@link ContentLoader.creators}
|
|
478
|
+
*/
|
|
479
|
+
this.creators = new Map();
|
|
480
|
+
this.disableCache = false;
|
|
481
|
+
this.containerCache = new Map();
|
|
482
|
+
this.assetCache = new Map();
|
|
483
|
+
/**
|
|
484
|
+
* Shorthand for `create(AssetType.Model, options)`
|
|
485
|
+
*/
|
|
486
|
+
this.createModel = (data) => {
|
|
487
|
+
return this.create(AssetType.Model, data);
|
|
488
|
+
};
|
|
489
|
+
/**
|
|
490
|
+
* Shorthand for `create(AssetType.Material, options)`
|
|
491
|
+
*/
|
|
492
|
+
this.createMaterial = (options) => {
|
|
493
|
+
return this.create(AssetType.Material, options);
|
|
494
|
+
};
|
|
495
|
+
/**
|
|
496
|
+
* Shorthand for `create(AssetType.Texture, options)`
|
|
497
|
+
*/
|
|
498
|
+
this.createTexture = (options) => {
|
|
499
|
+
return this.create(AssetType.Texture, options);
|
|
500
|
+
};
|
|
501
|
+
this.device = device;
|
|
502
|
+
this.http = (options === null || options === void 0 ? void 0 : options.http) || new HttpClient();
|
|
503
|
+
this.loaders = (options === null || options === void 0 ? void 0 : options.registry) || new AssetLoaderRegistry();
|
|
504
|
+
this.executor = (options === null || options === void 0 ? void 0 : options.executor) || new NaiveAsyncExecutor();
|
|
505
|
+
this.disableCache = !!(options === null || options === void 0 ? void 0 : options.disableCache);
|
|
506
|
+
this.cacheKey = (options === null || options === void 0 ? void 0 : options.cacheKey) || defaultAssetCacheKey;
|
|
507
|
+
this.uriResolver = (options === null || options === void 0 ? void 0 : options.resolveUri) || defaultUriResolver;
|
|
508
|
+
}
|
|
509
|
+
fetch(url, options) {
|
|
510
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
511
|
+
return this.executor.run(() => this.http.fetch(url, options), options === null || options === void 0 ? void 0 : options.signal);
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Loads an asset container from the given URL.
|
|
516
|
+
*
|
|
517
|
+
* @remarks
|
|
518
|
+
* Loads asset data from URL. Uses the `url` as cache key.
|
|
519
|
+
* First caller always populates the cache. Repeating calls with
|
|
520
|
+
* different `options.type` don't change the result.
|
|
521
|
+
*/
|
|
522
|
+
loadContainer(url, options) {
|
|
523
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
524
|
+
url = this.resolveUri(url, { baseUri: options === null || options === void 0 ? void 0 : options.baseUrl });
|
|
525
|
+
if (!this.disableCache && this.containerCache.has(url)) {
|
|
526
|
+
return this.containerCache.get(url);
|
|
527
|
+
}
|
|
528
|
+
const promise = this.resolveLoader(url, options === null || options === void 0 ? void 0 : options.type).then((loader) => {
|
|
529
|
+
if (!loader) {
|
|
530
|
+
throw new Error(`No loader found for URL: ${url}`);
|
|
531
|
+
}
|
|
532
|
+
return loader.load(url, Object.assign(Object.assign({}, (options || {})), { content: this }));
|
|
533
|
+
});
|
|
534
|
+
if (!this.disableCache) {
|
|
535
|
+
this.containerCache.set(url, promise);
|
|
536
|
+
}
|
|
537
|
+
return promise;
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Loads an asset from the container at the given URL and creates an instance.
|
|
542
|
+
*
|
|
543
|
+
* @remarks
|
|
544
|
+
* Loads the asset at `options.index` (default 0) of the container. The instance is cached,
|
|
545
|
+
* the key is given by the `cacheKey` option of the loader (url and index by default).
|
|
546
|
+
* Repeating calls with the same key return the same instance.
|
|
547
|
+
*/
|
|
548
|
+
load(type, url, options) {
|
|
549
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
550
|
+
if (!this.assetCache.has(type)) {
|
|
551
|
+
this.assetCache.set(type, new Map());
|
|
552
|
+
}
|
|
553
|
+
const _a = options || {}, { index, scope } = _a, context = __rest(_a, ["index", "scope"]);
|
|
554
|
+
const cache = this.assetCache.get(type);
|
|
555
|
+
const key = this.cacheKey(type, url, options);
|
|
556
|
+
if (!this.disableCache && cache.has(key)) {
|
|
557
|
+
return cache.get(key).then((instance) => {
|
|
558
|
+
if (isOwnable(instance) && instance.ownership.isFinalized) {
|
|
559
|
+
// finalized while the request was pending, the entry is evicted by now
|
|
560
|
+
return this.load(type, url, options);
|
|
561
|
+
}
|
|
562
|
+
return ownInstance(instance, scope, this.device);
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
const promise = this.loadContainer(url, context)
|
|
566
|
+
.then((container) => {
|
|
567
|
+
return container.load(type, index !== null && index !== void 0 ? index : 0, Object.assign(Object.assign({}, context), { content: this }));
|
|
568
|
+
})
|
|
569
|
+
.then((options) => {
|
|
570
|
+
const instance = this.create(type, options);
|
|
571
|
+
if (!this.disableCache && isOwnable(instance)) {
|
|
572
|
+
instance.ownership.onFinalize(() => {
|
|
573
|
+
if (cache.get(key) === promise) {
|
|
574
|
+
cache.delete(key);
|
|
575
|
+
}
|
|
576
|
+
// the container may have released its data after creating the instance, load the file again next time
|
|
577
|
+
this.containerCache.delete(this.resolveUri(url, { baseUri: context.baseUrl }));
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
return instance;
|
|
581
|
+
});
|
|
582
|
+
if (!this.disableCache) {
|
|
583
|
+
cache.set(key, promise);
|
|
584
|
+
}
|
|
585
|
+
return promise.then((instance) => ownInstance(instance, scope, this.device));
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Creates a new asset instance.
|
|
590
|
+
*
|
|
591
|
+
* @remarks
|
|
592
|
+
*
|
|
593
|
+
*/
|
|
594
|
+
create(type, options) {
|
|
595
|
+
const factory = this.findCreator(type);
|
|
596
|
+
if (!factory) {
|
|
597
|
+
throw new Error(`Factory not found for asset type: ${type}`);
|
|
598
|
+
}
|
|
599
|
+
return factory(this, options);
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Shorthand for `load(AssetType.Texture, options)`
|
|
603
|
+
*/
|
|
604
|
+
loadTexture(url, options) {
|
|
605
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
606
|
+
return this.load(AssetType.Texture, url, options);
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Shorthand for `load(AssetType.Material, options)`
|
|
611
|
+
*/
|
|
612
|
+
loadMaterial(url, options) {
|
|
613
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
614
|
+
return this.load(AssetType.Material, url, options);
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Shorthand for `load(AssetType.Model, options)`
|
|
619
|
+
*/
|
|
620
|
+
loadModel(url, options) {
|
|
621
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
622
|
+
return this.load(AssetType.Model, url, options);
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Creates a loader for the given URL.
|
|
627
|
+
*
|
|
628
|
+
* @remarks
|
|
629
|
+
* If no loader can be found from the URL or options, this method sends
|
|
630
|
+
* a HEAD request to the URL. The method then uses the returned content type to try again.
|
|
631
|
+
*/
|
|
632
|
+
resolveLoader(url, type) {
|
|
633
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
634
|
+
let factory = null;
|
|
635
|
+
if (type) {
|
|
636
|
+
factory = this.findLoader(type);
|
|
637
|
+
}
|
|
638
|
+
if (!factory) {
|
|
639
|
+
const ext = extname(url).toLowerCase();
|
|
640
|
+
factory = this.findLoader(ext);
|
|
641
|
+
}
|
|
642
|
+
if (!factory) {
|
|
643
|
+
const res = yield this.http.fetch(url, { method: 'HEAD' }).catch((err) => {
|
|
644
|
+
// ignore as it's just unable to determine the content type
|
|
645
|
+
// failed request is visible in the network tab
|
|
646
|
+
});
|
|
647
|
+
if (res) {
|
|
648
|
+
factory = this.findLoader(res.contentType);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
if (factory) {
|
|
652
|
+
return yield factory(this);
|
|
653
|
+
}
|
|
654
|
+
throw new Error(`No loader found for URL: ${url} with type: ${type || 'unknown'}`);
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
findLoader(type) {
|
|
658
|
+
if (!type) {
|
|
659
|
+
return null;
|
|
660
|
+
}
|
|
661
|
+
return this.loaders.find(type) || ContentLoader.loaders.find(type);
|
|
662
|
+
}
|
|
663
|
+
findCreator(type) {
|
|
664
|
+
if (this.creators.has(type)) {
|
|
665
|
+
return this.creators.get(type);
|
|
666
|
+
}
|
|
667
|
+
if (ContentLoader.creators.has(type)) {
|
|
668
|
+
return ContentLoader.creators.get(type);
|
|
669
|
+
}
|
|
670
|
+
switch (type) {
|
|
671
|
+
case AssetType.Material: {
|
|
672
|
+
return createMaterial;
|
|
673
|
+
}
|
|
674
|
+
case AssetType.Texture: {
|
|
675
|
+
return createTexture;
|
|
676
|
+
}
|
|
677
|
+
case AssetType.Model: {
|
|
678
|
+
return createModel;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
return null;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Registry for loader types
|
|
686
|
+
*/
|
|
687
|
+
ContentLoader.loaders = new AssetLoaderRegistry();
|
|
688
|
+
/**
|
|
689
|
+
* Registry for asset creators
|
|
690
|
+
*/
|
|
691
|
+
ContentLoader.creators = new Map();
|
|
692
|
+
const createModel = (content, data) => {
|
|
693
|
+
var _a;
|
|
694
|
+
return new Model(content.device, Object.assign(Object.assign({}, data), { meshes: (_a = data.meshes) === null || _a === void 0 ? void 0 : _a.map((mesh) => {
|
|
695
|
+
var _a;
|
|
696
|
+
if (mesh instanceof Mesh) {
|
|
697
|
+
return mesh;
|
|
698
|
+
}
|
|
699
|
+
return Object.assign(Object.assign({}, mesh), {
|
|
700
|
+
// meshes create (and own) their materials, routed through the creator registry of the content loader
|
|
701
|
+
materials: (_a = mesh.materials) === null || _a === void 0 ? void 0 : _a.map((it) => {
|
|
702
|
+
if (it instanceof Material) {
|
|
703
|
+
return it;
|
|
704
|
+
}
|
|
705
|
+
// the wrapper replaces `factory` on the copy only, `it` keeps its original factory, so this does not recurse
|
|
706
|
+
return Object.assign(Object.assign({}, it), { factory: () => content.create(AssetType.Material, it) });
|
|
707
|
+
}) });
|
|
708
|
+
}) }));
|
|
709
|
+
};
|
|
710
|
+
function ownInstance(instance, scope, device) {
|
|
711
|
+
if (!scope || !isOwnable(instance)) {
|
|
712
|
+
return instance;
|
|
713
|
+
}
|
|
714
|
+
if (scope.isDisposed) {
|
|
715
|
+
// the scope was disposed while loading, an instance nobody claimed would stay alive with the cache
|
|
716
|
+
if (!instance.ownership.isClaimed) {
|
|
717
|
+
device.scope.drop(instance);
|
|
718
|
+
}
|
|
719
|
+
throw new Error(`scope '${scope.name}' was disposed while loading`);
|
|
720
|
+
}
|
|
721
|
+
return scope.adopt(instance);
|
|
722
|
+
}
|
|
723
|
+
const createMaterial = (content, options) => {
|
|
724
|
+
if (!options) {
|
|
725
|
+
throw new Error('Material options are required');
|
|
726
|
+
}
|
|
727
|
+
if (options instanceof Material) {
|
|
728
|
+
return options;
|
|
729
|
+
}
|
|
730
|
+
if (options.effect) {
|
|
731
|
+
return new Material(null, options);
|
|
732
|
+
}
|
|
733
|
+
if (options.factory) {
|
|
734
|
+
return options.factory(content.device, options);
|
|
735
|
+
}
|
|
736
|
+
return new BasicMaterial(content.device, options);
|
|
737
|
+
};
|
|
738
|
+
function createTexture(content, options) {
|
|
739
|
+
if (isAquirableTextureOptions(options)) {
|
|
740
|
+
return content.device.acquireTexture(options);
|
|
741
|
+
}
|
|
742
|
+
return content.device.createTexture(options);
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
// for explanation see:
|
|
746
|
+
// https://www.debuggex.com/
|
|
747
|
+
const regex = /(([a-z0-9-]+)\/([a-z0-9+-]+))?((;\s*([a-z0-9-]+=)?(([a-z0-9]+)|("[^"]*")))+)?/;
|
|
748
|
+
/**
|
|
749
|
+
* Describes components of a mime type
|
|
750
|
+
*
|
|
751
|
+
* @public
|
|
752
|
+
*/
|
|
753
|
+
class ContentType {
|
|
754
|
+
static parse(input) {
|
|
755
|
+
const match = regex.exec(input) || [];
|
|
756
|
+
return match ? {
|
|
757
|
+
mimeType: match[1],
|
|
758
|
+
mediaType: match[2],
|
|
759
|
+
subType: match[3],
|
|
760
|
+
params: (match[4] || ''),
|
|
761
|
+
} : null;
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
class MaterialRegistry {
|
|
766
|
+
constructor() {
|
|
767
|
+
this.index = [];
|
|
768
|
+
}
|
|
769
|
+
register(spec, match) {
|
|
770
|
+
if (!match) {
|
|
771
|
+
this.index.push(spec);
|
|
772
|
+
return;
|
|
773
|
+
}
|
|
774
|
+
this.index.push({
|
|
775
|
+
match,
|
|
776
|
+
create: (device, asset) => new spec(device, asset),
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
find(asset) {
|
|
780
|
+
for (const entry of this.index) {
|
|
781
|
+
if (entry.match(asset)) {
|
|
782
|
+
return entry;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
return null;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
class TextureLoader {
|
|
790
|
+
static register(registry = ContentLoader.loaders) {
|
|
791
|
+
registry.register(TextureLoader);
|
|
792
|
+
}
|
|
793
|
+
get supportsImageBitmap() {
|
|
794
|
+
return typeof createImageBitmap === 'function';
|
|
795
|
+
}
|
|
796
|
+
load(url, context) {
|
|
797
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
798
|
+
var _a;
|
|
799
|
+
(_a = context.signal) === null || _a === void 0 ? void 0 : _a.throwIfAborted();
|
|
800
|
+
if (this.supportsImageBitmap) {
|
|
801
|
+
return this.loadWithImageBitmap(url, context);
|
|
802
|
+
}
|
|
803
|
+
return this.loadWithImageElement(url, context);
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
loadWithImageBitmap(url, context) {
|
|
807
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
808
|
+
var _a, _b;
|
|
809
|
+
const response = yield context.content.fetch(url, {
|
|
810
|
+
responseType: 'blob',
|
|
811
|
+
signal: context.signal,
|
|
812
|
+
});
|
|
813
|
+
(_a = context.signal) === null || _a === void 0 ? void 0 : _a.throwIfAborted();
|
|
814
|
+
const bitmap = yield createImageBitmap(response.body, createImageBitmapOptions());
|
|
815
|
+
(_b = context.signal) === null || _b === void 0 ? void 0 : _b.throwIfAborted();
|
|
816
|
+
const texture = {
|
|
817
|
+
key: url,
|
|
818
|
+
type: '2d',
|
|
819
|
+
source: createTextureSource(bitmap),
|
|
820
|
+
width: bitmap.width,
|
|
821
|
+
height: bitmap.height,
|
|
822
|
+
generateMipmap: true,
|
|
823
|
+
format: context.color === 'srgb' ? 'rgba8unorm-srgb' : 'rgba8unorm',
|
|
824
|
+
};
|
|
825
|
+
return new TextureAssetContainer([texture]);
|
|
826
|
+
});
|
|
827
|
+
}
|
|
828
|
+
loadWithImageElement(url, context) {
|
|
829
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
830
|
+
var _a;
|
|
831
|
+
var _b;
|
|
832
|
+
const image = new Image();
|
|
833
|
+
image.crossOrigin = (_b = context === null || context === void 0 ? void 0 : context.crossOrigin) !== null && _b !== void 0 ? _b : Texture.crossOrigin;
|
|
834
|
+
image.src = url;
|
|
835
|
+
yield image.decode();
|
|
836
|
+
(_a = context.signal) === null || _a === void 0 ? void 0 : _a.throwIfAborted();
|
|
837
|
+
const texture = {
|
|
838
|
+
key: url,
|
|
839
|
+
type: '2d',
|
|
840
|
+
source: createTextureSource(image),
|
|
841
|
+
width: image.naturalWidth,
|
|
842
|
+
height: image.naturalHeight,
|
|
843
|
+
generateMipmap: true,
|
|
844
|
+
format: context.color === 'srgb' ? 'rgba8unorm-srgb' : 'rgba8unorm',
|
|
845
|
+
};
|
|
846
|
+
return new TextureAssetContainer([texture]);
|
|
847
|
+
});
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
TextureLoader.extensions = ['.bmp', '.gif', '.jpg', '.jpeg', '.png', '.webp'];
|
|
851
|
+
TextureLoader.mimeTypes = ['image/jpg', 'image/jpeg', 'image/png', 'image/webp'];
|
|
852
|
+
TextureLoader.create = () => new TextureLoader();
|
|
853
|
+
TextureLoader.register();
|
|
854
|
+
|
|
855
|
+
class ResourceGraph {
|
|
856
|
+
constructor() {
|
|
857
|
+
this.graph = new Map();
|
|
858
|
+
this.results = new Map();
|
|
859
|
+
this.inflight = new Map();
|
|
860
|
+
}
|
|
861
|
+
/**
|
|
862
|
+
* Checks if a resource node with the given key exists in the graph
|
|
863
|
+
*/
|
|
864
|
+
has(key) {
|
|
865
|
+
return this.graph.has(key);
|
|
866
|
+
}
|
|
867
|
+
/**
|
|
868
|
+
* Gets the resource node associated with the given key, or undefined if it does not exist in the graph
|
|
869
|
+
*/
|
|
870
|
+
get(key) {
|
|
871
|
+
return this.graph.get(key);
|
|
872
|
+
}
|
|
873
|
+
/**
|
|
874
|
+
* Adds a resource node with the given key and optional data to the graph.
|
|
875
|
+
* The key must be unique within the graph, otherwise an error will be thrown.
|
|
876
|
+
*/
|
|
877
|
+
node(key, data) {
|
|
878
|
+
if (this.graph.has(key)) {
|
|
879
|
+
throw new Error(`Resource node with key ${key} already exists in the graph`);
|
|
880
|
+
}
|
|
881
|
+
const node = {
|
|
882
|
+
key,
|
|
883
|
+
data: data,
|
|
884
|
+
deps: [],
|
|
885
|
+
};
|
|
886
|
+
this.graph.set(node.key, node);
|
|
887
|
+
return node;
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
* Adds a dependency from the given node to the given dependency node, with an optional assignment
|
|
891
|
+
* function that will be called with the resolved value of the dependency once it is available.
|
|
892
|
+
*
|
|
893
|
+
* @returns the key of the dependency node for convenience.
|
|
894
|
+
*/
|
|
895
|
+
assign(node, dep, assign) {
|
|
896
|
+
const ref = dep.key;
|
|
897
|
+
node.deps.push({
|
|
898
|
+
ref,
|
|
899
|
+
assign,
|
|
900
|
+
});
|
|
901
|
+
return ref;
|
|
902
|
+
}
|
|
903
|
+
/**
|
|
904
|
+
* Adds a dependency from the given node to the given dependency node without an assignment function.
|
|
905
|
+
* This is useful when the target node will access the dependency's resolved value through the getResult
|
|
906
|
+
* function in its resolve method, rather than having it assigned directly.
|
|
907
|
+
*/
|
|
908
|
+
dependency(node, dep) {
|
|
909
|
+
return this.assign(node, dep, null);
|
|
910
|
+
}
|
|
911
|
+
getOrderedSubsetForNode(node) {
|
|
912
|
+
const subset = collectSubset(this.graph, node);
|
|
913
|
+
return toSortedSubset(this.graph, subset);
|
|
914
|
+
}
|
|
915
|
+
load(target, context) {
|
|
916
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
917
|
+
if (target == null) {
|
|
918
|
+
throw new Error('Target node is null or undefined');
|
|
919
|
+
}
|
|
920
|
+
if (!this.graph.has(target.key)) {
|
|
921
|
+
throw new Error(`Target node with key ${target.key} does not exist in the graph`);
|
|
922
|
+
}
|
|
923
|
+
const graph = this.graph;
|
|
924
|
+
const results = this.results;
|
|
925
|
+
const inflight = this.inflight;
|
|
926
|
+
const executor = context.content.executor;
|
|
927
|
+
const order = this.getOrderedSubsetForNode(target);
|
|
928
|
+
const getResult = (key) => {
|
|
929
|
+
if (results.has(key)) {
|
|
930
|
+
return results.get(key);
|
|
931
|
+
}
|
|
932
|
+
throw new Error(`Dependency ${key} has not been resolved yet`);
|
|
933
|
+
};
|
|
934
|
+
const finalize = (node, value) => {
|
|
935
|
+
for (const dep of node.deps) {
|
|
936
|
+
if (dep.assign) {
|
|
937
|
+
dep.assign(value, results.get(dep.ref));
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
results.set(node.key, value);
|
|
941
|
+
};
|
|
942
|
+
for (const node of order) {
|
|
943
|
+
if (results.has(node.key)) {
|
|
944
|
+
continue;
|
|
945
|
+
}
|
|
946
|
+
if (inflight.has(node.key)) {
|
|
947
|
+
// wait for concurrent execution
|
|
948
|
+
yield inflight.get(node.key);
|
|
949
|
+
continue;
|
|
950
|
+
}
|
|
951
|
+
// sanity: all deps must exist in graph
|
|
952
|
+
for (const dep of node.deps) {
|
|
953
|
+
if (!graph.has(dep.ref)) {
|
|
954
|
+
throw new Error(`Node ${node.key} depends on missing ${dep.ref}`);
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
if (!node.build && !node.buildAsync) {
|
|
958
|
+
const result = node.data;
|
|
959
|
+
finalize(node, result);
|
|
960
|
+
continue;
|
|
961
|
+
}
|
|
962
|
+
if (node.build) {
|
|
963
|
+
const result = node.build(context, node, getResult);
|
|
964
|
+
if (isThenable(result)) {
|
|
965
|
+
throw new Error(`build function for node ${node.key} returned a Promise, but should return synchronously`);
|
|
966
|
+
}
|
|
967
|
+
finalize(node, result);
|
|
968
|
+
continue;
|
|
969
|
+
}
|
|
970
|
+
// async result - defer assignment until resolution
|
|
971
|
+
const promise = executor.run(() => __awaiter(this, void 0, void 0, function* () {
|
|
972
|
+
const result = yield node.buildAsync(context, node, getResult);
|
|
973
|
+
finalize(node, result);
|
|
974
|
+
}), context.signal);
|
|
975
|
+
inflight.set(node.key, promise);
|
|
976
|
+
try {
|
|
977
|
+
yield promise;
|
|
978
|
+
}
|
|
979
|
+
finally {
|
|
980
|
+
inflight.delete(node.key);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
return results.get(target.key);
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
function collectSubset(graph, node) {
|
|
988
|
+
const result = new Set();
|
|
989
|
+
const visit = (key) => {
|
|
990
|
+
if (result.has(key))
|
|
991
|
+
return;
|
|
992
|
+
const node = graph.get(key);
|
|
993
|
+
if (!node) {
|
|
994
|
+
throw new Error(`Missing node: ${key}`);
|
|
995
|
+
}
|
|
996
|
+
result.add(key);
|
|
997
|
+
for (const dep of node.deps) {
|
|
998
|
+
visit(dep.ref);
|
|
999
|
+
}
|
|
1000
|
+
};
|
|
1001
|
+
visit(node.key);
|
|
1002
|
+
return result;
|
|
1003
|
+
}
|
|
1004
|
+
function toSortedSubset(graph, subset) {
|
|
1005
|
+
const inDegree = new Map();
|
|
1006
|
+
for (const key of subset) {
|
|
1007
|
+
inDegree.set(key, 0);
|
|
1008
|
+
}
|
|
1009
|
+
for (const key of subset) {
|
|
1010
|
+
const node = graph.get(key);
|
|
1011
|
+
for (const dep of node.deps) {
|
|
1012
|
+
if (subset.has(dep.ref)) {
|
|
1013
|
+
inDegree.set(key, inDegree.get(key) + 1);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
const queue = [];
|
|
1018
|
+
for (const key of subset) {
|
|
1019
|
+
if (inDegree.get(key) === 0) {
|
|
1020
|
+
queue.push(graph.get(key));
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
const order = [];
|
|
1024
|
+
while (queue.length) {
|
|
1025
|
+
const n = queue.shift();
|
|
1026
|
+
order.push(n);
|
|
1027
|
+
for (const key of subset) {
|
|
1028
|
+
const m = graph.get(key);
|
|
1029
|
+
for (const dep of m.deps) {
|
|
1030
|
+
if (dep.ref === n.key) {
|
|
1031
|
+
const d = inDegree.get(key) - 1;
|
|
1032
|
+
inDegree.set(key, d);
|
|
1033
|
+
if (d === 0) {
|
|
1034
|
+
queue.push(m);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
if (order.length !== subset.size) {
|
|
1041
|
+
throw new Error('Cycle detected in resource graph');
|
|
1042
|
+
}
|
|
1043
|
+
return order;
|
|
1044
|
+
}
|
|
1045
|
+
function isThenable(x) {
|
|
1046
|
+
return x && typeof x.then === 'function';
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
export { AssetLoaderRegistry, AssetType, BoundedAsyncExecutor, ContentLoader, ContentType, MaterialRegistry, NaiveAsyncExecutor, ResourceGraph, TextureAssetContainer, TextureLoader, createMaterial, createModel, createTexture, defaultAssetCacheKey, defaultUriResolver };
|
|
1050
|
+
//# sourceMappingURL=content.esm.js.map
|