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.

52 lines
1.3 KiB

  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Handles aligning audio listener when using speakers.
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using Valve.VR;
  8. [RequireComponent(typeof(AudioListener))]
  9. public class SteamVR_Ears : MonoBehaviour
  10. {
  11. public SteamVR_Camera vrcam;
  12. bool usingSpeakers;
  13. Quaternion offset;
  14. private void OnNewPosesApplied()
  15. {
  16. var origin = vrcam.origin;
  17. var baseRotation = origin != null ? origin.rotation : Quaternion.identity;
  18. transform.rotation = baseRotation * offset;
  19. }
  20. void OnEnable()
  21. {
  22. usingSpeakers = false;
  23. var settings = OpenVR.Settings;
  24. if (settings != null)
  25. {
  26. var error = EVRSettingsError.None;
  27. if (settings.GetBool(OpenVR.k_pch_SteamVR_Section, OpenVR.k_pch_SteamVR_UsingSpeakers_Bool, ref error))
  28. {
  29. usingSpeakers = true;
  30. var yawOffset = settings.GetFloat(OpenVR.k_pch_SteamVR_Section, OpenVR.k_pch_SteamVR_SpeakersForwardYawOffsetDegrees_Float, ref error);
  31. offset = Quaternion.Euler(0.0f, yawOffset, 0.0f);
  32. }
  33. }
  34. if (usingSpeakers)
  35. SteamVR_Events.NewPosesApplied.Listen(OnNewPosesApplied);
  36. }
  37. void OnDisable()
  38. {
  39. if (usingSpeakers)
  40. SteamVR_Events.NewPosesApplied.Remove(OnNewPosesApplied);
  41. }
  42. }