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

Does react-three-rapier support RN app? #383

Open
lsxBread opened this issue Apr 18, 2023 · 10 comments
Open

Does react-three-rapier support RN app? #383

lsxBread opened this issue Apr 18, 2023 · 10 comments
Labels
wontfix This will not be worked on

Comments

@lsxBread
Copy link

lsxBread commented Apr 18, 2023

Hi, I am trying to use this lib together with @react-three/fiber & @react-three/drei in the Expo project
"expo": "47.0.13"
"react-native": "0.70.5"

That's the demo in the docs I followed:

import { Box, Torus } from "@react-three/drei/native";
import { Canvas } from "@react-three/fiber/native";
import { Physics, RigidBody, Debug } from "@react-three/rapier";

const App = () => {
  return (
    <Canvas>
      <Suspense>
        <Physics debug>
          <RigidBody colliders={"hull"} restitution={2}>
            <Torus />
          </RigidBody>

          <CuboidCollider position={[0, -2, 0]} args={[20, .5, 20]} />
        </Physics>
      </Suspense>
    </Canvas>
  );
};

It works all good in browser but get below error when run it as the RN app in iOS:
MicrosoftTeams-image

Seems it doesn't expose an native module like fiber & drei does which could support RN. I don't really find any RN related stuff in the docs, so try to confirm that does rapier support RN app OR I miss some config to make it runnable?

Thanks

@isaac-mason
Copy link
Member

The issue could be that WebAssembly isn't supported in React Native?

I'm not familiar with it enough to know if that's the case though.

@isaac-mason
Copy link
Member

isaac-mason commented Apr 18, 2023

From a brief search, it seems that React Native for ios and android doesn't support WASM out of the box.

Some people have found workarounds though. Some people mention using webassemblyjs (though I can't imagine performance being good), and others say they mounted a webview in the RN app (webviews would support WASM). This was referenced: https://www.npmjs.com/package/react-native-react-bridge

If you get something working, please share! 🙂

@isaac-mason
Copy link
Member

This also looks interesting - https://github.com/cawfree/react-native-webassembly

@CodyJasonBennett
Copy link
Member

CodyJasonBennett commented Oct 17, 2023

iOS does not support JIT under the browser ban (WebKit has special permissions), but pending March 2024 EU legislation this can change.

In the meantime, no such implementation exists for WebAssembly even for Android, but it should be possible if Apple cedes ground here.

@PaulThiberville
Copy link

@lsxBread did you find a solution to make it work ? Or did you use something else ?

@wiledal wiledal added the wontfix This will not be worked on label Dec 5, 2023
@itsyoboieltr
Copy link

I hit the same error quite recently. Does anyone have some tips/examples on how to use this package with React Native?

@itsyoboieltr
Copy link

I actually found a fix for this. This is what I did to make it work in _layout.tsx using Expo Router:

if (Platform.OS !== 'web') {
  // Polyfill WebAssembly for the Rapier physics engine
  // https://github.com/pmndrs/react-three-rapier/issues/383
  const WebAssembly = require('polywasm').WebAssembly;
  globalThis.WebAssembly = WebAssembly;
}

I couldn't believe it, but it actually worked 😄

@isaac-mason
Copy link
Member

isaac-mason commented Mar 29, 2024

@itsyoboieltr thanks for sharing 🙏 would you mind sharing a demo / video? I'm curious to see what the performance is like.

@itsyoboieltr
Copy link

@itsyoboieltr thanks for sharing 🙏 would you mind sharing a demo / video? I'm curious to see what the performance is like.

The thing I am currently working on is not really computationally intense so I see no difference native vs browser. Do you have any ideas what/how should I benchmark? I can make a quick demo

@isaac-mason
Copy link
Member

isaac-mason commented Mar 29, 2024

@itsyoboieltr I was firstly just curious whether a basic simulation with a few physics bodies runs ok. Hearing that you don't see a difference between browser and native for your scene is interesting!

Looking at polywasm I see it works by translating wasm functions to javascript - which sounds like it would be very slow.

For some potentially easy comparisons, you could grab the source code of some react-three-rapier examples, e.g.
https://react-three-rapier.pmnd.rs/performance

export const PerformanceExample = () => {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

6 participants