| 1 | /************************************************************************************ |
| 2 | Filename : ONSPReflectionZone.cs |
| 3 | Content : Add reflection zone volumes to set reflection parameters via snapshots. |
| 4 | Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 5 | |
| 6 | Licensed under the Oculus SDK Version 3.5 (the "License"); |
| 7 | you may not use the Oculus SDK except in compliance with the License, |
| 8 | which is provided at the time of installation or download, or which |
| 9 | otherwise accompanies this software in either electronic or hard copy form. |
| 10 | |
| 11 | You may obtain a copy of the License at |
| 12 | |
| 13 | https://developer.oculus.com/licenses/sdk-3.5/ |
| 14 | |
| 15 | Unless required by applicable law or agreed to in writing, the Oculus SDK |
| 16 | distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | See the License for the specific language governing permissions and |
| 19 | limitations under the License. |
| 20 | ************************************************************************************/ |
| 21 | using UnityEngine; |
| 22 | using UnityEngine.Audio; |
| 23 | using System.Collections; |
| 24 | using System.Collections.Generic; |
| 25 | |
| 26 | public struct ReflectionSnapshot |
| 27 | { |
| 28 | public AudioMixerSnapshot mixerSnapshot; |
| 29 | public float fadeTime; |
| 30 | } |
| 31 | |
| 32 | public class ONSPReflectionZone : MonoBehaviour |
| 33 | { |
| 34 | public AudioMixerSnapshot mixerSnapshot = null; |
| 35 | public float fadeTime = 0.0f; |
| 36 | |
| 37 | // Push/pop list |
| 38 | private static Stack<ReflectionSnapshot> snapshotList = new Stack<ReflectionSnapshot>(); |
| 39 | private static ReflectionSnapshot currentSnapshot = new ReflectionSnapshot(); |
| 40 | |
| 41 | /// <summary> |
| 42 | /// Start this instance. |
| 43 | /// </summary> |
| 44 | void Start () |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | /// <summary> |
| 49 | /// Update this instance. |
| 50 | /// </summary> |
| 51 | void Update () |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | /// <summary> |
| 56 | /// Raises the trigger enter event. |
| 57 | /// </summary> |
| 58 | /// <param name="other">Other.</param> |
| 59 | void OnTriggerEnter(Collider other) |
| 60 | { |
| 61 | if(CheckForAudioListener(other.gameObject) == true) |
| 62 | { |
| 63 | PushCurrentMixerShapshot(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /// <summary> |
| 68 | /// Raises the trigger exit event. |
| 69 | /// </summary> |
| 70 | /// <param name="other">Other.</param> |
| 71 | void OnTriggerExit(Collider other) |
| 72 | { |
| 73 | if(CheckForAudioListener(other.gameObject) == true) |
| 74 | { |
| 75 | PopCurrentMixerSnapshot(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
| 80 | // * * * * * * * * * * * * * |
| 81 | // Private functions |
| 82 | |
| 83 | /// <summary> |
| 84 | /// Checks for audio listener. |
| 85 | /// </summary> |
| 86 | /// <returns><c>true</c>, if for audio listener was checked, <c>false</c> otherwise.</returns> |
| 87 | /// <param name="gameObject">Game object.</param> |
| 88 | bool CheckForAudioListener(GameObject gameObject) |
| 89 | { |
| 90 | AudioListener al = gameObject.GetComponentInChildren<AudioListener>(); |
| 91 | if(al != null) |
| 92 | return true; |
| 93 | |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | /// <summary> |
| 98 | /// Pushs the current mixer snapshot onto the snapshot stack |
| 99 | /// </summary> |
| 100 | void PushCurrentMixerShapshot() |
| 101 | { |
| 102 | ReflectionSnapshot css = currentSnapshot; |
| 103 | snapshotList.Push(css); |
| 104 | |
| 105 | // Set the zone reflection values |
| 106 | // NOTE: There will be conditions that might need resolution when dealing with volumes that |
| 107 | // overlap. Best practice is to never have volumes half-way inside other volumes; larger |
| 108 | // volumes should completely contain smaller volumes |
| 109 | SetReflectionValues(); |
| 110 | } |
| 111 | |
| 112 | /// <summary> |
| 113 | /// Pops the current reflection values from reflectionsList stack. |
| 114 | /// </summary> |
| 115 | void PopCurrentMixerSnapshot() |
| 116 | { |
| 117 | ReflectionSnapshot snapshot = snapshotList.Pop(); |
| 118 | |
| 119 | // Set the popped reflection values |
| 120 | SetReflectionValues(ref snapshot); |
| 121 | } |
| 122 | |
| 123 | /// <summary> |
| 124 | /// Sets the reflection values. This is done when entering a zone (use zone values). |
| 125 | /// </summary> |
| 126 | void SetReflectionValues() |
| 127 | { |
| 128 | if (mixerSnapshot != null) |
| 129 | { |
| 130 | Debug.Log("Setting off snapshot " + mixerSnapshot.name); |
| 131 | mixerSnapshot.TransitionTo(fadeTime); |
| 132 | |
| 133 | // Set the current snapshot to be equal to this one |
| 134 | currentSnapshot.mixerSnapshot = mixerSnapshot; |
| 135 | currentSnapshot.fadeTime = fadeTime; |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | Debug.Log("Mixer snapshot not set - Please ensure play area has at least one encompassing snapshot."); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /// <summary> |
| 144 | /// Sets the reflection values. This is done when exiting a zone (use popped values). |
| 145 | /// </summary> |
| 146 | /// <param name="rm">Rm.</param> |
| 147 | void SetReflectionValues(ref ReflectionSnapshot mss) |
| 148 | { |
| 149 | if(mss.mixerSnapshot != null) |
| 150 | { |
| 151 | Debug.Log("Setting off snapshot " + mss.mixerSnapshot.name); |
| 152 | mss.mixerSnapshot.TransitionTo(mss.fadeTime); |
| 153 | |
| 154 | // Set the current snapshot to be equal to this one |
| 155 | currentSnapshot.mixerSnapshot = mss.mixerSnapshot; |
| 156 | currentSnapshot.fadeTime = mss.fadeTime; |
| 157 | |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | Debug.Log("Mixer snapshot not set - Please ensure play area has at least one encompassing snapshot."); |
| 162 | } |
| 163 | } |
| 164 | } |