source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg @ 2825

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