NibiruMarkerCamera.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using UnityEngine;
  2. namespace Nxr.Internal
  3. {
  4. [RequireComponent(typeof(Transform))]
  5. public class NibiruMarkerCamera : MonoBehaviour
  6. {
  7. public NibiruMarker nibiruMarker;
  8. private int updateProjMat = 0;
  9. NibiruService nibiruService;
  10. Camera cam;
  11. Transform cacheTransform;
  12. Vector3 firstFoundPosition = Vector3.zero;
  13. private float lerpTime = 0;
  14. // Use this for initialization
  15. void Start()
  16. {
  17. nibiruService = NxrViewer.Instance.GetNibiruService();
  18. cam = this.gameObject.GetComponent<Camera>();
  19. cacheTransform = transform;
  20. }
  21. // Update is called once per frame
  22. void Update()
  23. {
  24. if (nibiruService != null && nibiruService.IsMarkerRecognizeRunning && (nibiruMarker != null && nibiruMarker.IsVisible()) && updateProjMat < 10)
  25. {
  26. float[] array = nibiruService.GetMarkerProjectionMatrix();
  27. if (array != null)
  28. {
  29. // Marker识别到才进行更新Projection
  30. // Debug.Log(" >>>>>>>>>>>>>>>>>>>>>>>>>>cam.projectionMatrix : " + cam.projectionMatrix.ToString());
  31. Matrix4x4 projectionMatrix = ARUtilityFunctions.MatrixFromFloatArray(nibiruService.GetMarkerProjectionMatrix());
  32. cam.projectionMatrix = projectionMatrix;
  33. NxrEye[] eyes = NxrViewer.Instance.eyes;
  34. if (eyes != null)
  35. {
  36. eyes[0].cam.projectionMatrix = projectionMatrix;
  37. eyes[1].cam.projectionMatrix = projectionMatrix;
  38. // 4:3=>12:9 || 16:9, 2/16
  39. //eyes[0].cam.rect = new Rect(0.0625f, 0, 0.375f, 1);
  40. //eyes[1].cam.rect = new Rect(0.5625f, 0, 0.375f, 1);
  41. }
  42. updateProjMat++;
  43. }
  44. }
  45. if (nibiruMarker != null && nibiruMarker.IsVisible())
  46. {
  47. Matrix4x4 cameraPose = nibiruMarker.CameraPose();
  48. Vector3 arPosition = ARUtilityFunctions.PositionFromMatrix(cameraPose);
  49. // Camera orientation: In ARToolKit, zero rotation of the camera corresponds to looking vertically down on a marker
  50. // lying flat on the ground. In Unity however, if we still treat markers as being flat on the ground, we clash with Unity's
  51. // camera "rotation", because an unrotated Unity camera is looking horizontally.
  52. // So we choose to treat an unrotated marker as standing vertically, and apply a transform to the scene to
  53. // to get it to lie flat on the ground.
  54. Quaternion arRotation = ARUtilityFunctions.QuaternionFromMatrix(cameraPose);
  55. Vector3 leftCameraPosDiff = nibiruMarker.CameraPosition(0) - arPosition;
  56. Vector3 rightCameraPosDiff = nibiruMarker.CameraPosition(1) - arPosition;
  57. leftCameraPosDiff.y = 0;
  58. leftCameraPosDiff.z = 0;
  59. rightCameraPosDiff.y = 0;
  60. rightCameraPosDiff.z = 0;
  61. //DefaultPosition=(-0.1, -0.2, -0.3), Left=(-0.2, -0.3, -0.2), Right=(-0.1, -0.3, -0.3)
  62. //Debug.Log("DefaultPosition=" + arPosition.ToString("f4") + ", Left=" + nibiruMarker.CameraPosition(0).ToString("f4")
  63. // + ", Right=" + nibiruMarker.CameraPosition(1).ToString("f4") + ", Left Diff=" + leftCameraPosDiff.ToString("f4")
  64. // + ", Right Diff=" + rightCameraPosDiff.ToString("f4"));
  65. bool dtrMode = NxrGlobal.supportDtr && NxrGlobal.distortionEnabled;
  66. if (cacheTransform.localPosition.x == 0 && cacheTransform.localPosition.y == 0 && cacheTransform.localPosition.z == 0
  67. && (arPosition.x != 0 || arPosition.y != 0 || arPosition.z != 0))
  68. {
  69. Debug.Log("Move from (0,0,0) to " + arPosition.ToString());
  70. firstFoundPosition = arPosition;
  71. }
  72. if((firstFoundPosition.x !=0 || firstFoundPosition.y != 0 || firstFoundPosition.z != 0) && lerpTime <= 1.0f)
  73. {
  74. // 插值移动到初始位置
  75. lerpTime += Time.deltaTime;
  76. cacheTransform.localPosition = Vector3.Lerp(cacheTransform.localPosition, firstFoundPosition, lerpTime);
  77. // Debug.Log("====>" + cacheTransform.localPosition.ToString());
  78. } else
  79. {
  80. cacheTransform.localPosition = arPosition;
  81. }
  82. cacheTransform.localRotation = arRotation;
  83. NxrGlobal.markerZDistance = Mathf.Abs(arPosition.z);
  84. // print
  85. // Debug.Log("POS = " + arPosition.ToString());
  86. // Debug.Log("ROT = " + arRotation.ToString());
  87. // print
  88. // 平滑处理
  89. // cacheTransform.localRotation = SmoothMove(cacheTransform.localRotation, arRotation);
  90. NxrEye[] eyes = NxrViewer.Instance.eyes;
  91. if (eyes != null)
  92. {
  93. for (int i = 0; i < 2; i++)
  94. {
  95. // 非DTR取消左右眼偏移
  96. if (!dtrMode && !NxrGlobal.offaxisDistortionEnabled)
  97. {
  98. // NED+
  99. eyes[i].cacheTransform.localPosition = new Vector3(0, 0, 0);
  100. }
  101. else if (!dtrMode && NxrGlobal.offaxisDistortionEnabled)
  102. {
  103. // Polaroid
  104. float offAxisCameraOffset = i == 0 ?
  105. -0.029f + leftCameraPosDiff.x + NxrGlobal.leftMarkerCameraOffSet :
  106. 0.029f + rightCameraPosDiff.x + NxrGlobal.rightMarkerCameraOffset;
  107. eyes[i].cacheTransform.localPosition = new Vector3(offAxisCameraOffset, 0, 0);
  108. // Debug.Log(i + "-------> " + eyes[i].cacheTransform.localPosition.ToString("f4") + ",leftCameraPosDiff="+ leftCameraPosDiff.x
  109. //+ ",rightCameraPosDiff="+ rightCameraPosDiff.x);
  110. }
  111. else
  112. {
  113. eyes[i].cacheTransform.localPosition = new Vector3(i == 0 ? -0.032f : 0.032f, 0, 0) + (i == 0 ? leftCameraPosDiff : rightCameraPosDiff);
  114. // Debug.Log(i + "-------> " + eyes[i].cacheTransform.localPosition.ToString("f4"));
  115. }
  116. }
  117. }
  118. // Debug.Log("camera pose->" + transform.localPosition.ToString() + "," + transform.localRotation.eulerAngles.ToString());
  119. }
  120. }
  121. private bool triggerStop = false;
  122. private void OnDestroy()
  123. {
  124. if (nibiruService != null && nibiruService.IsMarkerRecognizeRunning)
  125. {
  126. nibiruService.StopMarkerRecognize();
  127. triggerStop = true;
  128. }
  129. lerpTime = 0;
  130. firstFoundPosition = Vector3.zero;
  131. Debug.Log("NibiruMarkerCamera.OnDestroy");
  132. }
  133. private void OnApplicationPause(bool pause)
  134. {
  135. if (pause)
  136. {
  137. // 暂停相机
  138. OnDestroy();
  139. }
  140. else if (triggerStop)
  141. {
  142. if (nibiruService != null)
  143. {
  144. nibiruService.StartMarkerRecognize();
  145. triggerStop = false;
  146. updateProjMat = 0;
  147. }
  148. }
  149. }
  150. }
  151. }