18 [RequireComponent(typeof(LeapXRServiceProvider))]
22 private bool _useCustomBaseline =
false;
24 [
MinValue(0), Units(
"MM"), InspectorName(
"Baseline")]
26 private float _customBaselineValue = 64;
29 private bool _showEyePositions =
false;
33 private bool _hasVisitedPreCull =
false;
35 private Camera _cachedCamera;
36 private Camera _camera {
38 if (_cachedCamera ==
null) {
39 _cachedCamera = GetComponent<Camera>();
45 private void onDevice(
Device device) {
49 private void OnEnable() {
50 _provider = GetComponent<LeapServiceProvider>();
51 if (_provider ==
null) {
52 _provider = GetComponentInChildren<LeapServiceProvider>();
53 if (_provider ==
null) {
62 private void OnDisable() {
63 _camera.ResetStereoViewMatrices();
68 private void Update() {
69 _camera.ResetStereoViewMatrices();
70 _hasVisitedPreCull =
false;
73 private void OnPreCull() {
74 if (_hasVisitedPreCull) {
77 _hasVisitedPreCull =
true;
80 if (_useCustomBaseline) {
81 baselineToUse =
Maybe.
Some(_customBaselineValue);
83 baselineToUse = _deviceBaseline;
88 baselineValue *= 1e-3f;
90 Matrix4x4 leftMat = _camera.GetStereoViewMatrix(Camera.StereoscopicEye.Left);
91 Matrix4x4 rightMat = _camera.GetStereoViewMatrix(Camera.StereoscopicEye.Right);
93 Vector3 leftPos = leftMat.inverse.MultiplyPoint3x4(Vector3.zero);
94 Vector3 rightPos = rightMat.inverse.MultiplyPoint3x4(Vector3.zero);
95 float existingBaseline = Vector3.Distance(leftPos, rightPos);
97 float baselineAdjust = baselineValue - existingBaseline;
99 adjustViewMatrix(Camera.StereoscopicEye.Left, baselineAdjust);
100 adjustViewMatrix(Camera.StereoscopicEye.Right, baselineAdjust);
104 private void adjustViewMatrix(Camera.StereoscopicEye eye,
float baselineAdjust) {
105 float eyeOffset = eye == Camera.StereoscopicEye.Left ? 1 : -1;
106 Vector3 ipdOffset = eyeOffset * Vector3.right * baselineAdjust * 0.5f;
107 Vector3 providerForwardOffset = Vector3.zero,
108 providerVerticalOffset = Vector3.zero;
109 Quaternion providerRotation = Quaternion.Euler(0f, 180f, 0f);
114 providerRotation = Quaternion.AngleAxis(_xrProvider.
deviceTiltXAxis, Vector3.right);
116 Matrix4x4 imageMatWarp = _camera.projectionMatrix
117 * Matrix4x4.TRS(Vector3.zero, providerRotation, Vector3.one)
118 * _camera.projectionMatrix.inverse;
119 Shader.SetGlobalMatrix(
"_LeapGlobalWarpedOffset", imageMatWarp);
122 var existingMatrix = _camera.GetStereoViewMatrix(eye);
123 _camera.SetStereoViewMatrix(eye, Matrix4x4.TRS(Vector3.zero, providerRotation, Vector3.one) *
124 Matrix4x4.Translate(providerForwardOffset + ipdOffset) *
125 Matrix4x4.Translate(providerVerticalOffset) *
129 private void OnDrawGizmos() {
130 if (_showEyePositions && Application.isPlaying) {
131 Matrix4x4 leftMat = _camera.GetStereoViewMatrix(Camera.StereoscopicEye.Left);
132 Matrix4x4 rightMat = _camera.GetStereoViewMatrix(Camera.StereoscopicEye.Right);
134 Vector3 leftPos = leftMat.inverse.MultiplyPoint3x4(Vector3.zero);
135 Vector3 rightPos = rightMat.inverse.MultiplyPoint3x4(Vector3.zero);
137 Gizmos.color =
Color.white;
138 Gizmos.DrawSphere(leftPos, 0.02f);
139 Gizmos.DrawSphere(rightPos, 0.02f);
141 Gizmos.color =
Color.blue;
142 Gizmos.DrawLine(leftPos, rightPos);
The Device class represents a physically connected device.
float Baseline
The distance in mm between the center points of the stereo sensors.
Moves the camera to each eye position on pre-render. Only necessary for image pass-through (IR viewer...
The LeapServiceProvider provides tracked Leap Hand data and images from the device via the Leap servi...
Action< Device > OnDeviceSafe
A utility event to get a callback whenever a new device is connected to the service....
The LeapXRServiceProvider expands on the standard LeapServiceProvider to account for the offset of th...
static readonly NoneType None
static Maybe< T > Some(T t)
Constructs a Maybe given a specific value. This value needs to always be non-null if the type is a re...
bool TryGetValue(out T t)
If this Maybe has a value, the out argument is filled with that value and this method returns true,...