DepthOfFieldComponent.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using UnityEngine.Rendering;
  2. namespace UnityEngine.PostProcessing
  3. {
  4. using DebugMode = BuiltinDebugViewsModel.Mode;
  5. public sealed class DepthOfFieldComponent : PostProcessingComponentRenderTexture<DepthOfFieldModel>
  6. {
  7. static class Uniforms
  8. {
  9. internal static readonly int _DepthOfFieldTex = Shader.PropertyToID("_DepthOfFieldTex");
  10. internal static readonly int _DepthOfFieldCoCTex = Shader.PropertyToID("_DepthOfFieldCoCTex");
  11. internal static readonly int _Distance = Shader.PropertyToID("_Distance");
  12. internal static readonly int _LensCoeff = Shader.PropertyToID("_LensCoeff");
  13. internal static readonly int _MaxCoC = Shader.PropertyToID("_MaxCoC");
  14. internal static readonly int _RcpMaxCoC = Shader.PropertyToID("_RcpMaxCoC");
  15. internal static readonly int _RcpAspect = Shader.PropertyToID("_RcpAspect");
  16. internal static readonly int _MainTex = Shader.PropertyToID("_MainTex");
  17. internal static readonly int _CoCTex = Shader.PropertyToID("_CoCTex");
  18. internal static readonly int _TaaParams = Shader.PropertyToID("_TaaParams");
  19. internal static readonly int _DepthOfFieldParams = Shader.PropertyToID("_DepthOfFieldParams");
  20. }
  21. const string k_ShaderString = "Hidden/Post FX/Depth Of Field";
  22. public override bool active
  23. {
  24. get
  25. {
  26. return model.enabled
  27. && !context.interrupted;
  28. }
  29. }
  30. public override DepthTextureMode GetCameraFlags()
  31. {
  32. return DepthTextureMode.Depth;
  33. }
  34. RenderTexture m_CoCHistory;
  35. // Height of the 35mm full-frame format (36mm x 24mm)
  36. const float k_FilmHeight = 0.024f;
  37. float CalculateFocalLength()
  38. {
  39. var settings = model.settings;
  40. if (!settings.useCameraFov)
  41. return settings.focalLength / 1000f;
  42. float fov = context.camera.fieldOfView * Mathf.Deg2Rad;
  43. return 0.5f * k_FilmHeight / Mathf.Tan(0.5f * fov);
  44. }
  45. float CalculateMaxCoCRadius(int screenHeight)
  46. {
  47. // Estimate the allowable maximum radius of CoC from the kernel
  48. // size (the equation below was empirically derived).
  49. float radiusInPixels = (float)model.settings.kernelSize * 4f + 6f;
  50. // Applying a 5% limit to the CoC radius to keep the size of
  51. // TileMax/NeighborMax small enough.
  52. return Mathf.Min(0.05f, radiusInPixels / screenHeight);
  53. }
  54. bool CheckHistory(int width, int height)
  55. {
  56. return m_CoCHistory != null && m_CoCHistory.IsCreated() &&
  57. m_CoCHistory.width == width && m_CoCHistory.height == height;
  58. }
  59. RenderTextureFormat SelectFormat(RenderTextureFormat primary, RenderTextureFormat secondary)
  60. {
  61. if (SystemInfo.SupportsRenderTextureFormat(primary)) return primary;
  62. if (SystemInfo.SupportsRenderTextureFormat(secondary)) return secondary;
  63. return RenderTextureFormat.Default;
  64. }
  65. public void Prepare(RenderTexture source, Material uberMaterial, bool antialiasCoC, Vector2 taaJitter, float taaBlending)
  66. {
  67. var settings = model.settings;
  68. var colorFormat = RenderTextureFormat.DefaultHDR;
  69. var cocFormat = SelectFormat(RenderTextureFormat.R8, RenderTextureFormat.RHalf);
  70. // Avoid using R8 on OSX with Metal. #896121, https://goo.gl/MgKqu6
  71. #if (UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX) && !UNITY_2017_1_OR_NEWER
  72. if (SystemInfo.graphicsDeviceType == GraphicsDeviceType.Metal)
  73. cocFormat = SelectFormat(RenderTextureFormat.RHalf, RenderTextureFormat.Default);
  74. #endif
  75. // Material setup
  76. var f = CalculateFocalLength();
  77. var s1 = Mathf.Max(settings.focusDistance, f);
  78. var aspect = (float)source.width / source.height;
  79. var coeff = f * f / (settings.aperture * (s1 - f) * k_FilmHeight * 2);
  80. var maxCoC = CalculateMaxCoCRadius(source.height);
  81. var material = context.materialFactory.Get(k_ShaderString);
  82. material.SetFloat(Uniforms._Distance, s1);
  83. material.SetFloat(Uniforms._LensCoeff, coeff);
  84. material.SetFloat(Uniforms._MaxCoC, maxCoC);
  85. material.SetFloat(Uniforms._RcpMaxCoC, 1f / maxCoC);
  86. material.SetFloat(Uniforms._RcpAspect, 1f / aspect);
  87. // CoC calculation pass
  88. var rtCoC = context.renderTextureFactory.Get(context.width, context.height, 0, cocFormat, RenderTextureReadWrite.Linear);
  89. Graphics.Blit(null, rtCoC, material, 0);
  90. if (antialiasCoC)
  91. {
  92. // CoC temporal filter pass
  93. material.SetTexture(Uniforms._CoCTex, rtCoC);
  94. var blend = CheckHistory(context.width, context.height) ? taaBlending : 0f;
  95. material.SetVector(Uniforms._TaaParams, new Vector3(taaJitter.x, taaJitter.y, blend));
  96. var rtFiltered = RenderTexture.GetTemporary(context.width, context.height, 0, cocFormat);
  97. Graphics.Blit(m_CoCHistory, rtFiltered, material, 1);
  98. context.renderTextureFactory.Release(rtCoC);
  99. if (m_CoCHistory != null) RenderTexture.ReleaseTemporary(m_CoCHistory);
  100. m_CoCHistory = rtCoC = rtFiltered;
  101. }
  102. // Downsampling and prefiltering pass
  103. var rt1 = context.renderTextureFactory.Get(context.width / 2, context.height / 2, 0, colorFormat);
  104. material.SetTexture(Uniforms._CoCTex, rtCoC);
  105. Graphics.Blit(source, rt1, material, 2);
  106. // Bokeh simulation pass
  107. var rt2 = context.renderTextureFactory.Get(context.width / 2, context.height / 2, 0, colorFormat);
  108. Graphics.Blit(rt1, rt2, material, 3 + (int)settings.kernelSize);
  109. // Postfilter pass
  110. Graphics.Blit(rt2, rt1, material, 7);
  111. // Give the results to the uber shader.
  112. uberMaterial.SetVector(Uniforms._DepthOfFieldParams, new Vector3(s1, coeff, maxCoC));
  113. if (context.profile.debugViews.IsModeActive(DebugMode.FocusPlane))
  114. {
  115. uberMaterial.EnableKeyword("DEPTH_OF_FIELD_COC_VIEW");
  116. context.Interrupt();
  117. }
  118. else
  119. {
  120. uberMaterial.SetTexture(Uniforms._DepthOfFieldTex, rt1);
  121. uberMaterial.SetTexture(Uniforms._DepthOfFieldCoCTex, rtCoC);
  122. uberMaterial.EnableKeyword("DEPTH_OF_FIELD");
  123. }
  124. context.renderTextureFactory.Release(rt2);
  125. }
  126. public override void OnDisable()
  127. {
  128. if (m_CoCHistory != null)
  129. RenderTexture.ReleaseTemporary(m_CoCHistory);
  130. m_CoCHistory = null;
  131. }
  132. }
  133. }