DynamicDoF.cs 6.3 KB

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