BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Scripts / OVRHeadsetEmulator.cs

OVRHeadsetEmulator.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRHeadsetEmulator.cs 4.9 KiB Download Raw
1/************************************************************************************
2Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
3
4Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
5the Utilities SDK except in compliance with the License, which is provided at the time of installation
6or download, or which otherwise accompanies this software in either electronic or hard copy form.
7
8You may obtain a copy of the License at
9https://developer.oculus.com/licenses/utilities-1.31
10
11Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
12under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
13ANY KIND, either express or implied. See the License for the specific language governing
14permissions and limitations under the License.
15************************************************************************************/
16
17using System.Collections;
18using System.Collections.Generic;
19using UnityEngine;
20
21public class OVRHeadsetEmulator : MonoBehaviour {
22 public enum OpMode
23 {
24 Off,
25 EditorOnly,
26 AlwaysOn
27 }
28
29 public OpMode opMode = OpMode.EditorOnly;
30 public bool resetHmdPoseOnRelease = true;
31 public bool resetHmdPoseByMiddleMouseButton = true;
32
33 public KeyCode[] activateKeys = new KeyCode[] { KeyCode.LeftControl, KeyCode.RightControl };
34
35 public KeyCode[] pitchKeys = new KeyCode[] { KeyCode.LeftAlt, KeyCode.RightAlt };
36
37 OVRManager manager;
38
39 const float MOUSE_SCALE_X = -2.0f;
40 const float MOUSE_SCALE_X_PITCH = -2.0f;
41 const float MOUSE_SCALE_Y = 2.0f;
42 const float MOUSE_SCALE_HEIGHT = 1.0f;
43 const float MAX_ROLL = 85.0f;
44
45 private bool lastFrameEmulationActivated = false;
46
47 private Vector3 recordedHeadPoseRelativeOffsetTranslation;
48 private Vector3 recordedHeadPoseRelativeOffsetRotation;
49
50 private bool hasSentEvent = false;
51 private bool emulatorHasInitialized = false;
52
53 // Use this for initialization
54 void Start () {
55 }
56
57 // Update is called once per frame
58 void Update () {
59 if (!emulatorHasInitialized)
60 {
61 if (OVRManager.OVRManagerinitialized)
62 {
63 Cursor.lockState = CursorLockMode.None;
64 manager = OVRManager.instance;
65 recordedHeadPoseRelativeOffsetTranslation = manager.headPoseRelativeOffsetTranslation;
66 recordedHeadPoseRelativeOffsetRotation = manager.headPoseRelativeOffsetRotation;
67 emulatorHasInitialized = true;
68 }
69 else
70 return;
71 }
72 bool emulationActivated = IsEmulationActivated();
73 if (emulationActivated)
74 {
75 Cursor.lockState = CursorLockMode.Locked;
76
77 if (!lastFrameEmulationActivated && resetHmdPoseOnRelease)
78 {
79 manager.headPoseRelativeOffsetTranslation = recordedHeadPoseRelativeOffsetTranslation;
80 manager.headPoseRelativeOffsetRotation = recordedHeadPoseRelativeOffsetRotation;
81 }
82
83 if (resetHmdPoseByMiddleMouseButton && Input.GetMouseButton(2))
84 {
85 manager.headPoseRelativeOffsetTranslation = Vector3.zero;
86 manager.headPoseRelativeOffsetRotation = Vector3.zero;
87 }
88 else
89 {
90 Vector3 emulatedTranslation = manager.headPoseRelativeOffsetTranslation;
91 float deltaMouseScrollWheel = Input.GetAxis("Mouse ScrollWheel");
92 float emulatedHeight = deltaMouseScrollWheel * MOUSE_SCALE_HEIGHT;
93 emulatedTranslation.y += emulatedHeight;
94 manager.headPoseRelativeOffsetTranslation = emulatedTranslation;
95
96 float deltaX = Input.GetAxis("Mouse X");
97 float deltaY = Input.GetAxis("Mouse Y");
98
99 Vector3 emulatedAngles = manager.headPoseRelativeOffsetRotation;
100 float emulatedRoll = emulatedAngles.x;
101 float emulatedYaw = emulatedAngles.y;
102 float emulatedPitch = emulatedAngles.z;
103 if (IsTweakingPitch())
104 {
105 emulatedPitch += deltaX * MOUSE_SCALE_X_PITCH;
106 }
107 else
108 {
109 emulatedRoll += deltaY * MOUSE_SCALE_Y;
110 emulatedYaw += deltaX * MOUSE_SCALE_X;
111 }
112
113 manager.headPoseRelativeOffsetRotation = new Vector3(emulatedRoll, emulatedYaw, emulatedPitch);
114 }
115
116 if (!hasSentEvent)
117 {
118 OVRPlugin.SendEvent("headset_emulator", "activated");
119 hasSentEvent = true;
120 }
121 }
122 else
123 {
124 Cursor.lockState = CursorLockMode.None;
125 if (lastFrameEmulationActivated)
126 {
127 recordedHeadPoseRelativeOffsetTranslation = manager.headPoseRelativeOffsetTranslation;
128 recordedHeadPoseRelativeOffsetRotation = manager.headPoseRelativeOffsetRotation;
129
130 if (resetHmdPoseOnRelease)
131 {
132 manager.headPoseRelativeOffsetTranslation = Vector3.zero;
133 manager.headPoseRelativeOffsetRotation = Vector3.zero;
134 }
135 }
136 }
137 lastFrameEmulationActivated = emulationActivated;
138 }
139
140 bool IsEmulationActivated()
141 {
142 if (opMode == OpMode.Off)
143 {
144 return false;
145 }
146 else if (opMode == OpMode.EditorOnly && !Application.isEditor)
147 {
148 return false;
149 }
150
151 foreach (KeyCode key in activateKeys)
152 {
153 if (Input.GetKey(key))
154 return true;
155 }
156
157 return false;
158 }
159
160 bool IsTweakingPitch()
161 {
162 if (!IsEmulationActivated())
163 return false;
164
165 foreach (KeyCode key in pitchKeys)
166 {
167 if (Input.GetKey(key))
168 return true;
169 }
170
171 return false;
172 }
173}