Ignore:
Timestamp:
02/26/09 18:39:04 (15 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r3328 r3329  
    203203                float2 offset; 
    204204 
    205                 const float2 ssaoOffset = tex2Dlod(samples, float4((0.5f + i + idx) / NUM_PRECOMPUTED_SAMPLES, 0.5f, .0f, .0f)).xy; 
    206                 //const float2 ssaoOffset = samples[i]; 
     205                const float2 ssaoOffset =  
     206                        tex2Dlod(samples, float4((0.5f + i + idx) / NUM_PRECOMPUTED_SAMPLES, 0.5f, .0f, .0f)).xy; 
    207207 
    208208                //////////////////// 
     
    234234                float3 dirSample = samplePos - centerPosition; 
    235235 
    236                 //const float minDist = 9e-1f; 
    237                 const float minDist = 1e-2f; 
    238  
    239                 //const float sqrLen = max(SqrLen(dirSample), minDist); 
    240                 //const float lengthToSample = sqrt(sqrLen); 
    241                 const float lengthToSample =  max(length(dirSample), minDist); 
    242  
    243                 dirSample /= lengthToSample; // normalize 
     236                const float minDist = 1e-6f; 
     237                const float scaleFactor = 1e-3f; 
     238 
     239                const float lengthToSample = length(dirSample); 
     240                const float sampleWeight = 1.0f / (lengthToSample + scaleFactor); 
     241 
     242                dirSample /= max(lengthToSample, minDist); // normalize 
     243 
    244244 
    245245                // angle between current normal and direction to sample controls AO intensity. 
    246                 float cosAngle = dot(dirSample, normal); 
     246                const float cosAngle = dot(dirSample, normal); 
    247247 
    248248                // the normal of the current sample 
    249                 //const float3 sampleNormal = normalize(tex2Dlod(normalTex, float4(texcoord, 0, 0)).xyz); 
    250                 const float3 sampleNormal = tex2Dlod(normalTex, float4(texcoord, 0, 0)).xyz; 
    251  
     249                const float3 sampleNormal = normalize(tex2Dlod(normalTex, float4(texcoord, 0, 0)).xyz); 
     250                 
    252251                // angle between current normal and direction to sample controls AO intensity. 
    253                 float cosAngle2 = .5f + dot(sampleNormal, -normal) * 0.5f; 
    254                 //float cosAngle2 = dot(-dirSample, sampleNormal); 
     252                const float cosAngle2 = dot(-dirSample, sampleNormal); 
    255253                 
    256                 //const float aoContrib = sampleIntensity / sqrLen; 
    257                 const float aoContrib = sampleIntensity / lengthToSample; 
     254                dirSample *= minDist; 
     255                const float aoContrib = sampleIntensity * sampleWeight; 
    258256 
    259257                //const float aoContrib = (1.0f > lengthToSample) ? occlusionPower(9e-2f, DISTANCE_SCALE + lengthToSample): .0f; 
     
    277275 
    278276                const float partlyResetThres = 1.0f; 
     277 
    279278                if (pixelValid <= partlyResetThres) 
    280279                        validSamples = max(validSamples, pixelValid * (1.0f - tooFarAway) * step(-0.1f, cosAngle)); 
     
    326325                        sampler2D colors, 
    327326                        sampler2D noiseTex, 
    328                         float2 samples[NUM_SAMPLES], 
     327                        sampler2D samples, 
    329328                        float3 normal, 
    330329                        float3 centerPosition, 
     
    337336                        float convergence, 
    338337                        float sampleIntensity, 
    339                         bool isMovingObject 
     338                        bool isMovingObject, 
     339                        float oldIdx 
    340340                        ) 
    341341{ 
     
    348348                float2 offset; 
    349349 
     350                const float2 ssaoOffset =  
     351                        tex2Dlod(samples, float4((0.5f + i + oldIdx) / NUM_PRECOMPUTED_SAMPLES, 0.5f, .0f, .0f)).xy; 
     352 
    350353                //////////////////// 
    351354                //-- add random noise: reflect around random normal vector  
     
    356359                        float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy; 
    357360                        //offset = myreflect(samples[i], mynoise); 
    358                         offset = myrotate(samples[i], mynoise.x); 
     361                        //offset = myrotate(samples[i], mynoise.x); 
     362                        offset = myrotate(ssaoOffset, mynoise.x); 
    359363                } 
    360364                else 
    361365                { 
    362                         offset = samples[i]; 
    363                 } 
    364                  
     366                        offset = ssaoOffset; 
     367                } 
     368 
     369 
    365370                // weight with projected coordinate to reach similar kernel size for near and far 
    366371                const float2 texcoord = IN.texCoord.xy + offset * scaleFactor; 
     
    375380                float3 dirSample = samplePos - centerPosition; 
    376381 
    377                 //const float minDist = 9e-1f; 
    378                 const float minDist = 1e-2f; 
    379  
    380                 //const float sqrLen = max(SqrLen(dirSample), minDist); 
    381                 //const float lengthToSample = sqrt(sqrLen); 
    382                 const float lengthToSample =  max(length(dirSample), minDist); 
    383  
    384                 dirSample /= lengthToSample; // normalize 
     382                const float minDist = 1e-6f; 
     383                const float scaleFactor = 1e-3f; 
     384 
     385                const float lengthToSample = length(dirSample); 
     386                const float sampleWeight = 1.0f / (lengthToSample + scaleFactor); 
     387 
     388                dirSample /= max(length(dirSample), minDist); // normalize 
    385389 
    386390                // angle between current normal and direction to sample controls AO intensity. 
    387                 float cosAngle = dot(dirSample, normal); 
     391                const float cosAngle = dot(dirSample, normal); 
    388392 
    389393                //const float aoContrib = sampleIntensity / sqrLen; 
    390                 const float aoContrib = sampleIntensity / lengthToSample; 
     394                const float aoContrib = sampleIntensity * sampleWeight; 
    391395                //const float aoContrib = (1.0f > lengthToSample) ? occlusionPower(9e-2f, DISTANCE_SCALE + lengthToSample): .0f; 
    392396 
    393                 total_ao += max(cosAngle, 0) * aoContrib; 
     397                total_ao += max(cosAngle, .0f) * aoContrib; 
    394398 
    395399                ++ numSamples; 
     
    427431                        // (=> there was no discontinuity recently) 
    428432                        //else if (isMovingObject && (convergence > SSAO_CONVERGENCE_THRESHOLD)) break; 
    429                         //else if (isMovingObject && (convergence > NUM_SAMPLES * 5)) break; 
     433                        else if (isMovingObject && (convergence > NUM_SAMPLES * 5)) break; 
    430434                } 
    431435#endif 
     
    513517         
    514518        float oldWeight = temporalVals.y; 
    515         float oldIdx = temporalVals.z; 
    516  
     519        float oldIdx = temporalCoherence > 1 ? temporalVals.z : 0; 
     520         
    517521        float3 ao; 
    518522 
     
    520524        if (eyeSpaceDepth < DEPTH_THRESHOLD) 
    521525        { 
    522                 if (0) 
    523                 { 
    524                         ao = ssao(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir), oldWeight, sampleIntensity, isMovingObject); 
     526                if (1) 
     527                { 
     528                        ao = ssao(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz,  
     529                                      scaleFactor, bl, br, tl, tr, normalize(viewDir), 
     530                                          oldWeight, sampleIntensity, isMovingObject, oldIdx); 
    525531                } 
    526532                else 
    527533                { 
    528534                        ao = ssao2(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor,  
    529                                    bl, br, tl, tr, normalize(viewDir), oldWeight, sampleIntensity,  
    530                                            isMovingObject, normals, oldIdx); 
     535                                  bl, br, tl, tr, normalize(viewDir), oldWeight, sampleIntensity,  
     536                                          isMovingObject, normals, oldIdx); 
    531537                } 
    532538        } 
Note: See TracChangeset for help on using the changeset viewer.