/************************************************************************************ Filename : ONSPReflectionZone.cs Content : Add reflection zone volumes to set reflection parameters via snapshots. Created : September 8, 2016 Authors : Peter Giokaris Copyright : Copyright 2016 Oculus VR, Inc. All Rights reserved. Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License"); you may not use the Oculus VR Rift 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 http://www.oculusvr.com/licenses/LICENSE-3.1 Unless required by applicable law or agreed to in writing, the Oculus VR 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 snapshotList = new Stack(); private static ReflectionSnapshot currentSnapshot = new ReflectionSnapshot(); /// /// Start this instance. /// void Start () { } /// /// Update this instance. /// void Update () { } /// /// Raises the trigger enter event. /// /// Other. void OnTriggerEnter(Collider other) { if(CheckForAudioListener(other.gameObject) == true) { PushCurrentMixerShapshot(); } } /// /// Raises the trigger exit event. /// /// Other. void OnTriggerExit(Collider other) { if(CheckForAudioListener(other.gameObject) == true) { PopCurrentMixerSnapshot(); } } // * * * * * * * * * * * * * // Private functions /// /// Checks for audio listener. /// /// true, if for audio listener was checked, false otherwise. /// Game object. bool CheckForAudioListener(GameObject gameObject) { AudioListener al = gameObject.GetComponentInChildren(); if(al != null) return true; return false; } /// /// Pushs the current mixer snapshot onto the snapshot stack /// 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(); } /// /// Pops the current reflection values from reflectionsList stack. /// void PopCurrentMixerSnapshot() { ReflectionSnapshot snapshot = snapshotList.Pop(); // Set the popped reflection values SetReflectionValues(ref snapshot); } /// /// Sets the reflection values. This is done when entering a zone (use zone values). /// 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."); } } /// /// Sets the reflection values. This is done when exiting a zone (use popped values). /// /// Rm. 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."); } } }