Grain.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. namespace UnityEngine.Rendering.PostProcessing
  3. {
  4. /// <summary>
  5. /// This class holds settings for the Grain effect.
  6. /// </summary>
  7. [Serializable]
  8. [PostProcess(typeof(GrainRenderer), "Unity/Grain")]
  9. public sealed class Grain : PostProcessEffectSettings
  10. {
  11. /// <summary>
  12. /// Set to <c>true</c> to render colored grain, <c>false</c> for grayscale grain.
  13. /// </summary>
  14. [Tooltip("Enable the use of colored grain.")]
  15. public BoolParameter colored = new BoolParameter { value = true };
  16. /// <summary>
  17. /// The strength (or visibility) of the Grain effect on screen. Higher values mean more visible grain.
  18. /// </summary>
  19. [Range(0f, 1f), Tooltip("Grain strength. Higher values mean more visible grain.")]
  20. public FloatParameter intensity = new FloatParameter { value = 0f };
  21. /// <summary>
  22. /// The size of grain particle on screen.
  23. /// </summary>
  24. [Range(0.3f, 3f), Tooltip("Grain particle size.")]
  25. public FloatParameter size = new FloatParameter { value = 1f };
  26. /// <summary>
  27. /// Controls the noisiness response curve based on scene luminance. Lower values mean less noise in dark areas.
  28. /// </summary>
  29. [Range(0f, 1f), DisplayName("Luminance Contribution"), Tooltip("Controls the noise response curve based on scene luminance. Lower values mean less noise in dark areas.")]
  30. public FloatParameter lumContrib = new FloatParameter { value = 0.8f };
  31. /// <inheritdoc />
  32. public override bool IsEnabledAndSupported(PostProcessRenderContext context)
  33. {
  34. return enabled.value
  35. && intensity.value > 0f;
  36. }
  37. }
  38. #if POSTFX_DEBUG_STATIC_GRAIN
  39. #pragma warning disable 414
  40. #endif
  41. internal sealed class GrainRenderer : PostProcessEffectRenderer<Grain>
  42. {
  43. RenderTexture m_GrainLookupRT;
  44. const int k_SampleCount = 1024;
  45. int m_SampleIndex;
  46. public override void Render(PostProcessRenderContext context)
  47. {
  48. #if POSTFX_DEBUG_STATIC_GRAIN
  49. // Chosen by a fair dice roll
  50. float time = 0.4f;
  51. float rndOffsetX = 0f;
  52. float rndOffsetY = 0f;
  53. #else
  54. float time = Time.realtimeSinceStartup;
  55. float rndOffsetX = HaltonSeq.Get(m_SampleIndex & 1023, 2);
  56. float rndOffsetY = HaltonSeq.Get(m_SampleIndex & 1023, 3);
  57. if (++m_SampleIndex >= k_SampleCount)
  58. m_SampleIndex = 0;
  59. #endif
  60. // Generate the grain lut for the current frame first
  61. if (m_GrainLookupRT == null || !m_GrainLookupRT.IsCreated())
  62. {
  63. RuntimeUtilities.Destroy(m_GrainLookupRT);
  64. m_GrainLookupRT = new RenderTexture(128, 128, 0, GetLookupFormat())
  65. {
  66. filterMode = FilterMode.Bilinear,
  67. wrapMode = TextureWrapMode.Repeat,
  68. anisoLevel = 0,
  69. name = "Grain Lookup Texture"
  70. };
  71. m_GrainLookupRT.Create();
  72. }
  73. var sheet = context.propertySheets.Get(context.resources.shaders.grainBaker);
  74. sheet.properties.Clear();
  75. sheet.properties.SetFloat(ShaderIDs.Phase, time % 10f);
  76. sheet.properties.SetVector(ShaderIDs.GrainNoiseParameters, new Vector3(12.9898f, 78.233f, 43758.5453f));
  77. context.command.BeginSample("GrainLookup");
  78. context.command.BlitFullscreenTriangle(BuiltinRenderTextureType.None, m_GrainLookupRT, sheet, settings.colored.value ? 1 : 0);
  79. context.command.EndSample("GrainLookup");
  80. // Send everything to the uber shader
  81. var uberSheet = context.uberSheet;
  82. uberSheet.EnableKeyword("GRAIN");
  83. uberSheet.properties.SetTexture(ShaderIDs.GrainTex, m_GrainLookupRT);
  84. uberSheet.properties.SetVector(ShaderIDs.Grain_Params1, new Vector2(settings.lumContrib.value, settings.intensity.value * 20f));
  85. uberSheet.properties.SetVector(ShaderIDs.Grain_Params2, new Vector4((float)context.width / (float)m_GrainLookupRT.width / settings.size.value, (float)context.height / (float)m_GrainLookupRT.height / settings.size.value, rndOffsetX, rndOffsetY));
  86. }
  87. RenderTextureFormat GetLookupFormat()
  88. {
  89. if (RenderTextureFormat.ARGBHalf.IsSupported())
  90. return RenderTextureFormat.ARGBHalf;
  91. return RenderTextureFormat.ARGB32;
  92. }
  93. public override void Release()
  94. {
  95. RuntimeUtilities.Destroy(m_GrainLookupRT);
  96. m_GrainLookupRT = null;
  97. m_SampleIndex = 0;
  98. }
  99. }
  100. #if POSTFX_DEBUG_STATIC_GRAIN
  101. #pragma warning restore 414
  102. #endif
  103. }