SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
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.

133 lines
4.0 KiB

4 years ago
  1. using UnityEngine;
  2. using M = System.Math;
  3. namespace ARLocation
  4. {
  5. public class UnityLocationProvider : AbstractLocationProvider
  6. {
  7. private float androidMagneticDeclination;
  8. private AndroidNativeCompass androidNativeCompass;
  9. public override string Name => "UnityLocationProvider";
  10. public override bool IsCompassEnabled => Input.compass.enabled;
  11. protected override void RequestLocationAndCompassUpdates()
  12. {
  13. // Debug.Log("[UnityLocationProvider]: Requesting location updates...");
  14. Input.compass.enabled = true;
  15. Input.location.Start(
  16. (float)Options.AccuracyRadius,
  17. (float)Options.MinDistanceBetweenUpdates
  18. );
  19. }
  20. protected override void InnerOnEnabled()
  21. {
  22. androidMagneticDeclination = AndroidMagneticDeclination.GetDeclination(CurrentLocation.ToLocation());
  23. androidNativeCompass = new AndroidNativeCompass((float) (1.0 - LowPassFilterFactor));
  24. }
  25. protected override void UpdateLocationRequestStatus()
  26. {
  27. switch (Input.location.status)
  28. {
  29. case LocationServiceStatus.Initializing:
  30. Status = LocationProviderStatus.Initializing;
  31. break;
  32. case LocationServiceStatus.Failed:
  33. Status = LocationProviderStatus.Failed;
  34. break;
  35. case LocationServiceStatus.Running:
  36. Status = LocationProviderStatus.Started;
  37. break;
  38. case LocationServiceStatus.Stopped:
  39. Status = LocationProviderStatus.Idle;
  40. break;
  41. }
  42. }
  43. protected override LocationReading? ReadLocation()
  44. {
  45. if (!HasStarted)
  46. {
  47. return null;
  48. }
  49. var data = Input.location.lastData;
  50. return new LocationReading()
  51. {
  52. latitude = data.latitude,
  53. longitude = data.longitude,
  54. altitude = data.altitude,
  55. accuracy = data.horizontalAccuracy,
  56. floor = -1,
  57. timestamp = (long)(data.timestamp * 1000)
  58. };
  59. }
  60. protected override HeadingReading? ReadHeading()
  61. {
  62. if (!HasStarted)
  63. {
  64. return null;
  65. }
  66. // ReSharper disable once RedundantAssignment
  67. var magneticHeading = Input.compass.magneticHeading;
  68. // ReSharper disable once RedundantAssignment
  69. var trueHeading = Input.compass.trueHeading;
  70. #if PLATFORM_ANDROID
  71. var tiltCorrectedMagneticHeading = GetMagneticHeading();
  72. magneticHeading = tiltCorrectedMagneticHeading;
  73. trueHeading = tiltCorrectedMagneticHeading + androidMagneticDeclination;
  74. #endif
  75. if (trueHeading < 0)
  76. {
  77. trueHeading += 360;
  78. }
  79. return new HeadingReading()
  80. {
  81. heading = trueHeading,
  82. magneticHeading = magneticHeading,
  83. accuracy = Input.compass.headingAccuracy,
  84. timestamp = (long)(Input.compass.timestamp * 1000),
  85. isMagneticHeadingAvailable = Input.compass.enabled
  86. };
  87. }
  88. private float GetMagneticHeading()
  89. {
  90. #if PLATFORM_ANDROID
  91. if (!SystemInfo.supportsGyroscope || !ApplyCompassTiltCompensationOnAndroid || androidNativeCompass == null)
  92. {
  93. return Input.compass.magneticHeading;
  94. }
  95. return androidNativeCompass.GetMagneticHeading();
  96. // if (Screen.orientation == ScreenOrientation.Landscape)
  97. // {
  98. // return heading;// + 45;
  99. // }
  100. // else
  101. // {
  102. // return heading;
  103. // }
  104. #else
  105. return Input.compass.magneticHeading;
  106. #endif
  107. }
  108. }
  109. }