FocusDistPanel.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. using System.Collections;
  10. using UnityEngine;
  11. using UnityEngine.UI;
  12. namespace NRKernal.Experimental.NRExamples
  13. {
  14. public class FocusDistPanel : MonoBehaviour
  15. {
  16. [SerializeField]
  17. private Text m_TxtFocusDist;
  18. [SerializeField]
  19. private Slider m_DistanceAdjSlider;
  20. [SerializeField]
  21. private Toggle m_AutoFucusToggle;
  22. public FocusManager m_FocusMan;
  23. [SerializeField]
  24. private Text m_TxtTargetFPS;
  25. [SerializeField]
  26. private Button m_BtnTargetFPS;
  27. private int m_TargetFPS = 30;
  28. [SerializeField]
  29. GameObject m_FocusAnchorGO;
  30. void Start()
  31. {
  32. if (m_FocusMan == null)
  33. {
  34. m_FocusMan = GameObject.FindObjectOfType<FocusManager>();
  35. }
  36. float default_distance = 1.4f;
  37. m_TxtFocusDist.text = default_distance.ToString("F2");
  38. m_DistanceAdjSlider.maxValue = 100.0f;
  39. m_DistanceAdjSlider.minValue = 1.0f;
  40. m_DistanceAdjSlider.value = default_distance;
  41. m_DistanceAdjSlider.onValueChanged.AddListener(OnSlideValueChange);
  42. m_AutoFucusToggle.isOn = m_FocusMan != null;
  43. m_AutoFucusToggle.onValueChanged.AddListener(OnToggleChanged);
  44. /// targetFPS
  45. m_TargetFPS = 30;
  46. Application.targetFrameRate = m_TargetFPS;
  47. m_TxtTargetFPS.text = m_TargetFPS.ToString();
  48. m_BtnTargetFPS.onClick.AddListener(OnBtnTargetFPS);
  49. }
  50. void OnToggleChanged(bool isOn)
  51. {
  52. if (m_FocusMan != null)
  53. {
  54. m_FocusMan.enabled = isOn;
  55. }
  56. }
  57. void OnSlideValueChange(float val)
  58. {
  59. NRSessionManager.Instance.NRRenderer?.SetFocusDistance(val);
  60. m_TxtFocusDist.text = val.ToString("F2");
  61. }
  62. void OnBtnTargetFPS()
  63. {
  64. if (m_TargetFPS == 60)
  65. m_TargetFPS = 10;
  66. else if (m_TargetFPS == 10)
  67. m_TargetFPS = 15;
  68. else if (m_TargetFPS == 15)
  69. m_TargetFPS = 30;
  70. else if (m_TargetFPS == 30)
  71. m_TargetFPS = 60;
  72. else
  73. m_TargetFPS = 30;
  74. Application.targetFrameRate = m_TargetFPS;
  75. m_TxtTargetFPS.text = m_TargetFPS.ToString();
  76. }
  77. private void Update() {
  78. if (NRSessionManager.Instance.NRRenderer == null)
  79. return;
  80. float focusDist = NRSessionManager.Instance.NRRenderer.FocusDistance;
  81. m_TxtFocusDist.text = focusDist.ToString("F2");
  82. if (m_FocusAnchorGO != null)
  83. {
  84. m_FocusAnchorGO.transform.localPosition = new Vector3(0, 0, focusDist);
  85. }
  86. }
  87. }
  88. }