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

Revision 2819, 4.3 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           uniform sampler2D noiseTexture                               
63           )
64{
65  // the current world position
66  float4 centerPosition = tex2D(positions, IN.texCoord.xy);
67  // the w coordinate from the persp. projection
68  float w = centerPosition.w;
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    // rotation
91    //float2 offsetTransformed = offset;
92    float2 offsetTransformed;
93    offsetTransformed.x = noise.r * offset.x - noise.g * offset.y;
94    offsetTransformed.y = noise.g * offset.x + noise.r * offset.y;
95               
96    // weight with projected coordinate to reach similar kernel size for near and far
97    float2 texcoord = IN.texCoord.xy + offsetTransformed * areaSize * w;
98   
99    float3 sample_position = tex2D(positions, texcoord).xyz;
100   
101    float3 vector_to_sample = sample_position - centerPosition.xyz;
102    float length_to_sample = length(vector_to_sample);
103    float3 direction_to_sample = vector_to_sample / (length_to_sample + 1e-9f);
104   
105    // Angle between current normal and direction to sample controls AO intensity.
106    float cos_angle = dot(direction_to_sample, centerNormal);
107    cos_angle = max(cos_angle, 0.0f);
108   
109    // distance between current position and sample position controls AO intensity.
110    const float maxdist = 5e-1f;
111    //const float scale = 50.0f;
112
113    float distance_intensity = maxdist - length_to_sample;
114    distance_intensity *= distance_intensity;
115    distance_intensity = max(distance_intensity, 0.0f);
116   
117    total_ao += cos_angle * sampleIntensity * distance_intensity;
118  }
119       
120  return (1.0f - total_ao);
121}
122
123
124pixel main(fragment IN,
125           uniform sampler2D colors,
126           uniform sampler2D positions,
127           uniform sampler2D normals,
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 = normalize(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, noiseTexture);
155  float ao = 1.0f;
156  //OUT.color = ao;
157  OUT.color = ao * (ambient + diffuse) * color;
158  //OUT.color = (ambient + diffuse) * color;
159     
160  return OUT;
161}
Note: See TracBrowser for help on using the repository browser.