using System; using UnityEngine.Rendering; namespace UnityEngine.PostProcessing { public sealed class ScreenSpaceReflectionComponent : PostProcessingComponentCommandBuffer { public override DepthTextureMode GetCameraFlags() { return DepthTextureMode.Depth; } public override bool active { get { return base.model.enabled && this.context.isGBufferAvailable && !this.context.interrupted; } } public override void OnEnable() { this.m_ReflectionTextures[0] = Shader.PropertyToID("_ReflectionTexture0"); this.m_ReflectionTextures[1] = Shader.PropertyToID("_ReflectionTexture1"); this.m_ReflectionTextures[2] = Shader.PropertyToID("_ReflectionTexture2"); this.m_ReflectionTextures[3] = Shader.PropertyToID("_ReflectionTexture3"); this.m_ReflectionTextures[4] = Shader.PropertyToID("_ReflectionTexture4"); } public override string GetName() { return "Screen Space Reflection"; } public override CameraEvent GetCameraEvent() { return CameraEvent.AfterFinalPass; } public override void PopulateCommandBuffer(CommandBuffer cb) { ScreenSpaceReflectionModel.Settings settings = base.model.settings; Camera camera = this.context.camera; int num = (settings.reflection.reflectionQuality != ScreenSpaceReflectionModel.SSRResolution.High) ? 2 : 1; int num2 = this.context.width / num; int num3 = this.context.height / num; float num4 = (float)this.context.width; float num5 = (float)this.context.height; float num6 = num4 / 2f; float num7 = num5 / 2f; Material material = this.context.materialFactory.Get("Hidden/Post FX/Screen Space Reflection"); material.SetInt(ScreenSpaceReflectionComponent.Uniforms._RayStepSize, settings.reflection.stepSize); material.SetInt(ScreenSpaceReflectionComponent.Uniforms._AdditiveReflection, (settings.reflection.blendType != ScreenSpaceReflectionModel.SSRReflectionBlendType.Additive) ? 0 : 1); material.SetInt(ScreenSpaceReflectionComponent.Uniforms._BilateralUpsampling, (!this.k_BilateralUpsample) ? 0 : 1); material.SetInt(ScreenSpaceReflectionComponent.Uniforms._TreatBackfaceHitAsMiss, (!this.k_TreatBackfaceHitAsMiss) ? 0 : 1); material.SetInt(ScreenSpaceReflectionComponent.Uniforms._AllowBackwardsRays, (!settings.reflection.reflectBackfaces) ? 0 : 1); material.SetInt(ScreenSpaceReflectionComponent.Uniforms._TraceBehindObjects, (!this.k_TraceBehindObjects) ? 0 : 1); material.SetInt(ScreenSpaceReflectionComponent.Uniforms._MaxSteps, settings.reflection.iterationCount); material.SetInt(ScreenSpaceReflectionComponent.Uniforms._FullResolutionFiltering, 0); material.SetInt(ScreenSpaceReflectionComponent.Uniforms._HalfResolution, (settings.reflection.reflectionQuality == ScreenSpaceReflectionModel.SSRResolution.High) ? 0 : 1); material.SetInt(ScreenSpaceReflectionComponent.Uniforms._HighlightSuppression, (!this.k_HighlightSuppression) ? 0 : 1); float value = num4 / (-2f * Mathf.Tan(camera.fieldOfView / 180f * 3.1415927f * 0.5f)); material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._PixelsPerMeterAtOneMeter, value); material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._ScreenEdgeFading, settings.screenEdgeMask.intensity); material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._ReflectionBlur, settings.reflection.reflectionBlur); material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._MaxRayTraceDistance, settings.reflection.maxDistance); material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._FadeDistance, settings.intensity.fadeDistance); material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._LayerThickness, settings.reflection.widthModifier); material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._SSRMultiplier, settings.intensity.reflectionMultiplier); material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._FresnelFade, settings.intensity.fresnelFade); material.SetFloat(ScreenSpaceReflectionComponent.Uniforms._FresnelFadePower, settings.intensity.fresnelFadePower); Matrix4x4 projectionMatrix = camera.projectionMatrix; Vector4 value2 = new Vector4(-2f / (num4 * projectionMatrix[0]), -2f / (num5 * projectionMatrix[5]), (1f - projectionMatrix[2]) / projectionMatrix[0], (1f + projectionMatrix[6]) / projectionMatrix[5]); Vector3 v = (!float.IsPositiveInfinity(camera.farClipPlane)) ? new Vector3(camera.nearClipPlane * camera.farClipPlane, camera.nearClipPlane - camera.farClipPlane, camera.farClipPlane) : new Vector3(camera.nearClipPlane, -1f, 1f); material.SetVector(ScreenSpaceReflectionComponent.Uniforms._ReflectionBufferSize, new Vector2((float)num2, (float)num3)); material.SetVector(ScreenSpaceReflectionComponent.Uniforms._ScreenSize, new Vector2(num4, num5)); material.SetVector(ScreenSpaceReflectionComponent.Uniforms._InvScreenSize, new Vector2(1f / num4, 1f / num5)); material.SetVector(ScreenSpaceReflectionComponent.Uniforms._ProjInfo, value2); material.SetVector(ScreenSpaceReflectionComponent.Uniforms._CameraClipInfo, v); Matrix4x4 lhs = default(Matrix4x4); lhs.SetRow(0, new Vector4(num6, 0f, 0f, num6)); lhs.SetRow(1, new Vector4(0f, num7, 0f, num7)); lhs.SetRow(2, new Vector4(0f, 0f, 1f, 0f)); lhs.SetRow(3, new Vector4(0f, 0f, 0f, 1f)); Matrix4x4 value3 = lhs * projectionMatrix; material.SetMatrix(ScreenSpaceReflectionComponent.Uniforms._ProjectToPixelMatrix, value3); material.SetMatrix(ScreenSpaceReflectionComponent.Uniforms._WorldToCameraMatrix, camera.worldToCameraMatrix); material.SetMatrix(ScreenSpaceReflectionComponent.Uniforms._CameraToWorldMatrix, camera.worldToCameraMatrix.inverse); RenderTextureFormat format = (!this.context.isHdr) ? RenderTextureFormat.ARGB32 : RenderTextureFormat.ARGBHalf; int normalAndRoughnessTexture = ScreenSpaceReflectionComponent.Uniforms._NormalAndRoughnessTexture; int hitPointTexture = ScreenSpaceReflectionComponent.Uniforms._HitPointTexture; int blurTexture = ScreenSpaceReflectionComponent.Uniforms._BlurTexture; int filteredReflections = ScreenSpaceReflectionComponent.Uniforms._FilteredReflections; int finalReflectionTexture = ScreenSpaceReflectionComponent.Uniforms._FinalReflectionTexture; int tempTexture = ScreenSpaceReflectionComponent.Uniforms._TempTexture; cb.GetTemporaryRT(normalAndRoughnessTexture, -1, -1, 0, FilterMode.Point, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); cb.GetTemporaryRT(hitPointTexture, num2, num3, 0, FilterMode.Bilinear, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear); for (int i = 0; i < 5; i++) { cb.GetTemporaryRT(this.m_ReflectionTextures[i], num2 >> i, num3 >> i, 0, FilterMode.Bilinear, format); } cb.GetTemporaryRT(filteredReflections, num2, num3, 0, (!this.k_BilateralUpsample) ? FilterMode.Bilinear : FilterMode.Point, format); cb.GetTemporaryRT(finalReflectionTexture, num2, num3, 0, FilterMode.Point, format); cb.Blit(BuiltinRenderTextureType.CameraTarget, normalAndRoughnessTexture, material, 6); cb.Blit(BuiltinRenderTextureType.CameraTarget, hitPointTexture, material, 0); cb.Blit(BuiltinRenderTextureType.CameraTarget, filteredReflections, material, 5); cb.Blit(filteredReflections, this.m_ReflectionTextures[0], material, 8); for (int j = 1; j < 5; j++) { int nameID = this.m_ReflectionTextures[j - 1]; int num8 = j; cb.GetTemporaryRT(blurTexture, num2 >> num8, num3 >> num8, 0, FilterMode.Bilinear, format); cb.SetGlobalVector(ScreenSpaceReflectionComponent.Uniforms._Axis, new Vector4(1f, 0f, 0f, 0f)); cb.SetGlobalFloat(ScreenSpaceReflectionComponent.Uniforms._CurrentMipLevel, (float)j - 1f); cb.Blit(nameID, blurTexture, material, 2); cb.SetGlobalVector(ScreenSpaceReflectionComponent.Uniforms._Axis, new Vector4(0f, 1f, 0f, 0f)); nameID = this.m_ReflectionTextures[j]; cb.Blit(blurTexture, nameID, material, 2); cb.ReleaseTemporaryRT(blurTexture); } cb.Blit(this.m_ReflectionTextures[0], finalReflectionTexture, material, 3); cb.GetTemporaryRT(tempTexture, camera.pixelWidth, camera.pixelHeight, 0, FilterMode.Bilinear, format); cb.Blit(BuiltinRenderTextureType.CameraTarget, tempTexture, material, 1); cb.Blit(tempTexture, BuiltinRenderTextureType.CameraTarget); cb.ReleaseTemporaryRT(tempTexture); } private bool k_HighlightSuppression; private bool k_TraceBehindObjects = true; private bool k_TreatBackfaceHitAsMiss; private bool k_BilateralUpsample = true; private readonly int[] m_ReflectionTextures = new int[5]; private static class Uniforms { internal static readonly int _RayStepSize = Shader.PropertyToID("_RayStepSize"); internal static readonly int _AdditiveReflection = Shader.PropertyToID("_AdditiveReflection"); internal static readonly int _BilateralUpsampling = Shader.PropertyToID("_BilateralUpsampling"); internal static readonly int _TreatBackfaceHitAsMiss = Shader.PropertyToID("_TreatBackfaceHitAsMiss"); internal static readonly int _AllowBackwardsRays = Shader.PropertyToID("_AllowBackwardsRays"); internal static readonly int _TraceBehindObjects = Shader.PropertyToID("_TraceBehindObjects"); internal static readonly int _MaxSteps = Shader.PropertyToID("_MaxSteps"); internal static readonly int _FullResolutionFiltering = Shader.PropertyToID("_FullResolutionFiltering"); internal static readonly int _HalfResolution = Shader.PropertyToID("_HalfResolution"); internal static readonly int _HighlightSuppression = Shader.PropertyToID("_HighlightSuppression"); internal static readonly int _PixelsPerMeterAtOneMeter = Shader.PropertyToID("_PixelsPerMeterAtOneMeter"); internal static readonly int _ScreenEdgeFading = Shader.PropertyToID("_ScreenEdgeFading"); internal static readonly int _ReflectionBlur = Shader.PropertyToID("_ReflectionBlur"); internal static readonly int _MaxRayTraceDistance = Shader.PropertyToID("_MaxRayTraceDistance"); internal static readonly int _FadeDistance = Shader.PropertyToID("_FadeDistance"); internal static readonly int _LayerThickness = Shader.PropertyToID("_LayerThickness"); internal static readonly int _SSRMultiplier = Shader.PropertyToID("_SSRMultiplier"); internal static readonly int _FresnelFade = Shader.PropertyToID("_FresnelFade"); internal static readonly int _FresnelFadePower = Shader.PropertyToID("_FresnelFadePower"); internal static readonly int _ReflectionBufferSize = Shader.PropertyToID("_ReflectionBufferSize"); internal static readonly int _ScreenSize = Shader.PropertyToID("_ScreenSize"); internal static readonly int _InvScreenSize = Shader.PropertyToID("_InvScreenSize"); internal static readonly int _ProjInfo = Shader.PropertyToID("_ProjInfo"); internal static readonly int _CameraClipInfo = Shader.PropertyToID("_CameraClipInfo"); internal static readonly int _ProjectToPixelMatrix = Shader.PropertyToID("_ProjectToPixelMatrix"); internal static readonly int _WorldToCameraMatrix = Shader.PropertyToID("_WorldToCameraMatrix"); internal static readonly int _CameraToWorldMatrix = Shader.PropertyToID("_CameraToWorldMatrix"); internal static readonly int _Axis = Shader.PropertyToID("_Axis"); internal static readonly int _CurrentMipLevel = Shader.PropertyToID("_CurrentMipLevel"); internal static readonly int _NormalAndRoughnessTexture = Shader.PropertyToID("_NormalAndRoughnessTexture"); internal static readonly int _HitPointTexture = Shader.PropertyToID("_HitPointTexture"); internal static readonly int _BlurTexture = Shader.PropertyToID("_BlurTexture"); internal static readonly int _FilteredReflections = Shader.PropertyToID("_FilteredReflections"); internal static readonly int _FinalReflectionTexture = Shader.PropertyToID("_FinalReflectionTexture"); internal static readonly int _TempTexture = Shader.PropertyToID("_TempTexture"); } private enum PassIndex { RayTraceStep, CompositeFinal, Blur, CompositeSSR, MinMipGeneration, HitPointToReflections, BilateralKeyPack, BlitDepthAsCSZ, PoissonBlur } } }