forked from gradientspace/frame3Sharp
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVRMouseCursorController.cs
302 lines (246 loc) · 12.1 KB
/
VRMouseCursorController.cs
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using UnityEngine;
using System;
using System.Collections;
using g3;
namespace f3 {
public class VRMouseCursorController : ICursorController
{
Camera camera;
FContext context;
public GameObject Cursor { get; set; }
public float CursorVisualAngleInDegrees { get; set; }
public Material CursorDefaultMaterial { get; set; }
public Material CursorHitMaterial { get; set; }
public Material CursorCapturingMaterial { get; set; }
public Vector3 CurrentCursorPosWorld;
public Vector3 CurrentCursorRaySourceWorld;
public Ray3f CurrentCursorWorldRay()
{
Vector3f camPos = CurrentCursorRaySourceWorld;
Vector3f cursorPos = CurrentCursorPosWorld;
Ray3f ray = new Ray3f(camPos, (cursorPos - camPos).Normalized);
if (Math.Abs(ray.Direction.LengthSquared - 1) > 0.001f)
ray = new Ray3f(camPos, Vector3f.AxisY);
return ray;
}
public bool HasSecondPosition { get { return false; } }
public Ray3f SecondWorldRay() {
throw new NotImplementedException("VRMouseCursorController.SecondWorldRay: not supported!");
}
public Ray3f CurrentCursorOrthoRay()
{
throw new NotImplementedException("VRMouseCursorController.CurrentCursorUIRay: UI layer not supported in VR!");
}
GameObject xformObject; // [RMS] this is an internal GO we use basically just for a transform
// Actually a plane that stays in front of eye.
Vector3 vCursorPlaneOrigin;
Vector3 vCursorPlaneRight;
Vector3 vCursorPlaneUp;
Vector3 vRaySourcePosition;
float fCursorSpeedNormalization;
bool bWasInCaptureFreeze;
float fCurPlaneX;
float fCurPlaneY;
Vector3 vPlaneCursorPos;
Vector3 vSceneCursorPos;
Mesh standardCursorMesh;
Mesh activeToolCursorMesh;
float lastMouseEventTime;
bool mouseInactiveState;
public bool MouseInactive { get { return mouseInactiveState; } }
// to use this, see RequestFreezeCursor()
bool bFreezeCursor = false;
public VRMouseCursorController(Camera viewCam, FContext context) {
camera = viewCam;
this.context = context;
}
// Use this for initialization
public void Start () {
fCursorSpeedNormalization = 1.0f;
fCurPlaneX = 0;
fCurPlaneY = 0;
vPlaneCursorPos = Vector3.zero;
vSceneCursorPos = vPlaneCursorPos;
CursorDefaultMaterial = MaterialUtil.CreateTransparentMaterial (Color.grey, 0.6f);
//CursorHitMaterial = MaterialUtil.CreateTransparentMaterial (Color.yellow, 0.8f);
CursorHitMaterial = MaterialUtil.CreateStandardMaterial(Color.yellow);
CursorCapturingMaterial = MaterialUtil.CreateTransparentMaterial (Color.yellow, 0.75f);
CursorVisualAngleInDegrees = 1.5f;
standardCursorMesh = MeshGenerators.Create3DArrow(1.0f, 1.0f, 1.0f, 0.5f, 16);
UnityUtil.TranslateMesh(standardCursorMesh, 0, -2.0f, 0);
activeToolCursorMesh = MeshGenerators.Create3DArrow(1.0f, 1.0f, 1.0f, 1.0f, 16);
UnityUtil.TranslateMesh(activeToolCursorMesh, 0, -2.0f, 0);
Cursor = UnityUtil.CreateMeshGO("cursor", standardCursorMesh, CursorDefaultMaterial);
Cursor.SetSharedMesh(standardCursorMesh);
Cursor.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
Cursor.transform.localRotation = Quaternion.AngleAxis(45.0f, new Vector3(1, 0, 1).normalized);
MaterialUtil.DisableShadows(Cursor);
xformObject = GameObject.CreatePrimitive (PrimitiveType.Plane);
xformObject.SetName("cursor_plane");
MaterialUtil.DisableShadows(xformObject);
xformObject.GetComponent<MeshRenderer>().material
= MaterialUtil.CreateTransparentMaterial (Color.cyan, 0.2f);
xformObject.GetComponent<MeshRenderer>().enabled = false;
lastMouseEventTime = FPlatform.RealTime();
mouseInactiveState = false;
}
// FixedUpdate is called before any Update
public void Update () {
if (bFreezeCursor)
return;
// if we are in capture we freeze the cursor plane
if (context.InCaptureMouse == false) {
Vector3 camPos = camera.gameObject.transform.position;
Vector3 forward = camera.gameObject.transform.forward;
// orient Y-up plane so that it is in front of eye, perp to camera direction
float fCursorDepth = 10.0f;
fCursorSpeedNormalization = 1.0f;
if (context.ActiveCockpit != null && context.ActiveCockpit.DefaultCursorDepth > 0) {
fCursorDepth = context.ActiveCockpit.DefaultCursorDepth;
// cursor speed will change depending on cursor plane distance, unless we normalize
fCursorSpeedNormalization *= (fCursorDepth / 10.0f);
}
xformObject.transform.position = camPos + fCursorDepth * forward;
xformObject.transform.LookAt (camera.gameObject.transform);
xformObject.transform.RotateAround (xformObject.transform.position, xformObject.transform.right, 90);
// that plane is the plane the mouse cursor moves on
this.vCursorPlaneOrigin = xformObject.transform.position;
this.vCursorPlaneRight = xformObject.transform.right;
this.vCursorPlaneUp = xformObject.transform.forward; // because we rotated? weird...
this.vRaySourcePosition = camera.transform.position;
// if we were capturing, then plane was frozen and when we stop capturing, if
// head moved, the cursor will pop to a new position (because it is stored in
// local plane coords). So raycast through old cursor to hit new plane and figure
// out new local coords (fCurPlaneX, fCurPlaneY)
if (bWasInCaptureFreeze) {
Frame3f newF = new Frame3f(vCursorPlaneOrigin, this.camera.transform.forward);
Vector3 vPlaneHit = newF.RayPlaneIntersection(this.vRaySourcePosition,
(vPlaneCursorPos - vRaySourcePosition).normalized, 2);
fCurPlaneX = Vector3.Dot((vPlaneHit - vCursorPlaneOrigin), vCursorPlaneRight);
fCurPlaneY = Vector3.Dot((vPlaneHit - vCursorPlaneOrigin), vCursorPlaneUp);
bWasInCaptureFreeze = false;
}
} else {
bWasInCaptureFreeze = true;
}
Vector2f mousePos = InputExtension.Get.Mouse.PositionDelta;
Vector2f leftStick = InputExtension.Get.GamepadLeftStick.Position;
float fX = mousePos.x + leftStick.x;
float fY = mousePos.y + leftStick.y;
// auto-hide cursor if it doesn't move for a while
if (fX == 0 && fY == 0 && SceneGraphConfig.MouseCursorHideTimeout > 0) {
if ((FPlatform.RealTime() - lastMouseEventTime) > SceneGraphConfig.MouseCursorHideTimeout) {
Cursor.SetVisible(false);
mouseInactiveState = true;
}
if (mouseInactiveState)
return;
} else {
lastMouseEventTime = FPlatform.RealTime();
if (mouseInactiveState)
Cursor.SetVisible(true);
mouseInactiveState = false;
}
// update cursor location
fCurPlaneX -= 0.3f * fX * fCursorSpeedNormalization;
fCurPlaneY -= 0.3f * fY * fCursorSpeedNormalization;
vPlaneCursorPos =
vCursorPlaneOrigin + fCurPlaneX * vCursorPlaneRight + fCurPlaneY * vCursorPlaneUp;
vSceneCursorPos = vPlaneCursorPos;
// if cursor gets outside of viewpoint it is almost impossible to get it back.
// So, if it goes too far out of view (45 deg here), we snap it back to the origin
if (context.InCameraManipulation == false && context.InCaptureMouse == false) {
float fAngle = Vector3.Angle((vPlaneCursorPos - camera.transform.position).normalized, camera.transform.forward);
if (fAngle > 50.0f) {
fCurPlaneX = fCurPlaneY = 0;
vPlaneCursorPos =
vCursorPlaneOrigin + fCurPlaneX * vCursorPlaneRight + fCurPlaneY * vCursorPlaneUp;
vSceneCursorPos = vPlaneCursorPos;
}
}
bool bHit = false;
// [RMS] boundsHit cursor orientation could be useful for things where you are picking a point
// on the ground plane (eg like drawing contours). Not sure how to toggle that though.
// Just disabling for now...
//bool bIsBoundsHit = false;
if (context != null) {
Ray r = new Ray (camera.transform.position, (vPlaneCursorPos - camera.transform.position).normalized);
AnyRayHit hit = null;
if (context.FindAnyRayIntersection(r, out hit)) {
vSceneCursorPos = hit.hitPos;
bHit = true;
} else {
GameObjectRayHit ghit = null;
if (context.GetScene().FindWorldBoundsHit(r, out ghit)) {
vSceneCursorPos = ghit.hitPos;
//bIsBoundsHit = true;
}
}
}
this.CurrentCursorPosWorld = vPlaneCursorPos;
this.CurrentCursorRaySourceWorld = this.vRaySourcePosition;
Vector3 vEyeToPos = (vPlaneCursorPos - camera.transform.position).normalized;
//if (bIsBoundsHit) {
// Vector3 rotAxis = (vEyeToPos + camera.transform.right).normalized;
// Cursor.transform.localRotation = Quaternion.AngleAxis(180.0f-45.0f, rotAxis);
//} else {
Quaternion rotAlignUp = Quaternion.FromToRotation(Vector3.up, camera.transform.up);
Vector3 rotAxis = (vEyeToPos + camera.transform.right).normalized;
Cursor.transform.localRotation = Quaternion.AngleAxis(45.0f, rotAxis) * rotAlignUp;
//}
Cursor.transform.position = vSceneCursorPos;
if (context.InCaptureMouse)
Cursor.GetComponent<MeshRenderer> ().material = CursorCapturingMaterial;
else if (bHit)
Cursor.GetComponent<MeshRenderer> ().material = CursorHitMaterial;
else
Cursor.GetComponent<MeshRenderer> ().material = CursorDefaultMaterial;
Cursor.SetLayer(FPlatform.CursorLayer);
// maintain a consistent visual size for 3D cursor sphere
float fScaling = VRUtil.GetVRRadiusForVisualAngle(vSceneCursorPos, camera.transform.position, CursorVisualAngleInDegrees);
Cursor.transform.localScale = new Vector3 (fScaling, fScaling, fScaling);
// update cursor
Mesh useMesh = context.ToolManager.HasActiveTool(ToolSide.Right) ? activeToolCursorMesh : standardCursorMesh;
if ( Cursor.GetSharedMesh() != useMesh ) {
Cursor.SetSharedMesh(useMesh);
}
}
public void ResetCursorToCenter()
{
fCurPlaneX = 0;
fCurPlaneY = 0;
}
public void HideCursor() {
if (mouseInactiveState == false) {
Cursor.SetVisible(false);
mouseInactiveState = true;
}
}
public void ShowCursor()
{
Cursor.Show();
mouseInactiveState = false;
lastMouseEventTime = FPlatform.RealTime();
}
public class AutoUnfreezer {
Action unfreeze;
public AutoUnfreezer(Action unfreezer) { this.unfreeze = unfreezer; }
public void Unfreeze() { unfreeze(); unfreeze = null; }
~AutoUnfreezer() { if ( unfreeze != null) unfreeze(); }
}
void unfreeze_cursor() {
bFreezeCursor = false;
ShowCursor();
}
public AutoUnfreezer RequestFreezeCursor()
{
if (bFreezeCursor == false) {
bFreezeCursor = true;
HideCursor();
return new AutoUnfreezer(() => { unfreeze_cursor(); });
} else {
throw new System.Exception("MouseCursorController.RequestFreeze but already frozen");
}
}
}
}