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