-
Notifications
You must be signed in to change notification settings - Fork 172
WebGL "shim" #13
Comments
This is a huge challenge. That said, I would love to see a |
Thanks a lot for your answer! Since I don't have experience with OpenGL, I now got a clearer picture of the work involved for pulling this off. |
@nickdima GLKit uses OpenGL ES 2.0, which is the same thing WebGL uses (though I suppose there could be a few differences.) Would this not "just" be a case of mapping the HTML 5 canvas WebGL context to the native OpenGL ES 2.0 implementation? Android and iOS both support OpenGL ES 2.0. Typed arrays can be represented by regular arrays and casted into whatever you need on the Objective-C side. Might be a bit of a performance ding though. Obviously there are considerations to make about performance across the bridge - but we'll cross that bridge when we get there, literally. |
@lwansbrough sounds like you can start making a gl-react-native's focus is to have the thiner possible layer with OpenGL ES 2 but not exposing the GL API to the JS side. I guess you will also need to wire all the "gl commands" in JS <> ObjC . And this might be a complex challenge. The WebGL API is not asynchronous by design, you will reach a lot of problem with implementing all the synchronous APIs. |
@gre Oops meant to tag you. Yeah I agree it definitely isn't the job of this library, just wanted to pick your brain a bit. Indeed the synchronous nature of these libs presents a challenge. |
@lwansbrough I posted the initial question because I was building a small game with React (just with plain CSS and HTML) and I was thinking that it would be cool to have a way to build it for both the web and mobile while taking advantage of both React and OpenGL. |
Just generally having the ability to use 3D JS libraries like ThreeJS would open up a lot of doors for development with React Native. VR and AR via cardboard come to mind. |
Sounds good |
Here is some thoughts on the way I think this could be implemented: Dedicated Thread for WebGL & OpenGLthe WebGL JavaScript could be sandboxed to its own dedicated thread. OpenGL requires you to only use a single thread to run the GL Calls, this thread would be dedicated to this job as well as executing the user's JS, that way there should not be any round trip across 2 threads for bridging the WebGL API to the OpenGL API.
The JS that runs in the WebGL sandbox can be optimised and focused on WebGL job: Native Arrays can be implemented there to reach good performance. The sandbox would also polyfill APIs like (for actually implementing the wrapper, should look at initiative like Possible APIFrom the RN JavaScript code we could define it this way: <WebGL
entry="myWebGL.js"
width={width}
height={height}
onInit={socket => { socket.onmessage = answer => ...; socket.postMessage("foo"); }}
/> ^ that now I would imagine it would be great to be able to define some "loaders", like possible in webpack so you can do also I would expect either a global variable to be available or a function to be explosed in order to get the Canvas and the WebGL Context inside the sandbox. More thoughts need to be done on this, as I might miss some use-cases. |
@gre Interesting approaches. I would hope that an API like https://github.com/Izzimach/react-three would be possible. Being able to implement WebGL stuff as a part of the another higher level API should be considered a must have for any implementation. React Three has no DOM dependency, nor does ThreeJS by the looks of it (though you do have to provide a WebGL context). At first I thought the biggest problem to overcome would be the creation of the API, but now I'm pretty sure it's handling the asynchronous nature of React Native. I wonder if there could be a way to explicitly invoke a native method synchronously (JavascriptCore supports this.) Obviously this feature would have to be implemented into React Native, but it would open the door for modules like this. |
There's a bit of discussion around such a solution happening here: facebook/react-native#2212 |
@gre https://github.com/facebook/react-native/search?&q=nativeTraceEndSection Synchronous is possible :) |
cool :) though I wouldn't recommend to block on main JS thread. I think making a dedicated JS thread for WebGL will be an important point if you need to keep 60fps on other stuff. |
Yup, will definitely be on a different thread. I may tinker with it tonight, not sure. Thanks for your help. |
Sorry, if this is a bit out of the scope of the project but I was wondering if it would be possibile to build a "shim" for React Native that would let one transparently use OpenGL as using WebGL in the browser, and how to go about doing this?
The goal would be to use libraries like pixi.js (maybe trough react-pixi) for building universal components that render both with WebGL and OpenGL.
I've posted a related question also here: pixijs/pixijs#2120
The text was updated successfully, but these errors were encountered: