AR / VR示例

本教程为您提供了迈向Godot游戏引擎中AR和VR世界的跳板。

A new architecture was introduced in Godot 3 called the AR/VR Server. On top of this architecture, specific implementations are available as interfaces, most of which are plugins based on GDNative. This tutorial focuses purely on the core elements abstracted by the core architecture. This architecture has enough features for you to create an entire VR experience that can then be deployed for various interfaces. However, each platform often has some unique features that are impossible to abstract. Such features will be documented on the relevant interfaces and fall outside of the scope of this primer.

AR / VR服务器

When Godot starts, each available interface will make itself known to the AR/VR server. GDNative interfaces are setup as singletons; as long as they are added to the list of GDNative singletons in your project, they will make themselves known to the server.

You can use the function get_interfaces() to return a list of available interfaces, but for this tutorial, we’re going to use the native mobile VR interface in our examples. This interface is a straightforward implementation that uses the 3DOF sensors on your phone for orientation and outputs a stereoscopic image to the screen. It is also available in the Godot core and outputs to screen on desktop, which makes it ideal for prototyping or a tutorial such as this one.

To enable an interface, you execute the following code:

GDScript

C#

  1. var arvr_interface = ARVRServer.find_interface("Native mobile")
  2. if arvr_interface and arvr_interface.initialize():
  3. get_viewport().arvr = true
  1. var arvrInterface = ARVRServer.FindInterface("Native mobile");
  2. if (arvrInterface != null && arvrInterface.Initialize())
  3. {
  4. GetViewport().Arvr = true;
  5. }

This code finds the interface we wish to use, initializes it and, if that is successful, binds the main viewport to the interface. This last step gives some control over the viewport to the interface, which automatically enables things like stereoscopic rendering on the viewport.

For our mobile VR interface, and any interface where the main input is directly displayed on screen, the main viewport needs to be the viewport where arvr is set to true. But for interfaces that render on an externally attached device, you can use a secondary viewport. In the latter case, a viewport that shows its output on screen will show an undistorted version of the left eye, while showing the fully processed stereoscopic output on the device.

最后,您应该只初始化一次界面; 切换场景和重新初始化界面都会带来很多性能消耗。 如果您想暂时关闭耳机,只需禁用视口或在视口上将 arvr 设置为 false。 不过,在大多数情况下,一旦玩家进入VR,您最好不要禁用耳机,因为这会让玩家感到很不舒服。

新的AR / VR节点

添加了三种新节点类型,用于支持Godot中的AR和VR,以及一种额外的节点类型,尤其是AR。 这些是:

  • ARVROrigin - 我们在世界上的起源点
  • ARVRCamera - a special subclass of the camera, which is positionally tracked
  • ARVRController - 一个新的空间类,它跟踪控制器的位置
  • ARVRAnchor - AR实现的一个定位点,将真实世界的位置映射到您的虚拟世界

前两个必须存在于场景中,AR / VR才能工作,本教程仅关注它们。

ARVROrigin is an important node, you must have one and only one of these somewhere in your scene. This node maps the center of your real world tracking space to a location in your virtual world. Everything else is positionally tracked in relation to this point. Where this point lies exactly differs from one implementation to another, but the best example to understand how this node works is to take a look at a room scale location. While we have functions to adjust the point to center it on the player by default, the origin point will be the center location of the room you are in. As you physically walk around the room, the location of the HMD is tracked in relation to this center position and the tracking is mirror in the virtual world.

To keep things simple, when you physically move around your room, the ARVR Origin point stays where it is, the position of the camera and controllers will be adjusted according to your movements. When you move through the virtual world, either through controller input or when you implement a teleport system, it is the position of the origin point which you will have to adjust.

ARVRCamera is the second node that must always be a part of your scene and it must always be a child node of your origin node. It is a subclass of Godot’s normal camera. However, its position is automatically updated each frame based on the physical orientation and position of the HMD. Also due to the precision required for rendering to an HMD or rendering an AR overlay over a real world camera, most of the standard camera properties are ignored. The only properties of the camera that are used are the near and far plane settings. The FOV, aspect ratio and projection mode are all ignored.

Note that, for our native mobile VR implementation, there is no positional tracking, only the orientation of the phone and by extension, the HMD is tracked. This implementation artificially places the camera at a height (Y) of 1.85.

Conclusion: your minimum setup in your scene to make AR or VR work should look like this:

../../_images/minimum_setup.png

And that’s all you need to get started with the native mobile interface. Obviously, you need to add something more into your scene, so there is something to see, but after that, you can export the game to your phone of choice, pop it into a viewer and away you go.

Official plugins and resources

As mentioned earlier, Godot does not support the various VR and AR SDKs out of the box, you need a plugin for the specific SDK you want to use. There are several official plugins available in the GodotVR Repository.

  • Godot Oculus Mobile provides support for the Oculus Go and Oculus Quest. The Quest will require additional setup documented in 为Oculus Quest开发.
  • Godot OpenVR (not to be confused with OpenXR) supports the OpenVR SDK used by Steam.
  • Godot Oculus <https://github.com/GodotVR/godot_oculus> _支持Oculus SDK(仅PC平台头戴设备)。
  • Godot OpenHMD supports OpenHMD, an open source API and drivers for headsets.
  • Godot OpenXR supports OpenXR, an open standard for VR and AR software. This plugin is early in development, only supports Linux and requires extra setup described in the repository.

These plugins can be downloaded from GitHub or the Godot Asset Library.

In addition to the plugins, there are several official demos.

其他需要考虑的事情

我们需要在本入门手册中简要介绍一些其他一些重要的知识。

The first are our units. In normal 3D games, you don’t have to think a lot about units. As long as everything is at the same scale, a box sized 1 unit by 1 unit by 1 unit can be any size from a cub you can hold in your hand to something the size of a building. In AR and VR, this changes because things in your virtual world are mapped to things in the real world. If you step 1 meter forward in the real world, but you only move 1 cm forward in your virtual world, you have a problem. The same with the position of your controllers; if they don’t appear in the right relative space, it breaks the immersion for the player. Most VR platforms, including our AR/VR Server, assume that 1 unit = 1 meter. The AR/VR server, however, has a property that, for convenience, is also exposed on the ARVROrigin node called world scale. For instance, setting this to a value of 10 changes our coordinate system so 10 units = 1 meter.

Performance is another thing that needs to be carefully considered. Especially VR taxes your game a lot more than most people realize. For mobile VR, you have to be extra careful here, but even for desktop games, there are three factors that make life extra difficult:

  • 您正在渲染立体,两个以一个的价格。 虽然没有完全加倍工作量和管道中的东西,例如考虑到支持新的MultiView OpenGL扩展,但双眼渲染图像仍然存在额外的工作量
  • A normal game will run acceptably on 30fps and ideally manages 60fps. That gives you a big range to play with between lower end and higher end hardware. For any HMD application of AR or VR, however, 60fps is the absolute minimum and you should target your games to run at a stable 90fps to ensure your users don’t get motion sickness right off the bat.
  • 高FOV和相关的镜头失真效果需要许多VR体验才能以两倍的分辨率呈现。 是的,VIVE可能只有每只眼睛1080x1200的分辨率,因此我们将每只眼睛渲染为2160x2400。 对于大多数AR应用程序来说,这不是一个问题。

All in all, the workload your GPU has in comparison with a normal 3D game is a fair amount higher. While things are in the pipeline to improve this, such as MultiView and foveated rendering, these aren’t supported on all devices. This is why you see many VR games using a more art style and if you pay close attention to those VR games that go for realism, you’ll probably notice they’re a bit more conservative on the effects or use some good old optical trickery.