Hi,
I'm trying to combine a point light and directional light, with in the end both diffuse and specular components.
But I can't figure out why the combination of my diffuse directional light and diffuse point light are not working together. In the attenuation range the light gets 'purple', in the center of the point light it looks OK (same as when disabling the directional light).
I've stripped the pixel shader to find the case, but no success yet.
Any help is really appreciated.
When I use the light sources individual from each other (directional/ point) I get expected results.
// Complete version
float4 PS_function(VS_OUTPUT input): COLOR0
{
float4 textureColor = tex2D(textureSampler, input.TexCoord);
float3 normal = normalize(input.Normal);
// SPECULAR
float3 viewdir = normalize(CameraPos - input.wPos);
float specPower = exp2(MatSpecPower * 13.0f);
/** DIRECTIONAL LIGHT - PER PIXEL (diffuse and specular) **/
float3 diffuseDir = 0.0f;
float3 specularDir = 0.0f;
for(int i=0;i<MaxDirectionalLights;i++)
{
diffuseDir += saturate(DirLightColInt[i] * dot(normal, DirLightDir[i]));
float3 lightdir = normalize(DirLightDir[i] - input.wPos);
float3 h = normalize(lightdir + viewdir);
specularDir += pow(saturate(dot(h, normal)), specPower);
}
/** POINT LIGHTS - PER PIXEL (diffuse and specular) **/
float3 lightDir = normalize(PointLightPos[0] - input.wPos);
// per pixel attenuation
float dist = length(PointLightPos[0] - input.wPos);
float att = saturate(1 - (dist - PointLightFPRange[0]) / (PointLightRange[0] - PointLightFPRange[0]));
att *= att;
// per pixel diffuse
float diffIntPoint = dot(normal, lightDir) * att;
float3 diffusePoint = diffIntPoint * PointLightColInt[0];
// specular; using blinn half angle
float3 h = normalize(lightDir + viewdir);
float specularPoint = pow(saturate(dot(h, normal)), specPower) * att;
/** FINAL PIXEL COLOR (specular only for point lights **/
return float4((saturate(AmbientColInt + (MatDiff * (diffusePoint + diffuseDir)) + (MatSpec * (specularDir + specularPoint)) + MatEmi) * textureColor.rgb), textureColor.a);
}
// Stripped version just to test diffuse, not OK
float4 PS_function(VS_OUTPUT input): COLOR0
{
float4 textureColor = tex2D(textureSampler, input.TexCoord);
float3 normal = normalize(input.Normal);
/** DIRECTIONAL LIGHT - PER PIXEL (diffuse and specular) **/
float3 diffuseDir = 0.0f;
for(int i=0;i<MaxDirectionalLights;i++)
{
diffuseDir += saturate(DirLightColInt[i] * dot(normal, DirLightDir[i]));
}
/** POINT LIGHTS - PER PIXEL (diffuse and specular) **/
float3 lightDir = normalize(PointLightPos[0] - input.wPos);
// per pixel attenuation
float dist = length(PointLightPos[0] - input.wPos);
float att = saturate(1 - (dist - PointLightFPRange[0]) / (PointLightRange[0] - PointLightFPRange[0]));
att *= att;
// per pixel diffuse
float diffIntPoint = dot(normal, lightDir) * att;
float3 diffusePoint = diffIntPoint * PointLightColInt[0];
/** FINAL PIXEL COLOR (specular only for point lights **/
return float4((saturate((diffusePoint + diffuseDir)) * textureColor.rgb), textureColor.a);
}