UNPKG

3.45 kBJavaScriptView Raw
1import { resolveAsync } from 'expo-asset-utils';
2import resolveAsset, { stringFromAsset } from './resolveAsset';
3import { loadDaeAsync, loadObjAsync, loadMtlAsync, loadArrayBufferAsync, } from './loaders/loadModelsAsync';
4import './polyfillTextureLoader.fx';
5import { loadTextureAsync } from './loaders/loadTextureAsync';
6import { loaderClassForExtension, loaderClassForUri, } from './loaderClassForExtension';
7export async function loadBasicModelAsync(options) {
8 const { uri, onProgress, onAssetRequested, loader, LoaderClass } = options;
9 const _loader = loader || new LoaderClass();
10 if (_loader.setPath) {
11 _loader.setPath(onAssetRequested);
12 }
13 return new Promise((res, rej) => _loader.load(uri, res, onProgress, rej));
14}
15export default async function loadAsync(res, onProgress, onAssetRequested = function () { }) {
16 let urls = await resolveAsset(res);
17 if (!urls) {
18 throw new Error(`ExpoTHREE.loadAsync: Cannot parse undefined assets. Please pass valid resources for: ${res}.`);
19 }
20 const asset = urls[0];
21 let url = (await resolveAsync(asset)).localUri;
22 if (url == null) {
23 throw new Error(`ExpoTHREE.loadAsync: this asset couldn't be downloaded. Be sure that your app.json contains the correct extensions.`);
24 }
25 if (urls.length === 1) {
26 if (url.match(/\.(jpeg|jpg|gif|png)$/)) {
27 return loadTextureAsync({ asset });
28 }
29 else if (url.match(/\.dae$/i)) {
30 return loadDaeAsync({
31 asset: url,
32 onProgress,
33 onAssetRequested,
34 });
35 }
36 else if (url.match(/\.(glb|gltf)$/i)) {
37 const arrayBuffer = await loadArrayBufferAsync({ uri: url, onProgress });
38 const GLTFLoader = loaderClassForExtension('gltf');
39 const loader = new GLTFLoader();
40 return new Promise((res, rej) => loader.parse(arrayBuffer, onAssetRequested, res, rej));
41 }
42 else if (url.match(/\.json$/i)) {
43 throw new Error('loadAsync: Please use ExpoTHREE.parseAsync({json}) instead, json can be loaded in lots of different ways.');
44 }
45 else if (url.match(/\.obj$/i)) {
46 return loadObjAsync({ asset: url, onAssetRequested });
47 }
48 else if (url.match(/\.mtl$/i)) {
49 return loadMtlAsync({ asset: url, onAssetRequested });
50 }
51 else {
52 const LoaderClass = loaderClassForUri(url);
53 return loadBasicModelAsync({
54 uri: url,
55 onProgress,
56 onAssetRequested,
57 LoaderClass,
58 });
59 }
60 }
61 else if (urls.length === 2) {
62 let urlB = await stringFromAsset(urls[1]);
63 if (urlB != null) {
64 if (url.match(/\.mtl$/i) && urlB.match(/\.obj$/i)) {
65 return loadObjAsync({
66 asset: urlB,
67 mtlAsset: url,
68 onAssetRequested,
69 });
70 }
71 else if (url.match(/\.obj$/i) && urlB.match(/\.mtl$/i)) {
72 return loadObjAsync({
73 asset: url,
74 mtlAsset: urlB,
75 onAssetRequested,
76 });
77 }
78 }
79 throw new Error('Unrecognized File Type: ' + url);
80 }
81 else {
82 throw new Error('Too many arguments passed: ' + urls);
83 }
84}
85//# sourceMappingURL=loadAsync.js.map
\No newline at end of file