googlevr / cardboard

Open source Cardboard SDK and samples
Other
1.48k stars 249 forks source link

[Unity] Common Cardboard VR Tips #140

Open joaoborks opened 3 years ago

joaoborks commented 3 years ago

Since too many people are struggling to get this working (myself included), I decided to write a guide with everything I've been through and how to overcome the most common challenges. Please comment on any additions you might have.

Installation

Follow the instructions on this page. If you are using Firebase, you will need to switch some of your Gradle dependencies:

-    implementation 'com.android.support:appcompat-v7:28.0.0'
-    implementation 'com.android.support:support-v4:28.0.0'
+    implementation 'androidx.appcompat:appcompat:1.0.0'
+    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

Apparently, once the plugin migrates to AndroidX, it will be solved. It's on the roadmap for 1.5.

Toggling VR Mode

In order to start the VR mode, you need to:

Coroutine StartVR()
{
    return StartCoroutine(startVRRoutine());
    IEnumerator startVRRoutine()
    {
        // Add error handlers for both Instance and Manager
        var xrManager = XRGeneralSettings.Instance.Manager;
        if (!xrManager.isInitializationComplete)
            yield return xrManager.InitializeLoader();
        if (xrManager.activeLoader != null)
            xrManager.StartSubsystems();
        // Add else with error handling
    }
}

To stop it, simply call:

void StopXR()
{
    var xrManager = XRGeneralSettings.Instance.Manager;
    if (!xrManager.isInitializationComplete)
        return; // Safety check
    xrManager.StopSubsystems();
    xrManager.DeinitializeLoader();
}

Issues when disabling VR

For additional reference, here's the official Unity XR Management Manual.

I've created the PR googlevr/cardboard-xr-plugin#13 to add this to the unity plugin repository.

Desirable Features

Gaze/Pointer Reticle

The current version of this plugin (1.3.0) does not provide any kind of solution for that. There are a few options regarding this matter (also mentioned on #47, #104, #121, and #132):

I've created the PR googlevr/cardboard-xr-plugin#13 to add this to the unity plugin repository.

Non-VR Camera Controls

It's something you can implement fairly easily. Take this script as a starting point:

[SerializeField]
float dragRate;
[SerializeField]
Transform cameraTransform;

Quaternion initialRotation;
Quaternion attitude;
Vector2 dragDegrees;

void Awake()
{
    initialRotation = cameraTransform.rotation;
}

void Update()
{
    if (Input.touchCount == 1)
    {
        var touch = Input.GetTouch(0);
        dragDegrees.x += touch.deltaPosition.y * dragRate;
        dragDegrees.y += touch.deltaPosition.x * dragRate;
    }
    attitude = initialRotation * Quaternion.Euler(dragDegrees.x, 0, 0);
    cameraTransform.rotation = Quaternion.Euler(0, -dragDegrees.y, 0) * attitude;
}

I've created the PR googlevr/cardboard-xr-plugin#13 to add this to the unity plugin repository.

Magic Window

No official solution as well, but it has already been requested at #48.

VR Editor Simulation

There's no way to simulate the VR view on the Editor since the VR rendering is done natively on the device. However, you can implement at least a cursor simulation of the head movement, that works very close to the Magic Window, if it helps you debug your app. It has been already requested at #51.

I've created the PR googlevr/cardboard-xr-plugin#13 to add this to the unity plugin repository.

iOS Metal

Single Pass Rendering Support

Known Issues

magiwanders commented 3 years ago

Hi I rebuilt the same simple scene using legacy GoogleVR (compiling for Cardboard) and this new Cardboard SDK plugin to compare performance. Cardboard SDK Plugin has performance comparable to GoogleVR with multipass rendering, which means rather terrible (even with Snapdragon 855 android), which gives nausea very fast. Is there any setting which does anything similar to legacy single pass rendering?

joaoborks commented 3 years ago

@magiwanders I don't know exactly, but it doesn't look like we have control over the rendering process. It seems the rendering is done via the native plugins, while the Unity Package works as a bridge to them. Perhaps it would be a good thing to expose some settings to Unity and allow us to customize some of the stuff from there. Maybe the devs @jballoffet and @chaosemer can assist you further with your Multi Pass/Single Pass rendering question.

magiwanders commented 3 years ago

@JoaoBorks Tank you! I thought too rendering was handled by unity, but difference in performance (both fps and expecially latency of movement) is very noticeable, so what gives? I hope the devs can give us more insight about the topic.

dipaw commented 3 years ago

OpenGL ES 3.0

40 It seems it's far on the roadmap.

Oh, this should be the first along with single-pass / multiview mode. Without these technologies, we simply won't get enough FPS for comfort in VR.
Given the number of OpenGl es 2.0 devices and their performance, it is strange to support them.

arumons commented 3 years ago

If you are using Firebase, you will need to switch some of your Gradle dependencies:

Hi, I tried to rewrite the mainTemplate according to the procedure of ↑.

I can build it, but it seems to crash when I try to read the QR code by pressing the gear icon at the top right of the screen.

joaoborks commented 3 years ago

@arumons is it crashing only if you change those lines in the Gradle template? Officially, AndroidX is not supported, it's planned for release 1.5, but I didn't have any issues with it. I'm not using the QR Code though..

arumons commented 3 years ago

@JoaoBorks is it crashing only if you change those lines in the Gradle template?

yes. It works fine without firebase.

arjungaonkar commented 3 years ago

I am getting " java.lang.RuntimeException: Duplicate class com.google.protobuf.AbstractMessageLite found in modules protobuf-javalite-3.14.0.jar (com.google.protobuf:protobuf-javalite:3.14.0) and protobuf-lite-3.0.0.jar (com.google.protobuf:protobuf-lite:3.0.0)" Error with Firebase unity SDK 7.1.0 and Google Cardboard XR Plugin for Unity 1.4.1. How to fix this?

GonzaloEscamilla commented 3 years ago

Hi!

Any ideas on how can i achieve a Monoscopic and Stereoscopic 360 video viewer? I am tired of looking for a direct solution, but is useless.. Could you put me in the right path

Abbabon commented 1 year ago

Just wanted to drop a quick thank you from the future! Making Cardboard apps in 2023 proved to be quite difficult :D