Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualMedo3337

Posted 08 November 2013 - 05:44 PM

@Tispe: The code I posted is somehow close to the one that I use for soft particles, but of course the depth map is different as well as I'm comparing the pixel depth with the scene depth to determine if the pixel is in shadow

 

I tried using the code you mentioned, but it's still not working... what I see is that half of scene is darker instead of shadows.

 

Here is what I have done:

// Shadow map size: 1024 x 1024

VS_OUTPUT VS( VERTEX IN )
{
     VS_OUTPUT OUT;
     OUT = (VS_OUTPUT)0;
     OUT.Position  = mul(float4(IN.Position, 1.0f), WorldViewProjection);
     OUT.UV        = IN.UV;
     OUT.Normal    = mul(float4(IN.Normal, 0.0f), World).xyz;
     OUT.vTexCoord = mul( IN.Position, SMTexBias );
     OUT.ShadowZ = mul(float4(IN.Position, 1.0f), lightViewProj).z;
     return OUT;
}

float4 PS(VS_OUTPUT IN) : COLOR
{
    // Generate the 9 texture co-ordinates for a 3x3 PCF kernel
    float4 vTexCoords[9];
    // Texel size
    float fTexelSize = 1.0f / 1024.0f;

    // Generate the tecture co-ordinates for the specified depth-map size
    // 4 3 5
    // 1 0 2
    // 7 6 8
    vTexCoords[0] = IN.vTexCoord;
    vTexCoords[1] = IN.vTexCoord + float4( -fTexelSize, 0.0f, 0.0f, 0.0f );
    vTexCoords[2] = IN.vTexCoord + float4( fTexelSize, 0.0f, 0.0f, 0.0f );
    vTexCoords[3] = IN.vTexCoord + float4( 0.0f, -fTexelSize, 0.0f, 0.0f );
    vTexCoords[6] = IN.vTexCoord + float4( 0.0f, fTexelSize, 0.0f, 0.0f );
    vTexCoords[4] = IN.vTexCoord + float4( -fTexelSize, -fTexelSize, 0.0f, 0.0f );
    vTexCoords[5] = IN.vTexCoord + float4( fTexelSize, -fTexelSize, 0.0f, 0.0f );
    vTexCoords[7] = IN.vTexCoord + float4( -fTexelSize, fTexelSize, 0.0f, 0.0f );
    vTexCoords[8] = IN.vTexCoord + float4( fTexelSize, fTexelSize, 0.0f, 0.0f );
    // Sample each of them checking whether the pixel under test is shadowed or not
    float fShadowTerms[9];
    float fShadowTerm = 0.0f;
    for( int i = 0; i < 9; i++ )
    {
        float A = tex2Dproj( shadowMap, vTexCoords[i] ).r;
        float B = (IN.ShadowZ - 0.1f);
        // Texel is shadowed
        fShadowTerms[i] = A < B ? 0.0f : 1.0f;
        fShadowTerm += fShadowTerms[i];
    }
    // Get the average
    fShadowTerm /= 9.0f;
    return fShadowTerm;
}

#1Medo3337

Posted 08 November 2013 - 05:11 PM

@Tispe: The code I posted is somehow close to the one that I use for soft particles, but of course the depth map is different as well as I'm comparing the pixel depth with the scene depth to determine if the pixel is in shadow

 

I tried using the code you mentioned, but it's still not working... what I see is that half of scene is darker instead of shadows.

 

Here is what I have done:

// Shadow map size: 1024 x 1024

VS_OUTPUT VS( VERTEX IN )
{
     VS_OUTPUT OUT;
     OUT = (VS_OUTPUT)0;
     OUT.Position  = mul(float4(IN.Position, 1.0f), WorldViewProjection);
     OUT.UV        = IN.UV;
     OUT.worldPos  = mul(float4(IN.Position, 1.0f), World).xyz;
     OUT.Normal    = mul(float4(IN.Normal, 0.0f), World).xyz;
     OUT.vTexCoord = mul( IN.Position, SMTexBias );
     OUT.ShadowZ = mul(float4(IN.Position, 1.0f), lightViewProj).z;
     return OUT;
}

float4 PS(VS_OUTPUT IN) : COLOR
{
    // Generate the 9 texture co-ordinates for a 3x3 PCF kernel
    float4 vTexCoords[9];
    // Texel size
    float fTexelSize = 1.0f / 1024.0f;

    // Generate the tecture co-ordinates for the specified depth-map size
    // 4 3 5
    // 1 0 2
    // 7 6 8
    vTexCoords[0] = IN.vTexCoord;
    vTexCoords[1] = IN.vTexCoord + float4( -fTexelSize, 0.0f, 0.0f, 0.0f );
    vTexCoords[2] = IN.vTexCoord + float4( fTexelSize, 0.0f, 0.0f, 0.0f );
    vTexCoords[3] = IN.vTexCoord + float4( 0.0f, -fTexelSize, 0.0f, 0.0f );
    vTexCoords[6] = IN.vTexCoord + float4( 0.0f, fTexelSize, 0.0f, 0.0f );
    vTexCoords[4] = IN.vTexCoord + float4( -fTexelSize, -fTexelSize, 0.0f, 0.0f );
    vTexCoords[5] = IN.vTexCoord + float4( fTexelSize, -fTexelSize, 0.0f, 0.0f );
    vTexCoords[7] = IN.vTexCoord + float4( -fTexelSize, fTexelSize, 0.0f, 0.0f );
    vTexCoords[8] = IN.vTexCoord + float4( fTexelSize, fTexelSize, 0.0f, 0.0f );
    // Sample each of them checking whether the pixel under test is shadowed or not
    float fShadowTerms[9];
    float fShadowTerm = 0.0f;
    for( int i = 0; i < 9; i++ )
    {
        float A = tex2Dproj( shadowMap, vTexCoords[i] ).r;
        float B = (IN.ShadowZ - 0.1f);
        // Texel is shadowed
        fShadowTerms[i] = A < B ? 0.0f : 1.0f;
        fShadowTerm += fShadowTerms[i];
    }
    // Get the average
    fShadowTerm /= 9.0f;
    return fShadowTerm;
}

PARTNERS