Unity3D + Google Cardboard

Category: 

Google offers a SDK for Virtual Reality Apps with Unity3D. Google cardboard transforms your Android smartphone into virtual reality glasses. A VR app renders an image with a small offset for each eye. By two biconvex lenses, these images are perceived as a single three dimensional image.

Of course you can use the SDK with different VR goggles. I use it with Durovis Dive.

Cardboard SDK Features

Google Cardboard SDK offers the following features:

  • Head tracking
  • Stereoscopic rendering
  • Receicing input via magnetic trigger
  • Automatic stereo configuration for some Cardboard models
  • Distortion correction for Cardboard lenses
  • Automatic gyro drift correction
  • ...

As you can see, the SDK already cares about the main points. Furthermore the SDK is open source, so you can look into the source code.

Download and Demo

You can download the Cardboard SDK for Unity here: unitypackage (or as a zip archive).
First of all, you should take a look at a demoscene. To do so, open Unity, create a new project and import the Cardboard SDK:
Assets > Import Package > Custom Package and navigate to the unitypackage.
You find the scene in your project explorer: Cardboard > DemoScene > Demoscene
The scene can be started inside the editor, but a PC has no sensors, so you have to simulate the head tracking with your mouse. Hold the left Alt key and move your mouse to look around. Hold the left Ctrl key and move your mouse to tilt your head.

Building and deploying to an Android device

To deploy the scene to your Android device, you need to switch the editor to Android mode. Go to File > Build Settings, choose Android and click on Switch Platform. This may take a while and afterwards we have to change some other settings. Click on the Player Settings Button and change the Default Orientation under Resolution and Presentation to Landscape Left. Then we go to Other Settings and set the Bundle Identifier to a unique value.
Now we can click on Build And Run. The scene is compiled and transfered to your connected phone. Of course, the phone must be connected by cable or you could use my WiFiDebugging App.

If you deploy to an Android device for the first time, you'll be asked for the Android SDK path. Just navigate to your ADK and click OK.


Adding VR

If you want to add VR to a project, you first have to import the Cardboard SDK. It consists of several folders, but for now, we only look into the Scripts and Prefabs folder.
First of all, you have to decide whether you want to add the VR functionality via scripts or by a prefab.

Via script

This is probably the easiest way. You just have to attach a StereoController script to the MainCamera. When you press play, you can see in stereo. The script creates everything it needs at runtime (like left/right eye), however, you may want to change some settings in the editor. This could be easily done with prefabs.

Via prefabs

When you start a new project, you should delete the MainCamera and replace it with the CardBoardMain prefab. This already contains everything needed for a VR experience.
If you want to expand an existing project to VR, you can of course also use the CardBoardMain prefab, but if your MainCamera is tightly coupled to the rest of your scene, you may break something. So instead you could add the StereoController script and the CardBoardAdapter prefab to your camera.

Deferred Rendering and Image Effects

These effects generally do not work correctly unless the camera using them is drawing to the full screen or a target texture. But to improve performance, the Cardboard SDK renders each eye directly to the screen. Trying to use these effects will end up with one eye stretched to the entire screen.
If you need to use Deferred Rendering or Image Effects, you have to set Direct Render to false. By doing so, each eye is first rendered to a separate texture and afterwards both textures are rendered side by side to the screen.

Rendering each eye to a texture is more expensive than rendering directly to the screen. So you have a bit overhead.

Concluding remarks

We are now ready to develop a simple VR game. Of course there are several things to consider, such as GUI in VR, but more on that next time.