DynamicDoF.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.Rendering;
  7. using UnityEngine.Rendering.Universal;
  8. namespace SicknessReduction.Visual.DoF
  9. {
  10. //TODO: look at https://catlikecoding.com/unity/tutorials/advanced-rendering/depth-of-field/ or pseudocode in paper
  11. public class DynamicDoF : MonoBehaviour
  12. {
  13. private const int NUMBER_OF_RAYS = 9; // Carneige, Rhee (2015)
  14. private const float RAY_OFFSET = 6f; //Exact value not mentioned in paper
  15. [Tooltip("Max Radius of Circle of Confusion in percent of display width")]
  16. public float maxFactorRadiusCOC = 0.0175f; //Carneige, Rhee (2015)
  17. [Range(0.0f, 2.0f)]
  18. public float pow = 0.3f;
  19. public float refocusTimePerMeter = 0.0000017f; //seconds; Carneige, Rhee (2015)
  20. public float maxFocusDistance = 30000f; //metres; Carneige, Rhee (2015
  21. public Camera playerCamera;
  22. public VolumeProfile postProcessProfile;
  23. private Transform cameraTransform;
  24. private DepthOfField doF;
  25. private bool doFAvailable;
  26. private float maxCocRadius;
  27. private List<float> rayDistances = new List<float>(NUMBER_OF_RAYS);
  28. private Vector3[] ends = new Vector3[NUMBER_OF_RAYS];
  29. //TODO: debug, remove
  30. public GameObject gizmoPrefab;
  31. private GameObject[] hits = new GameObject[NUMBER_OF_RAYS];
  32. private void Start()
  33. {
  34. cameraTransform = playerCamera.transform;
  35. //Debug.Log($"Screen Width = {playerCamera.pixelWidth}, scaled = {playerCamera.scaledPixelWidth}");
  36. //maxCocRadius = maxFactorRadiusCOC * playerCamera.scaledPixelWidth; FIXME: waaaay to small
  37. for (int i = 0; i < NUMBER_OF_RAYS; i++)
  38. {
  39. hits[i] = Instantiate(gizmoPrefab);
  40. hits[i].SetActive(false);
  41. } //TODO: debug, remove
  42. doF = (DepthOfField) postProcessProfile.components.FirstOrDefault(c => c is DepthOfField);
  43. doFAvailable = doF != null;
  44. if (doFAvailable)
  45. {
  46. // ReSharper disable once PossibleNullReferenceException
  47. doF.mode.value = DepthOfFieldMode.Bokeh;
  48. doF.focalLength.min = 0f;
  49. doF.focalLength.max = float.MaxValue;
  50. doF.aperture.max = float.MaxValue;
  51. doF.aperture.min = 0f;
  52. }
  53. else
  54. {
  55. Debug.LogWarning("No DepthOfField found in PostProcessing Profile!");
  56. }
  57. }
  58. private void Update()
  59. {
  60. if (!doFAvailable) return;
  61. var focusDistance = CastRays();
  62. if (focusDistance < 0)
  63. {
  64. focusDistance = doF.focusDistance.value;
  65. //return;
  66. }
  67. /*For real-time performance, we
  68. simply assume all users will take a static 500 ms
  69. to refocus from an infinite distance to a close distance
  70. (≈ 1 m), using the value calculated in earlier
  71. work13 (assuming a typical adult’s eyes). This
  72. translates to a linear interpolation between focal
  73. distances that takes ≈ 1.7*/
  74. doF.active = true;
  75. var timeNeededToRefocus = Mathf.Abs(focusDistance - doF.focusDistance.value) * refocusTimePerMeter;
  76. focusDistance = Mathf.Lerp(doF.focusDistance.value, focusDistance, Time.deltaTime / timeNeededToRefocus);
  77. doF.focusDistance.value = focusDistance;
  78. doF.focalLength.value = 1;
  79. var coc = maxFactorRadiusCOC * 1 / Mathf.Pow(focusDistance, pow);
  80. //var coc = maxFactorRadiusCOC * 1 - ()
  81. doF.aperture.value = ApertureForCocAndFocusDistance(coc, focusDistance);
  82. }
  83. private float ApertureForCocAndFocusDistance(float coc, float focusDistance) =>
  84. 1 / (1000 * coc * (focusDistance - 0.001f));
  85. private float CastRays()
  86. {
  87. var position = cameraTransform.position;
  88. var forward = cameraTransform.forward;
  89. var start = position + forward * playerCamera.nearClipPlane;
  90. ends[0] = position + forward * playerCamera.farClipPlane;
  91. ends[1] = ends[0] + cameraTransform.TransformDirection(new Vector3(RAY_OFFSET, 0, 0));
  92. ends[2] = ends[0] + cameraTransform.TransformDirection(new Vector3(-RAY_OFFSET, 0, 0));
  93. ends[3] = ends[0] + cameraTransform.TransformDirection(new Vector3(0, RAY_OFFSET, 0));
  94. ends[4] = ends[0] + cameraTransform.TransformDirection(new Vector3(0, -RAY_OFFSET, 0));
  95. ends[5] = ends[0] + cameraTransform.TransformDirection(new Vector3(RAY_OFFSET, RAY_OFFSET, 0));
  96. ends[6] = ends[0] + cameraTransform.TransformDirection(new Vector3(-RAY_OFFSET, RAY_OFFSET, 0));
  97. ends[7] = ends[0] + cameraTransform.TransformDirection(new Vector3(RAY_OFFSET, -RAY_OFFSET, 0));
  98. ends[8] = ends[0] + cameraTransform.TransformDirection(new Vector3(-RAY_OFFSET, -RAY_OFFSET, 0));
  99. rayDistances.Clear();
  100. for (var i = 0; i < ends.Length; i++)
  101. {
  102. var end = ends[i];
  103. if (Physics.Linecast(start, end, out var hit, Physics.DefaultRaycastLayers))
  104. {
  105. Debug.DrawLine(start, hit.point, Color.green);
  106. hits[i].transform.position = hit.point;
  107. hits[i].transform.localScale = Vector3.one * (hit.distance * 0.01f);
  108. hits[i].SetActive(true);
  109. //Debug.Log("DoF - Hit, Distance = " + hit.distance);
  110. rayDistances.Add(hit.distance);
  111. }
  112. else
  113. {
  114. hits[i].SetActive(false);
  115. Debug.DrawRay(position, position + forward * playerCamera.farClipPlane, Color.red);
  116. }
  117. }
  118. if (rayDistances.Count < 1) return -1;
  119. return Helpers.RemoveOutliers(rayDistances).Average();
  120. }
  121. }
  122. }