NRTrackingModeChangedTip.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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
  10. {
  11. using UnityEngine;
  12. using UnityEngine.UI;
  13. using System.Collections;
  14. using System.IO;
  15. public class NRTrackingModeChangedTip : MonoBehaviour
  16. {
  17. [SerializeField]
  18. private Camera m_LeftCamera;
  19. [SerializeField]
  20. private Camera m_RightCamera;
  21. [SerializeField]
  22. private Transform m_Plane;
  23. [SerializeField]
  24. private float m_Distance = 25;
  25. [SerializeField]
  26. private Text m_Lable;
  27. [SerializeField]
  28. private Transform m_LoadingUI;
  29. public RenderTexture LeftRT { get; private set; }
  30. public RenderTexture RightRT { get; private set; }
  31. private static NativeResolution resolution = new NativeResolution(1920, 1080);
  32. public static NRTrackingModeChangedTip Create()
  33. {
  34. NRTrackingModeChangedTip lostTrackingTip;
  35. var config = NRSessionManager.Instance.NRSessionBehaviour?.SessionConfig;
  36. if (config == null || config.TrackingModeChangeTipPrefab == null)
  37. {
  38. lostTrackingTip = GameObject.Instantiate(Resources.Load<NRTrackingModeChangedTip>("NRTrackingModeChangedTip"));
  39. }
  40. else
  41. {
  42. lostTrackingTip = GameObject.Instantiate(config.TrackingModeChangeTipPrefab);
  43. }
  44. lostTrackingTip.transform.position = Vector3.one * 8888;
  45. #if !UNITY_EDITOR
  46. resolution = NRFrame.GetDeviceResolution(NativeDevice.LEFT_DISPLAY);
  47. #endif
  48. lostTrackingTip.LeftRT = UnityExtendedUtility.CreateRenderTexture(resolution.width, resolution.height, 24, RenderTextureFormat.Default);
  49. lostTrackingTip.m_LeftCamera.targetTexture = lostTrackingTip.LeftRT;
  50. lostTrackingTip.RightRT = UnityExtendedUtility.CreateRenderTexture(resolution.width, resolution.height, 24, RenderTextureFormat.Default);
  51. lostTrackingTip.m_RightCamera.targetTexture = lostTrackingTip.RightRT;
  52. return lostTrackingTip;
  53. }
  54. public void SetMessage(string msg)
  55. {
  56. m_Lable.text = msg;
  57. }
  58. void Update()
  59. {
  60. m_LoadingUI.Rotate(-Vector3.forward, 2f, Space.Self);
  61. }
  62. void LateUpdate()
  63. {
  64. var leftCameraPosition = m_LeftCamera.transform.localPosition;
  65. var rightCameraPosition = m_RightCamera.transform.localPosition;
  66. var leftCameraRotation = m_LeftCamera.transform.localRotation;
  67. var rightCameraRotation = m_RightCamera.transform.localRotation;
  68. var leftCameraForward = m_LeftCamera.transform.forward;
  69. var rightCameraForward = m_RightCamera.transform.forward;
  70. var centerPos = (leftCameraPosition + rightCameraPosition) * 0.5f;
  71. var forward = (leftCameraForward + rightCameraForward) * 0.5f;
  72. var centerRotation = Quaternion.Lerp(leftCameraRotation, rightCameraRotation, 0.5f);
  73. m_Plane.localPosition = centerPos + forward * m_Distance;
  74. m_Plane.localRotation = centerRotation;
  75. }
  76. void Start()
  77. {
  78. m_LeftCamera.aspect = (float)resolution.width / resolution.height;
  79. m_RightCamera.aspect = (float)resolution.width / resolution.height;
  80. }
  81. void OnEnable()
  82. {
  83. m_LeftCamera.enabled = true;
  84. m_RightCamera.enabled = true;
  85. }
  86. void OnDisable()
  87. {
  88. m_LeftCamera.enabled = false;
  89. m_RightCamera.enabled = false;
  90. }
  91. void OnDestroy()
  92. {
  93. if (LeftRT != null)
  94. {
  95. LeftRT.Release();
  96. Destroy(LeftRT);
  97. LeftRT = null;
  98. }
  99. if (RightRT != null)
  100. {
  101. RightRT.Release();
  102. Destroy(RightRT);
  103. RightRT = null;
  104. }
  105. }
  106. }
  107. }