Skip to content

Commit

Permalink
Fixed no texture on android release build
Browse files Browse the repository at this point in the history
  • Loading branch information
XantreDev authored Aug 28, 2023
1 parent ffd953e commit 20e2b1f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"eslint-plugin-react-hooks": "^4.4.0",
"expo-asset": "^8.4.6",
"expo-gl": "^11.1.2",
"expo-file-system": "^11.1.3",
"husky": "^7.0.4",
"jest": "^29.3.1",
"jest-cli": "^27.5.1",
Expand Down
4 changes: 4 additions & 0 deletions packages/fiber/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"expo": ">=43.0",
"expo-asset": ">=8.4",
"expo-gl": ">=11.0",
"expo-file-system": ">=11.0",
"react": ">=18.0",
"react-dom": ">=18.0",
"react-native": ">=0.64",
Expand All @@ -75,6 +76,9 @@
},
"expo-gl": {
"optional": true
},
"expo-file-system": {
"optional": true
}
}
}
1 change: 1 addition & 0 deletions packages/fiber/src/native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type { ThreeEvent, Events, EventManager, ComputeFunction } from './core/e
export { createEvents } from './core/events'
export type { ObjectMap, Camera } from './core/utils'
export * from './native/Canvas'
export * from './native/polyfills'
export { createTouchEvents as events } from './native/events'
export type { GlobalRenderCallback, GlobalEffectType } from './core/loop'
export * from './core'
54 changes: 39 additions & 15 deletions packages/fiber/src/native/polyfills.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import * as THREE from 'three'
import type { Asset } from 'expo-asset'
import type { RelocationOptions } from 'expo-file-system'

// Check if expo-asset is installed (available with expo modules)
let expAsset: typeof Asset | undefined
// expo-file-system will never be installed in case if expo is not here
let copyAsync: null | ((...args: RelocationOptions) => Promise<void>) = null
let cacheDirectory: string | null = null
try {
expAsset = require('expo-asset')?.Asset
const fs = require('expo-file-system')
copyAsync = fs.copyAsync
cacheDirectory = fs.cacheDirectory
} catch (_) {}

/**
Expand Down Expand Up @@ -38,22 +45,39 @@ export function polyfills() {

// @ts-ignore
texture.isDataTexture = true

getAsset(url)
.downloadAsync()
.then((asset: Asset) => {
texture.image = {
data: asset,
width: asset.width,
height: asset.height,
const asset = getAsset(url)

;(async () => {
// this means it's android liniking
if (copyAsync && cacheDirectory && !asset.uri.includes('://')) {
const localUri = `${cacheDirectory}ExponentAsset-${asset.hash}.${asset.type}`
await copyAsync({
from: asset.uri,
to: localUri,
})
return {
data: { localUri },
asset,
}
texture.flipY = true
texture.unpackAlignment = 1
texture.needsUpdate = true

onLoad?.(texture)
})
.catch(onError)
}

const data = await asset.downloadAsync()
return {
data,
asset: data,
}
})().then(({ data, asset }) => {
texture.image = {
data,
width: asset.width,
height: asset.height,
}
texture.flipY = true
texture.unpackAlignment = 1
texture.needsUpdate = true

onLoad?.(texture)
})

return texture
}
Expand Down

0 comments on commit 20e2b1f

Please sign in to comment.