AmbientOcclusionComponent.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using UnityEngine.Rendering;
  3. namespace UnityEngine.PostProcessing
  4. {
  5. public sealed class AmbientOcclusionComponent : PostProcessingComponentCommandBuffer<AmbientOcclusionModel>
  6. {
  7. private AmbientOcclusionComponent.OcclusionSource occlusionSource
  8. {
  9. get
  10. {
  11. if (this.context.isGBufferAvailable && !base.model.settings.forceForwardCompatibility)
  12. {
  13. return AmbientOcclusionComponent.OcclusionSource.GBuffer;
  14. }
  15. if (base.model.settings.highPrecision && (!this.context.isGBufferAvailable || base.model.settings.forceForwardCompatibility))
  16. {
  17. return AmbientOcclusionComponent.OcclusionSource.DepthTexture;
  18. }
  19. return AmbientOcclusionComponent.OcclusionSource.DepthNormalsTexture;
  20. }
  21. }
  22. private bool ambientOnlySupported
  23. {
  24. get
  25. {
  26. return this.context.isHdr && base.model.settings.ambientOnly && this.context.isGBufferAvailable && !base.model.settings.forceForwardCompatibility;
  27. }
  28. }
  29. public override bool active
  30. {
  31. get
  32. {
  33. return base.model.enabled && base.model.settings.intensity > 0f && !this.context.interrupted;
  34. }
  35. }
  36. public override DepthTextureMode GetCameraFlags()
  37. {
  38. DepthTextureMode depthTextureMode = DepthTextureMode.None;
  39. if (this.occlusionSource == AmbientOcclusionComponent.OcclusionSource.DepthTexture)
  40. {
  41. depthTextureMode |= DepthTextureMode.Depth;
  42. }
  43. if (this.occlusionSource != AmbientOcclusionComponent.OcclusionSource.GBuffer)
  44. {
  45. depthTextureMode |= DepthTextureMode.DepthNormals;
  46. }
  47. return depthTextureMode;
  48. }
  49. public override string GetName()
  50. {
  51. return "Ambient Occlusion";
  52. }
  53. public override CameraEvent GetCameraEvent()
  54. {
  55. return (!this.ambientOnlySupported || this.context.profile.debugViews.IsModeActive(BuiltinDebugViewsModel.Mode.AmbientOcclusion)) ? CameraEvent.BeforeImageEffectsOpaque : CameraEvent.BeforeReflections;
  56. }
  57. public override void PopulateCommandBuffer(CommandBuffer cb)
  58. {
  59. AmbientOcclusionModel.Settings settings = base.model.settings;
  60. Material mat = this.context.materialFactory.Get("Hidden/Post FX/Blit");
  61. Material material = this.context.materialFactory.Get("Hidden/Post FX/Ambient Occlusion");
  62. material.shaderKeywords = null;
  63. material.SetFloat(AmbientOcclusionComponent.Uniforms._Intensity, settings.intensity);
  64. material.SetFloat(AmbientOcclusionComponent.Uniforms._Radius, settings.radius);
  65. material.SetFloat(AmbientOcclusionComponent.Uniforms._Downsample, (!settings.downsampling) ? 1f : 0.5f);
  66. material.SetInt(AmbientOcclusionComponent.Uniforms._SampleCount, (int)settings.sampleCount);
  67. if (!this.context.isGBufferAvailable && RenderSettings.fog)
  68. {
  69. material.SetVector(AmbientOcclusionComponent.Uniforms._FogParams, new Vector3(RenderSettings.fogDensity, RenderSettings.fogStartDistance, RenderSettings.fogEndDistance));
  70. FogMode fogMode = RenderSettings.fogMode;
  71. if (fogMode != FogMode.Linear)
  72. {
  73. if (fogMode != FogMode.Exponential)
  74. {
  75. if (fogMode == FogMode.ExponentialSquared)
  76. {
  77. material.EnableKeyword("FOG_EXP2");
  78. }
  79. }
  80. else
  81. {
  82. material.EnableKeyword("FOG_EXP");
  83. }
  84. }
  85. else
  86. {
  87. material.EnableKeyword("FOG_LINEAR");
  88. }
  89. }
  90. else
  91. {
  92. material.EnableKeyword("FOG_OFF");
  93. }
  94. int width = this.context.width;
  95. int height = this.context.height;
  96. int num = (!settings.downsampling) ? 1 : 2;
  97. int nameID = AmbientOcclusionComponent.Uniforms._OcclusionTexture1;
  98. cb.GetTemporaryRT(nameID, width / num, height / num, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
  99. cb.Blit(null, nameID, material, (int)this.occlusionSource);
  100. int occlusionTexture = AmbientOcclusionComponent.Uniforms._OcclusionTexture2;
  101. cb.GetTemporaryRT(occlusionTexture, width, height, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
  102. cb.SetGlobalTexture(AmbientOcclusionComponent.Uniforms._MainTex, nameID);
  103. cb.Blit(nameID, occlusionTexture, material, (this.occlusionSource != AmbientOcclusionComponent.OcclusionSource.GBuffer) ? 3 : 4);
  104. cb.ReleaseTemporaryRT(nameID);
  105. nameID = AmbientOcclusionComponent.Uniforms._OcclusionTexture;
  106. cb.GetTemporaryRT(nameID, width, height, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
  107. cb.SetGlobalTexture(AmbientOcclusionComponent.Uniforms._MainTex, occlusionTexture);
  108. cb.Blit(occlusionTexture, nameID, material, 5);
  109. cb.ReleaseTemporaryRT(occlusionTexture);
  110. if (this.context.profile.debugViews.IsModeActive(BuiltinDebugViewsModel.Mode.AmbientOcclusion))
  111. {
  112. cb.SetGlobalTexture(AmbientOcclusionComponent.Uniforms._MainTex, nameID);
  113. cb.Blit(nameID, BuiltinRenderTextureType.CameraTarget, material, 8);
  114. this.context.Interrupt();
  115. }
  116. else if (this.ambientOnlySupported)
  117. {
  118. cb.SetRenderTarget(this.m_MRT, BuiltinRenderTextureType.CameraTarget);
  119. cb.DrawMesh(GraphicsUtils.quad, Matrix4x4.identity, material, 0, 7);
  120. }
  121. else
  122. {
  123. RenderTextureFormat format = (!this.context.isHdr) ? RenderTextureFormat.Default : RenderTextureFormat.DefaultHDR;
  124. int tempRT = AmbientOcclusionComponent.Uniforms._TempRT;
  125. cb.GetTemporaryRT(tempRT, this.context.width, this.context.height, 0, FilterMode.Bilinear, format);
  126. cb.Blit(BuiltinRenderTextureType.CameraTarget, tempRT, mat, 0);
  127. cb.SetGlobalTexture(AmbientOcclusionComponent.Uniforms._MainTex, tempRT);
  128. cb.Blit(tempRT, BuiltinRenderTextureType.CameraTarget, material, 6);
  129. cb.ReleaseTemporaryRT(tempRT);
  130. }
  131. cb.ReleaseTemporaryRT(nameID);
  132. }
  133. private const string k_BlitShaderString = "Hidden/Post FX/Blit";
  134. private const string k_ShaderString = "Hidden/Post FX/Ambient Occlusion";
  135. private readonly RenderTargetIdentifier[] m_MRT = new RenderTargetIdentifier[]
  136. {
  137. BuiltinRenderTextureType.GBuffer0,
  138. BuiltinRenderTextureType.CameraTarget
  139. };
  140. private static class Uniforms
  141. {
  142. internal static readonly int _Intensity = Shader.PropertyToID("_Intensity");
  143. internal static readonly int _Radius = Shader.PropertyToID("_Radius");
  144. internal static readonly int _FogParams = Shader.PropertyToID("_FogParams");
  145. internal static readonly int _Downsample = Shader.PropertyToID("_Downsample");
  146. internal static readonly int _SampleCount = Shader.PropertyToID("_SampleCount");
  147. internal static readonly int _OcclusionTexture1 = Shader.PropertyToID("_OcclusionTexture1");
  148. internal static readonly int _OcclusionTexture2 = Shader.PropertyToID("_OcclusionTexture2");
  149. internal static readonly int _OcclusionTexture = Shader.PropertyToID("_OcclusionTexture");
  150. internal static readonly int _MainTex = Shader.PropertyToID("_MainTex");
  151. internal static readonly int _TempRT = Shader.PropertyToID("_TempRT");
  152. }
  153. private enum OcclusionSource
  154. {
  155. DepthTexture,
  156. DepthNormalsTexture,
  157. GBuffer
  158. }
  159. }
  160. }