Hi all,
I'm working on some area light (forward rendering).
I searched on the net for some documentation and tried with what I found.
Here the actual code :
void Area( in SURFACE_DATA SurfaceData, in AREA_LIGHT Light, inout LIGHTING_RESULTS Result )
{
float3 ToLightPosition = SurfaceData.Position - Light.Position;
float DistOnLine = dot( ToLightPosition, Light.Direction );
float3 PointOnLine = SurfaceData.Position - Light.Direction * DistOnLine;
float3 FromCenter = PointOnLine - Light.Position;
float DiagonalX = dot( FromCenter, Light.Right );
float DiagonalY = dot( FromCenter, Light.Up );
float NearestX = clamp( DiagonalX, -Light.HalfSize.x, Light.HalfSize.x );
float NearestY = clamp( DiagonalY, -Light.HalfSize.y, Light.HalfSize.y );
float3 NearestInside = Light.Position + ( Light.Right*NearestX + Light.Up*NearestY );
float3 LightDirection = NearestInside - SurfaceData.Position;
float d = length( LightDirection );
if( d < Light.EndAttenuation )
{
LightDirection /= d;
float NdotL = max( 0.0f, dot( SurfaceData.Normal, LightDirection ) );
if( NdotL > 0.0f )
{
// Compute the specular factor.
float SpecFactor = ComputePhongFactor( SurfaceData, LightDirection );
// Compute the attenuation factor.
float Attenuation = smoothstep( Light.EndAttenuation, Light.StartAttenuation, d );
// Accumulate the light results.
Result.Diffuse += Light.Intensity * DiffuseColor.rgb * Light.Color * Attenuation;
Result.Specular += Light.Intensity * SpecularColor.rgb * SpecFactor * Attenuation;
}
}
}