Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.9 KiB

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