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

Revision 2813, 4.2 KB checked in by mattausch, 16 years ago (diff)

ssao working ok

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;
60  normalize(centerNormal);
61 
62  // Check in a circular area around the current position.
63  // Shoot vectors to the positions there, and check the angle to these positions.
64  // Summing up these angles gives an estimation of the occlusion at the current position.
65 
66  float total_ao = 0.0;
67
68  //const float areaSize = 4.0f; 
69  const float areaSize = 5e-1f;
70  //const float areaSize = 1e-4f;
71  const float sampleIntensity = 0.2f;
72 
73  for (int i = 0; i < SAMPLES; i ++) {
74    float2 offset = samples[i];
75   
76    //sample noisetex; r stores costheta, g stores sintheta
77    float3 noise = tex2D(noiseTexture, IN.texCoord.xy).xyz * 2.0f - 1.0f;
78
79    float2 offsetTransformed = offset;
80    //float2 offsetTransformed;
81    //offsetTransformed.x = noise.r*offset.x - noise.g*offset.y;
82    //offsetTransformed.y = noise.g*offset.x + noise.r*offset.y;
83               
84    // weight with projected coordinate to reach similar kernel size for near and far
85    float2 texcoord = IN.texCoord.xy + offsetTransformed * areaSize * centerPositionW.w;
86   
87    float3 sample_position = tex2D(positions, texcoord).xyz;
88   
89    float3 vector_to_sample = sample_position - centerPosition;
90    float length_to_sample = length(vector_to_sample);
91    float3 direction_to_sample = vector_to_sample / (length_to_sample + 1e-9f);
92   
93    // Angle between current normal and direction to sample controls AO intensity.
94    float cos_angle = dot(direction_to_sample, centerNormal);
95    cos_angle = max(cos_angle, 0.0f);
96   
97    // distance between current position and sample position controls AO intensity.
98    const float maxdist = 1e-1f;
99    //const float scale = 50.0f;
100
101    float distance_intensity = maxdist - length_to_sample;
102    distance_intensity = max(distance_intensity, 0.0f);
103   
104    total_ao += cos_angle * sampleIntensity * distance_intensity;
105  }
106       
107  return (1.0f - total_ao);
108}
109
110
111pixel main(fragment IN,
112           uniform sampler2D colors,
113           uniform sampler2D positions,
114           uniform sampler2D normals,
115           const uniform float4x4 ProjTrafo,
116           uniform sampler2D noiseTexture)
117{
118  pixel OUT;
119  float4 lightDir = float4(1.0f, 1.0f, 1.0f, 0.0f);
120  //float4 lightDir = float4(0.0f, 1.0f, 0.0f, 0.0f);
121 
122  float4 color = tex2D(colors, IN.texCoord.xy);
123  float3 normal = tex2D(normals, IN.texCoord.xy).xyz;
124  // expand normal
125  normal = normal * 2.0f - 1.0f;
126
127  float4 position = tex2D(positions, IN.texCoord.xy);
128
129  float4 ambient = float4(0.3f);
130
131  // float3 L = normalize(lightPosition - position);
132  float3 light = normalize(lightDir.xyz);
133
134  float diffuseLight = max(dot(normal, light), 0.0f);
135  float diffuse = diffuseLight;
136
137  float ao = ssao(IN, positions, normals, ProjTrafo, noiseTexture);
138  OUT.color = ao;
139  //OUT.color = ao * (ambient + diffuse) * color;
140
141  //float4 currentPos = tex2D(normals, IN.texCoord.xy) * 0.5f + 0.5f;
142  //float4 currentPos = tex2D(positions, IN.texCoord.xy);
143  //OUT.color = currentPos.w;
144 
145   
146  return OUT;
147}
Note: See TracBrowser for help on using the repository browser.