-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathPassthroughConfigurator.cs
381 lines (327 loc) · 13.1 KB
/
PassthroughConfigurator.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright (c) Meta Platforms, Inc. and affiliates.
using System;
using System.Collections;
using System.Runtime.CompilerServices;
using Fusion;
using UnityEngine;
namespace CrypticCabinet.Passthrough
{
[Serializable]
public enum ELutId
{
DEFAULT = 0,
DARKER_ROOM = 1,
}
/// <summary>
/// Allows to change settings of a specified passthrough layer on demand.
/// It can propagate the change to all players using RPC.
/// </summary>
public class PassthroughConfigurator : NetworkBehaviour
{
[SerializeField] private OVRPassthroughLayer m_passthroughLayer;
[SerializeField] private float m_currentOpacity = 1f;
[SerializeField] private float m_currentContrast;
[SerializeField] private float m_currentBrightness;
[SerializeField] private float m_currentSaturation;
[SerializeField] private ELutId m_currentLutId;
[SerializeField] private float m_currentLutBlend;
[SerializeField] private float m_opacityTransitionDuration = 1f;
[SerializeField] private float m_contrastTransitionDuration = 1f;
[SerializeField] private float m_brightnessTransitionDuration = 1f;
[SerializeField] private float m_saturationTransitionDuration = 1f;
[SerializeField] private float m_lutTransitionDuration = 1f;
[SerializeField] private Texture2D m_defaultLut;
[SerializeField] private Texture2D m_darkerRoomLut;
private OVRPassthroughColorLut m_ovrCurrentLut;
private OVRPassthroughColorLut m_ovrNextLut;
private OVRPassthroughColorLut m_defaultRoomLut;
private OVRPassthroughColorLut m_ovrDarkerRoomLut;
private float m_opacityElapsedTime;
private float m_contrastElapsedTime;
private float m_brightnessElapsedTime;
private float m_saturationElapsedTime;
private Coroutine m_opacityCoroutine;
private Coroutine m_contrastCoroutine;
private Coroutine m_brightnessCoroutine;
private Coroutine m_saturationCoroutine;
private Coroutine m_lutCoroutine;
public static PassthroughConfigurator Instance { get; private set; }
public void Awake()
{
// Enforce singleton across all Runners.
if (Instance)
{
Destroy(this);
}
Instance = this;
if (m_passthroughLayer == null)
{
m_passthroughLayer = FindObjectOfType<OVRPassthroughLayer>();
}
}
private void Start()
{
// Initialize passthrough with desired settings.
UpdatePassthroughSetup();
// Ensure the texture is supported for the LUT
if (!OVRPassthroughColorLut.IsTextureSupported(m_defaultLut, out var errorMsg))
{
Debug.LogError($"LUT texture not supported, reason: {errorMsg}");
}
else
{
m_defaultRoomLut = new OVRPassthroughColorLut(m_defaultLut);
}
if (!OVRPassthroughColorLut.IsTextureSupported(m_darkerRoomLut, out errorMsg))
{
Debug.LogError($"LUT texture not supported, reason: {errorMsg}");
}
else
{
m_ovrDarkerRoomLut = new OVRPassthroughColorLut(m_darkerRoomLut);
}
}
private void OnDisable()
{
SetLut(ELutId.DEFAULT, 1, true);
}
#region Smoothing changes
private void UpdatePassthroughLut(OVRPassthroughColorLut previousLut, OVRPassthroughColorLut newLut)
{
m_passthroughLayer.SetColorLut(newLut);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UpdatePassthroughSetup()
{
m_passthroughLayer.SetBrightnessContrastSaturation(m_currentBrightness, m_currentContrast, m_currentSaturation);
m_passthroughLayer.textureOpacity = m_currentOpacity;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static IEnumerator InterpolateValue(float startValue, float targetValue, float duration, Action<float> setValueAction)
{
var elapsedTime = 0f;
while (elapsedTime < duration)
{
elapsedTime += Time.deltaTime;
var t = Mathf.Clamp01(elapsedTime / duration);
var value = Mathf.Lerp(startValue, targetValue, t);
// Invoke action to forward the new value to the caller and its delegate.
setValueAction(value);
yield return null;
}
// Set the destination value to ensure it is exactly the expected one.
setValueAction(targetValue);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void StartInterpolation(ref Coroutine coroutine, float startValue, float targetValue, float duration, Action<float> setValueAction)
{
if (!m_passthroughLayer)
{
Debug.LogError("No passthrough layer specifier for PassthroughConfigurator." +
"Unable to update passthrough setup!");
return;
}
// If there's already an interpolation coroutine for the variable, stop it.
if (coroutine != null)
{
StopCoroutine(coroutine);
}
// Start the new interpolation coroutine.
coroutine = StartCoroutine(InterpolateValue(startValue, targetValue, duration, setValueAction));
}
#endregion
#region Passthrough configuration
public void ResetPassthrough()
{
if (!m_passthroughLayer)
{
Debug.LogError("No passthrough layer specifier for PassthroughConfigurator." +
"Unable to reset passthrough!");
return;
}
m_passthroughLayer.DisableColorMap();
}
public void SetOpacity(float opacity, bool propagateToAllClients)
{
if (!m_passthroughLayer)
{
Debug.LogError("No passthrough layer specifier for PassthroughConfigurator." +
"Unable to change opacity!");
return;
}
// Smooth change of opacity
StartInterpolation(ref m_opacityCoroutine, m_currentOpacity, opacity, m_opacityTransitionDuration,
value =>
{
m_currentOpacity = value;
UpdatePassthroughSetup();
});
if (propagateToAllClients)
{
RPC_SetOpacity(opacity);
}
}
public void SetContrast(float contrast, bool propagateToAllClients)
{
// Smooth change of contrast
StartInterpolation(ref m_contrastCoroutine, m_currentContrast, contrast, m_contrastTransitionDuration,
value =>
{
m_currentContrast = value;
UpdatePassthroughSetup();
});
if (propagateToAllClients)
{
RPC_SetContrast(contrast);
}
}
public void SetBrightness(float brightness, bool propagateToAllClients)
{
// Smooth change of brightness
StartInterpolation(ref m_brightnessCoroutine, m_currentBrightness, brightness, m_brightnessTransitionDuration,
value =>
{
m_currentBrightness = value;
UpdatePassthroughSetup();
});
if (propagateToAllClients)
{
RPC_SetBrightness(brightness);
}
}
public void SetSaturation(float saturation, bool propagateToAllClients)
{
// Smooth change of saturation
StartInterpolation(ref m_saturationCoroutine, m_currentSaturation, saturation, m_saturationTransitionDuration,
value =>
{
m_currentSaturation = value;
UpdatePassthroughSetup();
});
if (propagateToAllClients)
{
RPC_SetSaturation(saturation);
}
}
public void SetLut(ELutId lutID, float targetBlend, bool propagateToAllClients)
{
var previousLut = m_ovrCurrentLut;
var nextLut = lutID switch
{
ELutId.DEFAULT => m_defaultRoomLut,
ELutId.DARKER_ROOM => m_ovrDarkerRoomLut,
_ => throw new ArgumentOutOfRangeException(),
};
if (nextLut != null)
{
m_currentLutId = lutID;
m_ovrCurrentLut = nextLut;
m_currentLutBlend = 0;
// Smooth change of Lut, going from current blend to target blend from previous to next LUT
StartInterpolation(
ref m_lutCoroutine, m_currentLutBlend, targetBlend, m_lutTransitionDuration,
value =>
{
m_currentLutBlend = value;
UpdatePassthroughLut(previousLut, nextLut);
});
}
else
{
Debug.LogWarning("SetLut failed: new lut ID not found!");
}
if (propagateToAllClients && Object != null && Object.IsValid)
{
RPC_SetLut(lutID, targetBlend);
}
}
#endregion
#region RPC
[Rpc(RpcSources.All, RpcTargets.All)]
private void RPC_SetOpacity(float opacity, RpcInfo info = default)
{
// Only send this to other players
if (info.Source == PlayerRef.None || info.Source.PlayerId == Runner.LocalPlayer.PlayerId)
{
return;
}
Debug.Log("RPC: received passthrough change opacity request");
if (opacity.Equals(m_currentOpacity))
{
Debug.Log("RPC: passthrough opacity already equal to new value");
return;
}
Debug.Log("RPC: changing passthrough opacity with new value");
SetOpacity(opacity, false);
}
[Rpc(RpcSources.All, RpcTargets.All)]
private void RPC_SetContrast(float contrast, RpcInfo info = default)
{
// Only send this to other players
if (info.Source == PlayerRef.None || info.Source.PlayerId == Runner.LocalPlayer.PlayerId)
{
return;
}
Debug.Log("RPC: received passthrough change contrast request");
if (contrast.Equals(m_currentContrast))
{
Debug.Log("RPC: passthrough contrast already equal to new value");
return;
}
Debug.Log("RPC: changing passthrough contrast with new value");
SetContrast(contrast, false);
}
[Rpc(RpcSources.All, RpcTargets.All)]
private void RPC_SetBrightness(float brightness, RpcInfo info = default)
{
// Only send this to other players
if (info.Source == PlayerRef.None || info.Source.PlayerId == Runner.LocalPlayer.PlayerId)
{
return;
}
Debug.Log("RPC: received passthrough change brightness request");
if (brightness.Equals(m_currentBrightness))
{
Debug.Log("RPC: passthrough brightness already equal to new value");
return;
}
Debug.Log("RPC: changing passthrough brightness with new value");
SetBrightness(brightness, false);
}
[Rpc(RpcSources.All, RpcTargets.All)]
private void RPC_SetSaturation(float saturation, RpcInfo info = default)
{
// Only send this to other players
if (info.Source == PlayerRef.None || info.Source.PlayerId == Runner.LocalPlayer.PlayerId)
{
return;
}
Debug.Log("RPC: received passthrough change saturation request");
if (saturation.Equals(m_currentSaturation))
{
Debug.Log("RPC: passthrough saturation already equal to new value");
return;
}
Debug.Log("RPC: changing passthrough saturation with new value");
SetSaturation(saturation, false);
}
[Rpc(RpcSources.All, RpcTargets.All)]
private void RPC_SetLut(ELutId lutID, float blend, RpcInfo info = default)
{
// Only send this to other players
if (info.Source == PlayerRef.None || info.Source.PlayerId == Runner.LocalPlayer.PlayerId)
{
return;
}
Debug.Log("RPC: received passthrough change lut request");
if (lutID == m_currentLutId && Mathf.Approximately(blend, m_currentLutBlend))
{
Debug.Log("RPC: passthrough lut already equal to new value");
return;
}
Debug.Log("RPC: changing passthrough lut with new value");
SetLut(lutID, blend, false);
}
#endregion
}
}