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

Revision 2822, 5.4 KB checked in by mattausch, 16 years ago (diff)

worked on deferred shading

Line 
1#define NUM_SAMPLES 12
2//#define SAMPLE_INTENSITY 0.4f
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).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    //offsetTransformed.x = noise.r * offset.x - noise.g * offset.y;
106    //offsetTransformed.y = noise.g * offset.x + noise.r * offset.y;
107               
108    // weight with projected coordinate to reach similar kernel size for near and far
109    float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
110   
111    float3 sample_position = tex2D(positions, texcoord).xyz;
112   
113    float3 vector_to_sample = sample_position - centerPosition.xyz;
114    float length_to_sample = length(vector_to_sample);
115    float3 direction_to_sample = vector_to_sample / length_to_sample;
116   
117    // Angle between current normal and direction to sample controls AO intensity.
118    float cos_angle = dot(direction_to_sample, currentNormal);
119    cos_angle = max(cos_angle, 0.0f);
120   
121    // distance between current position and sample position controls AO intensity.
122    //const float maxdist = 2e-1f;
123    const float maxdist = 5e-1f;
124   
125    float distance_intensity = maxdist - length_to_sample;
126    //float distance_intensity = 0.5f / (1.0f + length_to_sample * length_to_sample);
127    distance_intensity = max(distance_intensity, 0.0f);
128    // quadratic influence
129    distance_intensity *= distance_intensity;
130   
131    total_ao += cos_angle * SAMPLE_INTENSITY * distance_intensity;
132  }
133       
134  return (1.0f - total_ao);
135}
136
137
138float4 shade(fragment IN,
139             uniform sampler2D colors,
140             uniform sampler2D positions,
141             uniform float3 normal,
142             uniform float amb)
143{
144  float4 lightDir = float4(0.8f, -1.0f, 0.7f, 0.0f);
145  float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f);
146 
147  float4 color = tex2D(colors, IN.texCoord.xy);
148 
149  float4 position = tex2D(positions, IN.texCoord.xy);
150
151  float4 ambient = 0.3f;
152
153  // float3 L = normalize(lightPosition - position);
154  float3 light = normalize(lightDir.xyz);
155  float3 light2 = normalize(lightDir2.xyz);
156 
157  float diffuseLight = max(dot(normal, light), 0.0f);
158  float diffuseLight2 = max(dot(normal, light2), 0.0f);
159
160  float diffuse = diffuseLight + diffuseLight2;
161
162  return (ambient + diffuse) * color * (1.0f - amb) + amb * color;
163}
164
165
166pixel main_ssao(fragment IN,
167               uniform sampler2D colors,
168               uniform sampler2D positions,
169               uniform sampler2D normals,
170               uniform sampler2D noiseTexture,
171               uniform float2 samples[NUM_SAMPLES])
172{
173  pixel OUT;
174
175  float4 normal = tex2D(normals, IN.texCoord.xy);
176  float amb = normal.w;
177
178  // expand normal
179  normal = normalize(normal * 2.0f - 1.0f);
180
181  float4 col = shade(IN, colors, positions, normal, amb);
182  float ao = ssao(IN, positions, noiseTexture, samples, normal);
183 
184  //OUT.color = tex2D(positions, IN.texCoord.xy);
185  //OUT.color = ao;
186  //OUT.color = col;
187  OUT.color = ao * col;
188
189  return OUT;
190}
191
192
193pixel 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}
Note: See TracBrowser for help on using the repository browser.