LeapVRCameraControl.cs
4.94 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
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using UnityEngine;
using UnityEngine.Rendering;
using System;
namespace Leap.Unity {
/**Provides a variety of VR related camera utilities, for example controlling IPD and camera distance */
[RequireComponent(typeof(Camera))]
[ExecuteInEditMode]
public class LeapVRCameraControl : MonoBehaviour {
public const string GLOBAL_EYE_UV_OFFSET_NAME = "_LeapGlobalStereoUVOffset";
private static Vector2 LEFT_EYE_UV_OFFSET = new Vector2(0, 0);
private static Vector2 RIGHT_EYE_UV_OFFSET = new Vector2(0, 0.5f);
//When using VR, the cameras do not have valid parameters until the first frame begins rendering,
//so if you need valid parameters for initialization, you can use this callback to get notified
//when they become available.
public static event Action<CameraParams> OnValidCameraParams;
private static bool _hasDispatchedValidCameraParams = false;
public static event Action<Camera> OnLeftPreRender;
public static event Action<Camera> OnRightPreRender;
[SerializeField]
private EyeType _eyeType = new EyeType(EyeType.OrderType.CENTER);
[SerializeField]
private bool _overrideEyePosition = true;
public bool OverrideEyePosition { get { return _overrideEyePosition; } set { _overrideEyePosition = value; } }
private Camera _cachedCamera;
private Camera _camera {
get {
if (_cachedCamera == null) {
_cachedCamera = GetComponent<Camera>();
}
return _cachedCamera;
}
}
private Matrix4x4 _finalCenterMatrix;
private LeapDeviceInfo _deviceInfo;
void Start() {
#if UNITY_EDITOR
if (!Application.isPlaying) {
return;
}
#endif
_deviceInfo = LeapDeviceInfo.GetLeapDeviceInfo();
}
void Update() {
#if UNITY_EDITOR
_eyeType.UpdateOrderGivenComponent(this);
if (!Application.isPlaying) {
return;
}
#endif
_hasDispatchedValidCameraParams = false;
}
void OnPreCull() {
#if UNITY_EDITOR
if (!Application.isPlaying) {
return;
}
#endif
_camera.ResetWorldToCameraMatrix();
_finalCenterMatrix = _camera.worldToCameraMatrix;
if (!_hasDispatchedValidCameraParams) {
CameraParams cameraParams = new CameraParams(_cachedCamera);
if (OnValidCameraParams != null) {
OnValidCameraParams(cameraParams);
}
_hasDispatchedValidCameraParams = true;
}
}
void OnPreRender() {
#if UNITY_EDITOR
if (!Application.isPlaying) {
return;
}
#endif
_eyeType.BeginCamera();
if (_eyeType.IsLeftEye) {
Shader.SetGlobalVector(GLOBAL_EYE_UV_OFFSET_NAME, LEFT_EYE_UV_OFFSET);
if (OnLeftPreRender != null) OnLeftPreRender(_cachedCamera);
} else {
Shader.SetGlobalVector(GLOBAL_EYE_UV_OFFSET_NAME, RIGHT_EYE_UV_OFFSET);
if (OnRightPreRender != null) OnRightPreRender(_cachedCamera);
}
Matrix4x4 offsetMatrix;
if (_overrideEyePosition) {
offsetMatrix = _finalCenterMatrix;
Vector3 ipdOffset = (_eyeType.IsLeftEye ? 1 : -1) * transform.right * _deviceInfo.baseline * 0.5f;
Vector3 forwardOffset = -transform.forward * _deviceInfo.forwardOffset;
offsetMatrix *= Matrix4x4.TRS(ipdOffset + forwardOffset, Quaternion.identity, Vector3.one);
} else {
offsetMatrix = _camera.worldToCameraMatrix;
}
_camera.worldToCameraMatrix = offsetMatrix;
}
public struct CameraParams {
public readonly Transform CenterEyeTransform;
public readonly Matrix4x4 ProjectionMatrix;
public readonly int Width;
public readonly int Height;
public CameraParams(Camera camera) {
CenterEyeTransform = camera.transform;
ProjectionMatrix = camera.projectionMatrix;
switch (SystemInfo.graphicsDeviceType) {
case GraphicsDeviceType.Direct3D11:
case GraphicsDeviceType.Direct3D12:
for (int i = 0; i < 4; i++) {
ProjectionMatrix[1, i] = -ProjectionMatrix[1, i];
}
// Scale and bias from OpenGL -> D3D depth range
for (int i = 0; i < 4; i++) {
ProjectionMatrix[2, i] = ProjectionMatrix[2, i] * 0.5f + ProjectionMatrix[3, i] * 0.5f;
}
break;
}
Width = camera.pixelWidth;
Height = camera.pixelHeight;
}
}
}
}