我正在单人制作游戏。我正在使用一个正向渲染,我决定编写一个着色器,该着色器将使用Blinn-Phong阴影模型调用闪电。我已经实施了该模型,以使用三种类型的光线 - 方向光,点光和点光。此后,有时间在我们的游戏中添加阴影。我决定将阴影映射和更接近过滤技术的百分比使用。我已经实施了它,但是不幸的是,铸造点灯的阴影存在问题。我将场景从点灯的角度(六个ViewProcottionMatrices,每个面向一个方向)渲染到Cube地图,然后将其与当前渲染的对象进行比较。我面临两个问题:
-
在平面边缘附近有一些奇怪的人工制品。尽管被另一个表面覆盖,但有一个圆形的表面部分。一个问题的屏幕截图:点浅色阴影映射问题#1-注意圆形的浅伪像(这个灰色球是光的位置)
-
对于这种阴影的工作,我必须有某种光边界对象,该对象与点光最远的位置最远,在该点光线下,光线可以看到它。如果我不绘制这个对象,它将引起某种相反的阴影 - 我会在表面后面的另一个对象的位置看到表面上的光线。屏幕截图以更好地理解:点浅色阴影映射问题#2-没有光边界 - 场景没有变亮,形状可见的角色位于墙后(再次,灰色球是光的位置
这是我的HLSL代码(我将用简短的评论 " there"替换无关的代码部分,以指向灯):
#define MAX_DIRECTIONAL_LIGHTS 3
#define MAX_POINT_LIGHTS 4
#define MAX_SPOT_LIGHTS 4
matrix worldMatrix;
matrix viewProjectionMatrix;
matrix currentLightVievProjectionMatrix;
float4 currentLightPosition;
float4 cameraPosition;
texture diffuseTexture;
texture normalTexture;
texture specularTexture;
texture opacityTexture;
float4 globalAmbient;
//Directional Lights related variables here
int currentPointLightsNumber;
float4 pointLightPosition[MAX_POINT_LIGHTS];
float4 pointLightAmbientColor[MAX_POINT_LIGHTS];
float4 pointLightDiffuseColor[MAX_POINT_LIGHTS];
float4 pointLightSpecularColor[MAX_POINT_LIGHTS];
float pointLightRadius[MAX_POINT_LIGHTS];
float pointLightTexelSize[MAX_POINT_LIGHTS];
matrix pointLightViewProjection0;
matrix pointLightViewProjection1;
matrix pointLightViewProjection2;
matrix pointLightViewProjection3;
texture pointLightShadowMap0;
texture pointLightShadowMap1;
texture pointLightShadowMap2;
texture pointLightShadowMap3;
//Spot lights related variables here
float materialShininessFactor;
float DepthBias = float(0.0004F);
sampler2D DiffuseMapSampler = sampler_state
{
Texture = <diffuseTexture>;
MinFilter = Anisotropic;
MagFilter = Linear;
MipFilter = Linear;
AddressU = wrap;
AddressV = wrap;
MaxAnisotropy = 16;
};
sampler2D NormalMapSampler = sampler_state
{
Texture = <normalTexture>;
MinFilter = Anisotropic;
MagFilter = Linear;
MipFilter = Linear;
AddressU = wrap;
AddressV = wrap;
MaxAnisotropy = 4;
};
sampler2D SecularMapSampler = sampler_state
{
Texture = <specularTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = wrap;
AddressV = wrap;
};
sampler2D OpacityMapSampler = sampler_state
{
Texture = <opacityTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = wrap;
AddressV = wrap;
};
//Directional light shadow map samplers here
samplerCUBE PointLightShadowMapSampler0 = sampler_state
{
Texture = <pointLightShadowMap0>;
MinFilter = Point;
MagFilter = Point;
MipFilter = None;
AddressU = clamp;
AddressV = clamp;
};
samplerCUBE PointLightShadowMapSampler1 = sampler_state
{
Texture = <pointLightShadowMap1>;
MinFilter = Point;
MagFilter = Point;
MipFilter = None;
AddressU = clamp;
AddressV = clamp;
};
samplerCUBE PointLightShadowMapSampler2 = sampler_state
{
Texture = <pointLightShadowMap2>;
MinFilter = Point;
MagFilter = Point;
MipFilter = None;
AddressU = clamp;
AddressV = clamp;
};
samplerCUBE PointLightShadowMapSampler3 = sampler_state
{
Texture = <pointLightShadowMap3>;
MinFilter = Point;
MagFilter = Point;
MipFilter = None;
AddressU = clamp;
AddressV = clamp;
};
//Spot light shadow map samplers here
struct BlinnPhongVertexShaderInput
{
float4 position : POSITION;
float2 textureCoordinates : TEXCOORD;
float3 normal : NORMAL;
float3 tangent : TANGENT;
float3 binormal : BINORMAL;
};
struct BlinnPhongPixelShaderInput
{
float4 position : SV_POSITION;
float4 worldPosition : TEXCOORD0;
float2 textureCoordinates : TEXCOORD1;
float4 viewDirection : TEXCOORD2;
float3 normal : TEXCOORD3;
float3 tangent : TANGENT;
float3 binormal : BINORMAL;
};
struct CreateShadowMapPixelShaderInput
{
float4 Position : POSITION;
float Depth : TEXCOORD0;
};
//Vertex shader for directional and spot lights here
CreateShadowMapPixelShaderInput CreateShadowMapForPointLightVertexShaderFunction(float4 Position : POSITION)
{
CreateShadowMapPixelShaderInput OUT;
OUT.Position = mul(Position, worldMatrix);
OUT.Depth = length(OUT.Position.xyz - currentLightPosition.xyz);
OUT.Position = mul(OUT.Position, currentLightVievProjectionMatrix);
return OUT;
}
float4 CreateShadowMapPixelShaderFunction(CreateShadowMapPixelShaderInput input) : COLOR
{
return float4(input.Depth, 0.0F, 0.0F, 0.0F);
}
BlinnPhongPixelShaderInput BlinnPhongVertexShaderFunction(BlinnPhongVertexShaderInput input)
{
BlinnPhongPixelShaderInput output;
float4 worldPosition = mul(input.position, worldMatrix);
output.position = mul(worldPosition, viewProjectionMatrix);
output.worldPosition = worldPosition;
output.textureCoordinates = input.textureCoordinates;
output.viewDirection = cameraPosition - output.worldPosition;
output.normal = mul(input.normal, (float3x3)worldMatrix);
output.tangent = mul(input.tangent, (float3x3)worldMatrix);
output.binormal = mul(input.binormal, (float3x3)worldMatrix);
return output;
}
//ShadowMapLookups for directional and spot lights here
float PointLightShadowMapLookup(samplerCUBE shadowMap, float3 shadowTexCoord, float3 offset, float ourDepth, float texelSize)
{
return (texCUBE(shadowMap, shadowTexCoord + offset * texelSize).r < ourDepth) ? 0.1f : 1.0f;
}
float4 BlinnPhongPixelShaderFunction(BlinnPhongPixelShaderInput input) : COLOR0
{
float4 color = globalAmbient;
float4 specularColor = float4(0.0F, 0.0F, 0.0F, 0.0F);
float3 V = normalize(input.viewDirection.xyz);
float3 L;
float3 H;
float NDotL;
float NDotH;
float attenuation;
float power;
float4 normalMap = tex2D(NormalMapSampler, input.textureCoordinates);
normalMap = (normalMap * 2.0F) - 1.0F;
float3 N = normalize((normalMap.x * normalize(input.tangent)) + (normalMap.y * normalize(input.binormal)) + (normalMap.z * normalize(input.normal)));
float4 specularMap;
specularMap = tex2D(SecularMapSampler, input.textureCoordinates);
float4 lightingPosition;
float2 ShadowTexCoord;
float3 PointLightShadowTexCoord;
float ourdepth;
float shadowOcclusion;
//Directional lights lightning callculations here
for (int j = 0; j < currentPointLightsNumber; ++j)
{
L = (pointLightPosition[j].xyz - input.worldPosition.xyz) / pointLightRadius[j];
attenuation = saturate(1.0F - dot(L, L));
L = normalize(L);
H = normalize(L + V);
NDotL = saturate(dot(N, L));
NDotH = saturate(dot(N, H));
power = (NDotL == 0.0F) ? 0.0F : saturate(pow(NDotH, materialShininessFactor / specularMap.a));
ourdepth = length((pointLightPosition[j].xyz - input.worldPosition.xyz) * 0.98);
PointLightShadowTexCoord = -normalize(pointLightPosition[j].xyz - input.worldPosition.xyz);
shadowOcclusion = 0.0F;
if (j == 0)
{
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(0.0f, 0.0f, 0.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(1.0f, 0.0f, 0.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(2.0f, 0.0f, 0.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(0.0f, 1.0f, 0.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(1.0f, 1.0f, 0.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(2.0f, 1.0f, 0.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(0.0f, 2.0f, 0.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(1.0f, 2.0f, 0.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(2.0f, 2.0f, 0.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(0.0f, 0.0f, 1.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(1.0f, 0.0f, 1.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(2.0f, 0.0f, 1.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(0.0f, 1.0f, 1.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(1.0f, 1.0f, 1.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(2.0f, 1.0f, 1.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(0.0f, 2.0f, 1.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(1.0f, 2.0f, 1.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(2.0f, 2.0f, 1.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(0.0f, 0.0f, 2.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(1.0f, 0.0f, 2.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(2.0f, 0.0f, 2.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(0.0f, 1.0f, 2.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(1.0f, 1.0f, 2.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(2.0f, 1.0f, 2.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(0.0f, 2.0f, 2.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(1.0f, 2.0f, 2.0f), ourdepth, pointLightTexelSize[j]);
shadowOcclusion += PointLightShadowMapLookup(PointLightShadowMapSampler0, PointLightShadowTexCoord, float3(2.0f, 2.0f, 2.0f), ourdepth, pointLightTexelSize[j]);
}
else if (j == 1)
{
//Same code for second point light
}
else if (j == 2)
{
//Same code for third point light
}
else
{
//Same code for fourth point light
}
shadowOcclusion /= 27.0F;
color += (pointLightAmbientColor[j] * attenuation) + (pointLightDiffuseColor[j] * NDotL * attenuation * shadowOcclusion);
specularColor += (pointLightSpecularColor[j] * power * attenuation * specularMap * shadowOcclusion);
}
//Spot lights lightning callculations here
color = saturate(color * tex2D(DiffuseMapSampler, input.textureCoordinates) + specularColor);
color.a = (float1)tex2D(OpacityMapSampler, input.textureCoordinates);
return color;
}
//technique for directional and spot lights shadow mapping here
technique CreateShadowMapForPointLight
{
pass Pass1
{
VertexShader = compile vs_4_0 CreateShadowMapForPointLightVertexShaderFunction();
PixelShader = compile ps_4_0 CreateShadowMapPixelShaderFunction();
}
}
technique BlinnPhong
{
pass Pass1
{
VertexShader = compile vs_4_0 BlinnPhongVertexShaderFunction();
PixelShader = compile ps_4_0 BlinnPhongPixelShaderFunction();
}
}
我知道它看起来很糟糕,但是让我解释一下。我无法将灯标记矩阵存储在数组中,因为它们的值是在运行时的净更新,因此我不得不将它们分为每盏灯的单个矩阵。至于纹理,有一个警告说,着色器编译器正在强迫循环展开,所以我只想确保一切都会好起来,所以我也将它们分开。不知道矩阵问题是单身或hlsl的想法。
回到影子问题,我提出的HLSL是否有任何问题?根据可能的需求,我将为负责阴影地图渲染的壁板提供代码。
这是我的立方体地图和vievprocodment矩阵的样子:
RenderTargetCube ShadowMapRenderTarget = new RenderTargetCube(GameObject.Scene.SceneManager.GameEngine.GraphicsDevice,
1024,
false,
SurfaceFormat.Single,
DepthFormat.Depth24);
ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver2, 1.0F, 0.1F, Radius) * Matrix.CreateScale(-1, 1, 1);
public void CreateViewMatrix(Vector3 targetVector)
{
Vector3 upVector;
if (targetVector.Y > 0)
{
upVector = Vector3.Forward;
}
else if (targetVector.Y < 0)
{
upVector = Vector3.Backward;
}
else
{
upVector = Vector3.Up;
}
ViewMatrix = Matrix.CreateLookAt(GameObject.Transform.Position,GameObject.Transform.Position + targetVector, upVector);
}
public override void CreateViewProjectionMatrix()
{
ViewProjectionFrustum.Matrix = ViewMatrix * ProjectionMatrix;
}
在拨打电话中:
foreach (PointLight pointLight in PointLights)
{
foreach (CubeMapFace cubeMapFace in Enum.GetValues(typeof(CubeMapFace)))
{
pointLight.CreateViewMatrix(cubeMapFace.GetDirection());
pointLight.CreateViewProjectionMatrix();
SceneManager.GameEngine.GraphicsDevice.SetRenderTarget(pointLight.ShadowMapRenderTarget, cubeMapFace);
SceneManager.GameEngine.GraphicsDevice.Clear(Color.White);
foreach (DrawShadowMapDelegateType DrawComponent in ComponentsDrawShadowMapForPointLightMethods)
{
DrawComponent(pointLight);
}
}
}
是否有一种简单的方法或简单的解释,为什么这种认为会发生?是否有方法可以修复它,或者我会被迫尝试实现双辣样映射?如果是这样,HLSL中是否有任何样本实现可成功地连接到单体或XNA?
预先感谢您的任何建议和您的时间。
在实现自己的阴影映射时遇到了第一个问题。原因是我计算顶点着色器中的深度,而不是碎片着色器,同时呈现阴影图。因此,如果您具有垂直于光源的多边形,则每个顶点将获得相同的深度。我通过使用不同的向量(世界碎片位置),然后在碎片着色器中设置深度来解决它。不是最好的解决方案,因为它是表现不好的做法。