NibiruMarker.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using UnityEngine;
  2. namespace Nxr.Internal
  3. {
  4. /// <summary>
  5. ///
  6. /// </summary>
  7. public class NibiruMarker : MonoBehaviour
  8. {
  9. [Tooltip("Scale the position from Marker units (mm) into Unity units (m). example : \n" +
  10. " 1 means = mm -> m \n 10 means = mm -> dm \n 100 means = mm ->cm ")]
  11. public float PositonScaleFactor = 1.0f;
  12. //
  13. public delegate void OnMarkerFound();
  14. public delegate void OnMarkerLost();
  15. /// <summary>
  16. /// The callback when Marker is found.
  17. /// </summary>
  18. public static OnMarkerFound OnMarkerFoundHandler = null;
  19. /// <summary>
  20. /// The callback when Marker is lost.
  21. /// </summary>
  22. public static OnMarkerLost OnMarkerLostHandler = null;
  23. NibiruService nibiruService;
  24. Transform mTransform;
  25. bool visible = false;
  26. private Matrix4x4 cameraPoseMat;
  27. private Matrix4x4 cameraPoseMatLeft;
  28. private Matrix4x4 cameraPoseMatRight;
  29. GameObject origin;
  30. public bool AutoStartMarkerRecognize = true;
  31. public bool IsVisible()
  32. {
  33. return visible;
  34. }
  35. // Use this for initialization
  36. void Start()
  37. {
  38. nibiruService = NxrViewer.Instance.GetNibiruService();
  39. mTransform = gameObject.transform;
  40. origin = GameObject.Find("MarkerRoot");
  41. // Polaroid DTR
  42. bool dtrMode = NxrGlobal.supportDtr && NxrGlobal.distortionEnabled;
  43. if (dtrMode)
  44. {
  45. NxrViewer.Instance.SwitchControllerMode(false);
  46. }
  47. NxrGlobal.isMarkerVisible = false;
  48. if (nibiruService != null && AutoStartMarkerRecognize)
  49. {
  50. nibiruService.StartMarkerRecognize();
  51. }
  52. cameraPoseMat = new Matrix4x4();
  53. }
  54. // Update is called once per frame
  55. void Update()
  56. {
  57. if (nibiruService != null && nibiruService.IsMarkerRecognizeRunning)
  58. {
  59. float[] leftEyeArray = nibiruService.GetMarkerViewMatrix(0);
  60. float[] rightEyeArray = nibiruService.GetMarkerViewMatrix(1);
  61. if (leftEyeArray != null && rightEyeArray != null)
  62. {
  63. leftEyeArray[12] *= 0.001f * PositonScaleFactor;
  64. leftEyeArray[13] *= 0.001f * PositonScaleFactor;
  65. leftEyeArray[14] *= 0.001f * PositonScaleFactor;
  66. rightEyeArray[12] *= 0.001f * PositonScaleFactor;
  67. rightEyeArray[13] *= 0.001f * PositonScaleFactor;
  68. rightEyeArray[14] *= 0.001f * PositonScaleFactor;
  69. Matrix4x4 matrixRawLeft = ARUtilityFunctions.MatrixFromFloatArray(leftEyeArray);
  70. Matrix4x4 matrixRawRight = ARUtilityFunctions.MatrixFromFloatArray(rightEyeArray);
  71. Matrix4x4 transformationMatrixLeft = ARUtilityFunctions.LHMatrixFromRHMatrix(matrixRawLeft);
  72. Matrix4x4 transformationMatrixRight = ARUtilityFunctions.LHMatrixFromRHMatrix(matrixRawRight);
  73. cameraPoseMatLeft = transformationMatrixLeft.inverse;
  74. cameraPoseMatRight = transformationMatrixRight.inverse;
  75. }
  76. float[] array = nibiruService.GetMarkerViewMatrix();
  77. if (array != null)
  78. {
  79. if (!visible)
  80. {
  81. if (OnMarkerFoundHandler != null)
  82. {
  83. OnMarkerFoundHandler();
  84. }
  85. visible = true;
  86. NxrGlobal.isMarkerVisible = true;
  87. for (int i = 0; i < mTransform.childCount; i++)
  88. {
  89. mTransform.GetChild(i).gameObject.SetActive(true);
  90. Debug.Log("MARKER VISBILE " + mTransform.GetChild(i).gameObject.name);
  91. }
  92. if (origin != null)
  93. {
  94. Matrix4x4 pose;
  95. // If this marker is the base, no need to take base inverse etc.
  96. pose = origin.transform.localToWorldMatrix;
  97. transform.position = ARUtilityFunctions.PositionFromMatrix(pose);
  98. transform.rotation = ARUtilityFunctions.QuaternionFromMatrix(pose);
  99. }
  100. }
  101. // Filter data
  102. array = FilterData(array);
  103. // Scale the position from ARToolKit units (mm) into Unity units (m).
  104. array[12] *= 0.001f * PositonScaleFactor;
  105. array[13] *= 0.001f * PositonScaleFactor;
  106. array[14] *= 0.001f * PositonScaleFactor;
  107. Matrix4x4 matrixRaw = ARUtilityFunctions.MatrixFromFloatArray(array);
  108. // Debug.Log("MarkerViewMatrix>>>>" + matrixRaw.ToString());
  109. Matrix4x4 transformationMatrix = ARUtilityFunctions.LHMatrixFromRHMatrix(matrixRaw);
  110. cameraPoseMat = transformationMatrix.inverse;
  111. //Debug.Log("transformationMatrix>>>>" + transformationMatrix.ToString());
  112. //Quaternion quaternion = ARUtilityFunctions.QuaternionFromMatrix(transformationMatrix);
  113. //Vector3 eulerAngles = quaternion.eulerAngles;
  114. // z轴朝上,转换成z轴朝里,符合unity
  115. //mTransform.rotation = Quaternion.Euler(eulerAngles.x - 90, eulerAngles.y, eulerAngles.z);
  116. //mTransform.position = ARUtilityFunctions.PositionFromMatrix(transformationMatrix);
  117. }
  118. else if (array == null)
  119. {
  120. if (visible)
  121. {
  122. Debug.Log("MARKER VISIBLE -> NOT VISBILE");
  123. }
  124. else
  125. {
  126. Debug.Log("MARKER INIT NOT VISBILE");
  127. }
  128. if (OnMarkerLostHandler != null)
  129. {
  130. OnMarkerLostHandler();
  131. }
  132. NxrGlobal.isMarkerVisible = false;
  133. visible = false;
  134. for (int i = 0; i < mTransform.childCount; i++)
  135. {
  136. mTransform.GetChild(i).gameObject.SetActive(false);
  137. }
  138. }
  139. }
  140. }
  141. public Matrix4x4 CameraPose()
  142. {
  143. return cameraPoseMat;
  144. }
  145. public Vector3 CameraPosition(int eyeType)
  146. {
  147. Vector3 arPosition = ARUtilityFunctions.PositionFromMatrix(eyeType == 0 ? cameraPoseMatLeft : cameraPoseMatRight);
  148. return arPosition;
  149. }
  150. private void OnApplicationPause(bool pause)
  151. {
  152. if (pause)
  153. {
  154. visible = false;
  155. NxrGlobal.isMarkerVisible = false;
  156. for (int i = 0; i < mTransform.childCount; i++)
  157. {
  158. mTransform.GetChild(i).gameObject.SetActive(false);
  159. }
  160. }
  161. if (AutoStartMarkerRecognize && pause && nibiruService != null)
  162. {
  163. nibiruService.StopMarkerRecognize();
  164. }
  165. else if (AutoStartMarkerRecognize && nibiruService != null)
  166. {
  167. nibiruService.StartMarkerRecognize();
  168. }
  169. }
  170. private void OnDestroy()
  171. {
  172. // change scene
  173. if (AutoStartMarkerRecognize && nibiruService != null && nibiruService.IsMarkerRecognizeRunning)
  174. {
  175. nibiruService.StopMarkerRecognize();
  176. }
  177. }
  178. private void OnApplicationQuit()
  179. {
  180. // exit app
  181. if (AutoStartMarkerRecognize && nibiruService != null && nibiruService.IsMarkerRecognizeRunning)
  182. {
  183. nibiruService.StopMarkerRecognize();
  184. }
  185. }
  186. //******************Filter Data******************
  187. private float[] lastDataArr;
  188. public bool filterMarkerJitter = true;
  189. private float[] FilterData(float[] newArray)
  190. {
  191. if (!filterMarkerJitter)
  192. {
  193. return newArray;
  194. }
  195. if (lastDataArr == null)
  196. {
  197. lastDataArr = new float[newArray.Length];
  198. newArray.CopyTo(lastDataArr, 0);
  199. }
  200. int length = newArray.Length;
  201. for (int i = 0; i < length; i++)
  202. {
  203. float diff = Mathf.Abs(newArray[i] - lastDataArr[i]);
  204. float absValue = Mathf.Abs(newArray[i]);
  205. if (absValue > 100 && diff < 0.05f)
  206. {
  207. continue;
  208. }
  209. else if (absValue > 20 && diff < 0.014f)
  210. {
  211. continue;
  212. }
  213. else if (diff < 0.004f)
  214. {
  215. continue;
  216. }
  217. // 大数据更新
  218. lastDataArr[i] = newArray[i];
  219. }
  220. return lastDataArr;
  221. }
  222. //******************Filter Data******************
  223. }
  224. }