1 | //////////////////// |
---|
2 | // Screen Spaced Ambient Occlusion shader |
---|
3 | // mainly based on Kustls shader |
---|
4 | |
---|
5 | #define NUM_SAMPLES 8 |
---|
6 | #define SAMPLE_INTENSITY 0.5f |
---|
7 | //#define SAMPLE_INTENSITY 0.7f |
---|
8 | #define AREA_SIZE 5e-1f |
---|
9 | |
---|
10 | // kustls magic sample positions |
---|
11 | /*static const float2 samples[NUM_SAMPLES] = |
---|
12 | { |
---|
13 | {-0.326212f, -0.405805f}, |
---|
14 | {-0.840144f, -0.07358f}, |
---|
15 | {-0.695914f, 0.457137f}, |
---|
16 | {-0.203345f, 0.620716}, |
---|
17 | {0.96234f, -0.194983f}, |
---|
18 | {0.473434f, -0.480026f}, |
---|
19 | {0.519456, 0.767022f}, |
---|
20 | {0.185461f, -0.893124f}, |
---|
21 | {0.507431f, 0.064425f}, |
---|
22 | {0.89642f, 0.412458f}, |
---|
23 | {-0.32194f, -0.932615f}, |
---|
24 | {-0.791559f, -0.597705f}, |
---|
25 | {0.326212f, 0.405805f}, |
---|
26 | {0.840144f, 0.07358f}, |
---|
27 | {0.695914f, -0.457137f}, |
---|
28 | {0.203345f, -0.620716}, |
---|
29 | {-0.96234f, 0.194983f}, |
---|
30 | {-0.473434f, 0.480026f}, |
---|
31 | {-0.519456, -0.767022f}, |
---|
32 | {-0.185461f, 0.893124f}, |
---|
33 | {-0.507431f, -0.064425f}, |
---|
34 | {-0.89642f, -0.412458f}, |
---|
35 | {0.32194f, 0.932615f}, |
---|
36 | {0.791559f, 0.597705f} |
---|
37 | };*/ |
---|
38 | |
---|
39 | |
---|
40 | struct fragment |
---|
41 | { |
---|
42 | float4 pos: WPOS; // normalized screen position |
---|
43 | float4 texCoord: TEXCOORD0; |
---|
44 | }; |
---|
45 | |
---|
46 | |
---|
47 | struct pixel |
---|
48 | { |
---|
49 | float4 color: COLOR0; |
---|
50 | }; |
---|
51 | |
---|
52 | |
---|
53 | float2 reflect(float2 pt, float2 n) |
---|
54 | { |
---|
55 | // distance to plane |
---|
56 | float d = dot(n, pt); |
---|
57 | // reflect around plane |
---|
58 | float2 rpt = pt - d * 2.0f * n; |
---|
59 | |
---|
60 | //return pt; |
---|
61 | return rpt; |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | float2 rotate(float2 pt, float2 n) |
---|
66 | { |
---|
67 | float2 ptTransformed; |
---|
68 | ptTransformed.x = n.r * pt.x - n.g * pt.y; |
---|
69 | ptTransformed.y = n.g * pt.x + n.r * pt.y; |
---|
70 | |
---|
71 | return ptTransformed; |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | //based on kustls shader |
---|
76 | float ssao(fragment IN, |
---|
77 | uniform sampler2D positions, |
---|
78 | uniform sampler2D noiseTexture, |
---|
79 | uniform float2 samples[NUM_SAMPLES], |
---|
80 | uniform float3 currentNormal |
---|
81 | ) |
---|
82 | { |
---|
83 | // the current world position |
---|
84 | float4 centerPosition = tex2D(positions, IN.texCoord.xy); |
---|
85 | // the w coordinate from the persp. projection |
---|
86 | float w = centerPosition.w; |
---|
87 | |
---|
88 | // Check in a circular area around the current position. |
---|
89 | // Shoot vectors to the positions there, and check the angle to these positions. |
---|
90 | // Summing up these angles gives an estimation of the occlusion at the current position. |
---|
91 | |
---|
92 | float total_ao = 0.0; |
---|
93 | |
---|
94 | const float areaSize = 5e-1f; |
---|
95 | //const float areaSize = 3e-1f; |
---|
96 | //const float sampleIntensity = 0.2f; |
---|
97 | |
---|
98 | for (int i = 0; i < NUM_SAMPLES; i ++) { |
---|
99 | float2 offset = samples[i]; |
---|
100 | |
---|
101 | //sample noisetex; r stores costheta, g stores sintheta |
---|
102 | float2 noise = tex2D(noiseTexture, IN.texCoord.xy * 7.0f).xy * 2.0f - 1.0f; |
---|
103 | |
---|
104 | // rotation |
---|
105 | //float2 offsetTransformed = offset; |
---|
106 | float2 offsetTransformed = rotate(offset, noise); |
---|
107 | //float2 offsetTransformed = reflect(offset, noise); |
---|
108 | |
---|
109 | // weight with projected coordinate to reach similar kernel size for near and far |
---|
110 | float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; |
---|
111 | |
---|
112 | float3 sample_position = tex2D(positions, texcoord).xyz; |
---|
113 | |
---|
114 | float3 vector_to_sample = sample_position - centerPosition.xyz; |
---|
115 | float length_to_sample = length(vector_to_sample); |
---|
116 | float3 direction_to_sample = vector_to_sample / length_to_sample; |
---|
117 | |
---|
118 | // Angle between current normal and direction to sample controls AO intensity. |
---|
119 | float cos_angle = dot(direction_to_sample, currentNormal); |
---|
120 | cos_angle = max(cos_angle, 0.0f); |
---|
121 | cos_angle *= cos_angle; |
---|
122 | |
---|
123 | // distance between current position and sample position controls AO intensity. |
---|
124 | //const float maxdist = 2e-1f; |
---|
125 | //const float maxdist = 5e-1f; |
---|
126 | const float distanceScale = 1e-6f; |
---|
127 | //float distance_intensity = maxdist - length_to_sample; |
---|
128 | float distance_intensity = (SAMPLE_INTENSITY * distanceScale) / (distanceScale + length_to_sample * length_to_sample); |
---|
129 | //distance_intensity = max(distance_intensity, 0.0f); |
---|
130 | // quadratic influence |
---|
131 | //distance_intensity *= distance_intensity; |
---|
132 | |
---|
133 | total_ao += cos_angle * distance_intensity; |
---|
134 | } |
---|
135 | |
---|
136 | return (1.0f - total_ao); |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | float4 shade(fragment IN, |
---|
141 | uniform sampler2D colors, |
---|
142 | uniform sampler2D positions, |
---|
143 | uniform float3 normal, |
---|
144 | uniform float amb) |
---|
145 | { |
---|
146 | float4 lightDir = float4(0.8f, -1.0f, 0.7f, 0.0f); |
---|
147 | float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f); |
---|
148 | |
---|
149 | float4 color = tex2D(colors, IN.texCoord.xy); |
---|
150 | |
---|
151 | float4 position = tex2D(positions, IN.texCoord.xy); |
---|
152 | |
---|
153 | float4 ambient = 0.3f; |
---|
154 | |
---|
155 | // float3 L = normalize(lightPosition - position); |
---|
156 | float3 light = normalize(lightDir.xyz); |
---|
157 | float3 light2 = normalize(lightDir2.xyz); |
---|
158 | |
---|
159 | float diffuseLight = max(dot(normal, light), 0.0f); |
---|
160 | float diffuseLight2 = max(dot(normal, light2), 0.0f); |
---|
161 | |
---|
162 | float diffuse = diffuseLight + diffuseLight2; |
---|
163 | |
---|
164 | return (ambient + diffuse) * color * (1.0f - amb) + amb * color; |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | pixel main_ssao(fragment IN, |
---|
169 | uniform sampler2D colors, |
---|
170 | uniform sampler2D positions, |
---|
171 | uniform sampler2D normals, |
---|
172 | uniform sampler2D noiseTexture, |
---|
173 | uniform float2 samples[NUM_SAMPLES]) |
---|
174 | { |
---|
175 | pixel OUT; |
---|
176 | |
---|
177 | float4 normal = tex2D(normals, IN.texCoord.xy); |
---|
178 | float amb = normal.w; |
---|
179 | |
---|
180 | // expand normal |
---|
181 | normal = normalize(normal * 2.0f - 1.0f); |
---|
182 | |
---|
183 | float4 col = shade(IN, colors, positions, normal, amb); |
---|
184 | float ao = ssao(IN, positions, noiseTexture, samples, normal); |
---|
185 | |
---|
186 | //OUT.color = ao; |
---|
187 | OUT.color = ao * col; |
---|
188 | |
---|
189 | return OUT; |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | pixel main(fragment IN, |
---|
194 | uniform sampler2D colors, |
---|
195 | uniform sampler2D positions, |
---|
196 | uniform sampler2D normals) |
---|
197 | { |
---|
198 | pixel OUT; |
---|
199 | |
---|
200 | float4 normal = tex2D(normals, IN.texCoord.xy); |
---|
201 | float amb = normal.w; |
---|
202 | |
---|
203 | // expand normal |
---|
204 | normal = normalize(normal * 2.0f - 1.0f); |
---|
205 | |
---|
206 | float4 col = shade(IN, colors, positions, normal.xyz, amb); |
---|
207 | OUT.color = col; |
---|
208 | |
---|
209 | return OUT; |
---|
210 | } |
---|