DirectX11阴影映射问题 [英] DirectX11 Shadow Mapping Issues

查看:289
本文介绍了DirectX11阴影映射问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加定向阴影贴图到我的地形项目,但我遇到了几个问题。



本教程基本上遵循以下过程:创建灯光>基于灯光视图创建深度纹理> Render模型和应用阴影着色器。



我努力的主要问题是如何教程处理光。它基本上模拟一个位置并创建一个邻域和视图矩阵。这个问题似乎从光如何设置升级。对于一个简单的测试,我创建了一个飞机,并设置光方向直接向下,所以一切都应该点亮,但是,发生以下情况:





当生成地形时:




这里是我认为有用的几个区域的代码:



灯光设置

  mLight-> SetPosition(XMFLOAT3(10.0f,30.0f,-0.1f)); 
mLight-> SetLookAt(XMFLOAT3(-10.0f,0.0f,0.0f));
mLight-> GenerateOthoMatrix(40.0f,1.0f,50.0f);

Light GenerateOthoMatrix

  void GenerateOthoMatrix(float width,float nearClip,float farClip)
{
mOrthoMatrix = XMMatrixOrthographicLH(width,width,nearClip,farClip);
}

Light GenerateViewMatrix

  XMVECTOR up = XMVectorSet(0.0f,1.0f,0.0f,0.0f); 
XMVECTOR pos = XMLoadFloat3(& mPosition);
XMVECTOR la = XMLoadFloat3(& mLookAt);

mViewMatrix = XMMatrixLookAtLH(pos,la,up);

深度渲染pass

  mRenderTexture-> SetRenderTarget(mGraphicsDevice-> GetContext()); 
mRenderTexture-> ClearRenderTarget(mGraphicsDevice-> GetContext());

mLight-> GenerateViewMatrix();

mDepthShader.Info.worldMatrix = mTerrain-> GetWorldMatrix();
mDepthShader.Info.viewMatrix = mLight-> GetViewMatrix();
mDepthShader.Info.projMatrix = mLight-> GetOrthoMatrix();

mTerrain-> Render(mGraphicsDevice-> GetContext());
mDepthShader.Render(mGraphicsDevice-> GetContext(),mTerrain-> GetIndexCount());

mGraphicsDevice-> ResetBackBuffer();
mGraphicsDevice-> ResetViewport();

Shader Render调用只需将'info'设置映射到常量缓冲区,然后调用它们的相对顶点/着色器。



Terrain Render调用只是建立索引/顶点缓冲区和拓扑,为着色器DrawIndexed做好准备。



RenderTexture本质上是一个第二个视口,可以从

中呈现和获取深度纹理。

传递

  mTerrain-> Render(mGraphicsDevice-> GetContext 
mLight-> GenerateViewMatrix();

mShader.Info.lightProj = mLight-> GetOrthoMatrix();
mShader.Info.lightView = mLight-> GetViewMatrix();
mShader.Info.depthTex = mRenderTexture-> GetSRV();
mShader.Render(mGraphicsDevice-> GetContext(),mTerrain-> GetIndexCount());

深度顶点着色器

  cbuffer SPerFrameCB:register(b0)
{
matrix worldMatrix;
matrix viewMatrix;
matrix projMatrix;
};

struct VertexIn
{
float4 Pos:POSITION;
};

struct VertexOut
{
float4 Pos:SV_POSITION;
float4 DPos:TEXTURE0;
};

VertexOut main(VertexIn vin)
{
VertexOut vout;

vin.Pos.w = 1.0f;

vout.Pos = mul(vin.Pos,worldMatrix);
vout.Pos = mul(vout.Pos,viewMatrix);
vout.Pos = mul(vout.Pos,projMatrix);

vout.DPos = vout.Pos;

return vout;
}

深度像素着色器

  struct PixelIn 
{
float4 Pos:SV_POSITION;
float4 DPos:TEXTURE0;
};

float4 main(PixelIn pin):SV_Target

{
float depthVal = pin.DPos.z / pin.DPos.w;
float4 color = float4(depthVal,depthVal,depthVal,1.0f);

返回颜色;
}

阴影顶点着色器

  cbuffer SPerFrameCB:register(b0)
{
matrix worldMatrix;
matrix viewMatrix;
matrix projMatrix;
matrix lightViewMatrix;
matrix lightProjMatrix
};

struct VertexIn
{
float4 Pos:POSITION;
float2 TX:TEXCOORD0;
float3正常:NORMAL;
};

struct VertexOut
{
float4 Pos:SV_POSITION;
float2 Tex:TEXCOORD0;
float3正常:NORMAL;
float4 LightV:TEXCOORD1;
};

VertexOut main(VertexIn vin)
{
VertexOut vout;

vin.Pos.w = 1.0f;

float4 worldPos = mul(vin.Pos,worldMatrix);

vout.Pos = worldPos;
vout.Pos = mul(vout.Pos,viewMatrix);
vout.Pos = mul(vout.Pos,projMatrix);

vout.LightV = worldPos;
vout.LightV = mul(vout.LightV,lightViewMatrix);
vout.LightV = mul(vout.LightV,lightProjMatrix);

vout.Tex = vin.Tex;

vout.Normal = mul(vin.Normal,(float3x3)worldMatrix);
vout.Normal = normalize(vout.Normal);

return vout;
}

阴影像素着色器

  Texture2D shaderTexture; 

Texture2D lowerTex:register(t0);
Texture2D mediumTex:register(t1);
Texture2D higherTex:register(t2);
Texture2D depthTex:register(t3);

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

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

struct PixelIn
{
float4 Pos:SV_POSITION;
float2 Tex:TEXCOORD0;
float3正常:NORMAL;
float4 LightV:TEXCOORD1;
};

float4 main(PixelIn pin):SV_Target
{
float bias = 0.001f;
float3 lightDir = -lightDirection;
float4 color = ambientColour;

float2 projTexCoord;
projTexCoord.x = pin.LightV.x / pin.LightV.w / 2.0f + 0.5f;
projTexCoord.y = -pin.LightV.y / pin.LightV.w / 2.0f + 0.5f;

if((saturate(projTexCoord.x)== projTexCoord.x)&&(saturate(projTexCoord.y)== projTexCoord.y))
{
float depthVal = depthTex.Sample(SampleTypeClamp,projTexCoord).r;
float lightDepthVal = pin.LightV.z / pin.LightV.w;
lightDepthVal - = bias;

if(lightDepthVal< depthVal)
{
float lightIntensity = saturate(dot(pin.Normal,lightDir)

if(lightIntensity> 0.0f)
{
color + = diffuseColour * lightIntensity;
color = saturate(colour);
}
}
}

float4 lowerColour = lowerTex.Sample(SampleTypeWrap,pin.Tex);
float4 mediumColour = mediumTex.Sample(SampleTypeWrap,pin.Tex);
float4 higherColour = higherTex.Sample(SampleTypeWrap,pin.Tex);
float4 texColour;
float slope = 1.0f - pin.Normal.y,bVal;

if(slope< 0.4f)
{
bVal = slope / 0.4f;
texColour = lerp(lowerColour,mediumColour,bVal);
}

if(slope> = 0.4f&&& slope< 0.6f)
{
bVal =(slope - 0.4f)* 1.0f /(0.6f-0.4f))。
texColour = lerp(mediumColour,higherColour,bVal);
}

if(slope> = 0.6f)
{
texColour = higherColour;
}

color * = texColour;

返回颜色;
}

很抱歉大量的代码 - 我不是确定哪些部分将有助于最好地确定问题。如果任何人可以帮助,或提供阴影贴图资源,我将非常感谢。

解决方案

没有很多阴影映射资源,或者至少我找不到很多阴影映射资源。看起来你的深度着色器看起来不错。我注意到你的阴影着色器的差异。我已经经历了完全两个系列的DirectX 10& 11到rastertek。我会告诉你我的阴影着色器是什么样子的;然而我不记得他们是否已经从一个教训改变到另一个教训。


$ b

Shadow.vsh

pre> /////////////////////////////////////// //////////
//文件名:shadow.vsh
///////////////////////// //////////////////////////

/////////////
/ / GLOBALS //
/////////////
cbuffer MatrixBuffer
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
matrix lightViewMatrix;
matrix lightProjectionMatrix;
};

////////////////////////
//恒定缓冲区//
////// ////////////////
cbuffer LightBuffer2
{
float3 lightPosition;
float padding;
};

//////////////
// TYPEDFES //
struct VertexInputType
{
float4 position:POSITION ;
float2 tex:TEXCOORD0;
float3 normal:NORMAL;
};

struct PixelInputType
{
float4 position:SV_POSITION;
float2 tex:TEXCOORD0;
float3 normal:NORMAL;
float4 lightViewPosition:TEXCOORD1;
float3 lightPos:TEXCOORD2;
};

////////////////////////////////////////// ///////
//顶点着色器
//////////////////////////////// ////////////////////
PixelInputType ShadowVertexShader(VertexInputType input)
{
PixelInputType output;
float4 worldPosition;

//将位置矢量更改为适合矩阵计算的4个单位
input.position.w = 1.0f;

//计算顶点对世界的位置,视图和投影矩阵
output.position = mul(input.position,worldMatrix);
output.position = mul(output.position,viewMatrix);
output.position = mul(output.position,projectionMatrix);

//计算光源所查看的顶点的位置
output.lightViewPosition = mul(input.position,worldMatrix);
output.lightViewPosition = mul(output.lightViewPosition,lightViewMatrix);
output.lightViewPosition = mul(output.lightViewPosition,lightProjectionMatrix);

//存储像素着色器的纹理坐标
output.tex = input.tex;

//只计算世界矩阵的法线向量
output.normal = mul(input.normal,(float3x3)worldMatrix);

//规范化正常向量
output.normal = normalize(output.normal);

//计算世界顶点的位置
worldPosition = mul(input.position,worldMatrix);

//根据光的位置和顶点在世界中的位置确定光位置
output.lightPos = lightPosition.xyz - worldPosition.xyz;

//规范光位置向量
output.lightPos = normalize(output.lightPos);

返回输出;
} // ShadowVertexShader

Shadow.psh

  ///////////////////////////// ////////////////////// 
//文件名:shadow.ps
/////////////// //////////////////////////////////

//////// //////
//纹理//
Texture2D depthMapTexture:register(t0);

///////////////////
//样品状态//
///////// //////////
SamplerState SampleTypeClamp:register(s0);

//////////////
// TYPEDEFS //
//////////////
struct PixelInputType
{
float4 position:SV_POSITION;
float2 tex:TEXCOORD0;
float3 normal:NORMAL;
float4 lightViewPosition:TEXCOORD1;
float3 lightPos:TEXCOORD2;
};

////////////////////////////////////////// ///////
//像素着色器
//////////////////////////////// ////////////////////
float4 ShadowPixelShader(PixelInputType input):SV_TARGET
{
float bias;
float4 color;
float2 projectTexCoord;
float depthValue;
float lightDepthValue;
float lightIntensity;

//设置用于修正浮点精度问题的偏置值
bias = 0.001f;

//设置默认输出颜色为黑色(阴影)
color = float4(0.0f,0.0f,0.0f,1.0f);

//计算投影纹理坐标
projectTexCoord.x = input.lightViewPosition.x / input.lightViewPosition.w / 2.0f + 0.5f;
projectTexCoord.y = -input.lightViewPosition.y / input.lightViewPosition.w / 2.0f + 0.5f;

//确定投影的坐标是否在[0,1]范围内。如果那么那么这个像素在光的视图
if((saturate(projectTexCoord.x)== projectTexCoord.x)&&(saturate(projectTexCoord.y)== projectTexCoord.y))
{
//使用采样器在深度纹理中的阴影贴图深度值坐标位置
depthValue = depthMapTexture.Sample(SampleTypeClamp,projectTexCoord).r;

//计算光的深度
lightDepthValue = input.lightViewPosition.z / input.lightViewPosition.w;

//从LightDepthValue减去偏差
lightDepthValue = lightDepthValue - bias;

//比较阴影的深度地图值和光的深度以确定是阴影还是光这个像素
//如果光在对象前面光像素,如果不是那么阴影这个像素从一个对象(封堵器)是铸造一个阴影它
if(lightDepthValue< depthValue)
{
//计算光量这个像素
lightIntensity = saturate(dot(input.normal,input.lightPos));

//如果该像素被照亮,则将其设置为纯白色(非阴影)
if(lightIntensity> 0.0f)
{
//确定基于漫射颜色和光强度的最终漫射颜色
color = float4(1.0f,1.0f,1.0f,1.0f);
}
}
}

返回颜色;
} // ShadowPixelShader

同时确保您对应的.h& .cpp着色器文件匹配着色器中正确的输入和输出结构。在计算像素着色器中的光强度时,看起来您正在使用光照方向而不是光照位置。你确实有更多的纹理添加到你的版本的着色器比我,但我不认为这将导致这种情况下的区别。我无法查看您的整个解决方案,所以很难知道错误可能来自哪里。我只是希望他的帮助为您服务作为指导。


I'm attempting to add directional shadow mapping to my terrain project, but I'm encountering a few issues. For reference, I'm following the RasterTek shadows tutorial.

The tutorial essentially follows the process of: Create a light > Create a depth texture based on the lights view > Render models and apply shadow shader.

The main issue I'm struggling with is how the tutorial handles the light. It essentially simulates a position and creates an ortho and view matrix. The issue seems to be escalating from how the light is set up. For a simple test, I created a plane, and set the light direction directly down, so everything should be lit, however, the following happens:

And when terrain is generated:

Here is code from a few areas that I think would be useful:

Light set up

mLight->SetPosition(XMFLOAT3(10.0f, 30.0f, -0.1f));
mLight->SetLookAt(XMFLOAT3(-10.0f, 0.0f, 0.0f));
mLight->GenerateOthoMatrix(40.0f, 1.0f, 50.0f);

Light GenerateOthoMatrix

void GenerateOthoMatrix(float width, float nearClip, float farClip)
{
    mOrthoMatrix = XMMatrixOrthographicLH(width, width, nearClip, farClip);
}

Light GenerateViewMatrix

XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
XMVECTOR pos = XMLoadFloat3(&mPosition);
XMVECTOR la = XMLoadFloat3(&mLookAt);

mViewMatrix = XMMatrixLookAtLH(pos, la, up);

Depth Render Pass

mRenderTexture->SetRenderTarget(mGraphicsDevice->GetContext());
mRenderTexture->ClearRenderTarget(mGraphicsDevice->GetContext());

mLight->GenerateViewMatrix();

mDepthShader.Info.worldMatrix = mTerrain->GetWorldMatrix();
mDepthShader.Info.viewMatrix = mLight->GetViewMatrix();
mDepthShader.Info.projMatrix = mLight->GetOrthoMatrix();

mTerrain->Render(mGraphicsDevice->GetContext());
mDepthShader.Render(mGraphicsDevice->GetContext(), mTerrain->GetIndexCount());

mGraphicsDevice->ResetBackBuffer();
mGraphicsDevice->ResetViewport();

Shader Render calls simply map the 'info' settings to constant buffers and then call their relative vertex/pixel shaders.

Terrain Render calls just setup Index/Vertex buffers and topology, ready for the shader DrawIndexed.

RenderTexture is essentially a second viewport to render to and get a depth texture from

Main Render Pass

mTerrain->Render(mGraphicsDevice->GetContext());
mLight->GenerateViewMatrix();

mShader.Info.lightProj = mLight->GetOrthoMatrix();
mShader.Info.lightView = mLight->GetViewMatrix();
mShader.Info.depthTex = mRenderTexture->GetSRV();
mShader.Render(mGraphicsDevice->GetContext(), mTerrain->GetIndexCount());

Depth Vertex Shader

cbuffer SPerFrameCB : register(b0)
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projMatrix;
};

struct VertexIn
{
    float4 Pos    : POSITION;
};

struct VertexOut
{
    float4 Pos    : SV_POSITION;
    float4 DPos   : TEXTURE0;
};

VertexOut main(VertexIn vin)
{
    VertexOut vout;

    vin.Pos.w = 1.0f;

    vout.Pos = mul(vin.Pos, worldMatrix);
    vout.Pos = mul(vout.Pos, viewMatrix);
    vout.Pos = mul(vout.Pos, projMatrix);

    vout.DPos = vout.Pos;

    return vout;
}

Depth Pixel Shader

struct PixelIn
{
    float4 Pos  : SV_POSITION;
    float4 DPos : TEXTURE0;
};

float4 main(PixelIn pin) : SV_Target

{
    float depthVal = pin.DPos.z / pin.DPos.w;
    float4 colour = float4(depthVal, depthVal, depthVal, 1.0f);

    return colour;
}

Shadow Vertex Shader

cbuffer SPerFrameCB : register(b0)
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projMatrix;
    matrix lightViewMatrix;
    matrix lightProjMatrix;
};

struct VertexIn
{
    float4 Pos    : POSITION;
    float2 Tex    : TEXCOORD0;
    float3 Normal : NORMAL;
};

struct VertexOut
{
    float4 Pos    : SV_POSITION;
    float2 Tex    : TEXCOORD0;
    float3 Normal : NORMAL;
    float4 LightV : TEXCOORD1;
};

VertexOut main(VertexIn vin)
{
    VertexOut vout;

    vin.Pos.w = 1.0f;

    float4 worldPos = mul(vin.Pos, worldMatrix);

    vout.Pos = worldPos;
    vout.Pos = mul(vout.Pos, viewMatrix);
    vout.Pos = mul(vout.Pos, projMatrix);

    vout.LightV = worldPos;
    vout.LightV = mul(vout.LightV, lightViewMatrix);
    vout.LightV = mul(vout.LightV, lightProjMatrix);

    vout.Tex = vin.Tex;

    vout.Normal = mul(vin.Normal, (float3x3)worldMatrix);
    vout.Normal = normalize(vout.Normal);

    return vout;
}

Shadow Pixel Shader

Texture2D shaderTexture;

Texture2D lowerTex  : register(t0);
Texture2D mediumTex : register(t1);
Texture2D higherTex : register(t2);
Texture2D depthTex : register(t3);

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

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

struct PixelIn
{
    float4 Pos    : SV_POSITION;
    float2 Tex    : TEXCOORD0;
    float3 Normal : NORMAL;
    float4 LightV : TEXCOORD1;
};

float4 main(PixelIn pin) : SV_Target
{
    float bias = 0.001f;
    float3 lightDir = -lightDirection;
    float4 colour = ambientColour;

    float2 projTexCoord;
    projTexCoord.x =  pin.LightV.x / pin.LightV.w / 2.0f + 0.5f;
    projTexCoord.y = -pin.LightV.y / pin.LightV.w / 2.0f + 0.5f;

    if ((saturate(projTexCoord.x) == projTexCoord.x) && (saturate(projTexCoord.y) == projTexCoord.y))
    {
        float depthVal = depthTex.Sample(SampleTypeClamp, projTexCoord).r;
        float lightDepthVal = pin.LightV.z / pin.LightV.w;
        lightDepthVal -= bias;

        if (lightDepthVal < depthVal)
        {
            float lightIntensity = saturate(dot(pin.Normal, lightDir));

            if (lightIntensity > 0.0f)
            {
                colour += diffuseColour * lightIntensity;
                colour = saturate(colour);
            }
        }
    }

    float4 lowerColour = lowerTex.Sample(SampleTypeWrap, pin.Tex);
    float4 mediumColour = mediumTex.Sample(SampleTypeWrap, pin.Tex);
    float4 higherColour = higherTex.Sample(SampleTypeWrap, pin.Tex);
    float4 texColour;
    float slope = 1.0f - pin.Normal.y, bVal;

    if (slope < 0.4f)
    {
        bVal = slope / 0.4f;
        texColour = lerp(lowerColour, mediumColour, bVal);
    }

    if (slope >= 0.4f && slope < 0.6f)
    {
        bVal = (slope - 0.4f) * (1.0f / (0.6f - 0.4f));
        texColour = lerp(mediumColour, higherColour, bVal);
    }

    if (slope >= 0.6f)
    {
        texColour = higherColour;
    }

    colour *= texColour;

    return colour;
}

I'm very sorry for the large amounts of code - I'm not sure which sections would help best in identifying the issue. If anyone could help, or provide a shadow mapping resource I would be very grateful. There doesn't seem to be many shadow mapping resources, or at least I haven't been able to find many.

解决方案

It appears that your Depth Shaders looks good. I've noticed differences in your Shadow shaders though. I have went through and completely both series for DirectX 10 & 11 through rastertek. I'll show you what my shadow shaders look like; however I do not remember if they have been changed from one lesson to another. I'll post them here for you to compare to.

Shadow.vsh

/////////////////////////////////////////////////
// Filename: shadow.vsh
/////////////////////////////////////////////////

/////////////
// GLOBALS //
/////////////
cbuffer MatrixBuffer
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projectionMatrix;
    matrix lightViewMatrix;
    matrix lightProjectionMatrix;
};

//////////////////////
// CONSTANT BUFFERS //
//////////////////////
cbuffer LightBuffer2
{
    float3  lightPosition;
    float   padding;
};

//////////////
// TYPEDFES //
struct VertexInputType
{
    float4 position : POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : NORMAL;
};

struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : NORMAL;
    float4 lightViewPosition : TEXCOORD1;
    float3 lightPos : TEXCOORD2;
};

/////////////////////////////////////////////////
// Vertex Shader
/////////////////////////////////////////////////
PixelInputType ShadowVertexShader( VertexInputType input )
{
    PixelInputType output;
    float4 worldPosition;

    // Change The Position Vector To Be 4 Units For Proper Matrix Calculations
    input.position.w = 1.0f;

    // Calculate The Position Of The Vertex Against The World, View And Projection Matrices
    output.position = mul( input.position, worldMatrix );
    output.position = mul( output.position, viewMatrix );
    output.position = mul( output.position, projectionMatrix );

    // Calculate The Position Of The Vertex As Viewed By The Light Source
    output.lightViewPosition = mul( input.position, worldMatrix );
    output.lightViewPosition = mul( output.lightViewPosition, lightViewMatrix );
    output.lightViewPosition = mul( output.lightViewPosition, lightProjectionMatrix );

    // Store The Texture Coordinate For The Pixel Shader
    output.tex = input.tex;

    // Calculate The Normal Vector Against The World Matrix Only
    output.normal = mul( input.normal, (float3x3)worldMatrix );

    // Normalize The Normal Vector
    output.normal = normalize( output.normal );

    // Calculate The Position Of The Vertex In The World
    worldPosition = mul( input.position, worldMatrix );

    // Determine The Light Position Based On The Position Of The Light And The Position Of The Vertex In The World
    output.lightPos = lightPosition.xyz - worldPosition.xyz;

    // Normalize The Light Position Vector
    output.lightPos = normalize( output.lightPos );

    return output;
} // ShadowVertexShader

Shadow.psh

/////////////////////////////////////////////////
// Filename: shadow.ps
/////////////////////////////////////////////////

//////////////
// TEXTURES //
Texture2D depthMapTexture : register(t0);

///////////////////
// SAMPLE STATES //
///////////////////
SamplerState SampleTypeClamp : register(s0);

//////////////
// TYPEDEFS //
//////////////
struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : NORMAL;
    float4 lightViewPosition : TEXCOORD1;
    float3 lightPos : TEXCOORD2;
};

/////////////////////////////////////////////////
// Pixel Shader
/////////////////////////////////////////////////
float4 ShadowPixelShader( PixelInputType input ) : SV_TARGET
{
    float   bias;
    float4  color;
    float2  projectTexCoord;
    float   depthValue;
    float   lightDepthValue;
    float   lightIntensity;

    // Set The Bias Value For Fixing The Floating Point Precision Issues
    bias = 0.001f;

    // Set The Default Output Color To Be Black (Shadow)
    color = float4( 0.0f, 0.0f, 0.0f, 1.0f );

    // Calculate The Projected Texture Coordinates
    projectTexCoord.x =  input.lightViewPosition.x / input.lightViewPosition.w / 2.0f + 0.5f;
    projectTexCoord.y = -input.lightViewPosition.y / input.lightViewPosition.w / 2.0f + 0.5f;

    // Determine If The Projected Coordinates Are In The [0,1] Range. If So Then This Pixel Is In The View Of The Light
    if ( (saturate( projectTexCoord.x) == projectTexCoord.x) && (saturate(projectTexCoord.y) == projectTexCoord.y) ) 
    {
        // Sample The Shadow Map Depth Value From The Depth Texture Using The Sampler At The Projected Texture Coordinate Location
        depthValue = depthMapTexture.Sample( SampleTypeClamp, projectTexCoord).r;

        // Calculate The Depth Of The Light
        lightDepthValue = input.lightViewPosition.z / input.lightViewPosition.w;

        // Subtract The Bias From The LightDepthValue
        lightDepthValue = lightDepthValue - bias;

        // Compare The Depth Of The Shadow Map Value And The Depth Of The Light To Determine Whether To Shadow Or To Light This Pixel
        // If The Light Is In Front Of The Object Then Light The Pixel, If Not Then Shadow This Pixel Since An Object (Occluder) Is Casting A Shadow On It
        if ( lightDepthValue < depthValue ) 
        {
            // Calculate The Amount Of Light On This Pixel
            lightIntensity = saturate( dot( input.normal, input.lightPos ) );

            // If This Pixel Is Illuminated Then Set It To Pure White (Non-Shadow)
            if ( lightIntensity > 0.0f )
            {
                // Determine The Final Diffuse Color Based On The Diffuse Color And The Amount Of Light Intensity
                color = float4( 1.0f, 1.0f, 1.0f, 1.0f );           
            }
        }
    }

    return color;
} // ShadowPixelShader

Also make sure that your corresponding .h & .cpp Shader files match the correct input and output structures that are in your shaders. It appears that you are using the light direction as opposed to the light position when calculating the light intensity in your pixel shader. You do have more textures added into your version of the shader than I but I don't think that would make the difference in this case. I do not have access to see your entire solution, so it is hard to tell where the error may be coming from. I just hope that his helps to serve you as a guide.

这篇关于DirectX11阴影映射问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆