FogComponent.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using UnityEngine.Rendering;
  2. namespace UnityEngine.PostProcessing
  3. {
  4. public sealed class FogComponent : PostProcessingComponentCommandBuffer<FogModel>
  5. {
  6. static class Uniforms
  7. {
  8. internal static readonly int _FogColor = Shader.PropertyToID("_FogColor");
  9. internal static readonly int _Density = Shader.PropertyToID("_Density");
  10. internal static readonly int _Start = Shader.PropertyToID("_Start");
  11. internal static readonly int _End = Shader.PropertyToID("_End");
  12. internal static readonly int _TempRT = Shader.PropertyToID("_TempRT");
  13. }
  14. const string k_ShaderString = "Hidden/Post FX/Fog";
  15. public override bool active
  16. {
  17. get
  18. {
  19. return model.enabled
  20. && context.isGBufferAvailable // In forward fog is already done at shader level
  21. && RenderSettings.fog
  22. && !context.interrupted;
  23. }
  24. }
  25. public override string GetName()
  26. {
  27. return "Fog";
  28. }
  29. public override DepthTextureMode GetCameraFlags()
  30. {
  31. return DepthTextureMode.Depth;
  32. }
  33. public override CameraEvent GetCameraEvent()
  34. {
  35. return CameraEvent.AfterImageEffectsOpaque;
  36. }
  37. public override void PopulateCommandBuffer(CommandBuffer cb)
  38. {
  39. var settings = model.settings;
  40. var material = context.materialFactory.Get(k_ShaderString);
  41. material.shaderKeywords = null;
  42. var fogColor = GraphicsUtils.isLinearColorSpace ? RenderSettings.fogColor.linear : RenderSettings.fogColor;
  43. material.SetColor(Uniforms._FogColor, fogColor);
  44. material.SetFloat(Uniforms._Density, RenderSettings.fogDensity);
  45. material.SetFloat(Uniforms._Start, RenderSettings.fogStartDistance);
  46. material.SetFloat(Uniforms._End, RenderSettings.fogEndDistance);
  47. switch (RenderSettings.fogMode)
  48. {
  49. case FogMode.Linear:
  50. material.EnableKeyword("FOG_LINEAR");
  51. break;
  52. case FogMode.Exponential:
  53. material.EnableKeyword("FOG_EXP");
  54. break;
  55. case FogMode.ExponentialSquared:
  56. material.EnableKeyword("FOG_EXP2");
  57. break;
  58. }
  59. var fbFormat = context.isHdr
  60. ? RenderTextureFormat.DefaultHDR
  61. : RenderTextureFormat.Default;
  62. cb.GetTemporaryRT(Uniforms._TempRT, context.width, context.height, 24, FilterMode.Bilinear, fbFormat);
  63. cb.Blit(BuiltinRenderTextureType.CameraTarget, Uniforms._TempRT);
  64. cb.Blit(Uniforms._TempRT, BuiltinRenderTextureType.CameraTarget, material, settings.excludeSkybox ? 1 : 0);
  65. cb.ReleaseTemporaryRT(Uniforms._TempRT);
  66. }
  67. }
  68. }