UNPKG

expo-three

Version:

Utilities for using THREE.js with Expo

46 lines 1.86 kB
import { resolveAsync } from 'expo-asset-utils'; import { Platform, Image } from 'react-native'; import THREE from './Three'; export default class ExpoTextureLoader extends THREE.TextureLoader { load(asset, onLoad, onProgress, onError) { if (!asset) { throw new Error('ExpoTHREE.TextureLoader.load(): Cannot parse a null asset'); } const texture = new THREE.Texture(); const loader = new THREE.ImageLoader(this.manager); loader.setCrossOrigin(this.crossOrigin); loader.setPath(this.path); (async () => { const nativeAsset = await resolveAsync(asset); function parseAsset(image) { texture.image = image; texture.needsUpdate = true; if (onLoad !== undefined) { onLoad(texture); } } if (Platform.OS === 'web') { loader.load(nativeAsset.localUri, (image) => { parseAsset(image); }, onProgress, onError); } else { if (!nativeAsset.width || !nativeAsset.height) { const { width, height } = await new Promise((res, rej) => { Image.getSize(nativeAsset.localUri, (width, height) => res({ width, height }), rej); }); nativeAsset.width = width; nativeAsset.height = height; } texture['isDataTexture'] = true; // Forces passing to `gl.texImage2D(...)` verbatim parseAsset({ data: nativeAsset, width: nativeAsset.width, height: nativeAsset.height, }); } })(); return texture; } } //# sourceMappingURL=TextureLoader.js.map