Debian Codegraph @

web/filament-js/wasmloader.js

raw
1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /// init ::function:: Downloads assets, loads the Filament module, and invokes a callback when done.
18 ///
19 /// All JavaScript clients must call the init function, passing in a list of asset URL's and a
20 /// callback. This callback gets invoked only after all assets have been downloaded and the Filament
21 /// WebAssembly module has been loaded. Clients should only pass asset URL's that absolutely must
22 /// be ready at initialization time.
23 ///
24 /// When the callback is called, each downloaded asset is available in the `Filament.assets` global
25 /// object, which contains a mapping from URL's to Uint8Array objects.
26 ///
27 /// assets ::argument:: Array of strings containing URL's of required assets.
28 /// onready ::argument:: callback that gets invoked after all assets have been downloaded and the \
29 /// Filament WebAssembly module has been loaded.
30 Filament.init = (assets, onready) => {
31 onready = onready || (() => {});
32 Filament.assets = {};
33
34 // Usage of glmatrix is optional. If it exists, then go ahead and augment it with some
35 // useful math functions.
36 if (typeof glMatrix !== 'undefined') {
37 Filament.loadMathExtensions();
38 }
39
40 // One task for compiling & loading the wasm file, plus one task for each asset.
41 let remainingTasks = 1 + assets.length;
42 const taskFinished = () => {
43 if (--remainingTasks == 0) {
44 onready();
45 }
46 };
47
48 // Issue a fetch for each asset.
49 Filament.fetch(assets, null, taskFinished);
50
51 // Emscripten creates a global function called "Filament" that returns a promise that
52 // resolves to a module. Here we replace the function with the module. Note that our
53 // TypeScript bindings assume that Filament is a namespace, not a function.
54 Filament().then(module => {
55 Filament = Object.assign(module, Filament);
56
57 // At this point, emscripten has finished compiling and instancing the WebAssembly module.
58 // The JS classes that correspond to core Filament classes (e.g., Engine) are not guaranteed
59 // to exist until now.
60
61 Filament.loadClassExtensions();
62 taskFinished();
63 });
64 };
65
66 Filament.clearAssetCache = () => {
67 for (const key in Filament.assets) delete Filament.assets[key];
68 };
69
70 /// fetch ::function:: Downloads assets and invokes a callback when done.
71 ///
72 /// This utility consumes an array of URI strings and invokes callbacks after each asset is
73 /// downloaded. Additionally, each downloaded asset becomes available in the `Filament.assets`
74 /// global object, which is a mapping from URI strings to `Uint8Array`. If desired, clients can
75 /// pre-populate entries in `Filament.assets` to circumvent HTTP requests (this should be done after
76 /// calling `Filament.init`).
77 ///
78 /// This function is used internally by `Filament.init` and `gltfio$FilamentAsset.loadResources`.
79 ///
80 /// assets ::argument:: Array of strings containing URL's of required assets.
81 /// onDone ::argument:: callback that gets invoked after all assets have been downloaded.
82 /// onFetched ::argument:: optional callback that's invoked after each asset is downloaded.
83 Filament.fetch = (assets, onDone, onFetched) => {
84 let remainingAssets = assets.length;
85 assets.forEach(name => {
86
87 // Check if a buffer already exists in case the client wishes
88 // to provide its own data rather than using a HTTP request.
89 if (Filament.assets[name]) {
90 if (onFetched) {
91 onFetched(name);
92 }
93 if (--remainingAssets === 0 && onDone) {
94 onDone();
95 }
96 } else {
97 fetch(name).then(response => {
98 if (!response.ok) {
99 throw new Error(name);
100 }
101 return response.arrayBuffer();
102 }).then(arrayBuffer => {
103 Filament.assets[name] = new Uint8Array(arrayBuffer);
104 if (onFetched) {
105 onFetched(name);
106 }
107 if (--remainingAssets === 0 && onDone) {
108 onDone();
109 }
110 });
111 }
112 });
113 };