Guide to displaying multiple scenes using THREE.js VREffect

In developing my application with Three.js, I utilized the StereoEffect and created 3 scenes for overlaying by clearing renderer depth. However, I now need to switch to VREffect for better compatibility with headsets like Gear VR using WebVR Polyfill.

Below are snippets of the code setup:

const renderer = new THREE.WebGLRenderer({antialias: false})
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.autoClear = false;
document.body.appendChild(renderer.domElement)

effect = new THREE.VREffect(renderer)
effect.separation = 0
effect.setSize(window.innerWidth, window.innerHeight)

let vrDisplay
navigator.getVRDisplays().then(displays => {
    if (displays.length > 0) 
        vrDisplay = displays[0]
})

// Add button to enable VR mode (display stereo)
const vrButton = VRSamplesUtil.addButton("Enter VR", "E", "/vr/cardboard64.png", () => {
    vrDisplay.requestPresent([{source: renderer.domElement}])
})

... Remaining code ...

Within my animation loop:

renderer.clear()
effect.render(scene, camera)
renderer.clearDepth()

effect.render(scene2, camera)
effect.render(scene3, camera)

However, this method does not seem to work when utilizing VREffect. It works fine in desktop view mode but is not rendering properly in VR mode. The canvas appears pitch black, except for elements in scene3.

When I remove scene2 and scene3 from rendering, everything in the first scene is displayed correctly.

I have examined the VREffect and StereoEffect code but cannot determine what is causing my changes to be ineffective.

Any assistance or hints would be greatly appreciated.

Answer №1

I never managed to resolve the issue, but I came up with a workaround by switching from VREffect to StereoEffect for browsers other than Gear VR. VREffect worked well on Gear VR, but not on regular phone browsers that might use cardboard. Here's what I did to address this problem:

In the example mentioned above, I modified the vrButton section like this:

const vrButton = VRSamplesUtil.addButton("Enter VR", "E", "/images/cardboard64.png", () => {

    if(navigator.userAgent.includes("Mobile VR")){
        vrDisplay.requestPresent([{source: renderer.domElement}])

    }else {
        effect = new THREE.StereoEffect(renderer)
        effect.separation = 0
        effect.setSize(window.innerWidth, window.innerHeight)
        document.getElementById("vr-sample-button-container").style.display = "none"
    } 
})

This change allowed me to switch from VREffect to StereoEffect when the 'View in VR' button is clicked.

However, with this approach, the content may not be displayed in fullscreen mode and the device may go into sleep mode. To address these issues, users can manually tap the screen to enable fullscreen with this code:

renderer.domElement.addEventListener("click", () => {
    if(document.fullscreenEnabled && renderer.domElement.requestFullScreen() ||
       document.webkitFullscreenEnabled && renderer.domElement.webkitRequestFullScreen() ||
       document.mozFullScreenEnabled && renderer.domElement.mozRequestFullScreen() ||
       document.msFullScreenEnabled && renderer.domElement.msRequestFullScreen() ){}
})

Admittedly, this is not an ideal user experience and lacks a polished UI. If anyone discovers a proper solution, please share it in the comments. I will update my approach accordingly.

On a related note, it seems like the Gear VR browser has a native WebVR implementation, whereas other browsers rely on a polyfill, which could contribute to the issue at hand.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Update depth has exceeded the limit, the useEffect in React is causing issues

I encountered a strange error while working with the component below. The error seems to occur only when I make changes to the file, like altering a default prop, and then saving the file. I believe the issue stems from the props triggering a re-render, as ...

Creating a Kendo UI grid within a Kendo UI window and utilizing Knockout JS bindings

I'm encountering an unusual issue while working with Kendo UI and Knockout JS libraries. When attempting to display a kendo grid within a kendo window, the rows inside the grid are being duplicated. Below is a snippet of the code I'm using: JS: ...

Error encountered using jQuery $.post: TypeError - e.type is not a valid function

I'm currently in the process of developing a feedback script specifically for the ZetaBoards forum software. My approach has been to create a $.post function, but when I attempt to submit the form by clicking the submit button, all I get in return is ...

Receiving "this" as an undefined value within the TypeScript class

Currently developing a rest api using express.js, typescript, and typeorm. Initially thought the issue was with AppDataSource, but it seems to be functioning properly. Below is my controller class: import { RequestWithBody } from '@utils/types/reque ...

Resizing a shape along a single axis with the top point as the anchor in three.js

I am attempting to create a unique effect by scaling a THREE.PlaneGeometry using slider events. Essentially, I am working on creating a window blind. However, when trying to adjust the scale of the geometry based on the handler movement, the blind scales u ...

Load CKEditor.js with RequireJS following the textarea element

If you need a WYSIWYG editor, CKEditor could be the answer. Check out their documentation here. To properly load CKEditor, make sure to add the following script tag in the header: <script src="../ckeditor/ckeditor.js"></script> ... Then, inc ...

What is the best way to detect if a user has reached the end of a container div while implementing Infinite Scroll

I am in the process of implementing infinite scroll on our website's dashboard, but I am currently facing a challenge in determining the bottom of the container div in my jsfiddle mockup. The original function works on a blank page with no container ...

Setting environment variables using the node command is successful on Linux and macOS platforms, however, it may not function properly

When I clone a project using git, I encounter issues running npm run build on Windows. The command works fine on Mac and Linux: "build": "API=https://dev-api.myexample.com/v1.0 babel-node build.js", An error message is displayed: 'API' is no ...

Troubleshooting: The issue of receiving a 403 error when trying to access

I'm currently using Codeigniter 3 and have encountered an issue with a script. When the code is in my HTML file, everything works perfectly fine. However, if I move the code to an external file, I receive a 403 error. The location of my JavaScript fi ...

Managing state in React for a nested object while still maintaining the data type of the

Exploring the question further from this particular query Updating State in React for Nested Objects The process of updating nested state involves breaking down the object and reconstructing it as shown below: this.setState({ someProperty: { ...this.stat ...

Incorporating a CSS stylesheet into the theme settings of the Stratus 2 Beta platform

I have been attempting to personalize my Stratus 2 Beta by implementing a custom theme. I created a separate CSS file named "stratus.css" and stored it in the CSS directory of my website - with the CSS code being identical to this example. Below is my Jav ...

Animating React components upon state change

Using Animate css in my current project involves a form where users need to provide answers to questions. If the answer is correct, the word correct should slowly appear, and if it's wrong, wrong should be displayed instead. Here's a simplified e ...

Top tips for handling HTML data in JSON format

I'm looking to fetch HTML content via JSON and I'm wondering if my current method is the most efficient... Here's a sample of what I'm doing: jsonRequest = [ { "id": "123", "template": '<div class=\"container\"&g ...

Efficient method to access two arrays simultaneously and combine them into an associative array in JavaScript

When using Ajax to return a table, you have the option of separating column names and row values. Here are two ways to do it: let columns = ["col1", "col2", "col3"]; let rows = [ ["row 1 col 1", "row 1 col 2", "row 1 col 3"] , ["row 2 col 1", "r ...

Analyzing arrays and object key/value pairs based on a specific value in javascript

I want to create a new object with key/value pairs. The new object should include values from an existing key/value object as well as unique values from an array. Here is the array: [{ name: "Computer", name: "Car", name: "House&q ...

Error message: "The use of Vue 3 refs in the render function is

I am facing an issue with my Vue component wherein the root element is set as ref="divRef". Strangely, when I try to access divRef.value inside the onMounted function, it returns undefined. Any assistance on this matter would be greatly appreci ...

What is causing my webpage to load items, flash, and then suddenly vanish?

I have written this code to fetch data from an API and display it on the screen. Interestingly, when I access localhost:3000, I can see all the information, but it disappears quickly afterwards. There are some warnings appearing in the console that say th ...

Strip the pound symbol from the end of a URL during an AJAX request

When I click on the News tab in my Rails application (version 2.3.4 and Ruby 1.8.7), an Ajax call is triggered to load data. <a href="News" onclick="load_feed();"><u>Show More...</u></a> <script> function load_feed() { $.a ...

Changing the appearance of a specific child component in React by referencing its id

There is an interface in my code. export interface DefaultFormList { defaultFormItems?: DefaultFormItems[]; } and export interface DefaultFormItems { id: string; name: string; formXml: string, isDefaultFormEnable: boolean; } I am looking ...

Using the KnockOut js script tag does not result in proper application of data binding

Being new to knockout js, I found that the official documentation lacked a complete html file example. This led me to write my own script tags, which initially resulted in unexpected behavior of my html markups. Strangely enough, simply rearranging the pos ...