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

Preface

Today, we are witnessing the burgeoning of virtual reality (VR), which is exciting new technology and a creative medium that promises to transform how we interact with our information, friends, and the world at large in a fundamental way.

Through wearing a VR head-mounted display (HMD), you can view stereoscopic three-dimensional scenes. You can look around by moving your head, walk around the space with room-scale tracking, and interact with virtual objects with positional hand controllers. With VR, you can engage in fully immersive experiences. It's like you're really in some other virtual world.

This book takes a practical, project-based approach to teach you the specifics of VR development using the Unity 3D game engine. We will walk through a series of hands-on projects, step-by-step tutorials, and in-depth discussions using Unity 2019.4 LTS or Unity 2020.x, and other free or open source software. While VR technology is rapidly advancing, we'll try to capture the basic principles and techniques that you can use to make your own VR games and applications immersive and comfortable.

You will learn how to use Unity to develop VR applications that can be experienced on devices such as Oculus Rift, Quest, HTC VIVE, and others. We'll also cover the technical considerations that are especially important and possibly unique to VR. By the end of this book, you will be equipped to develop rich and interactive VR experiences.

About the author and this third edition

Years ago, I studied 3D computer graphics in college and user interface design in graduate school, before starting a small software company developing a 3D graphics engine for managing AutoCAD engineering drawings. We sold the business to Autodesk. In the ensuing years, I focused on 2D web app development, blogged about my technical adventures, and pursued several new start-ups. Then, in March 2014, I read about Facebook purchasing Oculus for $2 billion; that certainly piqued my interest. I immediately ordered my first VR headset, the Oculus DK2 developer kit, and began developing small VR projects in Unity.

In February 2015, I had the idea to write a book on Unity VR development. Packt accepted my proposal right away, and suddenly I realized "Oh no! I have to do this!" Within 6 months, in August 2015, the first edition of this book was published. That's a short time to go from proposal to outline, to chapter drafts to review, to a final draft and publication. I was obsessed. At the time, I told my wife that I felt the book had a life of its own: "It's inside of me and struggling to get out, I just have to get out of its way." She replied, "It sounds like you're pregnant."

At the time of writing, Google Cardboard was a thing, but there were no consumer VR devices. The Oculus DK2 had no hand controllers, just an Xbox game controller. Months after the book was released, in November 2015, the HTC Vive came to market with room-scale and positionally tracked hand controllers. In March 2016, the consumer version of Oculus Riftwas released. Not until December 2016, almost a year and a half after the book came out, did Oculus release its positionally tracked Touch hand controllers.

Since the first edition of this book, many new VR devices have entered the market, hardware and software features have improved, and the Unity game engine continues to add native VR SDK integrations and new features to support them. Oculus, Google, Steam, Samsung, PlayStation, Microsoft, and many others have joined the fray as the industry continues to accelerate and blossom.

Meanwhile, in 2016, I coauthored another book with Packt, Cardboard VR Projects for Android, a non-Unity VR book using Java and Android Studio to build Google Daydream and Cardboard applications. (In that book, you build and use your own home-grown three-dimensional graphics engine for mobile devices.) Then, in 2017, I coauthored a third book with Packt, Augmented Reality for Developers, an exciting and timely Unity-based project book for AR applications on iOS, Android, and HoloLens devices.

In May 2018, I published the second edition of this book, Unity Virtual Reality Projects. When the time came to begin the second edition, I expected it to be a relatively simple task of updating to the current version of Unity, adding support for positionally tracked hand controllers, plus a few tweaks here and there. But it wasn't so simple! While much of the fundamentals and advice in the first edition did not change, as an industry, we have learned a lot in these few short years. For example, it's really not a great idea to implement a trampoline in VR (one of our projects that got scrapped from the first edition) as that can really cause motion sickness! Every chapter and project was updated. New chapters and projects were added, including an audio fireball game, a storytelling and animation experience, and a discussion of optimization best practices.

In this third edition, the book has again been significantly revised and expanded. With the introduction of the Unity XR platform architecture, plugins, and XR Interaction Toolkit, I decided to focus on this new standard API and components throughout the book. All of the chapters and projects have been adapted to use Unity's own XR SDK instead of provider-specific toolkits because I see this to be a hugely significant development and contribution by Unity to the progress of our industry. I sincerely hope you find this book fun, educational, and helpful as we create great new VR content and explore this amazing new medium.

Who this book is for

If you are interested in VR, want to learn how it works, or want to create your own VR experiences, this book is for you. Whether you're a non-programmer and are unfamiliar with three-dimensional computer graphics or you are experienced in both but new to VR, you will benefit from this book. Any experience in Unity is an advantage. If you are new to Unity, you can also pick up this book, although you might first want to work through some of Unity's own getting-started tutorials, which are available on their website (https://unity.com/learn).

Game developers may already be familiar with the concepts in this book that have been reapplied to VR projects, but might still learn many other ideas that are specific to VR. Mobile and 2D game designers who already know how to use Unity will discover another dimension! Engineers and 3D designers may understand many of the 3D concepts, but will learn to use the Unity engine for VR. Application developers may appreciate the potential non-gaming uses of VR and may want to learn how to use the tools to make that happen.

What this book covers

Chapter 1, Virtually Everything for Everyone, is an introduction to the new technologies and opportunities in consumer VR in gaming and non-gaming applications, including an explanation of stereoscopic viewing and head tracking.

Chapter 2, Understanding Unity, Content, and Scale, introduces the Unity game engine as we build a simple diorama scene using primitive GameObjects, prefabs, and imported three-dimensional content.

Chapter 3, Setting Up Your Project for VR, helps you set up your system and a Unity project to build and run on your target device(s), including SteamVR, Oculus Rift, Windows Immersive MR, Oculus Quest, and Google Cardboard.

Chapter 4, Using Gaze-Based Control, explores the relationship between the VR camera and objects in a scene, including 3D cursors and gaze-based ray guns. This chapter also introduces Unity scripting in C#.

Chapter 5, Interacting with Your Hands, looks at user input events, such as controller buttons and tracked hand controllers using components from the XR Interaction Toolkit. We also build a balloon gun for inflating and popping balloons!

Chapter 6, Canvasing World Space UI, implements many examples of the user interface (UI) for VR using a Unity world space canvas, including a heads-up display (HUD), info bubbles, in-game objects, a three-dimensional dashboard, and a wrist-based menu palette.

Chapter 7, Teleporting, Locomotion, and Comfort, dives into techniques for moving yourself around a VR scene, such as glide locomotion, climbing a wall, and teleporting to other locations.

Chapter 8, Lighting, Rendering, Realism, takes a closer look at the Unity render pipelines and choosing a lighting strategy for your projects. We build interactive controls for environmental lighting, PBR materials, light objects, reflection probes, postprocessing, and more.

Chapter 9, Playing with Physics and Fire, explores the Unity physics engine, physic materials, particle systems, and more C# scripting as we build a paddle ball game to whack fireballs in time to your favorite music.

Chapter 10, Exploring Interactive Spaces, teaches you how to build an interactive art gallery, including level design with ProBuilder, artwork lighting, data management using scriptable objects, and teleporting through space.

Chapter 11, Using All 360 Degrees, explains 360-degree media and uses photos and videos in a variety of examples, including globes, orbs, photospheres, and skyboxes.

Chapter 12, Animation and VR Storytelling, builds a complete VR storytelling experience using imported three-dimensional assets and a soundtrack, as well as Unity timelines and animation.

Chapter 13, Optimizing for Performance and Comfort, demonstrates how to use the Unity Profiler and Stats window to reduce latency in your VR app, including optimizing your three-dimensional art, static lighting, efficient coding, and GPU rendering.

To get the most out of this book

Before we get started, there are a few things that you'll need. Grab a snack, a bottle of water, or a cup of coffee. Besides that, you'll need a PC (Windows or Mac) with the current version of Unity installed (Unity 2019.4 LTS or later). Access to a VR HMD is strongly recommended in order to try out your builds and get first-hand experience of the projects developed in this book.

You don't need a super-powerful computer rig. While Unity can be a beast that can render complex scenes, the requirements for the projects in this book are not that demanding. If you are targeting an Android-based VR device (such as Oculus Go or Quest), you need just enough power to run Unity and build your project as if for any Android mobile device. If you are targeting desktop VR (such as HTC Vive, Valve Index, Oculus Rift, or Oculus Quest with a Quest Link cable), you simply need to meet the VR-ready requirements of the target device.

Chapter 3, Setting Up Your Project for VR, goes into detail of what you need for each device and platform, including SteamVR, Oculus Rift, Windows MR, Oculus Go and Quest, and Google Cardboard.

That should just about do it—a PC, the Unity software, a VR device, and the other tools described in Chapter 3, Setting Up Your Project for VR, and we're good to go! Oh, some projects will also be more complete if you download the associated assets from the Packt website, as follows.

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packt.com.
  2. Select the Support tab.
  3. Click on Code Downloads.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Unity-2020-Virtual-Reality-Projects-3rd-Edition-. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Open theAssets/Scenes/folder."

A block of code is set as follows:

public class BallsFromHeaven : MonoBehaviour 
{ public GameObject ballPrefab;
public float startHeight = 10f;
public float interval = 0.5f;

private float nextBallTime = 0f;

private void Start()
{
nextBallTime = Time.time + interval;
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

  public float startHeight = 10f;
public float interval = 0.5f;

private float nextBallTime = 0f;
private ObjectPooler objectPooler;

private void Start()
{
nextBallTime = Time.time + interval;
objectPooler = GetComponent<ObjectPooler>();
}

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Add anXR Rig object by going to GameObject | XR | Stationary XR Rig."

Warnings or important notes appear like this.
Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at customercare@packtpub.com.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.