ONSPReflectionZone.cs
4.86 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
156
157
158
159
160
161
162
163
164
/************************************************************************************
Filename : ONSPReflectionZone.cs
Content : Add reflection zone volumes to set reflection parameters via snapshots.
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
Licensed under the Oculus SDK Version 3.5 (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/sdk-3.5/
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 UnityEngine;
using UnityEngine.Audio;
using System.Collections;
using System.Collections.Generic;
public struct ReflectionSnapshot
{
public AudioMixerSnapshot mixerSnapshot;
public float fadeTime;
}
public class ONSPReflectionZone : MonoBehaviour
{
public AudioMixerSnapshot mixerSnapshot = null;
public float fadeTime = 0.0f;
// Push/pop list
private static Stack<ReflectionSnapshot> snapshotList = new Stack<ReflectionSnapshot>();
private static ReflectionSnapshot currentSnapshot = new ReflectionSnapshot();
/// <summary>
/// Start this instance.
/// </summary>
void Start ()
{
}
/// <summary>
/// Update this instance.
/// </summary>
void Update ()
{
}
/// <summary>
/// Raises the trigger enter event.
/// </summary>
/// <param name="other">Other.</param>
void OnTriggerEnter(Collider other)
{
if(CheckForAudioListener(other.gameObject) == true)
{
PushCurrentMixerShapshot();
}
}
/// <summary>
/// Raises the trigger exit event.
/// </summary>
/// <param name="other">Other.</param>
void OnTriggerExit(Collider other)
{
if(CheckForAudioListener(other.gameObject) == true)
{
PopCurrentMixerSnapshot();
}
}
// * * * * * * * * * * * * *
// Private functions
/// <summary>
/// Checks for audio listener.
/// </summary>
/// <returns><c>true</c>, if for audio listener was checked, <c>false</c> otherwise.</returns>
/// <param name="gameObject">Game object.</param>
bool CheckForAudioListener(GameObject gameObject)
{
AudioListener al = gameObject.GetComponentInChildren<AudioListener>();
if(al != null)
return true;
return false;
}
/// <summary>
/// Pushs the current mixer snapshot onto the snapshot stack
/// </summary>
void PushCurrentMixerShapshot()
{
ReflectionSnapshot css = currentSnapshot;
snapshotList.Push(css);
// Set the zone reflection values
// NOTE: There will be conditions that might need resolution when dealing with volumes that
// overlap. Best practice is to never have volumes half-way inside other volumes; larger
// volumes should completely contain smaller volumes
SetReflectionValues();
}
/// <summary>
/// Pops the current reflection values from reflectionsList stack.
/// </summary>
void PopCurrentMixerSnapshot()
{
ReflectionSnapshot snapshot = snapshotList.Pop();
// Set the popped reflection values
SetReflectionValues(ref snapshot);
}
/// <summary>
/// Sets the reflection values. This is done when entering a zone (use zone values).
/// </summary>
void SetReflectionValues()
{
if (mixerSnapshot != null)
{
Debug.Log("Setting off snapshot " + mixerSnapshot.name);
mixerSnapshot.TransitionTo(fadeTime);
// Set the current snapshot to be equal to this one
currentSnapshot.mixerSnapshot = mixerSnapshot;
currentSnapshot.fadeTime = fadeTime;
}
else
{
Debug.Log("Mixer snapshot not set - Please ensure play area has at least one encompassing snapshot.");
}
}
/// <summary>
/// Sets the reflection values. This is done when exiting a zone (use popped values).
/// </summary>
/// <param name="rm">Rm.</param>
void SetReflectionValues(ref ReflectionSnapshot mss)
{
if(mss.mixerSnapshot != null)
{
Debug.Log("Setting off snapshot " + mss.mixerSnapshot.name);
mss.mixerSnapshot.TransitionTo(mss.fadeTime);
// Set the current snapshot to be equal to this one
currentSnapshot.mixerSnapshot = mss.mixerSnapshot;
currentSnapshot.fadeTime = mss.fadeTime;
}
else
{
Debug.Log("Mixer snapshot not set - Please ensure play area has at least one encompassing snapshot.");
}
}
}