ScreenSpaceReflectionComponent.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using UnityEngine.Rendering;
  3. namespace UnityEngine.PostProcessing
  4. {
  5. public sealed class ScreenSpaceReflectionComponent : PostProcessingComponentCommandBuffer<ScreenSpaceReflectionModel>
  6. {
  7. public override DepthTextureMode GetCameraFlags()
  8. {
  9. return DepthTextureMode.Depth;
  10. }
  11. public override bool active
  12. {
  13. get
  14. {
  15. return base.model.enabled && this.context.isGBufferAvailable && !this.context.interrupted;
  16. }
  17. }
  18. public override void OnEnable()
  19. {
  20. this.m_ReflectionTextures[0] = Shader.PropertyToID("_ReflectionTexture0");
  21. this.m_ReflectionTextures[1] = Shader.PropertyToID("_ReflectionTexture1");
  22. this.m_ReflectionTextures[2] = Shader.PropertyToID("_ReflectionTexture2");
  23. this.m_ReflectionTextures[3] = Shader.PropertyToID("_ReflectionTexture3");
  24. this.m_ReflectionTextures[4] = Shader.PropertyToID("_ReflectionTexture4");
  25. }
  26. public override string GetName()
  27. {
  28. return "Screen Space Reflection";
  29. }
  30. public override CameraEvent GetCameraEvent()
  31. {
  32. return CameraEvent.AfterFinalPass;
  33. }
  34. public override void PopulateCommandBuffer(CommandBuffer cb)
  35. {
  36. ScreenSpaceReflectionModel.Settings settings = base.model.settings;
  37. Camera camera = this.context.camera;
  38. int num = (settings.reflection.reflectionQuality != ScreenSpaceReflectionModel.SSRResolution.High) ? 2 : 1;
  39. int num2 = this.context.width / num;
  40. int num3 = this.context.height / num;
  41. float num4 = (float)this.context.width;
  42. float num5 = (float)this.context.height;
  43. float num6 = num4 / 2f;
  44. float num7 = num5 / 2f;
  45. Material material = this.context.materialFactory.Get("Hidden/Post FX/Screen Space Reflection");
  46. material.SetInt(ScreenSpaceReflectionComponent.Uniforms._RayStepSize, settings.reflection.stepSize);
  47. material.SetInt(ScreenSpaceReflectionComponent.Uniforms._AdditiveReflection, (settings.reflection.blendType != ScreenSpaceReflectionModel.SSRReflectionBlendType.Additive) ? 0 : 1);
  48. material.SetInt(ScreenSpaceReflectionComponent.Uniforms._BilateralUpsampling, (!this.k_BilateralUpsample) ? 0 : 1);
  49. material.SetInt(ScreenSpaceReflectionComponent.Uniforms._TreatBackfaceHitAsMiss, (!this.k_TreatBackfaceHitAsMiss) ? 0 : 1);
  50. material.SetInt(ScreenSpaceReflectionComponent.Uniforms._AllowBackwardsRays, (!settings.reflection.reflectBackfaces) ? 0 : 1);
  51. material.SetInt(ScreenSpaceReflectionComponent.Uniforms._TraceBehindObjects, (!this.k_TraceBehindObjects) ? 0 : 1);
  52. material.SetInt(ScreenSpaceReflectionComponent.Uniforms._MaxSteps, settings.reflection.iterationCount);
  53. material.SetInt(ScreenSpaceReflectionComponent.Uniforms._FullResolutionFiltering, 0);
  54. material.SetInt(ScreenSpaceReflectionComponent.Uniforms._HalfResolution, (settings.reflection.reflectionQuality == ScreenSpaceReflectionModel.SSRResolution.High) ? 0 : 1);
  55. material.SetInt(ScreenSpaceReflectionComponent.Uniforms._HighlightSuppression, (!this.k_HighlightSuppression) ? 0 : 1);
  56. float value = num4 / (-2f * Mathf.Tan(camera.fieldOfView / 180f * 3.14159274f * 0.5f));
  57. material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._PixelsPerMeterAtOneMeter, value);
  58. material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._ScreenEdgeFading, settings.screenEdgeMask.intensity);
  59. material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._ReflectionBlur, settings.reflection.reflectionBlur);
  60. material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._MaxRayTraceDistance, settings.reflection.maxDistance);
  61. material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._FadeDistance, settings.intensity.fadeDistance);
  62. material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._LayerThickness, settings.reflection.widthModifier);
  63. material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._SSRMultiplier, settings.intensity.reflectionMultiplier);
  64. material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._FresnelFade, settings.intensity.fresnelFade);
  65. material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._FresnelFadePower, settings.intensity.fresnelFadePower);
  66. Matrix4x4 projectionMatrix = camera.projectionMatrix;
  67. Vector4 value2 = new Vector4(-2f / (num4 * projectionMatrix[0]), -2f / (num5 * projectionMatrix[5]), (1f - projectionMatrix[2]) / projectionMatrix[0], (1f + projectionMatrix[6]) / projectionMatrix[5]);
  68. Vector3 v = (!float.IsPositiveInfinity(camera.farClipPlane)) ? new Vector3(camera.nearClipPlane * camera.farClipPlane, camera.nearClipPlane - camera.farClipPlane, camera.farClipPlane) : new Vector3(camera.nearClipPlane, -1f, 1f);
  69. material.SetVector(ScreenSpaceReflectionComponent.Uniforms._ReflectionBufferSize, new Vector2((float)num2, (float)num3));
  70. material.SetVector(ScreenSpaceReflectionComponent.Uniforms._ScreenSize, new Vector2(num4, num5));
  71. material.SetVector(ScreenSpaceReflectionComponent.Uniforms._InvScreenSize, new Vector2(1f / num4, 1f / num5));
  72. material.SetVector(ScreenSpaceReflectionComponent.Uniforms._ProjInfo, value2);
  73. material.SetVector(ScreenSpaceReflectionComponent.Uniforms._CameraClipInfo, v);
  74. Matrix4x4 lhs = default(Matrix4x4);
  75. lhs.SetRow(0, new Vector4(num6, 0f, 0f, num6));
  76. lhs.SetRow(1, new Vector4(0f, num7, 0f, num7));
  77. lhs.SetRow(2, new Vector4(0f, 0f, 1f, 0f));
  78. lhs.SetRow(3, new Vector4(0f, 0f, 0f, 1f));
  79. Matrix4x4 value3 = lhs * projectionMatrix;
  80. material.SetMatrix(ScreenSpaceReflectionComponent.Uniforms._ProjectToPixelMatrix, value3);
  81. material.SetMatrix(ScreenSpaceReflectionComponent.Uniforms._WorldToCameraMatrix, camera.worldToCameraMatrix);
  82. material.SetMatrix(ScreenSpaceReflectionComponent.Uniforms._CameraToWorldMatrix, camera.worldToCameraMatrix.inverse);
  83. RenderTextureFormat format = (!this.context.isHdr) ? RenderTextureFormat.ARGB32 : RenderTextureFormat.ARGBHalf;
  84. int normalAndRoughnessTexture = ScreenSpaceReflectionComponent.Uniforms._NormalAndRoughnessTexture;
  85. int hitPointTexture = ScreenSpaceReflectionComponent.Uniforms._HitPointTexture;
  86. int blurTexture = ScreenSpaceReflectionComponent.Uniforms._BlurTexture;
  87. int filteredReflections = ScreenSpaceReflectionComponent.Uniforms._FilteredReflections;
  88. int finalReflectionTexture = ScreenSpaceReflectionComponent.Uniforms._FinalReflectionTexture;
  89. int tempTexture = ScreenSpaceReflectionComponent.Uniforms._TempTexture;
  90. cb.GetTemporaryRT(normalAndRoughnessTexture, -1, -1, 0, FilterMode.Point, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
  91. cb.GetTemporaryRT(hitPointTexture, num2, num3, 0, FilterMode.Bilinear, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
  92. for (int i = 0; i < 5; i++)
  93. {
  94. cb.GetTemporaryRT(this.m_ReflectionTextures[i], num2 >> i, num3 >> i, 0, FilterMode.Bilinear, format);
  95. }
  96. cb.GetTemporaryRT(filteredReflections, num2, num3, 0, (!this.k_BilateralUpsample) ? FilterMode.Bilinear : FilterMode.Point, format);
  97. cb.GetTemporaryRT(finalReflectionTexture, num2, num3, 0, FilterMode.Point, format);
  98. cb.Blit(BuiltinRenderTextureType.CameraTarget, normalAndRoughnessTexture, material, 6);
  99. cb.Blit(BuiltinRenderTextureType.CameraTarget, hitPointTexture, material, 0);
  100. cb.Blit(BuiltinRenderTextureType.CameraTarget, filteredReflections, material, 5);
  101. cb.Blit(filteredReflections, this.m_ReflectionTextures[0], material, 8);
  102. for (int j = 1; j < 5; j++)
  103. {
  104. int nameID = this.m_ReflectionTextures[j - 1];
  105. int num8 = j;
  106. cb.GetTemporaryRT(blurTexture, num2 >> num8, num3 >> num8, 0, FilterMode.Bilinear, format);
  107. cb.SetGlobalVector(ScreenSpaceReflectionComponent.Uniforms._Axis, new Vector4(1f, 0f, 0f, 0f));
  108. cb.SetGlobalFloat(ScreenSpaceReflectionComponent.Uniforms._CurrentMipLevel, (float)j - 1f);
  109. cb.Blit(nameID, blurTexture, material, 2);
  110. cb.SetGlobalVector(ScreenSpaceReflectionComponent.Uniforms._Axis, new Vector4(0f, 1f, 0f, 0f));
  111. nameID = this.m_ReflectionTextures[j];
  112. cb.Blit(blurTexture, nameID, material, 2);
  113. cb.ReleaseTemporaryRT(blurTexture);
  114. }
  115. cb.Blit(this.m_ReflectionTextures[0], finalReflectionTexture, material, 3);
  116. cb.GetTemporaryRT(tempTexture, camera.pixelWidth, camera.pixelHeight, 0, FilterMode.Bilinear, format);
  117. cb.Blit(BuiltinRenderTextureType.CameraTarget, tempTexture, material, 1);
  118. cb.Blit(tempTexture, BuiltinRenderTextureType.CameraTarget);
  119. cb.ReleaseTemporaryRT(tempTexture);
  120. }
  121. private bool k_HighlightSuppression;
  122. private bool k_TraceBehindObjects = true;
  123. private bool k_TreatBackfaceHitAsMiss;
  124. private bool k_BilateralUpsample = true;
  125. private readonly int[] m_ReflectionTextures = new int[5];
  126. private static class Uniforms
  127. {
  128. internal static readonly int _RayStepSize = Shader.PropertyToID("_RayStepSize");
  129. internal static readonly int _AdditiveReflection = Shader.PropertyToID("_AdditiveReflection");
  130. internal static readonly int _BilateralUpsampling = Shader.PropertyToID("_BilateralUpsampling");
  131. internal static readonly int _TreatBackfaceHitAsMiss = Shader.PropertyToID("_TreatBackfaceHitAsMiss");
  132. internal static readonly int _AllowBackwardsRays = Shader.PropertyToID("_AllowBackwardsRays");
  133. internal static readonly int _TraceBehindObjects = Shader.PropertyToID("_TraceBehindObjects");
  134. internal static readonly int _MaxSteps = Shader.PropertyToID("_MaxSteps");
  135. internal static readonly int _FullResolutionFiltering = Shader.PropertyToID("_FullResolutionFiltering");
  136. internal static readonly int _HalfResolution = Shader.PropertyToID("_HalfResolution");
  137. internal static readonly int _HighlightSuppression = Shader.PropertyToID("_HighlightSuppression");
  138. internal static readonly int _PixelsPerMeterAtOneMeter = Shader.PropertyToID("_PixelsPerMeterAtOneMeter");
  139. internal static readonly int _ScreenEdgeFading = Shader.PropertyToID("_ScreenEdgeFading");
  140. internal static readonly int _ReflectionBlur = Shader.PropertyToID("_ReflectionBlur");
  141. internal static readonly int _MaxRayTraceDistance = Shader.PropertyToID("_MaxRayTraceDistance");
  142. internal static readonly int _FadeDistance = Shader.PropertyToID("_FadeDistance");
  143. internal static readonly int _LayerThickness = Shader.PropertyToID("_LayerThickness");
  144. internal static readonly int _SSRMultiplier = Shader.PropertyToID("_SSRMultiplier");
  145. internal static readonly int _FresnelFade = Shader.PropertyToID("_FresnelFade");
  146. internal static readonly int _FresnelFadePower = Shader.PropertyToID("_FresnelFadePower");
  147. internal static readonly int _ReflectionBufferSize = Shader.PropertyToID("_ReflectionBufferSize");
  148. internal static readonly int _ScreenSize = Shader.PropertyToID("_ScreenSize");
  149. internal static readonly int _InvScreenSize = Shader.PropertyToID("_InvScreenSize");
  150. internal static readonly int _ProjInfo = Shader.PropertyToID("_ProjInfo");
  151. internal static readonly int _CameraClipInfo = Shader.PropertyToID("_CameraClipInfo");
  152. internal static readonly int _ProjectToPixelMatrix = Shader.PropertyToID("_ProjectToPixelMatrix");
  153. internal static readonly int _WorldToCameraMatrix = Shader.PropertyToID("_WorldToCameraMatrix");
  154. internal static readonly int _CameraToWorldMatrix = Shader.PropertyToID("_CameraToWorldMatrix");
  155. internal static readonly int _Axis = Shader.PropertyToID("_Axis");
  156. internal static readonly int _CurrentMipLevel = Shader.PropertyToID("_CurrentMipLevel");
  157. internal static readonly int _NormalAndRoughnessTexture = Shader.PropertyToID("_NormalAndRoughnessTexture");
  158. internal static readonly int _HitPointTexture = Shader.PropertyToID("_HitPointTexture");
  159. internal static readonly int _BlurTexture = Shader.PropertyToID("_BlurTexture");
  160. internal static readonly int _FilteredReflections = Shader.PropertyToID("_FilteredReflections");
  161. internal static readonly int _FinalReflectionTexture = Shader.PropertyToID("_FinalReflectionTexture");
  162. internal static readonly int _TempTexture = Shader.PropertyToID("_TempTexture");
  163. }
  164. private enum PassIndex
  165. {
  166. RayTraceStep,
  167. CompositeFinal,
  168. Blur,
  169. CompositeSSR,
  170. MinMipGeneration,
  171. HitPointToReflections,
  172. BilateralKeyPack,
  173. BlitDepthAsCSZ,
  174. PoissonBlur
  175. }
  176. }
  177. }