-
Notifications
You must be signed in to change notification settings - Fork 6
/
PassthroughFader.cs
283 lines (242 loc) · 9.15 KB
/
PassthroughFader.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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* Licensed under the Oculus SDK License Agreement (the "License");
* you may not use the Oculus SDK except in compliance with the License,
* which is provided at the time of installation or download, or which
* otherwise accompanies this software in either electronic or hard copy form.
*
* You may obtain a copy of the License at
*
* https://developer.oculus.com/licenses/oculussdk/
*
* Unless required by applicable law or agreed to in writing, the Oculus 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 UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
/// <summary>
/// This is an all-in-one solution for the passthrough fader containing both Selective and Underlay mode.
/// It allows you to switch between the two modes in the inspector or at runtime.
/// </summary>
public class PassthroughFader : MonoBehaviour
{
/// <summary>
/// Defines the direction of the fade effect.
/// </summary>
private enum FadeDirection
{
Normal,
RightToLeft,
TopToBottom,
InsideOut
}
/// <summary>
/// Defines the viewing mode of passthrough (choose selective for limiting distance).
/// </summary>
private enum PassthroughViewingMode
{
Underlay,
Selective
}
/// <summary>
/// Determines the current state we are in.
/// </summary>
private enum FaderState
{
MR,
VR,
InTransition
}
private FaderState State => Mathf.Approximately(_targetAlpha, 1) ? FaderState.MR :
Mathf.Approximately(_targetAlpha, 0) ? FaderState.VR :
FaderState.InTransition;
[Header("Passthrough Fader Settings")]
[Tooltip("The passthrough layer used for the fade effect.")]
[SerializeField] private OVRPassthroughLayer oVRPassthroughLayer;
[Tooltip("The mode of the passthrough view. Decides if we look at the regular Underlay or through the selective Passthrough on our sphere")]
[SerializeField] private PassthroughViewingMode passthroughViewingMode = PassthroughViewingMode.Selective;
[Tooltip("The size/range of the passthrough fader sphere (used when Passthrough Mode is set to Selective).")]
[Range(0.01f, 100f)]
[SerializeField] private float selectiveDistance = 5f;
[Tooltip("The speed of the fade effect.")]
[SerializeField] private float fadeSpeed = 1f;
[Tooltip("The direction of the fade effect.")]
[SerializeField] private FadeDirection fadeDirection = FadeDirection.TopToBottom;
[Header("Fade Events")]
[Tooltip("Event triggered when the fade in starts.")]
[SerializeField] private UnityEvent onStartFadeIn = new();
[Tooltip("Event triggered when the fade out starts.")]
[SerializeField] private UnityEvent onStartFadeOut = new();
[Tooltip("Event triggered when the fade in has ended.")]
[SerializeField] private UnityEvent onFadeInComplete = new();
[Tooltip("Event triggered when the fade out has ended.")]
[SerializeField] private UnityEvent onFadeOutComplete = new();
private Camera _mainCamera;
private Material _material;
private MeshRenderer _meshRenderer;
private UIPanel _uiPanel;
private Button _passthroughButton;
private Color _skyboxBackgroundColor;
private float _targetAlpha;
private const float FadeTolerance = 0.001f;
private static readonly int InvertedAlpha = Shader.PropertyToID("_InvertedAlpha");
private static readonly int Direction = Shader.PropertyToID("_FadeDirection");
private void Awake()
{
_mainCamera = Camera.main;
if (_mainCamera != null)
{
_skyboxBackgroundColor = _mainCamera.backgroundColor;
}
// This is a property that determines whether premultiplied alpha blending is used for the eye field of view
// layer, which can be adjusted to enhance the blending with underlays and potentially improve visual quality.
OVRManager.eyeFovPremultipliedAlphaModeEnabled = false;
_meshRenderer = GetComponent<MeshRenderer>();
_material = _meshRenderer.material;
_uiPanel = FindObjectOfType<UIPanel>();
if (_uiPanel != null)
{
_passthroughButton = _uiPanel.Button;
_passthroughButton.onClick.AddListener(TogglePassthrough);
}
oVRPassthroughLayer.PassthroughLayerResumed += OnPassthroughLayerResumed;
SetupPassthrough();
#if UNITY_ANDROID
CheckIfPassthroughIsRecommended();
#endif
}
private void OnDestroy()
{
if (_uiPanel != null)
{
_passthroughButton.onClick.RemoveListener(TogglePassthrough);
}
oVRPassthroughLayer.PassthroughLayerResumed -= OnPassthroughLayerResumed;
}
private void SetupPassthrough()
{
switch (passthroughViewingMode)
{
case PassthroughViewingMode.Underlay:
{
var maxCamView = _mainCamera.farClipPlane - 0.01f;
transform.localScale = new Vector3(maxCamView, maxCamView, maxCamView);
_meshRenderer.enabled = false;
break;
}
case PassthroughViewingMode.Selective:
transform.localScale = new Vector3(selectiveDistance, selectiveDistance, selectiveDistance);
_meshRenderer.enabled = true;
break;
}
}
private void CheckIfPassthroughIsRecommended()
{
if (_mainCamera == null)
{
return;
}
if (OVRManager.IsPassthroughRecommended())
{
if (passthroughViewingMode == PassthroughViewingMode.Underlay)
{
_mainCamera.clearFlags = CameraClearFlags.SolidColor;
_mainCamera.backgroundColor = Color.clear;
}
else
{
_mainCamera.clearFlags = CameraClearFlags.Skybox;
_mainCamera.backgroundColor = _skyboxBackgroundColor;
}
_material.SetFloat(InvertedAlpha, 1);
}
else
{
oVRPassthroughLayer.enabled = false;
_mainCamera.clearFlags = CameraClearFlags.Skybox;
_mainCamera.backgroundColor = _skyboxBackgroundColor;
_material.SetFloat(InvertedAlpha, 0);
}
}
public void TogglePassthrough()
{
UpdateFadeDirection();
switch (State)
{
case FaderState.MR:
{
if (passthroughViewingMode == PassthroughViewingMode.Underlay)
{
_meshRenderer.enabled = true;
_mainCamera.clearFlags = CameraClearFlags.Skybox;
_mainCamera.backgroundColor = _skyboxBackgroundColor;
}
_targetAlpha = 0;
onStartFadeOut?.Invoke();
StopAllCoroutines();
StartCoroutine(FadeToTarget());
break;
}
case FaderState.VR:
oVRPassthroughLayer.enabled = true;
onStartFadeIn?.Invoke();
break;
case FaderState.InTransition:
{
_targetAlpha = Mathf.Approximately(_targetAlpha, 0) ? 1 : 0;
var fadeEvent = Mathf.Approximately(_targetAlpha, 0) ? onStartFadeOut : onStartFadeIn;
fadeEvent?.Invoke();
StartCoroutine(FadeToTarget());
break;
}
}
}
private void UpdateFadeDirection()
{
_material.SetInt(Direction, (int)fadeDirection);
}
private void OnPassthroughLayerResumed()
{
_meshRenderer.enabled = passthroughViewingMode == PassthroughViewingMode.Underlay;
_targetAlpha = 1;
StopAllCoroutines();
StartCoroutine(FadeToTarget());
}
private IEnumerator FadeToTarget()
{
var currentAlpha = _material.GetFloat(InvertedAlpha);
while (Mathf.Abs(currentAlpha - _targetAlpha) > 0)
{
currentAlpha = Mathf.MoveTowards(currentAlpha, _targetAlpha, fadeSpeed * Time.deltaTime);
_material.SetFloat(InvertedAlpha, currentAlpha);
yield return null;
}
if (Mathf.Abs(_targetAlpha - 1) < FadeTolerance)
{
if (passthroughViewingMode == PassthroughViewingMode.Underlay)
{
_mainCamera.clearFlags = CameraClearFlags.SolidColor;
_mainCamera.backgroundColor = Color.clear;
}
onFadeInComplete?.Invoke();
}
else
{
oVRPassthroughLayer.enabled = false;
if (passthroughViewingMode == PassthroughViewingMode.Underlay)
{
_mainCamera.clearFlags = CameraClearFlags.Skybox;
_mainCamera.backgroundColor = _skyboxBackgroundColor;
}
onFadeOutComplete?.Invoke();
}
_meshRenderer.enabled = passthroughViewingMode != PassthroughViewingMode.Underlay;
}
}