VirtualNose.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.XR;
  5. using Valve.VR;
  6. namespace SicknessReduction.Visual
  7. {
  8. public class VirtualNose : MonoBehaviour
  9. {
  10. public GameObject prefabToSpawn;
  11. public Vector3 padding;
  12. public bool adjustToIpd = true;
  13. private Camera cam;
  14. private GameObject nose;
  15. private InputDevice leftEye;
  16. private InputDevice rightEye;
  17. private void OnEnable()
  18. {
  19. cam = Camera.main;
  20. if (cam == null)
  21. {
  22. Debug.LogWarning("No main camera found. Cannot place virtual nose!");
  23. return;
  24. }
  25. Debug.Log($"Main Camera IPD: {cam.stereoSeparation}");
  26. nose = Instantiate(prefabToSpawn, cam.transform);
  27. nose.transform.localPosition = Vector3.forward * cam.nearClipPlane + padding;
  28. }
  29. private void Start()
  30. {
  31. List<InputDevice> devices = new List<InputDevice>();
  32. InputDevices.GetDevicesWithCharacteristics(
  33. InputDeviceCharacteristics.Camera | InputDeviceCharacteristics.HeadMounted, devices);
  34. if (devices.Count == 0)
  35. {
  36. Debug.LogWarning("No devices");
  37. return;
  38. }
  39. devices[0].TryGetFeatureValue(CommonUsages.rightEyePosition, out var reye);
  40. devices[0].TryGetFeatureValue(CommonUsages.leftEyePosition, out var leye);
  41. Debug.Log($"Eyes: {leye}, {reye}");
  42. }
  43. private void OnDisable()
  44. {
  45. if (nose == null) return;
  46. Destroy(nose);
  47. }
  48. }
  49. }