1 Shader "Hidden/ChromaticAberration" {
2 Properties {
3 _MainTex ("Base", 2D) = "" {}
4 }
5
6 CGINCLUDE
7
8 #include "UnityCG.cginc"
9
10 struct v2f {
11 float4 pos : SV_POSITION;
12 float2 uv : TEXCOORD0;
13 };
14
15 sampler2D _MainTex;
16
17 float4 _MainTex_TexelSize;
18 half _ChromaticAberration;
19 half _AxialAberration;
20 half _Luminance;
21 half2 _BlurDistance;
22
23 v2f vert( appdata_img v )
24 {
25 v2f o;
26 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
27 o.uv = v.texcoord.xy;
28
29 return o;
30 }
31
32 half4 fragDs(v2f i) : SV_Target
33 {
34 half4 c = tex2D (_MainTex, i.uv.xy + _MainTex_TexelSize.xy * 0.5);
35 c += tex2D (_MainTex, i.uv.xy - _MainTex_TexelSize.xy * 0.5);
36 c += tex2D (_MainTex, i.uv.xy + _MainTex_TexelSize.xy * float2(0.5,-0.5));
37 c += tex2D (_MainTex, i.uv.xy - _MainTex_TexelSize.xy * float2(0.5,-0.5));
38 return c/4.0;
39 }
40
41 half4 frag(v2f i) : SV_Target
42 {
43 half2 coords = i.uv;
44 half2 uv = i.uv;
45
46 coords = (coords - 0.5) * 2.0;
47 half coordDot = dot (coords,coords);
48
49 half2 uvG = uv - _MainTex_TexelSize.xy * _ChromaticAberration * coords * coordDot;
50 half4 color = tex2D (_MainTex, uv);
51 #if SHADER_API_D3D9
52 // Work around Cg's code generation bug for D3D9 pixel shaders :(
53 color.g = color.g * 0.0001 + tex2D (_MainTex, uvG).g;
54 #else
55 color.g = tex2D (_MainTex, uvG).g;
56 #endif
57
58 return color;
59 }
60
61 // squeezing into SM2.0 with 9 samples:
62 static const int SmallDiscKernelSamples = 9;
63 static const half2 SmallDiscKernel[SmallDiscKernelSamples] =
64 {
65 half2(-0.926212,-0.40581),
66 half2(-0.695914,0.457137),
67 half2(-0.203345,0.820716),
68 half2(0.96234,-0.194983),
69 half2(0.473434,-0.480026),
70 half2(0.519456,0.767022),
71 half2(0.185461,-0.893124),
72 half2(0.89642,0.412458),
73 half2(-0.32194,-0.932615),
74 };
75
76 half4 fragComplex(v2f i) : SV_Target
77 {
78 half2 coords = i.uv;
79 half2 uv = i.uv;
80
81 // corner heuristic
82 coords = (coords - 0.5h) * 2.0h;
83 half coordDot = dot (coords,coords);
84
85 half4 color = tex2D (_MainTex, uv);
86 half tangentialStrength = _ChromaticAberration * coordDot * coordDot;
87 half maxOfs = clamp(max(_AxialAberration, tangentialStrength), _BlurDistance.x, _BlurDistance.y);
88
89 // we need a blurred sample tap for advanced aberration
90
91 // NOTE: it's relatively important that input is HDR
92 // and if you do have a proper HDR setup, lerping .rb might yield better results than .g
93 // (see below)
94
95 half4 blurredTap = color * 0.1h;
96 for(int l=0; l < SmallDiscKernelSamples; l++)
97 {
98 half2 sampleUV = uv + SmallDiscKernel[l].xy * _MainTex_TexelSize.xy * maxOfs;
99 half3 tap = tex2D(_MainTex, sampleUV).rgb;
100 blurredTap.rgb += tap;
101 }
102 blurredTap.rgb /= (float)SmallDiscKernelSamples + 0.2h;
103
104 // debug:
105 //return blurredTap;
106
107 half lumDiff = Luminance(abs(blurredTap.rgb-color.rgb));
108 half isEdge = saturate(_Luminance * lumDiff);
109
110 // debug #2:
111 //return isEdge;
112
113 color.rb = lerp(color.rb, blurredTap.rb, isEdge);
114
115 return color;
116 }
117
118 ENDCG
119
120 Subshader {
121
122 // 0: box downsample
123 Pass {
124 ZTest Always Cull Off ZWrite Off
125
126 CGPROGRAM
127
128 #pragma vertex vert
129 #pragma fragment fragDs
130
131 ENDCG
132 }
133 // 1: simple chrom aberration
134 Pass {
135 ZTest Always Cull Off ZWrite Off
136
137 CGPROGRAM
138
139 #pragma vertex vert
140 #pragma fragment frag
141
142 ENDCG
143 }
144 // 2: simulates more chromatic aberration effects
145 Pass {
146 ZTest Always Cull Off ZWrite Off
147
148 CGPROGRAM
149
150 #pragma vertex vert
151 #pragma fragment fragComplex
152
153 ENDCG
154 }
155 }
156
157 Fallback off
158
159 } // shader