So another function that we want to intercept to make WebXR work with Unity is that WebXR has its own frame buffer that it wants you to render into. This has, in the case of augmented reality, the camera feed already into it, and it wants you to render on top of that. Unity is trying to render on top of the canvas that's on your screen, and it doesn't know about the WebXR frame buffer.
So what we want to do is intercept the call that Unity is making when it finds the current frame buffer, and detect when Unity is trying to draw into the canvas frame buffer. We can know that because the null frame buffer is what WebGL uses to draw into the canvas, and when Unity does that we can intercept the bind frame buffer function, detect that it's trying to bind the null frame buffer on the canvas, and replace the null frame buffer with WebXR's frame buffer, and then call the original bind frame buffer function.
Now, when Unity binds the canvas, we intercept and actually bind the WebXR frame buffer, and Unity will then draw into the WebXR frame buffer. One other function we want to replace is WebGL's clear function because Unity will try and clear the canvas frame buffer when it draws into it. We don't want the WebXR frame buffer to be cleared because it already has the camera view in it. We want Unity to draw on top of that, so what we can do is intercept the WebGL clear function. If we know that the back buffer, the canvas is currently bound, or in this case, the WebXR context, we can just skip the clear function. Otherwise, we'd call it normal.
And now, when the WebXR frame buffer is bound, Unity tries to clear it for when it blenders, the clear will be blocked and Unity will just draw on top of what's already there, which is the camera view. One other function we want to intercept is requestAnimationFrame. Unity uses this to control frame rate, and this is what WebGL uses for its rendering, but WebXR has its own version of requestAnimationFrame that it wants you to render into. And so, we want to use that instead. But since Unity is using the built-in requestAnimation function, we can override the built-in requestAnimation function with our own, and route that to the WebXR version of the function.
Now, when Unity calls requestAnimationFunction, it's calling our function, which we then call WebXR requestAnimationFrame, which will call the callback, and then tell Unity to do its rendering from there. Now, that callback has all the WebXR information in it, and we can use that information to update Unity's camera view with that shared data and then call the callback for Unity to do its rendering.
So, if we put all this together, we can see Unity rendering on the webpage. We start with the WebXR context, and Unity is now rendering into the WebXR's frame view. It doesn't clear the back buffer, so it's rendering on top of the camera view, and everything comes together. WebXR's view matrix is being passed into Unity so that it can update its camera view, and it all integrates together.
Now, like I said, this is a very minimal implementation of WebXR. For a more full-featured implementation, other developers have done a great job with that. DeepAnthro has an excellent WebXR package available at his GitHub that you can use to render with virtual reality and augmented reality modes of WebXR, a much more full feature. I highly recommend that. And that's all I have to share with you today. Feel free to reach out for any questions you might have. I'm happy to answer, and thank you very much for taking the time. Awesome.
Comments