2
0

DynamicDoF.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. public float refocusTimePerMeter = 0.0000017f; //seconds; Carneige, Rhee (2015)
  18. public float maxFocusDistance = 30000f; //metres; Carneige, Rhee (2015
  19. public Camera playerCamera;
  20. public VolumeProfile postProcessProfile;
  21. private Transform cameraTransform;
  22. private DepthOfField doF;
  23. private bool doFAvailable;
  24. private float maxCocRadius;
  25. private List<float> rayDistances = new List<float>(NUMBER_OF_RAYS);
  26. private void Start()
  27. {
  28. cameraTransform = playerCamera.transform;
  29. //Debug.Log($"Screen Width = {playerCamera.pixelWidth}, scaled = {playerCamera.scaledPixelWidth}");
  30. //maxCocRadius = maxFactorRadiusCOC * playerCamera.scaledPixelWidth; FIXME: waaaay to small
  31. doF = (DepthOfField) postProcessProfile.components.FirstOrDefault(c => c is DepthOfField);
  32. doFAvailable = doF != null;
  33. if (doFAvailable)
  34. {
  35. // ReSharper disable once PossibleNullReferenceException
  36. doF.mode.value = DepthOfFieldMode.Bokeh;
  37. doF.focalLength.min = 0f;
  38. doF.focalLength.max = float.MaxValue;
  39. doF.aperture.max = float.MaxValue;
  40. doF.aperture.min = 0f;
  41. }
  42. else
  43. {
  44. Debug.LogWarning("No DepthOfField found in PostProcessing Profile!");
  45. }
  46. }
  47. private void Update()
  48. {
  49. if (!doFAvailable) return;
  50. var focusDistance = CastRays();
  51. if (focusDistance < 0)
  52. {
  53. focusDistance = doF.focusDistance.value;
  54. //return;
  55. }
  56. /*For real-time performance, we
  57. simply assume all users will take a static 500 ms
  58. to refocus from an infinite distance to a close distance
  59. (≈ 1 m), using the value calculated in earlier
  60. work13 (assuming a typical adult’s eyes). This
  61. translates to a linear interpolation between focal
  62. distances that takes ≈ 1.7*/
  63. doF.active = true;
  64. var timeNeededToRefocus = Mathf.Abs(focusDistance - doF.focusDistance.value) * refocusTimePerMeter;
  65. Debug.Log($"Lerping focusDistance before = {focusDistance}, dof.focusDistance = {doF.focusDistance.value}, time for refocus = {timeNeededToRefocus}, delta = {Time.deltaTime}");
  66. focusDistance = Mathf.Lerp(doF.focusDistance.value, focusDistance, Time.deltaTime / timeNeededToRefocus);
  67. Debug.Log($"Lerping focusDistance after = {focusDistance}");
  68. doF.focusDistance.value = focusDistance;
  69. doF.focalLength.value = 1;
  70. doF.aperture.value = ApertureForCocAndFocusDistance(maxFactorRadiusCOC, focusDistance);
  71. }
  72. private float ApertureForCocAndFocusDistance(float coc, float focusDistance) =>
  73. 1 / (1000 * coc * (focusDistance - 0.001f));
  74. private float CastRays()
  75. {
  76. var position = cameraTransform.position;
  77. var forward = cameraTransform.forward;
  78. var start = position + forward * playerCamera.nearClipPlane;
  79. var ends = new Vector3[NUMBER_OF_RAYS];
  80. ends[0] = position + forward * playerCamera.farClipPlane;
  81. ends[1] = ends[0] + cameraTransform.TransformDirection(new Vector3(RAY_OFFSET, 0, 0));
  82. ends[2] = ends[0] + cameraTransform.TransformDirection(new Vector3(-RAY_OFFSET, 0, 0));
  83. ends[3] = ends[0] + cameraTransform.TransformDirection(new Vector3(0, RAY_OFFSET, 0));
  84. ends[4] = ends[0] + cameraTransform.TransformDirection(new Vector3(0, -RAY_OFFSET, 0));
  85. ends[5] = ends[0] + cameraTransform.TransformDirection(new Vector3(RAY_OFFSET, RAY_OFFSET, 0));
  86. ends[6] = ends[0] + cameraTransform.TransformDirection(new Vector3(-RAY_OFFSET, RAY_OFFSET, 0));
  87. ends[7] = ends[0] + cameraTransform.TransformDirection(new Vector3(RAY_OFFSET, -RAY_OFFSET, 0));
  88. ends[8] = ends[0] + cameraTransform.TransformDirection(new Vector3(-RAY_OFFSET, -RAY_OFFSET, 0));
  89. rayDistances.Clear();
  90. foreach (var end in ends)
  91. {
  92. if (Physics.Linecast(start, end, out var hit, Physics.DefaultRaycastLayers))
  93. {
  94. Debug.DrawLine(start, (end - start).normalized * hit.distance, Color.green);
  95. //Debug.Log("DoF - Hit, Distance = " + hit.distance);
  96. rayDistances.Add(hit.distance);
  97. }
  98. else
  99. {
  100. Debug.DrawRay(position, position + forward * playerCamera.farClipPlane, Color.red);
  101. }
  102. }
  103. if (rayDistances.Count < 1) return -1;
  104. return Helpers.RemoveOutliers(rayDistances).Average();
  105. }
  106. }
  107. }