MinMaxReduction.shader 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. // Reduces input image (_MainTex) by 2x2.
  3. // Outputs maximum value in R, minimum in G.
  4. Shader "Hidden/Contrast Stretch Reduction" {
  5. Properties {
  6. _MainTex ("Base (RGB)", 2D) = "white" {}
  7. }
  8. Category {
  9. SubShader {
  10. Pass {
  11. ZTest Always Cull Off ZWrite Off
  12. Fog { Mode off }
  13. CGPROGRAM
  14. #pragma vertex vert
  15. #pragma fragment frag
  16. #pragma fragmentoption ARB_precision_hint_fastest
  17. #include "UnityCG.cginc"
  18. struct v2f {
  19. float4 position : POSITION;
  20. float2 uv[4] : TEXCOORD0;
  21. };
  22. uniform sampler2D _MainTex;
  23. v2f vert (appdata_img v) {
  24. v2f o;
  25. o.position = UnityObjectToClipPos (v.vertex);
  26. float2 uv = MultiplyUV (UNITY_MATRIX_TEXTURE0, v.texcoord);
  27. // Compute UVs to sample 2x2 pixel block.
  28. o.uv[0] = uv + float2(0,0);
  29. o.uv[1] = uv + float2(0,1);
  30. o.uv[2] = uv + float2(1,0);
  31. o.uv[3] = uv + float2(1,1);
  32. return o;
  33. }
  34. float4 frag (v2f i) : COLOR
  35. {
  36. // Sample pixel block
  37. float4 v00 = tex2D(_MainTex, i.uv[0]);
  38. float2 v01 = tex2D(_MainTex, i.uv[1]).xy;
  39. float2 v10 = tex2D(_MainTex, i.uv[2]).xy;
  40. float2 v11 = tex2D(_MainTex, i.uv[3]).xy;
  41. float4 res;
  42. // output x: maximum of the four values
  43. res.x = max( max(v00.x,v01.x), max(v10.x,v11.x) );
  44. // output y: minimum of the four values
  45. res.y = min( min(v00.y,v01.y), min(v10.y,v11.y) );
  46. // output zw unchanged from the first pixel
  47. res.zw = v00.zw;
  48. return res;
  49. }
  50. ENDCG
  51. }
  52. }
  53. }
  54. Fallback off
  55. }