-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathExpoModuleTextureLoader.js
77 lines (66 loc) · 1.8 KB
/
ExpoModuleTextureLoader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//@flow
import {
globalRegistry,
WebGLTextureLoaderAsyncHashCache,
} from "webgltexture-loader";
import * as AssetUtils from "expo-asset-utils";
import { Asset } from "expo-asset";
const neverEnding = new Promise(() => {});
const localAsset = (module: number) => {
const asset = Asset.fromModule(module);
return asset.downloadAsync().then(() => asset);
};
type AssetModel = {
width: number,
height: number,
uri: string,
localUri: string,
};
type M = number | { uri: string } | AssetModel;
export const loadAsset = (module: M): Promise<AssetModel> =>
typeof module === "number"
? localAsset(module)
: module.localUri
? // $FlowFixMe
Promise.resolve(module)
: AssetUtils.resolveAsync(module.uri);
class ExpoModuleTextureLoader extends WebGLTextureLoaderAsyncHashCache<M> {
objIds: WeakMap<WebGLTexture, number> = new WeakMap();
canLoad(input: any) {
return (
typeof input === "number" ||
(input && typeof input === "object" && typeof input.uri === "string")
);
}
inputHash(module: *) {
return typeof module === "number" ? module : module.uri;
}
loadNoCache(module: *) {
const { gl } = this;
let disposed = false;
const dispose = () => {
disposed = true;
};
const promise = loadAsset(module).then((asset) => {
if (disposed) return neverEnding;
const { width, height } = asset;
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
width,
height,
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
asset
);
return { texture, width, height };
});
return { promise, dispose };
}
}
globalRegistry.add(ExpoModuleTextureLoader);
export default ExpoModuleTextureLoader;