Graphics Reference
In-Depth Information
4.
As we will be supporting multiple light types with multiple pixel shaders we will split
our light calculations into the following two functions. The first calculates the final
light contribution from the lighting inputs and G-Buffer attributes. The second will
prepare the light inputs: to eye vector, to light vector, half vector for Blinn-Phong,
distance, and the light attenuation factor:
// Basic Lambert and BlinnPhong light contribution
float3 LightContribution(GBufferAttributes attrs, float3 V,
float3 L, float3 H, float3 D, float attenuation)
{
float NdotL = saturate(dot(attrs.Normal, L));
if (NdotL <= 0)
discard; // discard as no impact
float NdotH = saturate(dot(attrs.Normal, H));
// Lambert diffuse
float3 diffuse = NdotL * LightParams.Color *
attrs.Diffuse;
// BlinnPhong specular term
float specPower = max(attrs.SpecularPower,0.00001f);
float3 specular = pow(NdotH, specPower) *
attrs.SpecularInt * LightParams.Color;
return (diffuse + specular) * attenuation +
attrs.Emissive;
}
// Prepares the LightContribution inputs
void PrepareLightInputs(in float3 camera,
in float3 position, in float3 N, in LightStruct light,
out float3 V, out float3 L, out float3 H, out float D,
out float attenuation)
{
V = camera - position;
L = light.Position - position;
D = length(L);
L /= D;
H = normalize(L + V);
// Simple light attenuation
attenuation = max(1-D/light.Range, 0);
attenuation *= attenuation;
}
 
Search WWH ::




Custom Search