PhonePoseTrackerRGB.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using EZXR.Glass;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using EZXR.Glass.SixDof;
  6. using static EZXR.Glass.SixDof.HMDPoseTracker;
  7. using EZXR.Glass.Core;
  8. using EZXR.Glass.Device;
  9. namespace EZXR.Glass.Recording
  10. {
  11. public class PhonePoseTrackerRGB : MonoBehaviour
  12. {
  13. private static PhonePoseTrackerRGB _instance;
  14. public static PhonePoseTrackerRGB Instance
  15. {
  16. get
  17. {
  18. return _instance;
  19. }
  20. }
  21. private Transform m_Transform;
  22. private static int picwith;
  23. private static int picheight;
  24. Matrix4x4 offset_SpatialComputing = Matrix4x4.identity;
  25. Vector3 tempPosition;
  26. Quaternion tempRotation;
  27. Matrix4x4 tempMatrix;
  28. //https://gitfub.space/Pownie/arskrald/blob/716b28b3e55d0d2e253dc346ccc79b23a08bcd1c/AR-3/Assets/OpenCVForUnity/org/opencv/unity/ARUtils.cs
  29. static Matrix4x4 CalculateProjectionMatrixFromCameraMatrixValues(float fx, float fy, float cx, float cy, float width, float height, float near, float far)
  30. {
  31. Matrix4x4 projectionMatrix = new Matrix4x4();
  32. projectionMatrix.m00 = 2.0f * fx / width;
  33. projectionMatrix.m02 = 1.0f - 2.0f * cx / width;
  34. projectionMatrix.m11 = 2.0f * fy / height;
  35. projectionMatrix.m12 = -1.0f + 2.0f * cy / height;
  36. projectionMatrix.m22 = -(far + near) / (far - near);
  37. projectionMatrix.m23 = -2.0f * far * near / (far - near);
  38. projectionMatrix.m32 = -1.0f;
  39. return projectionMatrix;
  40. }
  41. private void Awake()
  42. {
  43. _instance = this;
  44. m_Transform = transform;
  45. }
  46. // Start is called before the first frame update
  47. void Start()
  48. {
  49. if (!Application.isEditor)
  50. {
  51. //@buqing init param when start
  52. StartCoroutine(InitParam());
  53. }
  54. }
  55. // Update is called once per frame
  56. void Update()
  57. {
  58. UpdatePoseByTrackingType();
  59. }
  60. private IEnumerator InitParam()
  61. {
  62. yield return new WaitUntil(() => SessionManager.Instance != null && SessionManager.Instance.IsInited);
  63. Camera camera = this.gameObject.GetComponent<Camera>();
  64. NormalRGBCameraDevice rgbCameraDevice = new NormalRGBCameraDevice();
  65. float[] intrinsic = new float[8];
  66. rgbCameraDevice.getRGBCameraIntrics(intrinsic);
  67. float fx = intrinsic[0];
  68. float fy = intrinsic[1];
  69. float cx = intrinsic[2];
  70. float cy = intrinsic[3];
  71. Debug.Log("=============Unity Log=============== PhonePoseTrackerRGB -- InitParam rgb intrinsic " + fx + " " + fy + " " + cx + " " + cy);
  72. int[] sizeRgbCamera = rgbCameraDevice.getCameraSize();
  73. picwith = sizeRgbCamera[0];
  74. picheight = sizeRgbCamera[1];
  75. camera.projectionMatrix = CalculateProjectionMatrixFromCameraMatrixValues(fx, fy, cx, cy, picwith, picheight, camera.nearClipPlane, camera.farClipPlane);
  76. }
  77. private void UpdatePoseByTrackingType()
  78. {
  79. // ARFrame.OnFixedUpdate();
  80. if (ARFrame.SessionStatus != EZVIOState.EZVIOCameraState_Tracking) return;
  81. //Pose phonePose = ARFrame.GetRGBCameraPose();
  82. Pose phonePose = ARFrame.GetLatestPose_RGBCamera();
  83. DegreeOfFreedom degreeOfFreedom = HMDPoseTracker.Instance.degreeOfFreedom;
  84. if (HMDPoseTracker.Instance.UseLocalPose)
  85. {
  86. if (degreeOfFreedom != DegreeOfFreedom.ZeroDof)
  87. {
  88. if (degreeOfFreedom == DegreeOfFreedom.SixDof)
  89. {
  90. m_Transform.localPosition = phonePose.position;
  91. }
  92. m_Transform.localRotation = phonePose.rotation;
  93. }
  94. }
  95. else
  96. {
  97. if (degreeOfFreedom != DegreeOfFreedom.ZeroDof)
  98. {
  99. if (degreeOfFreedom == DegreeOfFreedom.SixDof)
  100. {
  101. m_Transform.position = phonePose.position;
  102. }
  103. m_Transform.rotation = phonePose.rotation;
  104. }
  105. }
  106. // if (XRMan.Instance != null)
  107. // {
  108. //#if SpatialComputing
  109. // if (EZXRSpatialComputingManager.Instance != null)
  110. // {
  111. // tempMatrix = offset_SpatialComputing * Matrix4x4.TRS(m_Transform.position, m_Transform.rotation, Vector3.one);
  112. // tempPosition = tempMatrix.GetColumn(3);
  113. // tempRotation = tempMatrix.rotation;
  114. // }
  115. // m_Transform.position = tempPosition;
  116. // m_Transform.rotation = tempRotation;
  117. //#endif
  118. // }
  119. }
  120. }
  121. }