2
0

VirtualNose.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using UnityEngine;
  3. namespace SicknessReduction.Visual
  4. {
  5. public class VirtualNose : MonoBehaviour
  6. {
  7. public GameObject prefabToSpawn;
  8. public bool adjustToIpd = true;
  9. public Vector3 paddingAt58;
  10. public Vector3 scaleAt58;
  11. public Vector3 paddingAt70;
  12. public Vector3 scaleAt70;
  13. private Camera cam;
  14. private GameObject nose;
  15. private IpdInfo ipdInfo;
  16. private void OnEnable()
  17. {
  18. cam = Camera.main;
  19. if (cam == null)
  20. {
  21. Debug.LogError("No main camera found. Cannot place virtual nose!");
  22. return;
  23. }
  24. ipdInfo = GetComponentInParent<IpdInfo>();
  25. if (ipdInfo == null)
  26. {
  27. Debug.LogError("No IPD Info found as parent of virtual nose");
  28. }
  29. if (adjustToIpd) ipdInfo.onIpdChanged += AdjustToIpd;
  30. nose = Instantiate(prefabToSpawn, cam.transform);
  31. nose.transform.localPosition = Vector3.forward * cam.nearClipPlane;
  32. }
  33. private void AdjustToIpd(float ipdMeters)
  34. {
  35. var ipdMm = Mathf.Round(ipdMeters * 1000f);
  36. var ipdFactor = (ipdMm - 58f) / 12f;
  37. var padding = Vector3.Lerp(paddingAt58, paddingAt70, ipdFactor);
  38. nose.transform.localScale = Vector3.Lerp(scaleAt58, scaleAt70, ipdFactor);
  39. nose.transform.localPosition = Vector3.forward * cam.nearClipPlane + padding;
  40. }
  41. private void OnDisable()
  42. {
  43. ipdInfo.onIpdChanged -= AdjustToIpd;
  44. if (nose == null) return;
  45. Destroy(nose);
  46. }
  47. }
  48. }