NRCameraInitializer.cs 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.Record
  10. {
  11. using NRKernal;
  12. using UnityEngine;
  13. using System.Collections;
  14. /// <summary> A nr device param initializer. </summary>
  15. [RequireComponent(typeof(Camera))]
  16. public class NRCameraInitializer : MonoBehaviour
  17. {
  18. /// <summary> Type of the device. </summary>
  19. [SerializeField] NativeDevice m_DeviceType = NativeDevice.RGB_CAMERA;
  20. private Camera m_TargetCamera;
  21. public bool IsInitialized { get; private set; } = false;
  22. void Start()
  23. {
  24. m_TargetCamera = gameObject.GetComponent<Camera>();
  25. StartCoroutine(Initialize());
  26. }
  27. private IEnumerator Initialize()
  28. {
  29. #if !UNITY_EDITOR
  30. bool result;
  31. EyeProjectMatrixData matrix_data = NRFrame.GetEyeProjectMatrix(out result, m_TargetCamera.nearClipPlane, m_TargetCamera.farClipPlane);
  32. while (!result)
  33. {
  34. NRDebugger.Info("[NRCameraInitializer] Waitting to initialize camera param.");
  35. yield return new WaitForEndOfFrame();
  36. matrix_data = NRFrame.GetEyeProjectMatrix(out result, m_TargetCamera.nearClipPlane, m_TargetCamera.farClipPlane);
  37. }
  38. if (m_DeviceType == NativeDevice.RGB_CAMERA && !NRKernal.NRDevice.Subsystem.IsFeatureSupported(NRSupportedFeature.NR_FEATURE_RGB_CAMERA))
  39. {
  40. NRDebugger.Warning("[NRCameraInitializer] Auto adaption for DevieType : {0} ==> {1}", m_DeviceType, NativeDevice.LEFT_DISPLAY);
  41. m_DeviceType = NativeDevice.LEFT_DISPLAY;
  42. }
  43. var eyeposFromHead = NRFrame.EyePoseFromHead;
  44. switch (m_DeviceType)
  45. {
  46. case NativeDevice.LEFT_DISPLAY:
  47. m_TargetCamera.projectionMatrix = matrix_data.LEyeMatrix;
  48. NRDebugger.Info("[NRCameraInitializer] Left Camera Project Matrix :" + m_TargetCamera.projectionMatrix.ToString());
  49. transform.localPosition = eyeposFromHead.LEyePose.position;
  50. transform.localRotation = eyeposFromHead.LEyePose.rotation;
  51. NRDebugger.Info("[NRCameraInitializer] Left Camera pos:{0} rotation:{1}", transform.localPosition.ToString(), transform.localRotation.ToString());
  52. break;
  53. case NativeDevice.RIGHT_DISPLAY:
  54. m_TargetCamera.projectionMatrix = matrix_data.REyeMatrix;
  55. NRDebugger.Info("[NRCameraInitializer] Right Camera Project Matrix :" + m_TargetCamera.projectionMatrix.ToString());
  56. transform.localPosition = eyeposFromHead.REyePose.position;
  57. transform.localRotation = eyeposFromHead.REyePose.rotation;
  58. NRDebugger.Info("[NRCameraInitializer] Right Camera pos:{0} rotation:{1}", transform.localPosition.ToString(), transform.localRotation.ToString());
  59. break;
  60. case NativeDevice.RGB_CAMERA:
  61. m_TargetCamera.projectionMatrix = matrix_data.RGBEyeMatrix;
  62. NRDebugger.Info("[NRCameraInitializer] RGB Camera Project Matrix :" + m_TargetCamera.projectionMatrix.ToString());
  63. transform.localPosition = eyeposFromHead.RGBEyePose.position;
  64. transform.localRotation = eyeposFromHead.RGBEyePose.rotation;
  65. NRDebugger.Info("[NRCameraInitializer] RGB Camera pos:{0} rotation:{1}", transform.localPosition.ToString(), transform.localRotation.ToString());
  66. break;
  67. default:
  68. break;
  69. }
  70. #else
  71. yield return new WaitForEndOfFrame();
  72. #endif
  73. IsInitialized = true;
  74. }
  75. /// <summary> Switch to eye parameter. </summary>
  76. /// <param name="eye"> The eye.</param>
  77. public void SwitchToEyeParam(NativeDevice eye)
  78. {
  79. if (m_DeviceType == eye)
  80. {
  81. return;
  82. }
  83. m_DeviceType = eye;
  84. #if !UNITY_EDITOR
  85. StartCoroutine(Initialize());
  86. #endif
  87. }
  88. }
  89. }