0
由於標題表示在渲染具有多個點光源的場景時計算最終像素顏色時存在一些問題。當我計算一個燈光時,場景看起來很好,但是當我計算多個燈光時,它似乎有點古怪!下面的圖片顯示了對角線模式下的五個點光源(僅用於可視化調試)。使用多個點光源計算最終像素顏色的問題DX11
正如我上面提到,下面張貼如下所示的像素着色器的 http://imgur.com/9LXVz79
HLSL代碼時,我計算一個點光源它工作得很好。
//================
// PIXEL SHADER
//================
struct PointLight
{
float3 lightPosition;
float range;
float3 att;
float padding;
float4 ambient;
float4 diffuse;
};
cbuffer CB_PER_FRAME_LIGHT : register(b2)
{
PointLight lights[5];
};
Texture2D tex;
SamplerState samplerState;
float4 PS(DS_OUTPUT input) : SV_Target
{
float4 finalDiffuse;
float3 finalColor = float3(0.0f, 0.0f, 0.0f);
float3 LIGHTSTRENGTH;
float4 ambient, diffuse;
for (int i = 0; i < 5; i++)
{
float3 lightWorldPosition = mul(lights[i].lightPosition, world).xyz;
ambient = float4(0.0f, 0.0f, 0.0f, 0.0f);
diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f);
LIGHTSTRENGTH = float3(0.0f, 0.0f, 0.0f);
finalDiffuse = tex.Sample(samplerState, input.texCoord);
//Create the vector between light position and pixels position
float3 lightToPixelVec = lights[i].lightPosition - input.worldPosition;
//Find the distance between the light pos and pixel pos
float d = length(lightToPixelVec);
//Create the ambient light
float3 finalAmbient = finalDiffuse * lights[i].ambient;
//If pixel is too far, return pixel color with ambient light
if(d > lights[i].range)
return float4(finalAmbient, finalDiffuse.a);
//Turn lightToPixelVec into a unit length vector describing
//the pixels direction from the lights position
lightToPixelVec /= d;
//Calculate how much light the pixel gets by the angle
//in which the light strikes the pixels surface
float howMuchLight = dot(lightToPixelVec, input.normal);
//If light is striking the front side of the pixel
if(howMuchLight > 0.0f)
{
//Add light to the finalColor of the pixel
LIGHTSTRENGTH += howMuchLight * finalDiffuse * lights[i].diffuse;
//Calculate Light's Falloff factor
LIGHTSTRENGTH /= lights[i].att[0] + (lights[i].att[1] * d) + (lights[i].att[2] * (d*d));
}
//make sure the values are between 1 and 0, and add the ambient
finalColor += saturate(LIGHTSTRENGTH + finalAmbient);
}
//Return Final Color
return float4(finalColor, finalDiffuse.a);
}