ARController.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using UnityEngine;
  2. using System.Collections;
  3. #if UNITY_EDITOR
  4. using UnityEngine.Networking.PlayerConnection;
  5. using UnityEditor.Networking.PlayerConnection;
  6. #endif
  7. namespace UnityARInterface
  8. {
  9. public class ARController : MonoBehaviour
  10. {
  11. protected ARInterface m_ARInterface;
  12. [SerializeField]
  13. protected Camera m_ARCamera;
  14. public Camera arCamera { get { return m_ARCamera; } }
  15. [SerializeField]
  16. private bool m_PlaneDetection;
  17. [SerializeField]
  18. private bool m_LightEstimation;
  19. [SerializeField]
  20. private bool m_PointCloud;
  21. [SerializeField]
  22. private bool m_BackgroundRendering = true;
  23. [SerializeField]
  24. private float m_Scale = 1f;
  25. public virtual bool BackgroundRendering {
  26. get { return m_BackgroundRendering; }
  27. set {
  28. if(m_ARInterface != null){
  29. m_ARInterface.BackgroundRendering = m_BackgroundRendering = value;
  30. }
  31. }
  32. }
  33. public float scale
  34. {
  35. set
  36. {
  37. m_Scale = value;
  38. var root = m_ARCamera.transform.parent;
  39. if (root)
  40. {
  41. var poiInRootSpace = root.InverseTransformPoint(pointOfInterest);
  42. root.localPosition = m_InvRotation * (-poiInRootSpace * m_Scale) + pointOfInterest;
  43. }
  44. }
  45. get { return m_Scale; }
  46. }
  47. public Vector3 pointOfInterest;
  48. private Quaternion m_Rotation = Quaternion.identity;
  49. private Quaternion m_InvRotation = Quaternion.identity;
  50. public Quaternion rotation
  51. {
  52. get { return m_Rotation; }
  53. set
  54. {
  55. var root = m_ARCamera.transform.parent;
  56. if (root)
  57. {
  58. m_Rotation = value;
  59. m_InvRotation = Quaternion.Inverse(rotation);
  60. var poiInRootSpace = root.InverseTransformPoint(pointOfInterest);
  61. root.localPosition = m_InvRotation * (-poiInRootSpace * scale) + pointOfInterest;
  62. root.localRotation = m_InvRotation;
  63. }
  64. }
  65. }
  66. public bool IsRunning
  67. {
  68. get
  69. {
  70. if (m_ARInterface == null)
  71. return false;
  72. return m_ARInterface.IsRunning;
  73. }
  74. }
  75. public void AlignWithPointOfInterest(Vector3 position)
  76. {
  77. var root = m_ARCamera.transform.parent;
  78. if (root)
  79. {
  80. var poiInRootSpace = root.InverseTransformPoint(position - pointOfInterest);
  81. root.localPosition = m_InvRotation * (-poiInRootSpace * scale);
  82. }
  83. }
  84. void OnBeforeRender()
  85. {
  86. m_ARInterface.UpdateCamera(m_ARCamera);
  87. Pose pose = new Pose();
  88. if (m_ARInterface.TryGetPose(ref pose))
  89. {
  90. m_ARCamera.transform.localPosition = pose.position;
  91. m_ARCamera.transform.localRotation = pose.rotation;
  92. var parent = m_ARCamera.transform.parent;
  93. if (parent != null)
  94. parent.localScale = Vector3.one * scale;
  95. }
  96. }
  97. protected virtual void SetupARInterface()
  98. {
  99. m_ARInterface = ARInterface.GetInterface();
  100. }
  101. private void OnEnable()
  102. {
  103. Application.targetFrameRate = 60;
  104. Screen.sleepTimeout = SleepTimeout.NeverSleep;
  105. Input.simulateMouseWithTouches = true;
  106. if (m_ARInterface == null)
  107. SetupARInterface();
  108. // See if we are on a camera
  109. if (m_ARCamera == null)
  110. m_ARCamera = GetComponent<Camera>();
  111. // Fallback to main camera
  112. if (m_ARCamera == null)
  113. m_ARCamera = Camera.main;
  114. StopAllCoroutines();
  115. StartCoroutine(StartServiceRoutine());
  116. }
  117. IEnumerator StartServiceRoutine()
  118. {
  119. yield return m_ARInterface.StartService(GetSettings());
  120. if (IsRunning)
  121. {
  122. m_ARInterface.SetupCamera(m_ARCamera);
  123. m_ARInterface.BackgroundRendering = BackgroundRendering;
  124. Application.onBeforeRender += OnBeforeRender;
  125. }
  126. else
  127. {
  128. enabled = false;
  129. }
  130. }
  131. void OnDisable()
  132. {
  133. StopAllCoroutines();
  134. if (IsRunning)
  135. {
  136. m_ARInterface.StopService();
  137. Application.onBeforeRender -= OnBeforeRender;
  138. }
  139. }
  140. void Update()
  141. {
  142. m_ARInterface.Update();
  143. }
  144. public ARInterface.Settings GetSettings()
  145. {
  146. return new ARInterface.Settings()
  147. {
  148. enablePointCloud = m_PointCloud,
  149. enablePlaneDetection = m_PlaneDetection,
  150. enableLightEstimation = m_LightEstimation
  151. };
  152. }
  153. }
  154. }