ZED_Post-Processing.shader 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. //Set noises and min black
  3. Shader "ZED/ZED Post-Processing" {
  4. Properties
  5. {
  6. _MainTex("Texture", 2D) = "white" {}
  7. _MinBlack("Min black threshold", Range(0,1)) = 0.01
  8. }
  9. SubShader
  10. {
  11. Tags{ "RenderType" = "Opaque" }
  12. ZWrite Off
  13. ZTest Always
  14. Cull Off
  15. Pass
  16. {
  17. CGPROGRAM
  18. #pragma vertex vert
  19. #pragma fragment frag
  20. #include "UnityCG.cginc"
  21. #pragma multi_compile ___ UNITY_HDR_ON
  22. struct appdata
  23. {
  24. float4 vertex : POSITION;
  25. float2 uv : TEXCOORD0;
  26. };
  27. struct v2f
  28. {
  29. float2 uv : TEXCOORD0;
  30. float4 vertex : SV_POSITION;
  31. };
  32. uniform sampler2D _MainTex;
  33. float4 _MainTex_ST;
  34. float4 _MainTex_TexelSize;
  35. sampler2D ZEDMaskPostProcess;
  36. uniform float _gamma;
  37. uniform float _MinBlack;
  38. uniform int _NoiseSize;
  39. float rand(float2 co) {
  40. return frac(sin(dot(co.xy, float2(12.9898, 78.233))) * 43758.5453);
  41. }
  42. //Vertex Shader
  43. v2f vert(appdata v)
  44. {
  45. v2f o;
  46. o.vertex = UnityObjectToClipPos(v.vertex);
  47. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  48. return o;
  49. }
  50. //Fragment Shader
  51. float4 frag(v2f i) : SV_Target{
  52. float2 invertUV = i.uv;
  53. invertUV.y = 1 - i.uv.y;
  54. float mask = tex2D(ZEDMaskPostProcess, i.uv);
  55. float4 zed = tex2D(_MainTex, i.uv);
  56. if (mask > 0.9f)
  57. {
  58. //zed = (zed * 0.187) / (1.035 - zed);
  59. float SqrtPixel = sqrt(zed.r * zed.r + zed.g * zed.g + zed.b * zed.b);
  60. float3 NoiseFactors = 2;
  61. float2 random = _Time.x*_NoiseSize*floor(i.uv / _MainTex_TexelSize.xy / _NoiseSize) / 3.;
  62. float3 NoiseValue = float3(rand(random),
  63. rand(random),
  64. rand(random));
  65. float4 res = pow(zed, _gamma);
  66. res.r += (NoiseFactors.r * NoiseValue.r - NoiseFactors.r * 0.5) / 255;
  67. res.g += (NoiseFactors.g * NoiseValue.g - NoiseFactors.g * 0.5) / 255;
  68. res.b += (NoiseFactors.b * NoiseValue.b - NoiseFactors.b * 0.5) / 255;
  69. //res = res / (res + 0.187) * 1.035;
  70. res.a = 1.0f;
  71. //res *= mask;
  72. #if UNITY_COLORSPACE_GAMMA
  73. return clamp(res, _MinBlack, 1.0f);
  74. #else
  75. return clamp(res, GammaToLinearSpaceExact(_MinBlack), 1.0f);
  76. #endif
  77. }
  78. return zed;
  79. }
  80. ENDCG
  81. }
  82. }
  83. }