LookAround360.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2015-2021 RenderHeads Ltd. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. namespace RenderHeads.Media.AVProVideo.Demos
  7. {
  8. /// <summary>
  9. /// Rotate the transform (usually with Camera attached) to look around during playback of 360/180 videos.
  10. /// Unity will rotate the camera automatically if VR is enabled, in which case this script does nothing.
  11. /// Otherwise if there is a gyroscope it will be used, otherwise the mouse/touch can be used.
  12. /// </summary>
  13. public class LookAround360 : MonoBehaviour
  14. {
  15. [SerializeField] bool _lockPitch = false;
  16. [SerializeField] float _maxSpinSpeed = 40f;
  17. [SerializeField, Range(1f, 10f)] float _spinDamping = 5f;
  18. private float _spinX;
  19. private float _spinY;
  20. private static bool IsVrPresent()
  21. {
  22. bool result = false;
  23. #if UNITY_2019_3_OR_NEWER
  24. var xrDisplaySubsystems = new List<UnityEngine.XR.XRDisplaySubsystem>();
  25. SubsystemManager.GetInstances<UnityEngine.XR.XRDisplaySubsystem>(xrDisplaySubsystems);
  26. foreach (var xrDisplay in xrDisplaySubsystems)
  27. {
  28. if (xrDisplay.running)
  29. {
  30. result = true;
  31. break;
  32. }
  33. }
  34. #else
  35. #if UNITY_2018_1_OR_NEWER
  36. result = (UnityEngine.XR.XRDevice.isPresent);
  37. #else
  38. result = (UnityEngine.VR.VRDevice.isPresent);
  39. #endif
  40. #endif
  41. return result;
  42. }
  43. void Start()
  44. {
  45. if (IsVrPresent())
  46. {
  47. this.enabled = false;
  48. return;
  49. }
  50. if (SystemInfo.supportsGyroscope)
  51. {
  52. Input.gyro.enabled = true;
  53. }
  54. }
  55. void Update()
  56. {
  57. if (SystemInfo.supportsGyroscope && Input.gyro.enabled)
  58. {
  59. RotateFromGyro();
  60. }
  61. else
  62. {
  63. RotateFromMouseOrTouch();
  64. }
  65. }
  66. void OnDestroy()
  67. {
  68. if (SystemInfo.supportsGyroscope)
  69. {
  70. Input.gyro.enabled = false;
  71. }
  72. }
  73. void RotateFromGyro()
  74. {
  75. // Invert the z and w of the gyro attitude
  76. this.transform.localRotation = new Quaternion(Input.gyro.attitude.x, Input.gyro.attitude.y, -Input.gyro.attitude.z, -Input.gyro.attitude.w);
  77. }
  78. void RotateFromMouseOrTouch()
  79. {
  80. if (Input.GetMouseButton(0))
  81. {
  82. float h = _maxSpinSpeed * -Input.GetAxis("Mouse X") * Time.deltaTime;
  83. float v = 0f;
  84. if (!_lockPitch)
  85. {
  86. v = _maxSpinSpeed * Input.GetAxis("Mouse Y") * Time.deltaTime;
  87. }
  88. h = Mathf.Clamp(h, -0.5f, 0.5f);
  89. v = Mathf.Clamp(v, -0.5f, 0.5f);
  90. _spinX += h;
  91. _spinY += v;
  92. }
  93. if (!Mathf.Approximately(_spinX, 0f) || !Mathf.Approximately(_spinY, 0f))
  94. {
  95. this.transform.Rotate(Vector3.up, _spinX);
  96. this.transform.Rotate(Vector3.right, _spinY);
  97. _spinX = Mathf.MoveTowards(_spinX, 0f, _spinDamping * Time.deltaTime);
  98. _spinY = Mathf.MoveTowards(_spinY, 0f, _spinDamping * Time.deltaTime);
  99. }
  100. }
  101. }
  102. }