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

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