Ignore:
Timestamp:
07/13/08 23:51:02 (16 years ago)
Author:
mattausch
Message:

research version: implemented temporal smoothing and color bleeding

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg

    r2833 r2834  
    11//////////////////// 
    22// Screen Spaced Ambient Occlusion shader 
    3 // mainly based on Kustls shader 
    4  
    5 #define NUM_SAMPLES 8 
    6 //#define SAMPLE_INTENSITY 0.5f 
    7 //#define SAMPLE_INTENSITY 1.1f 
    8 #define SAMPLE_INTENSITY 0.25f 
     3// mainly based on shader of Alexander Kusternig 
     4 
     5//#define NUM_SAMPLES 8 
     6#define NUM_SAMPLES 16 
     7//#define SAMPLE_INTENSITY 0.125f 
     8// rule of thumb: approx 1 / NUM_SAMPLES 
     9#define SAMPLE_INTENSITY 0.2 //0.0625f 
     10 
    911#define AREA_SIZE 5e-1f 
    1012 
     
    3032  // reflect around plane 
    3133  float2 rpt = pt - d * 2.0f * n; 
    32    
     34 
    3335  //return pt; 
    3436  return rpt; 
     
    3840float2 rotate(float2 pt, float2 n) 
    3941{ 
    40   float2 ptTransformed; 
    41   ptTransformed.x = n.r * pt.x - n.g * pt.y; 
    42   ptTransformed.y = n.g * pt.x + n.r * pt.y; 
    43  
    44   return ptTransformed; 
    45 } 
    46  
    47  
    48 //based on kustls shader         
     42        float2 ptTransformed; 
     43        ptTransformed.x = n.r * pt.x - n.g * pt.y; 
     44        ptTransformed.y = n.g * pt.x + n.r * pt.y; 
     45 
     46        return ptTransformed; 
     47} 
     48 
     49 
    4950float ssao(fragment IN, 
    50            uniform sampler2D positions, 
    51            uniform sampler2D noiseTexture, 
    52            uniform float2 samples[NUM_SAMPLES], 
    53            uniform float3 currentNormal, 
    54            uniform float3 currentViewDir) 
    55 { 
    56   // the current world position 
    57   float4 centerPosition = tex2D(positions, IN.texCoord.xy); 
    58   // the w coordinate from the persp. projection 
    59   float w = centerPosition.w; 
    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 = 5e-1f; 
    68   //const float areaSize = 3e-1f; 
    69   //const float sampleIntensity = 0.2f; 
    70    
    71   for (int i = 0; i < NUM_SAMPLES; i ++) { 
    72     float2 offset = samples[i]; 
    73      
    74     //sample noisetex; r stores costheta, g stores sintheta 
    75     float2 noise = tex2D(noiseTexture, IN.texCoord.xy * 7.1f).xy * 2.0f - 1.0f; 
     51                   uniform sampler2D positions, 
     52                   uniform sampler2D noiseTexture, 
     53                   uniform float2 samples[NUM_SAMPLES], 
     54                   uniform float3 currentNormal, 
     55                   uniform float3 currentViewDir, 
     56                   uniform float noiseMultiplier, 
     57                   uniform float4 centerPosition 
     58                   ) 
     59{ 
     60        // the w coordinate from the persp. projection 
     61        float w = centerPosition.w; 
     62 
     63        // Check in a circular area around the current position. 
     64        // Shoot vectors to the positions there, and check the angle to these positions. 
     65        // Summing up these angles gives an estimation of the occlusion at the current position. 
     66 
     67        float total_ao = 0.0; 
     68 
     69        const float areaSize = 5e-1f; 
     70        //const float areaSize = 3e-1f; 
     71        //const float sampleIntensity = 0.2f; 
     72 
     73        for (int i = 0; i < NUM_SAMPLES; i ++)  
     74        { 
     75                float2 offset = samples[i]; 
     76 
     77                //sample noisetex; r stores costheta, g stores sintheta 
     78                float2 noise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f; 
     79 
     80                // rotation 
     81                //float2 offsetTransformed = offset; 
     82                float2 offsetTransformed = rotate(offset, noise); 
     83                //float2 offsetTransformed = reflect(offset, noise); 
     84 
     85                // weight with projected coordinate to reach similar kernel size for near and far 
     86                float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; 
     87 
     88                float3 sample_position = tex2D(positions, texcoord).xyz; 
     89 
     90                float3 vector_to_sample = sample_position - centerPosition.xyz; 
     91                float length_to_sample = length(vector_to_sample); 
     92                float3 direction_to_sample = vector_to_sample / length_to_sample; 
     93 
     94                // Angle between current normal and direction to sample controls AO intensity. 
     95                float cos_angle = dot(direction_to_sample, currentNormal); 
     96                cos_angle = max(cos_angle, 0.0f); 
     97                // take quadratic influence to sharpen contrast 
     98                //cos_angle *= cos_angle; 
     99 
     100                // distance between current position and sample position controls AO intensity. 
     101                //const float maxdist = 2e-1f; 
     102                //const float maxdist = 5e-1f; 
     103                const float distanceScale = 1e-6f; 
     104 
     105                float distance_intensity =  
     106                        (SAMPLE_INTENSITY * distanceScale) / (distanceScale + length_to_sample * length_to_sample); 
     107 
     108                // if normal perpenticular to view dir, only half of the samples count 
     109                float view_correction = (2.0f - dot(currentViewDir, currentNormal)); 
     110                view_correction *= view_correction; 
     111 
     112                total_ao += cos_angle * distance_intensity * view_correction; 
     113        } 
     114 
     115        return (1.0f - total_ao); 
     116} 
     117 
     118 
     119float4 globIllum(fragment IN, 
     120                                 uniform sampler2D colors, 
     121                                 uniform sampler2D positions, 
     122                                 uniform sampler2D noiseTexture, 
     123                                 uniform float2 samples[NUM_SAMPLES], 
     124                                 uniform float3 currentNormal, 
     125                                 uniform float3 currentViewDir, 
     126                                 uniform float noiseMultiplier, 
     127                                 uniform float4 centerPosition) 
     128{ 
     129        // the w coordinate from the persp. projection 
     130        float w = centerPosition.w; 
     131 
     132        // Check in a circular area around the current position. 
     133        // Shoot vectors to the positions there, and check the angle to these positions. 
     134        // Summing up these angles gives an estimation of the occlusion at the current position. 
     135 
     136        float total_ao = 0.0; 
     137        float3 total_color = float3(0.0f); 
     138 
     139        const float areaSize = 5e-1f; 
     140 
     141        for (int i = 0; i < NUM_SAMPLES; i ++)  
     142        { 
     143                float2 offset = samples[i]; 
     144 
     145                //sample noisetex; r stores costheta, g stores sintheta 
     146                float2 noise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f; 
     147 
     148                // rotation 
     149                float2 offsetTransformed = rotate(offset, noise); 
     150 
     151                // weight with projected coordinate to reach similar kernel size for near and far 
     152                float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; 
     153 
     154                float3 sample_position = tex2D(positions, texcoord).xyz; 
     155                float3 sample_color = tex2D(colors, texcoord).xyz; 
     156 
     157                float3 vector_to_sample = sample_position - centerPosition.xyz; 
     158                float length_to_sample = length(vector_to_sample); 
     159                float3 direction_to_sample = vector_to_sample / length_to_sample; 
     160 
     161                // Angle between current normal and direction to sample controls AO intensity. 
     162                float cos_angle = dot(direction_to_sample, currentNormal); 
     163                cos_angle = max(cos_angle, 0.0f); 
     164 
     165                // distance between current position and sample position controls AO intensity. 
     166                const float distanceScale = 1e-6f; 
     167 
     168                float distance_intensity =  
     169                        (SAMPLE_INTENSITY * distanceScale) / (distanceScale + length_to_sample * length_to_sample); 
     170 
     171                // if normal perpenticular to view dir, only half of the samples count 
     172                float view_correction = 1.0f + 0.5f * (1.0f - dot(currentViewDir, currentNormal)); 
     173                //view_correction *= view_correction; 
     174 
     175                total_ao += cos_angle * distance_intensity * view_correction; 
     176                total_color += cos_angle * distance_intensity * view_correction * sample_color; 
     177        } 
     178 
     179        return float4(total_color, 1.0f - total_ao); 
     180} 
     181 
     182 
     183float4 shade(fragment IN,  
     184                         uniform sampler2D colors, 
     185                         uniform sampler2D positions, 
     186                         uniform float3 normal, 
     187                         uniform float amb) 
     188{ 
     189        float4 lightDir = float4(0.8f, -1.0f, 0.7f, 0.0f); 
     190        float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f); 
     191 
     192        float4 color = tex2D(colors, IN.texCoord.xy); 
     193 
     194        float4 position = tex2D(positions, IN.texCoord.xy); 
     195 
     196        float4 ambient = 0.3f; 
     197 
     198        // float3 L = normalize(lightPosition - position); 
     199        float3 light = normalize(lightDir.xyz); 
     200        float3 light2 = normalize(lightDir2.xyz); 
     201 
     202        float diffuseLight = max(dot(normal, light), 0.0f); 
     203        float diffuseLight2 = max(dot(normal, light2), 0.0f); 
     204 
     205        float diffuse = diffuseLight + diffuseLight2; 
     206        //float diffuse = diffuseLight; 
     207 
     208        return (ambient + diffuse) * color * (1.0f - amb) + amb * color; 
     209} 
     210 
     211 
     212pixel main_ssao(fragment IN,  
     213                                uniform sampler2D colors, 
     214                                uniform sampler2D positions, 
     215                                uniform sampler2D normals, 
     216                                uniform sampler2D noiseTexture, 
     217                                uniform float2 samples[NUM_SAMPLES], 
     218                                uniform float noiseMultiplier, 
     219                                uniform sampler2D oldTex, 
     220                                const uniform float4x4 oldModelViewProj 
     221                                ) 
     222{ 
     223        pixel OUT; 
     224 
     225        float4 normal = tex2D(normals, IN.texCoord.xy); 
     226        float amb = normal.w; 
     227 
     228        // expand normal 
     229        normal = normalize(normal * 2.0f - 1.0f); 
     230        float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f)); 
     231 
     232        // the current world position 
     233        float4 centerPosition = tex2D(positions, IN.texCoord.xy); 
    76234         
    77     // rotation 
    78     //float2 offsetTransformed = offset; 
    79     float2 offsetTransformed = rotate(offset, noise); 
    80     //float2 offsetTransformed = reflect(offset, noise); 
    81                 
    82     // weight with projected coordinate to reach similar kernel size for near and far 
    83     float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; 
    84      
    85     float3 sample_position = tex2D(positions, texcoord).xyz; 
    86      
    87     float3 vector_to_sample = sample_position - centerPosition.xyz; 
    88     float length_to_sample = length(vector_to_sample); 
    89     float3 direction_to_sample = vector_to_sample / length_to_sample; 
    90      
    91     // Angle between current normal and direction to sample controls AO intensity. 
    92     float cos_angle = dot(direction_to_sample, currentNormal); 
    93     cos_angle = max(cos_angle, 0.0f); 
    94     // take quadratic influence to sharpen contrast 
    95     //cos_angle *= cos_angle; 
    96  
    97     // distance between current position and sample position controls AO intensity. 
    98     //const float maxdist = 2e-1f; 
    99     //const float maxdist = 5e-1f; 
    100     const float distanceScale = 1e-6f; 
    101  
    102     float distance_intensity =  
    103       (SAMPLE_INTENSITY * distanceScale) / (distanceScale + length_to_sample * length_to_sample); 
    104  
    105     // if normal perpenticular to view dir, only half of the samples count 
    106     float view_correction = (2.0f - dot(currentViewDir, currentNormal)); 
    107  
    108     total_ao += cos_angle * distance_intensity * view_correction; 
    109   } 
    110          
    111   return (1.0f - total_ao); 
    112 } 
    113  
    114  
    115 float4 shade(fragment IN,  
    116              uniform sampler2D colors, 
    117              uniform sampler2D positions, 
    118              uniform float3 normal, 
    119              uniform float amb) 
    120 { 
    121   float4 lightDir = float4(0.8f, -1.0f, 0.7f, 0.0f); 
    122   float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f); 
    123    
    124   float4 color = tex2D(colors, IN.texCoord.xy); 
     235        float4 col = shade(IN, colors, positions, normal.xyz, amb); 
     236 
     237        //float ao = ssao(IN, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition); 
     238        float4 new_col = globIllum(IN, colors, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition); 
     239        float ao = new_col.w; 
     240 
     241        //float4 attenuated_color = ao * col + new_col; 
     242        float4 attenuated_color = ao * col; 
     243 
     244        //OUT.color = ao; 
     245        //OUT.color = ao * col;  
     246        float4 dummy = centerPosition / 0.000487676f; 
     247        dummy.w = 1.0f; 
     248        float4 oldPos = mul(oldModelViewProj, dummy); 
     249 
     250        float newDepth = oldPos.z / oldPos.w; 
    125251  
    126   float4 position = tex2D(positions, IN.texCoord.xy); 
    127  
    128   float4 ambient = 0.3f; 
    129  
    130   // float3 L = normalize(lightPosition - position); 
    131   float3 light = normalize(lightDir.xyz); 
    132   float3 light2 = normalize(lightDir2.xyz); 
    133   
    134   float diffuseLight = max(dot(normal, light), 0.0f); 
    135   float diffuseLight2 = max(dot(normal, light2), 0.0f); 
    136  
    137   //float diffuse = diffuseLight + diffuseLight2; 
    138   float diffuse = diffuseLight; 
    139  
    140   return (ambient + diffuse) * color * (1.0f - amb) + amb * color; 
    141 } 
    142  
    143  
    144 pixel main_ssao(fragment IN,  
    145                 uniform sampler2D colors, 
    146                 uniform sampler2D positions, 
    147                 uniform sampler2D normals, 
    148                 uniform sampler2D noiseTexture, 
    149                 uniform float2 samples[NUM_SAMPLES]) 
    150 { 
    151   pixel OUT; 
    152  
    153   float4 normal = tex2D(normals, IN.texCoord.xy); 
    154   float amb = normal.w; 
    155  
    156   // expand normal 
    157   normal = normalize(normal * 2.0f - 1.0f); 
    158   float3 viewDir = normalize(IN.view * 2.0f - float(1.0f)); 
    159  
    160   float4 col = shade(IN, colors, positions, normal, amb); 
    161   float ao = ssao(IN, positions, noiseTexture, samples, normal, viewDir); 
    162    
    163    //OUT.color = ao; 
    164    OUT.color = ao * col;  
    165  
    166   return OUT; 
     252        const float x = 0.05f; 
     253 
     254        float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f; 
     255        float4 col1 = tex2D(oldTex, tex); 
     256 
     257        float oldDepth = col1.w; 
     258 
     259        if ((tex.x >= 0) && (tex.x <= 1) && (tex.y >= 0) && (tex.y <= 1) && (abs(newDepth - oldDepth) < 5e-4f)) 
     260                OUT.color = attenuated_color * x + col1 * float4(1.0f - x); 
     261        else            OUT.color = attenuated_color;// * x + col1 * float4(1.0f - x); 
     262 
     263        //OUT.color = float4(centerPosition.w); 
     264        //OUT.color = float4(oldDepth); 
     265        //OUT.color = float4(newDepth); 
     266        //OUT.color = float4(abs(newDepth - oldDepth)); 
     267        OUT.color.w = tex2D(colors, IN.texCoord.xy).w; 
     268 
     269        return OUT; 
    167270} 
    168271 
    169272 
    170273pixel main(fragment IN,  
    171            uniform sampler2D colors, 
    172            uniform sampler2D positions, 
    173            uniform sampler2D normals) 
    174 { 
    175   pixel OUT; 
    176    
    177   float4 normal = tex2D(normals, IN.texCoord.xy); 
    178   float amb = normal.w; 
    179  
    180   //OUT.color.xyz = IN.view; 
    181   // expand normal 
    182   normal = normalize(normal * 2.0f - float4(1.0f)); 
    183    
    184   float4 col = shade(IN, colors, positions, normal.xyz, amb); 
    185   OUT.color = col; 
    186  
    187   return OUT; 
    188 } 
     274                   uniform sampler2D colors, 
     275                   uniform sampler2D positions, 
     276                   uniform sampler2D normals, 
     277                   uniform sampler2D oldTex 
     278                   ) 
     279{ 
     280        pixel OUT; 
     281 
     282        float4 normal = tex2D(normals, IN.texCoord.xy); 
     283        float amb = normal.w; 
     284 
     285        //OUT.color.xyz = IN.view; 
     286        // expand normal 
     287        normal = normalize(normal * 2.0f - float4(1.0f)); 
     288 
     289        float4 col = shade(IN, colors, positions, normal.xyz, amb); 
     290        //OUT.color = col; 
     291 
     292        float4 col1 = tex2D(oldTex, IN.texCoord.xy); 
     293 
     294        const float x = 0.1f; 
     295        OUT.color = col * x + col1 * float4(1.0f - x); 
     296 
     297        return OUT; 
     298} 
Note: See TracChangeset for help on using the changeset viewer.