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

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