Graphics Reference
In-Depth Information
// Calculate attenuation based on distance from the light source
float dist = length( L );
attenuation = max( 0, 1.0f - ( 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 * diffuseAlbedo;
// Calculate the specular term
float3 V = CameraPos - position;
float3 H = normalize( L + V );
float3 specular = pow( saturate( dot( normal, H ) ), specular-Power )
* LightColor * specularAlbedo.xyz * nDotL;
// Final value is the sum of the albedo and diffuse with attenuation applied
return ( diffuse + specular ) * attenuation;
}
// Lighting pixel shader
float4 PSMain( in float4 screenPos : SV_Position ) : SV_Target0
{
float3 normal;
float3 position;
float3 diffuseAlbedo;
float3 specularAlbedo;
float specularPower;
// Sample the G-Buffer properties from the textures
GetGBufferAttributes( screenPos.xy, normal, position, diffuseAlbedo,
specularAlbedo, specularPower );
fioat3 lighting = CalcLighting( normal, position, diffuseAlbedo,
specularAlbedo, specularPower );
return float4( lighting, 1.0f );
}
Listing 11.3. Lighting shader code.
Search WWH ::




Custom Search