iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🙆

[PlayCanvas] Implementing a freely navigable VR world in Oculus Browser using WebXR

に公開
2025/02/17

This is an article for the 13th day of the WebXR (WebVR/WebAR) Advent Calendar 2021.

This is a project I created for verification purposes to support WebXR (Oculus Browser) using PlayCanvas. Please feel free to try it out from the link below, and I would love to hear your thoughts.

↓↓↓↓
https://playcanv.as/e/p/xxOO8fvR/

※ Note

※ Only Oculus Browser on Oculus Quest / Oculus Quest 2 is confirmed to work.

This is the actual operation. You can also enter the VR space by accessing the URL from an Oculus Quest.

What is PlayCanvas?

PlayCanvas is a WebGL-based game engine JavaScript library. It has an open-source engine and a web-based editor, and it supports WebXR for development.

PlayCanvas GitHub
https://github.com/playcanvas/engine

PlayCanvas Official Japanese Website
https://playcanvas.com/

How to develop for WebXR with PlayCanvas?

There are XR examples available in the official PlayCanvas repository under the MIT license.
I recommend using these samples to proceed with your development. For this project, I referred to these projects for character movement and controller (hand tracking) implementation.
https://playcanvas.github.io/#/xr/xr-picking
https://playcanvas.github.io/#/xr/vr-movement

Basically, referring to the samples above is very helpful, but in this article, I will introduce some techniques I used when developing with the samples, mainly regarding scene transitions and some tricky points.

This project is developed using the visual editor.
You can also copy (fork) this project to use it yourself.
https://playcanvas.com/project/1053423

※ For development on a PC (Google Chrome / Firefox), please use this extension.
https://github.com/MozillaReality/WebXR-emulator-extension

This project is built with the following scene structure.

  • Home (scene called at startup)
  • Lobby
  • World 1
  • World 2
    Also, the scene transitions are as follows:
    Home (startup scene) -> Lobby (VR) <-> World 1 (VR) <-> World 2 (VR)
    Once you enter the VR space, you can go back and forth between the lobby and the worlds.

Explanation of the Scenes

Home (Startup Scene)


In the startup scene, I handle the display of the button to enter VR, audio playback, and camera preparation.
This is the only scene that can be used from devices other than the Oculus Browser.
I also use this scene for conditional branching based on browser compatibility.

About button display

In the Home scene, I perform UI display using HTML and logic to enter the VR camera.
For information on UI display, please see here:
https://qiita.com/yushimatenjin/items/8253b964365dc3e62682

About entering the VR scene

In PlayCanvas, to enter a VR scene, you need to prepare a Camera component and entity in advance.
You can enter the VR scene by calling the startXr method within the Camera component.

Also, you can specify seated/room-scale settings via the second argument of startXr.
https://developer.playcanvas.com/en/user-manual/xr/types-of-xr/

// Camera component
camera.startXr(pc.XRTYPE_VR, this.XRReferenceSpaceType);
About PlayCanvas scene transitions when entering VR

When the button to enter VR is pressed, I perform a scene transition like this: moving the scene from Home -> Lobby. The following process is used:

  async enterVrScene(sceneName) {
    if (this.app.xr.supported && pc.XRTYPE_VR) {
      const cameraEntityParent = this.app.root.findByTag("vrcamera")[0]; // Get the VR camera
      const cameraEntity = cameraEntityParent.children[0]; // Get the VR camera
      cameraEntityParent.enabled = true; // Switch the hidden VR camera to visible
      cameraEntityParent.reparent(this.app.root); // 
      // ※※ Before the scene transition, I use reparent to place the VR camera outside the Root hierarchy.
      // ※※ This prevents the camera from disappearing due to scene transitions.
      cameraEntity.camera.startXr(pc.XRTYPE_VR, this.XRReferenceSpaceType); // Use startXr method in the camera component to enter the VR scene
      const old = await this.loadScene(sceneName); // Load new scene
      old.destroy(); // Destroy old scene
    }
  }

Lobby (First scene entered in VR mode)


In this scene, you can move around in VR and move to other scenes via portals.
Each is implemented as follows:

About movement in VR

For movement in VR, I am using the code from this GitHub repository:
https://playcanvas.github.io/#/xr/vr-movement

About portals

This portal uses placed PlayCanvas Element components.

The operation is set up so that if the part labeled "Go" is clicked with an Oculus Quest 2 controller, it transitions to a different scene.
Since Element component Hover/Click events work even in VR, when the button is pressed, I trigger an event via this.app.fire to transition scenes.

Button for triggering events

class LoadSceneButton extends pc.ScriptType{
    initialize(){
        this.entity.button.on("click",  async () => {
            this.app.fire("vr:scene:change", this.sceneName);
        });
    }
}

pc.registerScript(LoadSceneButton);
LoadSceneButton.attributes.add("sceneName", {type: "string"});

Called process
When the vr:scene:change event is received, I load the new scene and destroy the old one. Also, since the camera in the VR state is not destroyed, you can use the same entity to move between scenes.

async changeVrScene(sceneName) {
    this.app.root.findByName("CameraParent").setPosition(0, 1.5, 0); // Initialize camera position when moving scenes
    const old = await this.loadScene(sceneName); // Load new scene
    old.destroy(); // Destroy old scene
}

World 1 / World 2 (Test Worlds ※ Transition Destinations)

World 1 and World 2 are test scenes for actual content integration. They have similar functionality to the Lobby. Aside from scene movement via portals, they are basically standard PlayCanvas scene structures.

In this scene, I am loading and playing a video.
For video loading, please see here:
https://qiita.com/yushimatenjin/items/4c3a3839bffdd8859a6f

In this way, you can implement scene transitions relatively easily in PlayCanvas.
I think it would be quite interesting to implement features like multiplayer.
If you have any questions, please feel free to contact me on Twitter: mxcn3.

Discussion