暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PostProcessUtils.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using UnityEngine.Experimental.Rendering;
  2. namespace UnityEngine.Rendering.Universal
  3. {
  4. /// <summary>
  5. /// Utility class for post processing effects.
  6. /// </summary>
  7. public static class PostProcessUtils
  8. {
  9. /// <summary>
  10. /// Configures the blue noise dithering used.
  11. /// </summary>
  12. /// <param name="data">The <c>PostProcessData</c> resources to use.</param>
  13. /// <param name="index">The current array index to the Blue noise textures.</param>
  14. /// <param name="camera">The camera using the dithering effect.</param>
  15. /// <param name="material">The material used with the dithering effect.</param>
  16. /// <returns>The new array index to the Blue noise textures.</returns>
  17. [System.Obsolete("This method is obsolete. Use ConfigureDithering override that takes camera pixel width and height instead.")]
  18. public static int ConfigureDithering(PostProcessData data, int index, Camera camera, Material material)
  19. {
  20. return ConfigureDithering(data, index, camera.pixelWidth, camera.pixelHeight, material);
  21. }
  22. /// <summary>
  23. /// Configures the blue noise dithering used.
  24. /// </summary>
  25. /// <param name="data">The <c>PostProcessData</c> resources to use.</param>
  26. /// <param name="index">The current array index to the Blue noise textures.</param>
  27. /// <param name="cameraPixelWidth">The camera pixel width.</param>
  28. /// <param name="cameraPixelHeight">The camera pixel height.</param>
  29. /// <param name="material">The material used with the dithering effect.</param>
  30. /// <returns>The new array index to the Blue noise textures.</returns>
  31. public static int ConfigureDithering(PostProcessData data, int index, int cameraPixelWidth, int cameraPixelHeight, Material material)
  32. {
  33. var blueNoise = data.textures.blueNoise16LTex;
  34. if (blueNoise == null || blueNoise.Length == 0)
  35. return 0; // Safe guard
  36. #if LWRP_DEBUG_STATIC_POSTFX // Used by QA for automated testing
  37. index = 0;
  38. float rndOffsetX = 0f;
  39. float rndOffsetY = 0f;
  40. #else
  41. if (++index >= blueNoise.Length)
  42. index = 0;
  43. var oldState = Random.state;
  44. Random.InitState(Time.frameCount);
  45. float rndOffsetX = Random.value;
  46. float rndOffsetY = Random.value;
  47. Random.state = oldState;
  48. #endif
  49. // Ideally we would be sending a texture array once and an index to the slice to use
  50. // on every frame but these aren't supported on all Universal targets
  51. var noiseTex = blueNoise[index];
  52. material.SetTexture(ShaderConstants._BlueNoise_Texture, noiseTex);
  53. material.SetVector(ShaderConstants._Dithering_Params, new Vector4(
  54. cameraPixelWidth / (float)noiseTex.width,
  55. cameraPixelHeight / (float)noiseTex.height,
  56. rndOffsetX,
  57. rndOffsetY
  58. ));
  59. return index;
  60. }
  61. /// <summary>
  62. /// Configures the Film grain shader parameters.
  63. /// </summary>
  64. /// <param name="data">The <c>PostProcessData</c> resources to use.</param>
  65. /// <param name="settings">The Film Grain settings. </param>
  66. /// <param name="camera">The camera using the dithering effect.</param>
  67. /// <param name="material">The material used with the dithering effect.</param>
  68. [System.Obsolete("This method is obsolete. Use ConfigureFilmGrain override that takes camera pixel width and height instead.")]
  69. public static void ConfigureFilmGrain(PostProcessData data, FilmGrain settings, Camera camera, Material material)
  70. {
  71. ConfigureFilmGrain(data, settings, camera.pixelWidth, camera.pixelHeight, material);
  72. }
  73. /// <summary>
  74. /// Configures the Film grain shader parameters.
  75. /// </summary>
  76. /// <param name="data">The <c>PostProcessData</c> resources to use.</param>
  77. /// <param name="settings">The Film Grain settings. </param>
  78. /// <param name="cameraPixelWidth">The camera pixel width.</param>
  79. /// <param name="cameraPixelHeight">The camera pixel height.</param>
  80. /// <param name="material">The material used with the dithering effect.</param>
  81. public static void ConfigureFilmGrain(PostProcessData data, FilmGrain settings, int cameraPixelWidth, int cameraPixelHeight, Material material)
  82. {
  83. var texture = settings.texture.value;
  84. if (settings.type.value != FilmGrainLookup.Custom)
  85. texture = data.textures.filmGrainTex[(int)settings.type.value];
  86. #if LWRP_DEBUG_STATIC_POSTFX
  87. float rndOffsetX = 0f;
  88. float rndOffsetY = 0f;
  89. #else
  90. var oldState = Random.state;
  91. Random.InitState(Time.frameCount);
  92. float rndOffsetX = Random.value;
  93. float rndOffsetY = Random.value;
  94. Random.state = oldState;
  95. #endif
  96. var tilingParams = texture == null
  97. ? Vector4.zero
  98. : new Vector4(cameraPixelWidth / (float)texture.width, cameraPixelHeight / (float)texture.height, rndOffsetX, rndOffsetY);
  99. material.SetTexture(ShaderConstants._Grain_Texture, texture);
  100. material.SetVector(ShaderConstants._Grain_Params, new Vector2(settings.intensity.value * 4f, settings.response.value));
  101. material.SetVector(ShaderConstants._Grain_TilingParams, tilingParams);
  102. }
  103. internal static void SetSourceSize(RasterCommandBuffer cmd, RTHandle source)
  104. {
  105. float width = source.rt.width;
  106. float height = source.rt.height;
  107. if (source.rt.useDynamicScale)
  108. {
  109. width *= ScalableBufferManager.widthScaleFactor;
  110. height *= ScalableBufferManager.heightScaleFactor;
  111. }
  112. cmd.SetGlobalVector(ShaderConstants._SourceSize, new Vector4(width, height, 1.0f / width, 1.0f / height));
  113. }
  114. internal static void SetSourceSize(CommandBuffer cmd, RTHandle source)
  115. {
  116. SetSourceSize(CommandBufferHelpers.GetRasterCommandBuffer(cmd), source);
  117. }
  118. // Precomputed shader ids to same some CPU cycles (mostly affects mobile)
  119. static class ShaderConstants
  120. {
  121. public static readonly int _Grain_Texture = Shader.PropertyToID("_Grain_Texture");
  122. public static readonly int _Grain_Params = Shader.PropertyToID("_Grain_Params");
  123. public static readonly int _Grain_TilingParams = Shader.PropertyToID("_Grain_TilingParams");
  124. public static readonly int _BlueNoise_Texture = Shader.PropertyToID("_BlueNoise_Texture");
  125. public static readonly int _Dithering_Params = Shader.PropertyToID("_Dithering_Params");
  126. public static readonly int _SourceSize = Shader.PropertyToID("_SourceSize");
  127. }
  128. }
  129. }