OVRDebugHeadController.cs
4.56 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
/************************************************************************************
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
Your use of this SDK or tool is subject to the Oculus SDK License Agreement, available at
https://developer.oculus.com/licenses/oculussdk/
Unless required by applicable law or agreed to in writing, the Utilities SDK 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.
************************************************************************************/
#if USING_XR_MANAGEMENT && USING_XR_SDK_OCULUS
#define USING_XR_SDK
#endif
#if UNITY_2020_1_OR_NEWER
#define REQUIRES_XR_SDK
#endif
using UnityEngine;
using System.Collections;
#if USING_XR_SDK
using UnityEngine.XR;
using UnityEngine.Experimental.XR;
#endif
/// <summary>
/// This is a simple behavior that can be attached to a parent of the CameraRig in order
/// to provide movement via the gamepad. This is useful when testing an application in
/// the Unity editor without the HMD.
/// To use it, create a game object in your scene and drag your CameraRig to be a child
/// of the game object. Then, add the OVRDebugHeadController behavior to the game object.
/// Alternatively, this behavior can be placed directly on the OVRCameraRig object, but
/// that is not guaranteed to work if OVRCameraRig functionality changes in the future.
/// In the parent case, the object with OVRDebugHeadController can be thougt of as a
/// platform that your camera is attached to. When the platform moves or rotates, the
/// camera moves or rotates, but the camera can still move independently while "on" the
/// platform.
/// In general, this behavior should be disabled when not debugging.
/// </summary>
public class OVRDebugHeadController : MonoBehaviour
{
[SerializeField]
public bool AllowPitchLook = false;
[SerializeField]
public bool AllowYawLook = true;
[SerializeField]
public bool InvertPitch = false;
[SerializeField]
public float GamePad_PitchDegreesPerSec = 90.0f;
[SerializeField]
public float GamePad_YawDegreesPerSec = 90.0f;
[SerializeField]
public bool AllowMovement = false;
[SerializeField]
public float ForwardSpeed = 2.0f;
[SerializeField]
public float StrafeSpeed = 2.0f;
protected OVRCameraRig CameraRig = null;
void Awake()
{
// locate the camera rig so we can use it to get the current camera transform each frame
OVRCameraRig[] CameraRigs = gameObject.GetComponentsInChildren<OVRCameraRig>();
if( CameraRigs.Length == 0 )
Debug.LogWarning("OVRCamParent: No OVRCameraRig attached.");
else if (CameraRigs.Length > 1)
Debug.LogWarning("OVRCamParent: More then 1 OVRCameraRig attached.");
else
CameraRig = CameraRigs[0];
}
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if ( AllowMovement )
{
float gamePad_FwdAxis = OVRInput.Get(OVRInput.RawAxis2D.LThumbstick).y;
float gamePad_StrafeAxis = OVRInput.Get(OVRInput.RawAxis2D.LThumbstick).x;
Vector3 fwdMove = ( CameraRig.centerEyeAnchor.rotation * Vector3.forward ) * gamePad_FwdAxis * Time.deltaTime * ForwardSpeed;
Vector3 strafeMove = ( CameraRig.centerEyeAnchor.rotation * Vector3.right ) * gamePad_StrafeAxis * Time.deltaTime * StrafeSpeed;
transform.position += fwdMove + strafeMove;
}
bool hasDevice = false;
#if USING_XR_SDK
XRDisplaySubsystem currentDisplaySubsystem = OVRManager.GetCurrentDisplaySubsystem();
if (currentDisplaySubsystem != null)
hasDevice = currentDisplaySubsystem.running;
#elif REQUIRES_XR_SDK
hasDevice = false;
#else
hasDevice = UnityEngine.XR.XRDevice.isPresent;
#endif
if ( !hasDevice && ( AllowYawLook || AllowPitchLook ) )
{
Quaternion r = transform.rotation;
if ( AllowYawLook )
{
float gamePadYaw = OVRInput.Get(OVRInput.RawAxis2D.RThumbstick).x;
float yawAmount = gamePadYaw * Time.deltaTime * GamePad_YawDegreesPerSec;
Quaternion yawRot = Quaternion.AngleAxis( yawAmount, Vector3.up );
r = yawRot * r;
}
if ( AllowPitchLook )
{
float gamePadPitch = OVRInput.Get(OVRInput.RawAxis2D.RThumbstick).y;
if ( Mathf.Abs( gamePadPitch ) > 0.0001f )
{
if ( InvertPitch )
{
gamePadPitch *= -1.0f;
}
float pitchAmount = gamePadPitch * Time.deltaTime * GamePad_PitchDegreesPerSec;
Quaternion pitchRot = Quaternion.AngleAxis( pitchAmount, Vector3.left );
r = r * pitchRot;
}
}
transform.rotation = r;
}
}
}