DepthOfFieldComponent.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. namespace UnityEngine.PostProcessing
  3. {
  4. public sealed class DepthOfFieldComponent : PostProcessingComponentRenderTexture<DepthOfFieldModel>
  5. {
  6. public override bool active
  7. {
  8. get
  9. {
  10. return base.model.enabled && !this.context.interrupted;
  11. }
  12. }
  13. public override DepthTextureMode GetCameraFlags()
  14. {
  15. return DepthTextureMode.Depth;
  16. }
  17. private float CalculateFocalLength()
  18. {
  19. DepthOfFieldModel.Settings settings = base.model.settings;
  20. if (!settings.useCameraFov)
  21. {
  22. return settings.focalLength / 1000f;
  23. }
  24. float num = this.context.camera.fieldOfView * 0.017453292f;
  25. return 0.012f / Mathf.Tan(0.5f * num);
  26. }
  27. private float CalculateMaxCoCRadius(int screenHeight)
  28. {
  29. float num = (float)base.model.settings.kernelSize * 4f + 6f;
  30. return Mathf.Min(0.05f, num / (float)screenHeight);
  31. }
  32. private bool CheckHistory(int width, int height)
  33. {
  34. return this.m_CoCHistory != null && this.m_CoCHistory.IsCreated() && this.m_CoCHistory.width == width && this.m_CoCHistory.height == height;
  35. }
  36. private RenderTextureFormat SelectFormat(RenderTextureFormat primary, RenderTextureFormat secondary)
  37. {
  38. if (SystemInfo.SupportsRenderTextureFormat(primary))
  39. {
  40. return primary;
  41. }
  42. if (SystemInfo.SupportsRenderTextureFormat(secondary))
  43. {
  44. return secondary;
  45. }
  46. return RenderTextureFormat.Default;
  47. }
  48. public void Prepare(RenderTexture source, Material uberMaterial, bool antialiasCoC, Vector2 taaJitter, float taaBlending)
  49. {
  50. DepthOfFieldModel.Settings settings = base.model.settings;
  51. RenderTextureFormat format = RenderTextureFormat.DefaultHDR;
  52. RenderTextureFormat format2 = this.SelectFormat(RenderTextureFormat.R8, RenderTextureFormat.RHalf);
  53. float num = this.CalculateFocalLength();
  54. float num2 = Mathf.Max(settings.focusDistance, num);
  55. float num3 = (float)source.width / (float)source.height;
  56. float num4 = num * num / (settings.aperture * (num2 - num) * 0.024f * 2f);
  57. float num5 = this.CalculateMaxCoCRadius(source.height);
  58. Material material = this.context.materialFactory.Get("Hidden/Post FX/Depth Of Field");
  59. material.SetFloat(DepthOfFieldComponent.Uniforms._Distance, num2);
  60. material.SetFloat(DepthOfFieldComponent.Uniforms._LensCoeff, num4);
  61. material.SetFloat(DepthOfFieldComponent.Uniforms._MaxCoC, num5);
  62. material.SetFloat(DepthOfFieldComponent.Uniforms._RcpMaxCoC, 1f / num5);
  63. material.SetFloat(DepthOfFieldComponent.Uniforms._RcpAspect, 1f / num3);
  64. RenderTexture renderTexture = this.context.renderTextureFactory.Get(this.context.width, this.context.height, 0, format2, RenderTextureReadWrite.Linear, FilterMode.Bilinear, TextureWrapMode.Clamp, "FactoryTempTexture");
  65. Graphics.Blit(null, renderTexture, material, 0);
  66. if (antialiasCoC)
  67. {
  68. material.SetTexture(DepthOfFieldComponent.Uniforms._CoCTex, renderTexture);
  69. float z = (!this.CheckHistory(this.context.width, this.context.height)) ? 0f : taaBlending;
  70. material.SetVector(DepthOfFieldComponent.Uniforms._TaaParams, new Vector3(taaJitter.x, taaJitter.y, z));
  71. RenderTexture temporary = RenderTexture.GetTemporary(this.context.width, this.context.height, 0, format2);
  72. Graphics.Blit(this.m_CoCHistory, temporary, material, 1);
  73. this.context.renderTextureFactory.Release(renderTexture);
  74. if (this.m_CoCHistory != null)
  75. {
  76. RenderTexture.ReleaseTemporary(this.m_CoCHistory);
  77. }
  78. renderTexture = (this.m_CoCHistory = temporary);
  79. }
  80. RenderTexture renderTexture2 = this.context.renderTextureFactory.Get(this.context.width / 2, this.context.height / 2, 0, format, RenderTextureReadWrite.Default, FilterMode.Bilinear, TextureWrapMode.Clamp, "FactoryTempTexture");
  81. material.SetTexture(DepthOfFieldComponent.Uniforms._CoCTex, renderTexture);
  82. Graphics.Blit(source, renderTexture2, material, 2);
  83. RenderTexture renderTexture3 = this.context.renderTextureFactory.Get(this.context.width / 2, this.context.height / 2, 0, format, RenderTextureReadWrite.Default, FilterMode.Bilinear, TextureWrapMode.Clamp, "FactoryTempTexture");
  84. Graphics.Blit(renderTexture2, renderTexture3, material, (int)(3 + settings.kernelSize));
  85. Graphics.Blit(renderTexture3, renderTexture2, material, 7);
  86. uberMaterial.SetVector(DepthOfFieldComponent.Uniforms._DepthOfFieldParams, new Vector3(num2, num4, num5));
  87. if (this.context.profile.debugViews.IsModeActive(BuiltinDebugViewsModel.Mode.FocusPlane))
  88. {
  89. uberMaterial.EnableKeyword("DEPTH_OF_FIELD_COC_VIEW");
  90. this.context.Interrupt();
  91. }
  92. else
  93. {
  94. uberMaterial.SetTexture(DepthOfFieldComponent.Uniforms._DepthOfFieldTex, renderTexture2);
  95. uberMaterial.SetTexture(DepthOfFieldComponent.Uniforms._DepthOfFieldCoCTex, renderTexture);
  96. uberMaterial.EnableKeyword("DEPTH_OF_FIELD");
  97. }
  98. this.context.renderTextureFactory.Release(renderTexture3);
  99. }
  100. public override void OnDisable()
  101. {
  102. if (this.m_CoCHistory != null)
  103. {
  104. RenderTexture.ReleaseTemporary(this.m_CoCHistory);
  105. }
  106. this.m_CoCHistory = null;
  107. }
  108. private const string k_ShaderString = "Hidden/Post FX/Depth Of Field";
  109. private RenderTexture m_CoCHistory;
  110. private const float k_FilmHeight = 0.024f;
  111. private static class Uniforms
  112. {
  113. internal static readonly int _DepthOfFieldTex = Shader.PropertyToID("_DepthOfFieldTex");
  114. internal static readonly int _DepthOfFieldCoCTex = Shader.PropertyToID("_DepthOfFieldCoCTex");
  115. internal static readonly int _Distance = Shader.PropertyToID("_Distance");
  116. internal static readonly int _LensCoeff = Shader.PropertyToID("_LensCoeff");
  117. internal static readonly int _MaxCoC = Shader.PropertyToID("_MaxCoC");
  118. internal static readonly int _RcpMaxCoC = Shader.PropertyToID("_RcpMaxCoC");
  119. internal static readonly int _RcpAspect = Shader.PropertyToID("_RcpAspect");
  120. internal static readonly int _MainTex = Shader.PropertyToID("_MainTex");
  121. internal static readonly int _CoCTex = Shader.PropertyToID("_CoCTex");
  122. internal static readonly int _TaaParams = Shader.PropertyToID("_TaaParams");
  123. internal static readonly int _DepthOfFieldParams = Shader.PropertyToID("_DepthOfFieldParams");
  124. }
  125. }
  126. }