2D Specular Lighting

Started by
0 comments, last by CSharpCoder 5 years, 4 months ago

I have written the following pixel shader for point lighting in a 2D platformer.
A normal map and a specularity map is generated using Sprite DLight.
colorMap is the original sprite, normalMap.rgb is the normal and normalMap.a is the specularity from the specularity map.
lightPosition has a z coordinate to position it further away from the scene.
Is this correct for 2D specular lighting?

 


float4 PointLightShader(VertexToPixel PSIn) : COLOR0
{
    float4 colorMap = tex2D(ColorMapSampler, PSIn.TexCoord);
    clip(colorMap.a - 0.001);

    float3 pixelPosition = float3(PSIn.WorldPos, 0);
    float3 lightDirection = lightPosition - pixelPosition;

    float coneAttenuation = saturate(1.0f - length(lightDirection) / lightDecay);

    float4 normalColor = tex2D(NormalMapSampler, PSIn.TexCoord);

    float3 normal = normalColor.rgb;
    float3 normalTangent = (2.0f * normal) - 1.0f;

    float3 lightDirNorm = normalize(lightDirection);
    float3 halfVec = float3(0, 0, 1);
		
    float amount = max(dot(normalTangent, lightDirNorm), 0);
				
    float3 reflect = normalize(2 * amount * normalTangent - lightDirNorm);
    float specular = min(pow(saturate(dot(reflect, halfVec)), 10), amount);
				
    return (colorMap * coneAttenuation * lightColor * lightStrength) + (colorMap * specular * coneAttenuation * normalColor.a);
}

 

This topic is closed to new replies.

Advertisement