WebXR Guide
This is my recent presentation. And I will share it with some comments.
Today I am going to share very funny topics;
WebXR
You know, Meta and Apple invested a lot of resources in this field; Some people here don’t have headsets or never experience it.
But it doesn’t matter; Today I will just make a simple introduction about it.
Here are agendas
In the 1940s, the first computer was born in the world. At that time, the computer was still huge, and the interface looks so old style, and we can only get specific information from the different colors of lights;
A few years later, major companies(like IBM) entered the personal computer market;
That’s the computer’s appearance we are seen now. we use the mouse and keyboard and a big display.
In the new century, we have an iPad with a touch screen, which is one of the great inventions in human history; Von Neumann model, the input and output are perfectly combined on one display On the screen; it brings us a lot of innovative interactions;
In the last ten years, humans are still looking for the next generation of computers; maybe headsets will be one.
Why?
Let’s talk about “XR”
Some people are willing to use X to represent anything; Because we know there are VR, AR;
And also with the new generation headsets, we created the MR(mixed-reality); Via The Video See-Through technology, we cannot only see the display graph, but we can also see the outside world graph from the camera located in the headset.
The biggest difference from XR; It gives us is the immersive experience;
The first part: we could experience the visual 3d graph and spatial audio; That’s one part of `reality`
The second part: Our interaction is not simply a button, but will dynamically combine the position of our physical space and the position of the device, so it will respond according to our actions;
After talking about XR, maybe you get to know what webXR is by its name;
Then let’s talk about WebXR;
WebXR means us to develop web applications in the browser of XR OS;
Introduction to the WebXR API
The first thing we do with any new API is to determine its compatibility; we can do this by
if ("xr" in window.navigator) {
/* WebXR can be used! */
} else {
/* WebXR isn't available */
}After judging, we just need to start;
among them, XRSession is a very important concept;
In the session, we can access some basic WebXR APi and change some common properties;
we can requestSessionenter our VR mode;
navigator.xr.requestSession('immersive-vr').then(onSessionStarted)There are two modes in XRSession;
inline can be in normal mode, that is, our pages will be displayed according to the execution method of the browser. For example, the Weibo we browse normally can be displayed normally on Zhihu
immersive-vr is our expected VR mode, we will enter a three-dimensional scene, it depends on how we realize;
In XRSession, we can implement some operations on WebXR API, such as:
It helps us to detect it in AR or VR mode (environmentBlendMode)
renderState: describing how the user’s environment which should be rendered
inputSources: I will talk about it later
requestAnimationFrame: schedules a callback to be executed the next time
Next is what we need to focus on WebGL;
the WebXR API itself does not include a 3D graphics API, it must be used in conjunction with the WebGL API (or the more modern WebGPU API);
therefore, if we want to in-depth application development, WebGL learning is definitely inevitable;
XRSpace In WebXR;
a “space (XRSpace)” represents a set of 3D coordinate systems in virtual space, and its origin corresponds to a position in the physical real world, so “space” can serve as a link between digital virtual space and physical reality The role of spatial association; with it, we can better handle our interactions; both from the controller and the headset itself;
Get JackPu’s stories in your inbox
Join Medium for free to get updates from this writer.
Let me make an example;
When we implement web chess, we can control chess pieces according to mouse clicks, mouseover, and other events;
if we want to implement a three-dimensional chessboard placed in front of us, we want to realize chess moves, what should the interactive information be like?
The next step is also very important; how do we handle interaction;
We know the Von Neumann architecture;
VR also has many sources of interaction, for example, we often have two controllers; before the first time, the headset is also one of the interactive devices we need to track; With increasing precision, our hands will also become the objects we interact with;
This is Oculus quest pro controller. There is some press button and a joystick, And also there is a touch area; It could be very complex;
Through the above figure, we can simply compare how we realize the binding of events;
In addition to the APIs introduced above, you can also learn about the following if you are interested
XRFrame: access to the information needed in order to render a single frame
XRPose: a position and orientation in the 3D space,
XRViewport: it provides properties used to describe the size and position of the current viewport
How do we start it?
How do we develop the basic API introduced earlier; I have some small tips to share
1 Web project initialization (PWA is better); because it is a Web project, we can use our favorite technical scaffolding or engineering, what Webpack/TS/Eslnt can be freely matched; if you want to publish the application to the Quest store, it is best Use some auxiliary tools of PWA; I wrote an article about Oculus and PWA before, you can take a look;
2 WebGL is the core, so we’d better choose a suitable WebGL framework (Three.js, Babylon.js), writing native code is really cumbersome;
3 Initialize WebXR and process XRFrame (you can also choose the WebXR framework); this is what we all need to do, with the help of documentation, refer to WebXR Sample to help you understand them;
4 Testing and real machine debugging; in any case, it must be tested on a real machine;
I will show you a video demo;
https://drive.google.com/file/d/1Ib_ztIKZekv48eVPICYbPIE44cwPgBD_/view?resourcekey
We usually develop, we can use webxr-api-emulator to do some interactive processing;(simulation)
if we are debugging on the real machine;
We can use adb reversethe command to perform localhost proxy; of course, other proxy network tools are also available; because the API of WebXR can only localhost work under HTTPS;
After the development is complete, you can of course publish your own application, don’t forget to package PWA and APK packages;
Some useful tools:
- @bubblewrap/cli
- Oculus Platform Utility
- PWA on Oculus Quest2
Here I also briefly compare the similarities and differences between traditional Web and WebXR development; mainly
WebXR is more graphically related to development
We use many model files (bx,gLTE,obj)
It will be more complicated to handle the interaction
Although the development of WebXR is very difficult, due to the natural cross-platform compatibility of the Web, as well as the out-of-the-box feature without installation, I believe that there must be a world of its own in the future;
Thanks. I just make a brief share about WebXR; There is one tip for you.The most of APIs are under-experimental. And We cannot guarantee that Oculus or App will support them well. So if you are interested in it; maybe you could explore more by yourself. Because three are so less helpful documents. Anyway, we can know what our web developers can do if the XR moment is coming.
Read More:
- https://zhuanlan.zhihu.com/p/622957093
- https://web.dev/pwas-on-oculus-2/
- https://developer.mozilla.org/en-US/docs/Web/API/WebXR_Device_API
- https://immersive-web.github.io/webxr-samples/
- https://developer.oculus.com/documentation/web/pwa-gs/
- https://medium.com/@lazerwalker/how-to-easily-test-your-webvr-and-webxr-projects-locally-on-your-oculus-quest-eec26a03b7ee










