PupilDistanceAdjustment.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 PupilDistanceAdjustment : MonoBehaviour
  15. {
  16. [SerializeField]
  17. private Text m_Value;
  18. [SerializeField]
  19. private Slider m_DistanceAdjSlider;
  20. [SerializeField]
  21. private Toggle m_NativeToggle;
  22. private Transform leftCamera;
  23. private Transform rightCamera;
  24. private Vector3 centerPose;
  25. private Vector3 forward;
  26. IEnumerator Start()
  27. {
  28. m_NativeToggle.isOn = false;
  29. leftCamera = NRSessionManager.Instance.NRHMDPoseTracker.leftCamera.transform;
  30. rightCamera = NRSessionManager.Instance.NRHMDPoseTracker.rightCamera.transform;
  31. while (NRFrame.SessionStatus != SessionState.Running)
  32. {
  33. yield return new WaitForEndOfFrame();
  34. }
  35. yield return new WaitForSeconds(0.2f);
  36. float default_distance = Vector3.Distance(leftCamera.localPosition, rightCamera.localPosition);
  37. centerPose = (leftCamera.localPosition + rightCamera.localPosition) * 0.5f;
  38. forward = (rightCamera.localPosition - leftCamera.localPosition).normalized;
  39. m_Value.text = string.Format("Value:{0}", default_distance);
  40. m_DistanceAdjSlider.maxValue = 0.075f;
  41. m_DistanceAdjSlider.minValue = 0.05f;
  42. m_DistanceAdjSlider.value = default_distance;
  43. m_DistanceAdjSlider.onValueChanged.AddListener(OnValueChange);
  44. }
  45. void OnValueChange(float val)
  46. {
  47. leftCamera.localPosition = centerPose - forward * 0.5f * val;
  48. rightCamera.localPosition = centerPose + forward * 0.5f * val;
  49. // if (m_NativeToggle.isOn)
  50. // NRDevice.Subsystem.SetEyeIPD((int)(val*100));
  51. m_Value.text = val.ToString();
  52. }
  53. }
  54. }