GvrVRHelpers.cs
5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//-----------------------------------------------------------------------
// <copyright file="GvrVRHelpers.cs" company="Google Inc.">
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-----------------------------------------------------------------------
using System.Collections;
using Gvr.Internal;
using UnityEngine;
using UnityEngine.EventSystems;
#if UNITY_2017_2_OR_NEWER
using UnityEngine.XR;
#else
using UnityEngine.VR;
using XRNode = UnityEngine.VR.VRNode;
using XRSettings = UnityEngine.VR.VRSettings;
#endif // UNITY_2017_2_OR_NEWER
/// <summary>Helper functions common to GVR VR applications.</summary>
public static class GvrVRHelpers
{
/// <summary>Gets the center of the screen or eye texture, in pixels.</summary>
/// <returns>The center of the screen, in pixels.</returns>
public static Vector2 GetViewportCenter()
{
int viewportWidth = Screen.width;
int viewportHeight = Screen.height;
if (XRSettings.enabled)
{
viewportWidth = XRSettings.eyeTextureWidth;
viewportHeight = XRSettings.eyeTextureHeight;
}
return new Vector2(0.5f * viewportWidth, 0.5f * viewportHeight);
}
/// <summary>Gets the forward vector relative to the headset's rotation.</summary>
/// <returns>The forward vector relative to the headset's rotation.</returns>
public static Vector3 GetHeadForward()
{
return GetHeadRotation() * Vector3.forward;
}
/// <summary>Gets the headset's rotation.</summary>
/// <returns>The headset's rotation.</returns>
public static Quaternion GetHeadRotation()
{
#if UNITY_EDITOR
if (InstantPreview.IsActive)
{
// In-editor; Instant Preview is active:
return Camera.main.transform.localRotation;
}
else
{
// In-editor; Instant Preview is not active:
if (GvrEditorEmulator.Instance == null)
{
Debug.LogWarning("No GvrEditorEmulator instance was found in your scene. Please " +
"ensure that GvrEditorEmulator exists in your scene.");
return Quaternion.identity;
}
return GvrEditorEmulator.Instance.HeadRotation;
}
#else
// Not running in editor:
return InputTracking.GetLocalRotation(XRNode.Head);
#endif // UNITY_EDITOR
}
/// <summary>Gets the head's position.</summary>
/// <returns>The head's position.</returns>
public static Vector3 GetHeadPosition()
{
#if UNITY_EDITOR
if (GvrEditorEmulator.Instance == null)
{
Debug.LogWarning("No GvrEditorEmulator instance was found in your scene. Please " +
"ensure that GvrEditorEmulator exists in your scene.");
return Vector3.zero;
}
return GvrEditorEmulator.Instance.HeadPosition;
#else
return InputTracking.GetLocalPosition(XRNode.Head);
#endif // UNITY_EDITOR
}
/// <summary>Gets the recommended max laser distance, based on raycast mode.</summary>
/// <param name="mode">The `RaycastMode` for which to get the recommended distance.</param>
/// <returns>The recommended maximum laser distance for the given mode.</returns>
public static float GetRecommendedMaxLaserDistance(GvrBasePointer.RaycastMode mode)
{
switch (mode)
{
case GvrBasePointer.RaycastMode.Direct:
return 20.0f;
case GvrBasePointer.RaycastMode.Hybrid:
return 1.0f;
case GvrBasePointer.RaycastMode.Camera:
default:
return 0.75f;
}
}
/// <summary>Gets the distance at which the `Direct` and `Camera` raycasts intersect.</summary>
/// <remarks>
/// This is the the point at which `Hybrid` mode will transition from `Direct` (closer than the
/// intersection) to `Camera` (further than the intersection) mode.
/// </remarks>
/// <param name="mode">
/// The `RaycastMode` for which to get the intersection distance. Only returns non-zero when
/// this is `RaycastMode.Camera`.
/// </param>
/// <returns>The distance at which the `Direct` and `Camera` raycasts intersect.</returns>
public static float GetRayIntersection(GvrBasePointer.RaycastMode mode)
{
switch (mode)
{
case GvrBasePointer.RaycastMode.Direct:
return 0.0f;
case GvrBasePointer.RaycastMode.Hybrid:
return 0.0f;
case GvrBasePointer.RaycastMode.Camera:
default:
return 2.5f;
}
}
/// <summary>Returns a value indicating whether the laser is visually shrunken.</summary>
/// <param name="mode">The `RaycastMode` for which to check behavior.</param>
/// <returns>Returns `true` if the laser is shrunken, `false`a otherwise.</returns>
public static bool GetShrinkLaser(GvrBasePointer.RaycastMode mode)
{
switch (mode)
{
case GvrBasePointer.RaycastMode.Direct:
return false;
case GvrBasePointer.RaycastMode.Hybrid:
return true;
case GvrBasePointer.RaycastMode.Camera:
default:
return false;
}
}
}