© Rakesh Baruah 2020
R. BaruahVirtual Reality with VRTK4 https://doi.org/10.1007/978-1-4842-5488-2_2

2. The Virtual Camera

Rakesh Baruah1 
(1)
Brookfield, WI, USA
 

In Chapter 1, you left off running the sample scene prepackaged with VRTK. Although the VRTK demo scene is pretty nifty, it might not have been what you imagined the immersive power of VR could fully create.

This chapter will reacquaint you with the Unity and VRTK setup processes and provide you with the knowledge of how to move beyond blocks in a virtual space. We’re going to get introduced to VRTK and set up a 3D, VR-enabled Unity project; connect the VRTK interface; connect our head-mounted display to a virtual camera; and create an application that will put our user into a photorealistic, immersive space!

In this chapter you will do the following:
  • Learn the meaning and importance of high dynamic range (HDR) images to VR design.

  • Change the settings for a default Unity Skybox asset.

  • Download and import HDR images into a Unity project.

  • Place a virtual camera in a photorealistic 360-degree environment.

  • Place and manipulate a 3D object within an HDRI Skybox.

Lights, Camera, Render!

I came to Unity and VR with the experience of a filmmaker. Movies and TV were much more familiar to me than 3D graphics or computer programming. However, it wasn’t long before I felt comfortable creating my own VR apps in Unity. The key to Unity’s accessibility to professional developers and novices alike, in my opinion, is its similarity to movies. You don’t need a master’s degree in fine arts to understand that movies and TV shows come about from pointing cameras and lights at subjects. With that intuitive understanding under your belt, you’re already well on your way to becoming a Unity developer.

By using the language and concepts of a medium so familiar to many of us, Unity makes it possible for almost anyone to get their first VR scene up and running in minutes. Here’s how simple the premise of Unity is: Every project starts with a scene. Every scene starts with a light and a camera. Instead of actors, we call the subjects of our scenes game objects. That’s it! Everything else is dressing on the mixed green salad.

Of course, things can get much more complicated very quickly if you’d like. But presumably you’re not here to learn how to make a AAA game for an Xbox in a weekend. Although Unity can indeed help you accomplish such a task, you and I are going to focus on VR applications beyond gaming. Yes, we will use elements and principles of game design in our exercises, but Unity and VRTK are so powerful in concert precisely because they make creating VR accessible to everyone, not just tech-savvy gamers or programming savants.

Not to worry, though, because by the end of this book you will have the knowledge you need to jump-start an even deeper dive into the promises of Unity as a game engine. If that’s not your aim, that’s fine, too! You’ll still reach the last page of this book with the skills you need to confidently prototype whatever VR experience you can imagine. That’s a lofty promise. I beg you to hold me to it!

Getting Started

To begin, first make sure you have completed the ‘getting started’ guide and are able to run the VRTK demo scene. When you are comfortable with the steps in that process, proceed.

Open a new 3D project in Unity. If you have not already, and you intend to test your application on a VR headset of your own, download the necessary files as per the instructions from your headset provider. Most popular headset manufacturers have a section of their web site where they explain how to integrate their tools into Unity. Make sure you have gone through the setup requirements for your device of choice. For example, creating scenes for the Oculus platform might require you to download the Oculus features from the Unity Package Manager, or the Oculus Integration package from the Unity Asset Store. Similarly, developing a scene for SteamVR might require you to install the necessary OpenVR files. Refer to the documentation on the web site of your headset provider for platform-specific requirements. If you prefer to develop and play-test your VR scenes without the use of a head-mounted display, remember that VRTK conveniently provides a simulated camera rig that allows us to test our VR project without a headset connected to our computers.

To set up our first exercise of this chapter, I’ll review the procedure for creating a VR project in Unity and importing the VRTK files through your operating system’s Git Bash. You can find a more detailed, illustrated description in Chapter 1.

Review: Setting Up a VR Scene in Unity and Importing VRTK

You might be saying to yourself, “I already did this. Why do I have to do it again?” To be honest with you, the most difficult part about learning to use VRTK for me was becoming comfortable with its setup process. Because VRTK version 4 is so new, it’s not fully integrated into the Unity ecosystem yet. It’s so convenient as a VR prototyping framework, though, that I have good reason to believe its setup process will be streamlined over time. Until then, however, the number of steps it takes to get VRTK up and running with Unity can be a bit confusing. In my experience, the best practice for becoming comfortable with creating a project with VRTK is just that—practice! I’ll hold your hand through the setup of a VRTK Unity project once more, and within a short amount of time I’m certain you’ll be able to conduct the process yourself. Until then, it’s once more into the breach.
  1. 1.

    Create a new 3D Unity project and navigate to Edit ➤ Project Settings ➤ Player. Under XR, click the Virtual Reality Supported check box. If the headset on which you plan to test your scene is not already listed, click the + icon. Select the headset system of your choice.

     
  2. 2.
    Next, navigate to the Assets folder of your project in your file explorer. In File Explorer or Finder navigate to and open the Assets folder. Here, you will need to make sure you have installed Git on your computer. If you have, right-click in the Assets folder and select Open Git Bash Here. A terminal or command prompt window will open. Copy and paste the following text:
    git clone --recurse-submodules https://github.com/ExtendRealityLtd/VRTK.git
     
  3. 3.

    Press Enter and navigate to the newly created VRTK directory by typing:

     
cd VRTK
This is command line syntax to change into the VRTK directory. Press Enter again and type or paste the following:
git submodule init && git submodule update
  1. 4.

    Press Enter. You will see a progress bar countdown in your terminal window. Your computer is downloading the files required to run VRTK in your Unity application.

     
  2. 5.

    When the download is complete, return to Unity. As soon as you are back in Unity another progress bar should appear. This progress bar tells you that Unity is processing the files you downloaded from Git. Click OK on the prompt that appears.

     
  3. 6.

    Because VRTK is so new and the most recent 2019 edition of Unity is still relatively new, there’s one more step we need to complete. Navigate to Window ➤ Package Manager. On the bottom left of the Package Manager window, click the XR Legacy Input Helpers tab. On the bottom right, click Install. This action downloads the remaining files required to facilitate the connection between Unity and VRTK.

     
When the install is complete, return to your default layout in Unity. Make sure you have a new empty scene open in the Project Hierarchy. When a new scene opens in the Project Hierarchy, Unity adds a main camera and directional light by default (Figure 2-1).
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig1_HTML.jpg
Figure 2-1

This is the default starting state of a new Unity scene

In your Project window, navigate to the CameraRig folder beneath VRTK/Prefabs. From the CameraRig folder, select the UnityXRCameraRig prefab (Figure 2-2).
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig2_HTML.jpg
Figure 2-2

The UnityXRCameraRig prefab is shown in the VRTK folder structure

Drag this object into your project’s Scene Hierarchy. Make sure the Unity Camera Rig stands on its own in your Project Hierarchy and not as a child object (Figure 2-3). You can tell if an object is the child of another object if a small triangle appears next to the parent object. The Unity Camera Rig should be its own object with its own children beneath it.
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig3_HTML.jpg
Figure 2-3

The UnityXRCameraRig is shown instantiated in a scene

Why Is My UnityXRCameraRig Game Object Blue?

In the Scene Hierarchy, you might have noticed that the UnityXRCameraRig is blue, and the Main Camera object is not (Figure 2-3). Blue game objects indicate the object is an instance of a prefab.

In Unity a prefab is a game object that has been designed independent of the scene to which you apply it. If you create a custom game object consisting of its own unique materials, textures, and child game objects, for example, you can drag the parent object into the Assets folder in your Project window to save it as a prefab. A prefab acts as a template from which you can create copies of a game object to populate your scenes.

The UnityXRCameraRig object is a prefab created by the makers of VRTK. Unlike the Main Camera object that appears in a default scene, the UnityXRCameraRig prefab is not a native Unity game object.

If you intend to play-test your scene with VRTK’s Simulated Camera Rig, then drag that object’s prefab into the project Scene Hierarchy instead of the Unity XR Rig (Figure 2-4).
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig4_HTML.jpg
Figure 2-4

VRTK’s SimulatedCameraRig object mimics a VR system on a monitor

Let’s give our scene a test! Press the play button at the top of the Unity application. Make sure the Unity Camera Rig button is toggled in the top right corner of your game screen, if you plan to use your own head-mounted display. If not, select the simulated camera rig.

Uh-oh. Did you get an error? If you did, it might read something like this: “There are [more than 1] audio listeners in the scene” (Figure 2-5).
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig5_HTML.jpg
Figure 2-5

A warning message is shown in the Unity Console

This error means more than one game object with an audio listener is active in the Scene Hierarchy. Each camera game object has an audio listener component attached. Hosting more than one active camera in a scene prompts Unity to notify you that only one audio listener can exist in a scene. We can prevent this message from appearing in our console by hosting only one camera object in the Scene Hierarchy. In your Scene Hierarchy, delete the default main camera provided by Unity. You can either select the object and press Delete, or right-click/Ctrl+click the object and select Delete from the shortcut menu that appears. It is okay if you keep both the Unity XR Camera Rig and the Simulated Camera Rig in your hierarchy. However, only one camera object can be toggled on in the scene at a time. If you plan to test your scene using the simulated camera, then you will have to toggle off the Unity XR Camera Rig. If you plan to test your scene with the Unity XR Camera Rig attached to a headset of your choice, then you will have to toggle off the Simulated Camera Rig.

To toggle the state (on/off) of a camera object, select the camera object in your project hierarchy. At the top of your Inspector window, you will see the name of the camera object preceded by a check box (Figure 2-6). If the check box says Static to its right, then that is the incorrect check box to mark for your camera’s on/off state. Instead, look to the left of the camera object’s name; there you will see a check box without a label. Selecting this check box marks your camera object as on and clearing the check box toggles your camera object off. A game object unchecked in the Inspector appears grayed out in the Scene Hierarchy. Make sure the check box is selected only for the camera object you plan to use for your testing process. Remember, if you’re using an external, head-mounted display like the Oculus Rift, toggle the Unity XR Rig to on. If you are using the built-in VRTK Simulated Camera Rig, toggle only the Simulated Camera Rig object to on.
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig6_HTML.jpg
Figure 2-6

A cleared check box to the left of a camera object’s name in the Inspector window means the camera object has been turned off for the scene

Now, run your scene. In the Console window of your Unity application the error message you received previously should not appear. If you still see the same error message regarding multiple audio listeners, and you have made sure only one camera object is activated in your scene, then stop your scene from running. At the top of your Console menu, select Clear on Play (Figure 2-7). If you are unable to locate the Console window in your Unity layout then navigate to Main Menu ➤ Window to select the Console window to show.
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig7_HTML.jpg
Figure 2-7

Selecting Clear on Play in the Unity Console resets the errors and warnings that notify you of something that might need your attention

Enabling the Clear on Play feature for your Console window will clear your Console window of errors and warnings every time you stop and restart your application for testing. Of course, the errors and warnings will persist on each restart of the scene if you leave them unaddressed.

Run your scene again. If no errors regarding duplicate audio listeners appear in your Console window, then you’re ready to move on to play-testing your app. Press the Play triangle at the top of the Unity application window to start your scene. Press the Stop button when you are finished.

Congratulations, you did it! You integrated a camera into your VR scene. Now, let’s take this exercise one step further to introduce you to the tools you need to create an experience of your own.

Because this book is meant to introduce you to VR experiences beyond games, each exercise focuses on a single use-case that might have an application in your daily life. Each exercise addresses a scenario in a particular industry to demonstrate the versatility of VR. It is the aim of these exercises to provide you with the knowledge of Unity and VRTK to quickly prototype whatever immersive experience you can imagine.

Exercise 1: GlobeHopper

In this exercise, we are going to address the following use-case.

Priyanka just returned from a vacation to the U.S. Southwest, where she took a 360-degree panoramic photograph on her camera phone. She was so enamored with the scenery she hopes to share it with her friends. Unfortunately, seeing the photo on a phone doesn’t capture the emotion she felt standing on the peak in the desert. Let’s pretend we’re Priyanka and that we want to create a 360-degree immersive experience from the photo we took on our trip.

The Panorama

As with preparing a delicious meal, the success of our immersive experience depends almost entirely on the quality of our ingredients. Some camera phones allow users to capture a 360-degree panoramic photo natively, whereas others require additional hardware, software, or both. My phone, for example, only allows me to capture 180-degree panoramic images. Of course, in VR, where our canvas is completely immersive, presenting a user with only half of a 360-degree environment isn’t a successful recipe for good VR content.

To follow along with this exercise, feel empowered to use your own original 360-degree panoramic image, or feel free to use the assets included in the repository for this book. You can also make use of the assets available to download through the Unity Asset Store, which you can reach directly through the Unity Application (Figure 2-8).
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig8_HTML.jpg
Figure 2-8

Located as a tab in the same window as your Scene View in the default Unity layout, the Unity Asset Store gives you immediate access to thousands of items you can include in your scenes

I have included three different 360-degree images in the Assets folder for this project at the GitHub URL. http://​www.​apress.​com/​source-code. You are free to use these images for this exercise, but if you plan to use them for your own purposes please review the licensing terms provided by the original artists.

The 360-Degree Image vs. the HDRI

A 360-degree image simply describes the orientation of our photo asset. What truly makes a 360-degree image useful for inclusion in our GlobeHopper application is its quality. In our current context, we define the quality of an image as the amount of information an image file contains to help the Unity Engine render a surrounding as similar to reality as possible. We achieve this high quality through the use of high dynamic range images (HDRIs).

The dynamic range of an image measures the amount of color information an image contains. Digital cameras create HDRIs by snapping several frames of a subject at different exposures. This is called bracketing. For example, when your parents took a photograph with an analog, manual camera, they set the exposure level for the subject determined by the level of available light. Today, digital cameras, some by default, capture several photos at different exposure levels ranging from darkest to brightest at a single press of your finger. Although these files are larger in size than traditional JPEG images, the color information they pack is essential to creating the illusion of reality required for an immersive experience.

The steps in this exercise assume you are working with the images provided in the Assets folder for this book. If you are using an original image, please follow the instructions from your camera manufacturer on how to create HDRIs. The final settings for the image we import into our Unity project as an asset will be a 4096 × 2048 image. This is known by some as a 4K resolution asset. As of this writing, 4096 × 2048 is the largest file format allowed by Unity for skymap images. The Unity best practices guide suggests using an image no larger than 2048 × 2048 because that is the maximum size handled by many graphics cards.

The Cubemap

Photographs are two-dimensional, of course. So how are we going to use them to effectively wrap a three-dimensional space? Enter the cubemap. A cubemap is a folding pattern that arranges a set of two-dimensional images into a three-dimensional geometry (Figure 2-9). Unity wraps a cubemap version of our 360-degree HDRI as a texture around a skybox material. Although there are advanced ways you can create a cubemap of your own, Unity provides a convenient feature to handle the manipulation for us. Let’s give it a try!
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig9_HTML.jpg
Figure 2-9

A cubemap is a collection of 2D images that can be arranged into a 3D representation of a space

Let’s start from the very top, as if we’re going to make a recipe from scratch, so we can reinforce what we’ve already covered about integrating VRTK with our Unity project.

Create a new Unity 3D project. Name the project GlobeHopper and save it in a file destination of your choice. It is important that you are able to navigate to the saved destination of your project later in this exercise so note the file path if you must.

Now that we have our 3D project created, let’s make it interactive by importing VRTK. Navigate to the Assets folder of your GlobeHopper project in your File Explorer or Finder. Double-click the Assets folder to make sure you are within the directory GlobeHopper/Assets. If you see a folder labeled Scenes, you are in the right place.

After you’re sure you’ve installed the Git client on your computer and have navigated to the inside of your GlobeHopper Assets folder, right-click in File Explorer and select Open Git Bash Here.

A command prompt will appear with a blinking cursor. Type the following commands into your Git Bash command window where the cursor blinks. After each line, press Enter before typing the next command.
git clone --recurse-submodules https://github.com/ExtendRealityLtd/VRTK.git
cd VRTK
git submodule init && git submodule update

For future reference, you can always find the steps to import VRTK at the VRTK GitHub page: https://github.com/ExtendRealityLtd/VRTK.

Return to Unity and wait as Unity compiles the VRTK scripts you just cloned from GitHub.

If a pop-up menu appears that says “The VRTK Example Scene requires additional Unity Input Axes to be defined,” then click the Add Input Mappings button at the bottom of the menu. If you did not install the VRTK input mappings at this point, it’s okay. We review them in more detail in Chapter 7.

If you haven’t already, also be sure to update the Legacy XR Inputs in the Unity Package Manager to make sure VRTK is fully compatible with your current version of Unity.

The third and final step for our project setup is to confirm that our VR settings are correctly set in our project. To do this, navigate to Edit ➤ Project Settings ➤ Player. On the Player settings tab, navigate to Other Settings and make sure the Scripting Runtime Version is set to .NET 4.x Equivalent, and the Api Compatibility Level is set to .NET Standard 2.0 (Figure 2-10). If you are running Unity on a Mac, then select Mono as your scripting back end. If you are using Windows, select the IL2CPP back end. If you are unsure which scripting back end applies to your project, refer to the Unity web site for further information.
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig10_HTML.jpg
Figure 2-10

Details regarding the scripting runtime of your project can be found on the Player tab of the Project Settings window

Under XR Settings, select the Virtual Reality Supported Check box. If you do not see your Virtual Reality SDK listed, click the + button on the bottom right of the Virtual Reality SDK menu and select the device for which you intend to build your experience. If the SDK for your headset does not appear in the list, then visit your headset manufacturer’s web site for specific information on how to connect your headset’s SDK to Unity. If you continue to have trouble finding your headset listed as an option, then confirm the build settings of your project (Menu ➤ File ➤ Build Settings (Ctrl+Shift+B) are set to PC, Mac, & Linux Standalone; Universal Windows Platform; or even Android if targeting mobile VR devices like the Oculus Go. If you are targeting Universal Windows Platform in your Build Settings Unity might only give you the option IL2CPP as your scripting back end, which is fine. Check the Build Settings section of the Unity documentation for further reference. When this step is complete you can close the Player Settings window. You might see some yellow exclamation marks in your Console window. As long as none of them are red, you can click Clear at the top left of your Console window and proceed.

Now, we drag our UnityXRCameraRig object from our Project window into our Scene Hierarchy and delete our default camera object. As always, if you do not have a headset mounted to your computer you can select VRTK’s SimulatedCameraRig instead of the UnityXRCameraRig prefab.

Finally, press the Play triangle at the top of your Unity project. If you are using an external headset, move it around to verify that the Unity Game Window tracks your headset’s movement. If you are using the VRTK simulated camera rig, you can confirm your setup works by following the onscreen instructions for manipulating the viewfinder on your virtual camera.

As is, our default scene in Unity is pretty plain. All it is is a camera, a light object mimicking the sun, and a blue sky called a Skybox (Figure 2-11). Let’s see what happens if we swap out that sky for a more immersive representation of the real world.
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig11_HTML.jpg
Figure 2-11

The default state of a new Unity scene contains only a camera object and a directional light object

In the Project window of your GlobeHopper project, right-click the Assets folder and select Create ➤ Folder. Let’s name this new folder Images. Double-click the Images folder to open it. If you’ve downloaded the assets from the repo associated with this exercise, then navigate to the location of the rocky_dawn_4k.hdr file . Drag and drop rocky_dawn_4k.hdr into the Images folder you created in your Unity Project. If you downloaded HDRI content from the Unity Asset Store, the file extensions of your assets might be *.exr and *.mat. Although the file formats are different, the data they represent are the same.

Click once on the file name in your Project window. The file’s details appear in the Inspector window to the right of your scene view in the Default Unity Layout. We’ll learn more about Unity Textures soon. For now, we will leave the Texture Type of our rocky_dawn image asset at its default settings. Its Texture Shape, however, we will change from 2D to Cube (Figure 2-12). Recall that Unity converts our 2D environmental images into a 3D representation by reconfiguring the image into a six-sided cubemap.
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig12_HTML.jpg
Figure 2-12

Changing the texture shape of an HDRI to Cube reformats the image into a cubemap

After you change the Texture Shape from 2D to Cube, new parameters appear in your Inspector. One of them is labeled Mapping. Using the drop-down menu change the Mapping value from Auto to 6 Frames Layout (Cubic Environment). Leave the remaining values in the Inspector at their defaults (Figure 2-13).
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig13_HTML.jpg
Figure 2-13

The Mapping property of an HDRI asset includes options for transforming the image’s layout

We’ve got our cubemap for our environment, so now what? Now, we have to replace Unity’s default sky with our own image. Let’s navigate to the Skybox settings and see what we can change. In the Unity menu select Window ➤ Rendering ➤ Lighting Settings (Figure 2-14).
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig14_HTML.jpg
Figure 2-14

The Lighting Settings menu can be found through the Window tab on the main menu bar

The Lighting menu appears with three tabs at its top. We select Scene and turn our attention to the first category listed on the Scene tab, which is Environment. If the Environment drop-down list is collapsed, click the arrow to the left of its name to expand its properties. The first is labeled Skybox Material (Figure 2-15). Excellent! The value for this field is currently set to Unity’s Default-Skybox. Let’s drag and drop the cubemap we created from our Images folder in our Project window into the Skybox Material field of the Lighting menu.
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig15_HTML.jpg
Figure 2-15

Settings for a scene’s Skybox can be found on the Lighting menu

Uh-oh. What happened? Nothing? Good! When you dragged your Rocky Dawn image into the Lighting window, Unity gave you the “not allowed” symbol of a circle with a line through it. What gives?

Well, remember when we set the Texture property of our Rocky Dawn image to a Cube? Unity, as a 3D engine, uses a graphics pipeline common to all 3D asset programs, and in that pipeline there are three unique (although interdependent) categories that inform the way an object looks in 3D space. One is the Texture property, which helps us to render the difference between textures such as sand and brick, for example. Another is the Shader property, which primarily defines the way our objects interact with light in our virtual space. Different shaders can transform the same object from a highly reflective mirror to a muddy, matte puddle of sludge. The third property is an object’s Material. Personally, I find it helpful to imagine a property’s Material as the fabric pulled over the chicken wire skeleton of an object, called its mesh. It is a material to which we can add texture to transform a cylinder from an aluminum pipe to a bratwurst. We’ll play much more with these parameters in later exercises. For now, though, let’s create a Material to replace our current default Skybox material, a new material onto which we will project our Rocky Dawn texture asset.

Let’s repeat the process we performed to create a folder for our images to create a folder called Materials in our project’s Asset folder. Navigate into the Materials folder you created, right-click in the empty folder space of your Project window, and select Create ➤ Material. Let’s name our new material mySkybox. After renaming your new Material asset, select it and turn your attention to the Inspector window to view its properties. The very first property listed in the Inspector is labeled Shader. Unity defaults to its Standard shader, but let’s see if we can find something more specific.

From the Shader drop-down menu, select Skybox ➤ Cubemap (Figure 2-16). Beneath the Rotation property of our material there is a property called Cubemap. To its right is a thumbnail window that allows us to select the cubemap we’d like to project onto this material via the Skybox/Cubemap shader. Click Select in this window, and a new window will appear listing the assets that fit the criteria to act as a cubemap on our new Skybox material (Figure 2-17). Select the rocky_dawn_4k file. In the preview window at the bottom of the Inspector, we see what our Skybox will look like in our scene. Click in the preview window and drag the image left, right, up, and down. Well, it’s definitely a six-sided cube! Let’s add mySkybox to our Skybox Materials field in the Lighting menu (Hint: Window ➤ Rendering ➤ Lighting Settings).
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig16_HTML.jpg
Figure 2-16

The Skybox/Cubemap shader includes four options from which to choose

../images/488645_1_En_2_Chapter/488645_1_En_2_Fig17_HTML.jpg
Figure 2-17

Clicking Select on the Skybox Shader menu allows you to choose an HDRI for your scene’s cubemap

Now, when we drag our new mySkybox material into the box presently occupied by our Default-Skybox material, we receive a + sign that lets us know we can proceed (Figure 2-18). Let’s test this out!
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig18_HTML.jpg
Figure 2-18

Changing the shader of our HDRI to that of a Skybox allows us to drag and drop the asset into the Skybox Material object field on the Lighting tab

Close the Lighting settings window. If you’re play-testing on an external headset make sure the UnityXR Camera Rig is the only camera in your Scene Hierarchy on the top-left of the Unity Default Layout. Further, verify that the check box to the left of the UnityXR Camera Rig game object is selected in the Inspector when you highlight the UnityXR Camera Rig in the hierarchy. If you’re using the VRTK SimulatedCameraRig, then make sure it’s the only camera game object activated in your Scene Hierarchy. Ready? Press Play and enter your immersive, photorealistic world.

Welcome back! How did it go? Oh no, it’s not right? I’m sorry for leading you astray (not really). We’ll iron out all the wrinkles right now, I promise.

If you play-tested the GlobeHopper app in the previous step you noticed that our six-sided cubemap is quite literally a six-sided cube (Figure 2-19). Well, that might work if we have a cubemap comprised of images of the inside of a box or a near-featureless room, but it’s definitely a nonstarter for an immersive experience intended to re-create the great outdoors. Let’s amend our texture settings to get things Goldi (as in Goldilocks—“just right”).
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig19_HTML.jpg
Figure 2-19

A 360-degree HDRI is shown converted into a six-sided cubemap and applied as a texture to a Skybox material

In the Unity Project Explorer, navigate to the rocky_dawn-4k asset you saved in your Images folder. Select the asset, and in the Inspector window change the value of the texture’s Mapping property from 6 Frames Layout (Cubic Environment) to Latitude-Longitude Layout (Cylindrical) (Figure 2-20). We’ll leave the other settings at their defaults. Click Apply in the Inspector window to save the changes we made to our texture’s Mapping type. Now, let’s try things again, shall we?
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig20_HTML.jpg
Figure 2-20

Changing the Mapping property of an HDRI redefines how Unity will wrap the image around a scene

Confirm your camera settings are to your preference in your Scene Hierarchy, and press Play.

Hey, that’s much better, isn’t it (Figure 2-21)?
../images/488645_1_En_2_Chapter/488645_1_En_2_Fig21_HTML.jpg
Figure 2-21

Changing the texture mapping of a 360-degree HDRI from a six-sided cube to a globe creates a convincing Skybox asset

Texture mapping is one of the many tools Unity offers us as a 3D game engine to quickly create photorealistic, immersive experiences. It took humans until the Renaissance to master the complicated mathematics that create the illusion of not only depth, but also curved space. Unity does the same for us in a matter of seconds and a few clicks of a mouse. For further practice, try swapping out our desert setting at dusk for the other HDRIs included in the GlobeHopper repo.

Summary

A convenient way to imagine the makeup of a default Unity scene is through the language of movie-making. Each Unity scene begins with a camera, a light, and a background—called a Skybox. By importing the VRTK Unity XRCameraRig prefab or the SimulatedCameraRig prefab into our scene, we develop the ability to connect the virtual camera in our scene to our user’s movements.

Because of its intuitive layout and drag-and-drop features, Unity makes it easy for developers to quickly jump into manipulating the appearance of our scenes. A quick, straightforward process like changing the Skybox material of a Unity scene can transform a mundane experience into something novel.

In this chapter you learned how to import a virtual camera into a new Unity scene; how to toggle the state of a virtual camera object in the Inspector window; the definition of HDRIs and their value to immersive experiences; the importance of the Mapping parameter on an HDRI texture to the creation of an original Skybox; how to transform an HDRI from a 2D texture into a cubemap through the image’s Texture Shape property; that textures like HDRIs must exist on a Material object for Unity to recognize it as a potential Skybox; how to access the Lighting Settings window; and how to swap the Skybox material for a scene.

Before we move on to learning about creating truly interactive experiences with Unity and the VRTK, let’s try one more thing in our GlobeHopper app to whet our appetite for things to come. As is, our GlobeHopper project doesn’t feel very dynamic. The only thing that moves in the scene is our user’s point of view. The next chapter introduces the concepts of game objects, components, and scripting in Unity. Together they provide a rich foundation from which we can create immersive experiences that combine the real with the virtual.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.178.133