DynamicDoF.cs 6.4 KB

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