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

Revision 2810, 3.9 KB checked in by mattausch, 16 years ago (diff)
Line 
1#define SAMPLES 24
2
3// kustls magic sample positions
4static const float2 samples[SAMPLES] =
5{
6  {-0.326212f, -0.405805f},
7  {-0.840144f, -0.07358f},
8  {-0.695914f, 0.457137f},
9  {-0.203345f, 0.620716},
10  {0.96234f, -0.194983f},
11  {0.473434f, -0.480026f},
12  {0.519456, 0.767022f},
13  {0.185461f, -0.893124f},
14  {0.507431f, 0.064425f},
15  {0.89642f, 0.412458f},
16  {-0.32194f, -0.932615f},
17  {-0.791559f, -0.597705f},
18  {0.326212f, 0.405805f},
19  {0.840144f, 0.07358f},
20  {0.695914f, -0.457137f},
21  {0.203345f, -0.620716},
22  {-0.96234f, 0.194983f},
23  {-0.473434f, 0.480026f},
24  {-0.519456, -0.767022f},
25  {-0.185461f, 0.893124f},
26  {-0.507431f, -0.064425f},
27  {-0.89642f, -0.412458f},
28  {0.32194f, 0.932615f},
29  {0.791559f, 0.597705f}
30};
31
32
33struct fragment
34{
35  float4 pos: WPOS; // normalized screen position
36  float4 texCoord: TEXCOORD0;
37};
38
39
40struct pixel
41{
42  float4 color: COLOR0;
43};
44
45
46//based on kustls shader       
47float ssao(fragment IN,
48           uniform sampler2D positions,
49           uniform sampler2D normals,
50           const uniform float4x4 ProjTrafo,
51           uniform sampler2D noiseTexture                               
52           )
53{
54  // the current world position
55  float4 centerPositionW = tex2D(positions, IN.texCoord.xy);
56  float3 centerPosition = centerPositionW.xyz;
57
58  // the normal on the current position
59  float3 centerNormal = tex2D(normals, IN.texCoord.xy).xyz * 2.0f - 1.0f;
60
61  // Check in a circular area around the current position.
62  // Shoot vectors to the positions there, and check the angle to these positions.
63  // Summing up these angles gives an estimation of the occlusion at the current position.
64 
65  float total_ao = 0.0;
66
67  //const float areaSize = 4.0f; 
68  const float areaSize = 5e-3f;
69  const float sampleIntensity = 0.1;
70 
71  for (int i = 0; i < SAMPLES; i ++) {
72    float2 offset = samples[i];
73   
74    //sample noisetex; r stores costheta, g stores sintheta
75    float3 noise = tex2D(noiseTexture, IN.texCoord.xy).xyz * 2.0f - 1.0f;
76
77    float2 offsetTransformed;
78    offsetTransformed.x = noise.r*offset.x - noise.g*offset.y;
79    offsetTransformed.y = noise.g*offset.x + noise.r*offset.y;
80               
81    float2 texcoord = IN.texCoord.xy + offsetTransformed * areaSize;// * (1.0f - centerPositionW.w);
82   
83    float3 sample_position = tex2D(positions, texcoord).xyz;
84   
85    float3 vector_to_sample = sample_position - centerPosition;
86    float length_to_sample = length(vector_to_sample);
87    float3 direction_to_sample = vector_to_sample / length_to_sample;
88   
89    // Angle between current normal and direction to sample controls AO intensity.
90    float cos_angle = dot(direction_to_sample, centerNormal);
91    cos_angle = max(cos_angle, 0.0f);
92   
93    // distance between current position and sample position controls AO intensity.
94    const float scale = 5e-1f;
95
96    float distance_intensity = scale * (1.0f - length_to_sample);
97    distance_intensity = max(distance_intensity, 0.0f);
98   
99    total_ao += cos_angle * sampleIntensity * distance_intensity;
100  }
101       
102  return (1.0f - total_ao);
103}
104
105
106pixel main(fragment IN,
107           uniform sampler2D colors,
108           uniform sampler2D positions,
109           uniform sampler2D normals,
110           const uniform float4x4 ProjTrafo,
111           uniform sampler2D noiseTexture)
112{
113  pixel OUT;
114  float4 lightDir = float4(1.0f, 1.0f, 1.0f, 0.0f);
115 
116  float4 color = tex2D(colors, IN.texCoord.xy);
117  float3 normal = tex2D(normals, IN.texCoord.xy).xyz;
118  // expand normal
119  normal = normal * 2.0f - 1.0f;
120
121  float4 position = tex2D(positions, IN.texCoord.xy);
122
123  float4 ambient = float4(0.3f);
124
125  // float3 L = normalize(lightPosition - position);
126  float3 light = normalize(lightDir.xyz);
127
128  float diffuseLight = max(dot(normal, light), 0.0f);
129  float diffuse = diffuseLight;
130
131  float ao = ssao(IN, positions, normals, ProjTrafo, noiseTexture);
132  OUT.color = ao; //(ambient + diffuse) * color;
133  //OUT.color = (ambient + diffuse) * color;
134  //float4 currentPos = tex2D(positions, IN.texCoord.xy);
135  //OUT.color = currentPos;
136 
137  return OUT;
138}
Note: See TracBrowser for help on using the repository browser.