Graphics Reference
In-Depth Information
{
// Calculate the diffuse term
float3 L = 0 ;
float attenuation = 1.0f;
#if POINTLIGHT || SPOTLIGHT
// Base the the light vector on the light position
L = LightPos - position;
// Calculate attenuation based on distance from the light source
float dist = length ( L );
attenuation = max( 0, l.ef - ( dist / LightRange.x ) );
L /= dist;
#elif DIRECTIONALLIGHT
// Light direction is explicit for directional lights
L = -LightDirection;
#endif
#if SPOTLIGHT
// Also add in the spotlight attenuation factor
float3 L2 = LightDirection;
float rho = dot( -L, L2 );
attenuation *= saturate( ( rho - SpotlightAngles.y )
/ ( SpotlightAngles.x - SpotlightAngles.y ) );
#endif
float nDotL = saturate( dot( normal, L ) );
float3 diffuse = nDotL * LightColor * attenuation;
// Calculate the specular term
float3 V = CameraPos - position;
float3 H = normalize( L + V );
float specular = pow( saturate( dot( normal, H ) ), specularPower )
* attenuation * nDotL;
// Final value is diffuse RGB + mono specular
return float4( diffuse, specular );
}
// Pixel shader for lighting pass of light prepass deferred rendering
float4 PSMain( in float4 screenPos : SV_Position ) : SV_Target0
{
float3 normal;
float3 position;
float specularPower;
// Get the G-Buffer values
GetGBufferAttributes( screenPos.xy, normal, position, specularPower );
return CalcLighting( normal, position, specularPower );
}
Listing 1 1.5. Light buffer generation pixel shader code.
Search WWH ::




Custom Search