PostProcessUtils.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. namespace UnityEngine.Rendering.Universal
  2. {
  3. public static class PostProcessUtils
  4. {
  5. [System.Obsolete("This method is obsolete. Use ConfigureDithering override that takes camera pixel width and height instead.")]
  6. public static int ConfigureDithering(PostProcessData data, int index, Camera camera, Material material)
  7. {
  8. return ConfigureDithering(data, index, camera.pixelWidth, camera.pixelHeight, material);
  9. }
  10. // TODO: Add API docs
  11. public static int ConfigureDithering(PostProcessData data, int index, int cameraPixelWidth, int cameraPixelHeight, Material material)
  12. {
  13. var blueNoise = data.textures.blueNoise16LTex;
  14. if (blueNoise == null || blueNoise.Length == 0)
  15. return 0; // Safe guard
  16. #if LWRP_DEBUG_STATIC_POSTFX // Used by QA for automated testing
  17. index = 0;
  18. float rndOffsetX = 0f;
  19. float rndOffsetY = 0f;
  20. #else
  21. if (++index >= blueNoise.Length)
  22. index = 0;
  23. float rndOffsetX = Random.value;
  24. float rndOffsetY = Random.value;
  25. #endif
  26. // Ideally we would be sending a texture array once and an index to the slice to use
  27. // on every frame but these aren't supported on all Universal targets
  28. var noiseTex = blueNoise[index];
  29. material.SetTexture(ShaderConstants._BlueNoise_Texture, noiseTex);
  30. material.SetVector(ShaderConstants._Dithering_Params, new Vector4(
  31. cameraPixelWidth / (float)noiseTex.width,
  32. cameraPixelHeight / (float)noiseTex.height,
  33. rndOffsetX,
  34. rndOffsetY
  35. ));
  36. return index;
  37. }
  38. [System.Obsolete("This method is obsolete. Use ConfigureFilmGrain override that takes camera pixel width and height instead.")]
  39. public static void ConfigureFilmGrain(PostProcessData data, FilmGrain settings, Camera camera, Material material)
  40. {
  41. ConfigureFilmGrain(data, settings, camera.pixelWidth, camera.pixelHeight, material);
  42. }
  43. // TODO: Add API docs
  44. public static void ConfigureFilmGrain(PostProcessData data, FilmGrain settings, int cameraPixelWidth, int cameraPixelHeight, Material material)
  45. {
  46. var texture = settings.texture.value;
  47. if (settings.type.value != FilmGrainLookup.Custom)
  48. texture = data.textures.filmGrainTex[(int)settings.type.value];
  49. #if LWRP_DEBUG_STATIC_POSTFX
  50. float offsetX = 0f;
  51. float offsetY = 0f;
  52. #else
  53. float offsetX = Random.value;
  54. float offsetY = Random.value;
  55. #endif
  56. var tilingParams = texture == null
  57. ? Vector4.zero
  58. : new Vector4(cameraPixelWidth / (float)texture.width, cameraPixelHeight / (float)texture.height, offsetX, offsetY);
  59. material.SetTexture(ShaderConstants._Grain_Texture, texture);
  60. material.SetVector(ShaderConstants._Grain_Params, new Vector2(settings.intensity.value * 4f, settings.response.value));
  61. material.SetVector(ShaderConstants._Grain_TilingParams, tilingParams);
  62. }
  63. internal static void SetSourceSize(CommandBuffer cmd, RenderTextureDescriptor desc)
  64. {
  65. float width = desc.width;
  66. float height = desc.height;
  67. if (desc.useDynamicScale)
  68. {
  69. width *= ScalableBufferManager.widthScaleFactor;
  70. height *= ScalableBufferManager.heightScaleFactor;
  71. }
  72. cmd.SetGlobalVector(ShaderConstants._SourceSize, new Vector4(width, height, 1.0f / width, 1.0f / height));
  73. }
  74. // Precomputed shader ids to same some CPU cycles (mostly affects mobile)
  75. static class ShaderConstants
  76. {
  77. public static readonly int _Grain_Texture = Shader.PropertyToID("_Grain_Texture");
  78. public static readonly int _Grain_Params = Shader.PropertyToID("_Grain_Params");
  79. public static readonly int _Grain_TilingParams = Shader.PropertyToID("_Grain_TilingParams");
  80. public static readonly int _BlueNoise_Texture = Shader.PropertyToID("_BlueNoise_Texture");
  81. public static readonly int _Dithering_Params = Shader.PropertyToID("_Dithering_Params");
  82. public static readonly int _SourceSize = Shader.PropertyToID("_SourceSize");
  83. }
  84. }
  85. }