VirtualNose.cs 1.6 KB

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