OVRHeadsetEmulator.cs
4.91 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/************************************************************************************
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.
************************************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OVRHeadsetEmulator : MonoBehaviour {
public enum OpMode
{
Off,
EditorOnly,
AlwaysOn
}
public OpMode opMode = OpMode.EditorOnly;
public bool resetHmdPoseOnRelease = true;
public bool resetHmdPoseByMiddleMouseButton = true;
public KeyCode[] activateKeys = new KeyCode[] { KeyCode.LeftControl, KeyCode.RightControl };
public KeyCode[] pitchKeys = new KeyCode[] { KeyCode.LeftAlt, KeyCode.RightAlt };
OVRManager manager;
const float MOUSE_SCALE_X = -2.0f;
const float MOUSE_SCALE_X_PITCH = -2.0f;
const float MOUSE_SCALE_Y = 2.0f;
const float MOUSE_SCALE_HEIGHT = 1.0f;
const float MAX_ROLL = 85.0f;
private bool lastFrameEmulationActivated = false;
private Vector3 recordedHeadPoseRelativeOffsetTranslation;
private Vector3 recordedHeadPoseRelativeOffsetRotation;
private bool hasSentEvent = false;
private bool emulatorHasInitialized = false;
private CursorLockMode previousCursorLockMode = CursorLockMode.None;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (!emulatorHasInitialized)
{
if (OVRManager.OVRManagerinitialized)
{
previousCursorLockMode = Cursor.lockState;
manager = OVRManager.instance;
recordedHeadPoseRelativeOffsetTranslation = manager.headPoseRelativeOffsetTranslation;
recordedHeadPoseRelativeOffsetRotation = manager.headPoseRelativeOffsetRotation;
emulatorHasInitialized = true;
lastFrameEmulationActivated = false;
}
else
return;
}
bool emulationActivated = IsEmulationActivated();
if (emulationActivated)
{
if (!lastFrameEmulationActivated)
{
previousCursorLockMode = Cursor.lockState;
Cursor.lockState = CursorLockMode.Locked;
}
if (!lastFrameEmulationActivated && resetHmdPoseOnRelease)
{
manager.headPoseRelativeOffsetTranslation = recordedHeadPoseRelativeOffsetTranslation;
manager.headPoseRelativeOffsetRotation = recordedHeadPoseRelativeOffsetRotation;
}
if (resetHmdPoseByMiddleMouseButton && Input.GetMouseButton(2))
{
manager.headPoseRelativeOffsetTranslation = Vector3.zero;
manager.headPoseRelativeOffsetRotation = Vector3.zero;
}
else
{
Vector3 emulatedTranslation = manager.headPoseRelativeOffsetTranslation;
float deltaMouseScrollWheel = Input.GetAxis("Mouse ScrollWheel");
float emulatedHeight = deltaMouseScrollWheel * MOUSE_SCALE_HEIGHT;
emulatedTranslation.y += emulatedHeight;
manager.headPoseRelativeOffsetTranslation = emulatedTranslation;
float deltaX = Input.GetAxis("Mouse X");
float deltaY = Input.GetAxis("Mouse Y");
Vector3 emulatedAngles = manager.headPoseRelativeOffsetRotation;
float emulatedRoll = emulatedAngles.x;
float emulatedYaw = emulatedAngles.y;
float emulatedPitch = emulatedAngles.z;
if (IsTweakingPitch())
{
emulatedPitch += deltaX * MOUSE_SCALE_X_PITCH;
}
else
{
emulatedRoll += deltaY * MOUSE_SCALE_Y;
emulatedYaw += deltaX * MOUSE_SCALE_X;
}
manager.headPoseRelativeOffsetRotation = new Vector3(emulatedRoll, emulatedYaw, emulatedPitch);
}
if (!hasSentEvent)
{
OVRPlugin.SendEvent("headset_emulator", "activated");
hasSentEvent = true;
}
}
else
{
if (lastFrameEmulationActivated)
{
Cursor.lockState = previousCursorLockMode;
recordedHeadPoseRelativeOffsetTranslation = manager.headPoseRelativeOffsetTranslation;
recordedHeadPoseRelativeOffsetRotation = manager.headPoseRelativeOffsetRotation;
if (resetHmdPoseOnRelease)
{
manager.headPoseRelativeOffsetTranslation = Vector3.zero;
manager.headPoseRelativeOffsetRotation = Vector3.zero;
}
}
}
lastFrameEmulationActivated = emulationActivated;
}
bool IsEmulationActivated()
{
if (opMode == OpMode.Off)
{
return false;
}
else if (opMode == OpMode.EditorOnly && !Application.isEditor)
{
return false;
}
foreach (KeyCode key in activateKeys)
{
if (Input.GetKey(key))
return true;
}
return false;
}
bool IsTweakingPitch()
{
if (!IsEmulationActivated())
return false;
foreach (KeyCode key in pitchKeys)
{
if (Input.GetKey(key))
return true;
}
return false;
}
}