Unity 2020 Virtual Reality Projects
上QQ阅读APP看书,第一时间看更新

Building for SteamVR

This section describes how to set up and build your VR project so that it runs on a SteamVR supported VR device. VR applications that run on SteamVR are built using the OpenVR plugin. It can drive a variety of models of VR, including HTC Vive, Valve Index, Oculus Rift, and Windows MR devices; that is, basically all the major PC-based six degrees of freedom (6-DOF) VR rigs with trackable hand controllers.

Before you get started, ensure you have your VR device connected to the PC and that you're running the SteamVR runtime. This may include installing the Lighthouse base stations for the HTC and Valve devices that require it. If you're running SteamVR for an Oculus Rift, you also need to be running the Oculus desktop app. Likewise, if you're running SteamVR for immersive Windows Mixed Reality (WMR), you need to be running the Mixed Reality Portal. Test that you can see and interact with the SteamVR Home in the headset.

Setting up for OpenVR

TheOpenVR XR plugin is provided and maintained by Valve (not by Unity). At the time of writing, you must install the plugin usingPackage Manager using a GitHub URL. Please review the README instructions on the GitHub page at https://github.com/ValveSoftware/steamvr_unity_plugin before completing this section, as it may be more up to date than this book.

To target SteamVR, use the following Build Settings:

  1. Configure your UnityBuild Settingsso that they target the Standalone platform, if they aren't already (File | Build Settings... | PC, Mac & Linux Standalone | Switch Platform).
  2. SetTarget Platformto Windows andArchitecture to x86_64.

Use the following steps to install the OpenVR XR plugin:

  1. Open the Package Manager window usingWindow | Package Manager.
  2. Press the + button in the upper left of the window.
  3. Select Add Package from GIT URL.
  4. Paste in the following URL: https://github.com/ValveSoftware/steamvr_unity_plugin.git#UnityXRPlugin.
  5. Open XR Management Settings using Edit | Project Settings | XR Plug-in Management.
  6. If you see OpenVR in the Plug-in Providers list, check its checkbox.
  7. You will see that the OpenVR plugin is now listed under XR Plug-in Management. Click it to review its settings.

The OpenVR plugin has several options. Generally, you'll keep the default values, as shown in the following screenshot:

If you pressPlaynow, you should see the scene playing in your VR headset. At this point, it's also advised that youBuild And Runthe project to verify it will build correctly and run as expected in your VR device.

The SDK we've installed is sufficient for the projects in this book. We are going to use the XR Interaction Toolkit, including the Tracked Pose Driver, for the left- and right-hand controllers. However, Steam provides an alternative input system that uses the SteamVR input bindings and 3D controller models.

Installing the SteamVR Unity Plugin toolkit

As a SteamVR app, you have the option of using the SteamVR plugin for the Unity toolkit from Valve Software, which provides user rebindable inputs, skeletal inputs, 3D controller models, and more, including an example interaction system with useful scripts and assets and some pretty good example scenes that are fun and instructional. Details can be found at https://valvesoftware.github.io/steamvr_unity_plugin/articles/intro.html.

Note that the use of the word "plugin" for this SteamVR Unity Plugin toolkit is a high-level toolkit unrelated to the low-level Unity XR plugins. You've already installed the separate low-level OpenVR plugin device driver for the XR Management system.

For the projects in this book, we do not depend on having this toolkit installed, and it is best to not try to mix the SteamVR Unity Plugin toolkit assets with the Unity XR ones in your project scenes, especially while you're learning the basics of VR development and following along with our project tutorials. I recommend that you do not install it here. For your own projects, however, you can decide to use the SteamVR Input System instead of the Unity one, especially if you plan to only target the Steam Store and target only the desktop VR devices supported by SteamVR.

At the time of writing, there are two versions of the SteamVR Unity Plugin. The older one is compatible with the Unity built-in XR, which supports Unity versions earlier than Unity 2019.3. It can be installed from the Asset Store ( https://assetstore.unity.com/packages/tools/integration/steamvr-plugin-32647). The newer version is presently in prerelease and can be downloaded as a .unitypackage file from GitHub ( https://github.com/ValveSoftware/steamvr_unity_plugin/releases).

To install the SteamVR Plugin version that is compatible with XR PluginManagement from GitHub, use the following steps (subject to change in the future):

  1. In your web browser, go to https://github.com/ValveSoftware/steamvr_unity_plugin/releases and choose the current Assets file link to begin the download.
  2. Import the package into Unity using Assets | Import Package | Custom Package or simply drag and drop the package file into the Project window.
  3. At the bottom of the Import Unity Package window, select Import.
  4. Since we're using the Universal Render Pipeline, you should convert the imported materials by selectingEdit | Render Pipeline | Universal Render Pipeline | Upgrade Project Materials.

The package installs several new folders in your ProjectAssets/, including SteamVR/, SteamVR_Resources/, and StreamingAssets/SteamVR/. If you drill down into the Assets/SteamVR_Resources/Resources/ folder and select the SteamVR_Settings, you will be able to see the current settings in the Inspector window, as shown in the following screenshot:

For more information, review the SteamVR Unity Plugin.pdf and SteamVR Unity Plugin - Input System.pdf documents, which can be found in the Assets/SteamVR/ folder. Be sure to explore the various example scenes, such as Simple Sample in SteamVR/, Interactions_Examplein SteamVR/InteractionSystem/Samples/, and multiple example scenes in SteamVR/Extras/.

Please refer to the plugin documentation for details on the features and usage of the assets. For example, in your own projects, you can choose to replace our XR Rig camera rig with the [CameraRig] prefab provided with the SteamVR Plugin, which can be found in the Assets/SteamVR/Prefabs/ folder and is used in their various example scenes. The plugin includes the following:

  • Render Models: The virtual 3D models of the hand controllers adapt to the actual physical VR device the player is using.
  • SteamVR Input: Manage user input actions. Handles user rebindable input mappings that are controlled from a .JSON file and are accessible via the Window | SteamVR Input window.
  • Skeleton Input: Hand animation with a full finger and joint estimates and range of motion.
  • Interaction System: A set of scripts, prefabs, and other assets that serve as examples. These can be used in your own projects.
  • Skeleton Poser: Tools you can use to design your own hand poses and finger constraints, used together with Skeleton Input.

If you don't need to set up an additional VR device, you can move onto the next chapter. We will consider the setup for Oculus Rift next.