- Timestamp:
- 08/27/08 15:09:04 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
r2872 r2873 3 3 // based on shader of Alexander Kusternig 4 4 5 //#define NUM_SAMPLES 8 6 #define NUM_SAMPLES 165 #define NUM_SAMPLES 10 6 //#define NUM_SAMPLES 16 7 7 8 8 // rule of thumb: approx 1 / NUM_SAMPLES 9 #define SAMPLE_INTENSITY 0.15 10 //#define SAMPLE_INTENSITY 0.125f9 //#define SAMPLE_INTENSITY 0.15f 10 #define SAMPLE_INTENSITY 0.2f 11 11 12 12 #define AREA_SIZE 7e-1f … … 43 43 44 44 45 /*float2 rotate(float2 pt, float2 n)46 {47 float2 ptTransformed;48 ptTransformed.x = n.r * pt.x - n.g * pt.y;49 ptTransformed.y = n.g * pt.x + n.r * pt.y;50 51 return ptTransformed;52 }*/53 54 45 55 46 /** The ssao shader returning the an intensity value between 0 and 1 … … 74 65 float total_ao = 0.0; 75 66 76 const float areaSize = 5e-1f;77 78 67 for (int i = 0; i < NUM_SAMPLES; i ++) 79 68 { 80 69 float2 offset = samples[i]; 81 70 82 //sample noisetex; r stores costheta, g stores sintheta 71 //////////////////// 72 // add random noise: r stores costheta, g stores sintheta 73 83 74 //float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f; 84 75 float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy; 85 76 86 // rotation87 //float2 offsetTransformed = offset;88 //float2 offsetTransformed = rotate(offset, mynoise);89 77 float2 offsetTransformed = reflect(offset, mynoise); 90 78 … … 117 105 return max(0.0f, 1.0f - total_ao); 118 106 //return dot(currentViewDir, currentNormal); 119 }120 121 122 /** Computes ambient occlusion + diffuse reflections123 */124 float4 globIllum(fragment IN,125 uniform sampler2D colors,126 uniform sampler2D positions,127 uniform sampler2D noiseTexture,128 uniform float2 samples[NUM_SAMPLES],129 uniform float3 currentNormal,130 uniform float3 currentViewDir,131 uniform float noiseMultiplier,132 uniform float4 centerPosition133 )134 {135 // the w coordinate from the persp. projection136 float w = centerPosition.w;137 138 // Check in a circular area around the current position.139 // Shoot vectors to the positions there, and check the angle to these positions.140 // Summing up these angles gives an estimation of the occlusion at the current position.141 142 // ao is in stored in the w143 float4 total_color = float4(0, 0, 0, 1);144 145 const float areaSize = 5e-1f;146 147 for (int i = 0; i < NUM_SAMPLES; i ++)148 {149 float2 offset = samples[i];150 151 //sample noisetex; r stores costheta, g stores sintheta152 float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy;153 154 // rotation155 float2 offsetTransformed = reflect(offset, mynoise);156 157 // weight with projected coordinate to reach similar kernel size for near and far158 float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;159 160 float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;161 float3 sample_color = tex2Dlod(colors, float4(texcoord, 0, 1)).xyz;162 163 float3 vector_to_sample = sample_position - centerPosition.xyz;164 float length_to_sample = length(vector_to_sample);165 float3 direction_to_sample = vector_to_sample / length_to_sample;166 167 // Angle between current normal and direction to sample controls AO intensity.168 float cos_angle = dot(direction_to_sample, currentNormal);169 cos_angle = max(cos_angle, 0.0f);170 171 // distance between current position and sample position controls AO intensity.172 float distance_intensity =173 (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);174 175 // if normal perpenticular to view dir, only half of the samples count176 float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));177 178 total_color.w -= cos_angle * distance_intensity * view_correction;179 180 const float scale_factor = 0.3f;181 total_color.xyz += cos_angle * distance_intensity * view_correction * sample_color * scale_factor;182 }183 184 return saturate(total_color);185 107 } 186 108 … … 220 142 const float currentDepth = currentCol.w; 221 143 222 //float4 new_col = (float4)ssao(IN, positions, noiseTexture, samples, normal, viewDir, noiseMultiplier, centerPosition); 223 float4 new_col = globIllum(IN, colors, positions, noiseTexture, samples, normal, viewDir, noiseMultiplier, centerPosition); 224 144 const float ao = ssao(IN, positions, noiseTexture, samples, normal, viewDir, noiseMultiplier, centerPosition); 145 225 146 226 147 ///////////////// 227 //-- compute temporally smooth ed value148 //-- compute temporally smoothing 228 149 229 150 float4 realPos = centerPosition * maxDepth; … … 240 161 const float depthDif = 1.0f - newDepth / oldDepth; 241 162 163 242 164 if ((tex.x >= 0.0f) && (tex.x < 1.0f) && 243 165 (tex.y >= 0.0f) && (tex.y < 1.0f) && 244 (abs(depthDif) < 1e- 4f))166 (abs(depthDif) < 1e-3f)) 245 167 { 246 OUT.illum_col = new_col * expFactor + oldCol * float4(1.0f - expFactor);168 OUT.illum_col.x = ao * expFactor + oldCol.x * (1.0f - expFactor); 247 169 } 248 170 else 249 171 { 250 OUT.illum_col = new_col;172 OUT.illum_col.x = ao; 251 173 } 252 174 253 //const float ao =OUT.illum_col.x;254 const float ao = OUT.illum_col.w;175 OUT.combined_col = currentCol * OUT.illum_col.x; 176 OUT.illum_col.w = currentDepth; 255 177 256 //OUT.combined_col = (currentCol + OUT.illum_col) * ao; 257 OUT.combined_col = currentCol * ao; 258 259 OUT.illum_col.w = currentDepth; 178 260 179 261 180 return OUT; 262 181 } 263 264 /*265 pixel combined(fragment IN,266 uniform sampler2D colors,267 uniform sampler2D ssaoTex268 )269 {270 pixel OUT;271 272 float4 col = tex2D(colors, IN.texCoord.xy);273 float4 ao = tex2D(ssaoTex, IN.texCoord.xy);274 //float4 illum = tex2D(ssaoTex, IN.texCoord.xy);275 276 OUT.illum_col = col * ao;277 278 return OUT;279 }*/
Note: See TracChangeset
for help on using the changeset viewer.