1

I'm working on a graphics program in DirectX11 which takes a heightmap, manipulates a plane based on the heightmap, calculates the normals, and lights the scene with a directional light. I'm trying to get shadow mapping to work with the directional light, but whenever I do the depth test in the pixel shader it always returns 1, instead of the expected value.

Here's my main pixel shader

Texture2D t0 : register(t0);
Texture2D t1 : register(t1);

SamplerState s0 : register(s0);
SamplerState s1 : register(s1);

cbuffer LightBuffer : register(b0)
{
    float4 diffuseColour;
    float3 lightDirection;
    float padding;    
}

struct InputType
{
    float4 position : SV_POSITION;
    float4 tex: TEXCOORD0;
    float3 normal : normal;
    float4 lightViewPos : TEXCOORD1;
};

float4 calculateLighting(float3 lightDirection, float3 normal, float4 diffuse)
{
    float intensity = saturate(dot(normal, lightDirection));
    float4 colour = saturate(diffuse * intensity);
    return colour;
}

float4 main(InputType input) : SV_TARGET
{
    float depthValue;
    float lightDepthValue;
    float shadowMapBias = 0.005f;
    float4 colour = float4(0.f, 0.f, 0.f, 1);
    float4 textureColour = t0.Sample(s0, input.tex.xy);

    float2 pTexCoord = input.lightViewPos.xy / input.lightViewPos.w;
    pTexCoord *= float2(0.5, -0.5);
    pTexCoord += float2(0.5, 0.5);

    if (pTexCoord.x < 0.0f || pTexCoord.x > 1.0f ||
        pTexCoord.y < 0.0f || pTexCoord.y > 1.0f)
    {
        return textureColour;
    }

    depthValue = t1.Sample(s1, pTexCoord).r;

    lightDepthValue = input.lightViewPos.z / input.lightViewPos.w;
    lightDepthValue -= shadowMapBias;

    if (lightDepthValue < depthValue)
    {
        //colour = float4(1, 1, 1, 1);
        colour = calculateLighting(-lightDirection, input.normal, diffuseColour);
    }

    // This is a test to see what the depth value outputs as
    if (depthValue == 0)
        return float4(1, 0, 1, 1);
    else if (depthValue == 1)
        return float4(1, 1, 1, 1); // Always returns this
    else
        return float4(0, 1, 0, 1);
    //return float4(depthValue, depthValue, depthValue, 1.0f);
    // return saturate(colour * textureColour);
}

My depth vertex shader:

Texture2D t0 : register(t0);
SamplerState s0 : register(s0);

cbuffer MatrixBuffer : register(b0)
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projectionMatrix;
}

struct InputType
{
    float4 position : position;
    float2 tex : TEXCOORD0;
};

struct OutputType
{
    float4 position : SV_POSITION;
    float4 depthPosition : TEXCOORD0;
};

OutputType main(InputType input)
{
    OutputType output;
    output.position = mul(input.position, worldMatrix);
    if (t0.SampleLevel(s0, input.tex, 0).r > 0.05f)
        output.position.y += (t0.SampleLevel(s0, input.tex, 0).r * 50.0f);

    output.position = mul(output.position, viewMatrix);
    output.position = mul(output.position, projectionMatrix);

    output.depthPosition = output.position;

    return output;
}

And my depth pixel shader:

struct InputType
{
    float4 position : SV_POSITION;
    float4 depthPosition : TEXCOORD0;
};

float4 main(InputType input) : SV_Target
{
    float depthValue;
    depthValue = input.depthPosition.z / input.depthPosition.w;
    return float4(depthValue, depthValue, depthValue, 1.0f);
}

I know the depth data is being written to the render texture correctly as I have a preview of it on the screen current view + normals shown to demonstrate topography instead of a flat white plane.

halfer
  • 19,824
  • 17
  • 99
  • 186
Konomira
  • 13
  • 2

0 Answers0