Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: softShadow.hlsl
- ===================================================================
- --- softShadow.hlsl (revision 47328)
- +++ softShadow.hlsl (working copy)
- @@ -35,6 +35,7 @@
- #else
- #define NUM_PRE_TAPS 5
- +#define NUM_TAPS 5
- /// The non-uniform poisson disk used in the
- /// high quality shadow filtering.
- @@ -69,18 +70,44 @@
- float2 tap = 0;
- for ( int t = startTap; t < endTap; t++ )
- {
- - tap.x = ( sNonUniformTaps[t].x * sinCos.y - sNonUniformTaps[t].y * sinCos.x ) * filterRadius;
- - tap.y = ( sNonUniformTaps[t].y * sinCos.y + sNonUniformTaps[t].x * sinCos.x ) * filterRadius;
- - float occluder = tex2Dlod( shadowMap, float4( shadowPos + tap, 0, 0 ) ).r;
- + tap.x = ( sNonUniformTaps[t].x * sinCos.y - sNonUniformTaps[t].y * sinCos.x ) * filterRadius * 0.5;
- + tap.y = ( sNonUniformTaps[t].y * sinCos.y + sNonUniformTaps[t].x * sinCos.x ) * filterRadius * 0.5;
- + float depth = tex2Dlod( shadowMap, float4( shadowPos + tap, 0, 0 ) ).r;
- - float esm = saturate( exp( esmFactor * ( occluder - distToLight ) ) );
- - shadow += esm / float( endTap - startTap );
- + shadow += saturate( 2.0 - exp( ( distToLight - depth ) * esmFactor ) );
- }
- return shadow;
- }
- +float pcfFilter(sampler2D shadowMap, float2 uv, float filterWidth, float distToLight, int numSamples, float esmFactor)
- +{
- + // Compute step size for iterating through the kernel.
- + float stepSize = 2 * filterWidth / numSamples;
- + // Compute uv coordinates for upper left corner of the kernel.
- + uv = uv - float2(filterWidth,filterWidth);
- +
- + // Iterate through the kernel and add the shadow samples.
- + float shadow = 0;
- + for (int i=0; i<numSamples; i++)
- + {
- + float tx = uv.x + (i*stepSize);
- +
- + for (int j=0; j<numSamples; j++)
- + {
- + // Get depth at this tap.
- + float depth = tex2Dlod(shadowMap, float4(tx, uv.y + j*stepSize, 0, 0)).r;
- +
- + // We use an ESM filter to hide shadow acne and soften the results.
- + shadow += saturate( 2.0 - exp( ( distToLight - depth ) * esmFactor ) );
- + }
- + }
- +
- + // Return the average of the samples.
- + return shadow / (numSamples*numSamples);
- +}
- +
- float softShadow_filter( sampler2D shadowMap,
- float2 vpos,
- float2 shadowPos,
- @@ -89,6 +116,9 @@
- float dotNL,
- float esmFactor )
- {
- + return pcfFilter( shadowMap, shadowPos, filterRadius, distToLight, 9, esmFactor );
- +
- +
- #ifndef SOFTSHADOW
- // If softshadow is undefined then we skip any complex
- @@ -114,11 +144,16 @@
- // We live with only the pretap results if we don't
- // have high quality shadow filtering enabled.
- - #ifdef SOFTSHADOW_HIGH_QUALITY
- + #ifndef SOFTSHADOW_HIGH_QUALITY
- + shadow /= (float)NUM_PRE_TAPS;
- +
- + #else
- +
- // Only do the expensive filtering if we're really
- // in a partially shadowed area.
- - if ( shadow * ( 1.0 - shadow ) * max( dotNL, 0 ) > 0.06 )
- + float preshadow = shadow / (float)NUM_PRE_TAPS;
- + if ( preshadow * ( 1.0 - preshadow ) * max( dotNL, 0 ) > 0.06 )
- {
- shadow += softShadow_sampleTaps( shadowMap,
- sinCos,
- @@ -131,8 +166,10 @@
- // This averages the taps above with the results
- // of the prediction samples.
- - shadow *= 0.5;
- + shadow /= (float)NUM_TAPS;
- }
- + else
- + shadow = preshadow;
- #endif // SOFTSHADOW_HIGH_QUALITY
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement