MinMaxReduction.shader 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. CGPROGRAM
  13. #pragma vertex vert
  14. #pragma fragment frag
  15. #include "UnityCG.cginc"
  16. struct v2f {
  17. float4 position : SV_POSITION;
  18. float2 uv[4] : TEXCOORD0;
  19. };
  20. uniform sampler2D _MainTex;
  21. v2f vert (appdata_img v) {
  22. v2f o;
  23. o.position = UnityObjectToClipPos (v.vertex);
  24. float2 uv = MultiplyUV (UNITY_MATRIX_TEXTURE0, v.texcoord);
  25. // Compute UVs to sample 2x2 pixel block.
  26. o.uv[0] = uv + float2(0,0);
  27. o.uv[1] = uv + float2(0,1);
  28. o.uv[2] = uv + float2(1,0);
  29. o.uv[3] = uv + float2(1,1);
  30. return o;
  31. }
  32. float4 frag (v2f i) : SV_Target
  33. {
  34. // Sample pixel block
  35. float4 v00 = tex2D(_MainTex, i.uv[0]);
  36. float2 v01 = tex2D(_MainTex, i.uv[1]).xy;
  37. float2 v10 = tex2D(_MainTex, i.uv[2]).xy;
  38. float2 v11 = tex2D(_MainTex, i.uv[3]).xy;
  39. float4 res;
  40. // output x: maximum of the four values
  41. res.x = max( max(v00.x,v01.x), max(v10.x,v11.x) );
  42. // output y: minimum of the four values
  43. res.y = min( min(v00.y,v01.y), min(v10.y,v11.y) );
  44. // output zw unchanged from the first pixel
  45. res.zw = v00.zw;
  46. return res;
  47. }
  48. ENDCG
  49. }
  50. }
  51. }
  52. Fallback off
  53. }