FogComponent.cs 2.6 KB

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