-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathPassthroughSlider.cs
137 lines (113 loc) · 4.36 KB
/
PassthroughSlider.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
// Copyright (c) Meta Platforms, Inc. and affiliates.
using MRMotifs.SharedAssets;
using UnityEngine;
using UnityEngine.UI;
namespace MRMotifs.PassthroughTransitioning
{
public class PassthroughSlider : MonoBehaviour
{
[Tooltip("The passthrough layer used for the fade effect.")]
[SerializeField]
private OVRPassthroughLayer oVRPassthroughLayer;
[Tooltip("The direction of the fade effect.")]
[SerializeField]
private FadeDirection fadeDirection = FadeDirection.InsideOut;
[Tooltip("The range of the passthrough fader sphere.")]
[SerializeField]
private float selectiveDistance = 2f;
[Tooltip("The inverted alpha value at which the contextual boundary should be enabled/disabled.")]
[SerializeField]
private float boundaryThreshold = 0.75f;
private Camera m_mainCamera;
private Material m_material;
private MeshRenderer m_meshRenderer;
private MenuPanel m_menuPanel;
private Slider m_alphaSlider;
private bool m_hasCrossedThreshold;
private static readonly int s_invertedAlpha = Shader.PropertyToID("_InvertedAlpha");
private static readonly int s_direction = Shader.PropertyToID("_FadeDirection");
/// <summary>
/// Defines the direction of the fade effect.
/// </summary>
private enum FadeDirection
{
Normal,
RightToLeft,
TopToBottom,
InsideOut
}
private void Awake()
{
m_mainCamera = Camera.main;
if (m_mainCamera != null)
{
m_mainCamera.clearFlags = CameraClearFlags.Skybox;
}
// 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;
m_meshRenderer = GetComponent<MeshRenderer>();
m_material = m_meshRenderer.material;
m_material.SetFloat(s_invertedAlpha, 1);
oVRPassthroughLayer.enabled = true;
m_meshRenderer.enabled = true;
SetSphereSize(selectiveDistance);
SetFadeDirection((int)fadeDirection);
m_menuPanel = FindAnyObjectByType<MenuPanel>();
if (m_menuPanel != null)
{
m_alphaSlider = m_menuPanel.PassthroughFaderSlider;
m_alphaSlider.onValueChanged.AddListener(HandleSliderChange);
}
#if UNITY_ANDROID
CheckIfPassthroughIsRecommended();
#endif
}
private void OnDestroy()
{
if (m_menuPanel != null)
{
m_alphaSlider.onValueChanged.RemoveListener(HandleSliderChange);
}
}
private void SetSphereSize(float size)
{
transform.localScale = new Vector3(size, size, size);
}
private void SetFadeDirection(int direction)
{
m_material.SetInt(s_direction, direction);
}
private void CheckIfPassthroughIsRecommended()
{
m_material.SetFloat(s_invertedAlpha, OVRManager.IsPassthroughRecommended() ? 1 : 0);
if (m_menuPanel != null)
{
m_alphaSlider.value = OVRManager.IsPassthroughRecommended() ? 0 : 1;
}
}
private void HandleSliderChange(float value)
{
float normalizedAlpha;
if (fadeDirection == FadeDirection.InsideOut)
{
normalizedAlpha = 1.1f - value * 0.45f;
}
else
{
normalizedAlpha = 1.0f - value;
}
m_material.SetFloat(s_invertedAlpha, normalizedAlpha);
if (value > boundaryThreshold * m_alphaSlider.maxValue && !m_hasCrossedThreshold)
{
OVRManager.instance.shouldBoundaryVisibilityBeSuppressed = false;
m_hasCrossedThreshold = true;
}
else if (value < boundaryThreshold * m_alphaSlider.maxValue && m_hasCrossedThreshold)
{
OVRManager.instance.shouldBoundaryVisibilityBeSuppressed = true;
m_hasCrossedThreshold = false;
}
}
}
}