SvrConfigSettings.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using UnityEngine;
  2. using System.Collections;
  3. public class SvrConfigSettings : MonoBehaviour
  4. {
  5. public Transform reticle;
  6. void Awake()
  7. {
  8. }
  9. IEnumerator Start()
  10. {
  11. if (SvrManager.Instance == null || SvrConfigOptions.Instance == null)
  12. {
  13. yield break;
  14. }
  15. if (SvrConfigOptions.Instance.TrackEyesEnabled.HasValue)
  16. {
  17. SvrManager.Instance.settings.trackEyes = SvrConfigOptions.Instance.TrackEyesEnabled.Value;
  18. }
  19. if (SvrConfigOptions.Instance.TrackPositionEnabled.HasValue)
  20. {
  21. SvrManager.Instance.settings.trackPosition = SvrConfigOptions.Instance.TrackPositionEnabled.Value;
  22. }
  23. if (SvrManager.Instance.gaze != null && SvrConfigOptions.Instance.GazeReticleEnabled.HasValue)
  24. {
  25. SvrManager.Instance.gaze.gameObject.SetActive(SvrConfigOptions.Instance.GazeReticleEnabled.Value);
  26. }
  27. if (reticle == null && SvrManager.Instance.reticleOverlay != null)
  28. {
  29. reticle = SvrManager.Instance.reticleOverlay.transform;
  30. }
  31. if (reticle != null)
  32. {
  33. if (SvrConfigOptions.Instance.GazeReticleEnabled.HasValue)
  34. reticle.gameObject.SetActive(SvrConfigOptions.Instance.GazeReticleEnabled.Value);
  35. else if (SvrConfigOptions.Instance.FocusEnabled)
  36. reticle.gameObject.SetActive(SvrConfigOptions.Instance.FocusEnabled);
  37. }
  38. yield return new WaitUntil(() => SvrManager.Instance.Initialized);
  39. if (SvrConfigOptions.Instance.UseFixedViewport)
  40. {
  41. DisableSvrInput();
  42. SetSvrCameraView(SvrConfigOptions.Instance.FixedViewportPosition, SvrConfigOptions.Instance.FixedViewportEulerAnglesRotation);
  43. }
  44. if (SvrConfigOptions.Instance.OverrideRenderTextureMSAA != 0)
  45. {
  46. SetSvrRenderTextureAntialiasing(SvrConfigOptions.Instance.OverrideRenderTextureMSAA);
  47. }
  48. if (SvrConfigOptions.Instance.FreezeAnimations)
  49. {
  50. FreezeAllAnimationsAtTime(Mathf.Max(0, SvrConfigOptions.Instance.FreezeAnimationsAtTimeInSecs));
  51. }
  52. if (SvrConfigOptions.Instance.DisableAudio)
  53. {
  54. DisableAudio();
  55. }
  56. if (SvrConfigOptions.Instance.FoveationEnabled)
  57. {
  58. SetFoveatedRendering(SvrConfigOptions.Instance.FoveationGain, SvrConfigOptions.Instance.FoveationArea, SvrConfigOptions.Instance.FoveationMinimum);
  59. }
  60. }
  61. void Update()
  62. {
  63. if (!SvrManager.Instance)
  64. {
  65. return;
  66. }
  67. if (SvrConfigOptions.Instance.FocusEnabled)
  68. {
  69. UpdateFocus();
  70. }
  71. }
  72. private void FreezeAllAnimationsAtTime(float timeInSec)
  73. {
  74. Animator[] animators = GameObject.FindObjectsOfType<Animator>();
  75. foreach (Animator animator in animators)
  76. {
  77. animator.updateMode = AnimatorUpdateMode.Normal;
  78. animator.Update(timeInSec);
  79. }
  80. Time.timeScale = 0;
  81. }
  82. private void DisableSvrInput()
  83. {
  84. if (!SvrManager.Instance)
  85. {
  86. return;
  87. }
  88. SvrManager.Instance.DisableInput = true;
  89. }
  90. private void SetSvrCameraView(Vector3 position, Vector3 eulerAnglesRotation)
  91. {
  92. if(!SvrManager.Instance)
  93. {
  94. return;
  95. }
  96. SvrManager.Instance.transform.position = position;
  97. SvrManager.Instance.transform.eulerAngles = eulerAnglesRotation;
  98. }
  99. private void SetSvrRenderTextureAntialiasing(int mode)
  100. {
  101. if (!SvrManager.Instance)
  102. {
  103. return;
  104. }
  105. switch (mode)
  106. {
  107. case 1:
  108. SvrOverrideSettings.EyeAntiAliasing = SvrOverrideSettings.eAntiAliasing.k1;
  109. break;
  110. case 2:
  111. SvrOverrideSettings.EyeAntiAliasing = SvrOverrideSettings.eAntiAliasing.k2;
  112. break;
  113. case 4:
  114. SvrOverrideSettings.EyeAntiAliasing = SvrOverrideSettings.eAntiAliasing.k4;
  115. break;
  116. default:
  117. Debug.LogError("Antialiasing: " + mode + " not supported!");
  118. break;
  119. }
  120. }
  121. private void DisableAudio()
  122. {
  123. AudioSource [] audioSources = GameObject.FindObjectsOfType<AudioSource>();
  124. foreach(AudioSource audio in audioSources)
  125. {
  126. audio.enabled = false;
  127. }
  128. }
  129. private void SetFoveatedRendering(Vector2 gain, float area, float minimum)
  130. {
  131. SvrManager.Instance.settings.foveationGain = gain;
  132. SvrManager.Instance.settings.foveationArea = area;
  133. SvrManager.Instance.settings.foveationMinimum = minimum;
  134. }
  135. private float focusTime = 0;
  136. private Vector2 focusPosition = Vector2.zero;
  137. private float focusLength = 3;
  138. private void UpdateFocus()
  139. {
  140. var amplitude = SvrConfigOptions.Instance.FocusAmplitude;
  141. var frequency = SvrConfigOptions.Instance.FocusFrequency;
  142. var speed = SvrConfigOptions.Instance.FocusSpeed;
  143. if (speed == 0f) speed = 1f;
  144. focusPosition.x = Mathf.Cos(focusTime * frequency.x) * amplitude.x;
  145. focusPosition.y = Mathf.Cos(focusTime * frequency.y) * amplitude.y;
  146. focusTime += Time.deltaTime * speed;
  147. SvrManager.Instance.FocalPoint = focusPosition;
  148. if (reticle != null)
  149. {
  150. var position = reticle.localPosition;
  151. position.x = focusPosition.x;
  152. position.y = focusPosition.y;
  153. position.z = 1;
  154. position *= focusLength;
  155. reticle.localPosition = position;
  156. }
  157. }
  158. }