Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(native): use FS for Android Release Mode #2980

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you using this for testing? We don't want an asymmetrical export between web and native since that can create broken behavior in isomorphic apps.

Copy link
Author

@XantreDev XantreDev Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Monkeypathing on useLayoutEffect is not really correct, because if we are using some loader with preload on top level - it throws. For example this code will thrown, because it's called before monkeypathing.

useGLTF.preload(require('sdfsdf.glb))

Maybe the good way to provide polyfills is to use polyfills on top level

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure. We'll have to polyfill at the native entrypoint.

export { createTouchEvents as events } from './native/events'
export type { GlobalRenderCallback, GlobalEffectType } from './core/loop'
export * from './core'
55 changes: 39 additions & 16 deletions packages/fiber/src/native/polyfills.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import * as THREE from 'three'
import type { Asset } from 'expo-asset'
import * as THREE from 'three'

// 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 | ((options: { from: string; to: string }) => 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 +44,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
Loading
Loading