Fix spot lights rendering on D3D12
This commit is contained in:
@@ -122,7 +122,7 @@ float4 GetLighting(float3 viewPos, LightData lightData, GBufferSample gBuffer, f
|
||||
// Calculate attenuation
|
||||
if (isRadial)
|
||||
{
|
||||
GetRadialLightAttenuation(lightData, isSpotLight, gBuffer.WorldPos, N, 1, lightData.Direction, L, NoL, distanceAttenuation, lightRadiusMask, spotAttenuation);
|
||||
GetRadialLightAttenuation(lightData, isSpotLight, gBuffer.WorldPos, N, 1, L, NoL, distanceAttenuation, lightRadiusMask, spotAttenuation);
|
||||
}
|
||||
float attenuation = distanceAttenuation * lightRadiusMask * spotAttenuation;
|
||||
|
||||
@@ -141,7 +141,7 @@ float4 GetLighting(float3 viewPos, LightData lightData, GBufferSample gBuffer, f
|
||||
// Calculate direct lighting
|
||||
LightingData lighting = SurfaceShading(gBuffer, energy, L, V, N);
|
||||
#if NO_SPECULAR
|
||||
lighting.Specular = 0;
|
||||
lighting.Specular = float3(0, 0, 0);
|
||||
#endif
|
||||
|
||||
// Calculate final light color
|
||||
|
||||
@@ -43,21 +43,6 @@ struct LightingData
|
||||
float3 Transmission;
|
||||
};
|
||||
|
||||
// Gets a radial attenuation factor for a point light.
|
||||
// WorldLightVector is the vector from the position being shaded to the light, divided by the radius of the light.
|
||||
float RadialAttenuation(float3 worldLightVector, half falloffExponent)
|
||||
{
|
||||
float t = dot(worldLightVector, worldLightVector);
|
||||
return pow(1.0f - saturate(t), falloffExponent);
|
||||
}
|
||||
|
||||
// Calculates attenuation for a spot light. Where L normalize vector to light.
|
||||
float GetSpotAttenuation(LightData lightData, float3 L)
|
||||
{
|
||||
// SpotAngles.x is CosOuterCone, SpotAngles.y is InvCosConeDifference
|
||||
return Square(saturate((dot(normalize(-L), lightData.Direction) - lightData.SpotAngles.x) * lightData.SpotAngles.y));
|
||||
}
|
||||
|
||||
// Calculates radial light (point or spot) attenuation factors (distance, spot and radius mask)
|
||||
void GetRadialLightAttenuation(
|
||||
LightData lightData,
|
||||
@@ -65,15 +50,13 @@ void GetRadialLightAttenuation(
|
||||
float3 worldPosition,
|
||||
float3 N,
|
||||
float distanceBiasSqr,
|
||||
inout float3 toLight,
|
||||
inout float3 L,
|
||||
inout float NoL,
|
||||
inout float distanceAttenuation,
|
||||
inout float lightRadiusMask,
|
||||
inout float spotAttenuation)
|
||||
{
|
||||
toLight = lightData.Position - worldPosition;
|
||||
|
||||
float3 toLight = lightData.Position - worldPosition;
|
||||
float distanceSqr = dot(toLight, toLight);
|
||||
L = toLight * rsqrt(distanceSqr);
|
||||
|
||||
@@ -102,13 +85,15 @@ void GetRadialLightAttenuation(
|
||||
{
|
||||
distanceAttenuation = 1;
|
||||
NoL = saturate(dot(N, L));
|
||||
|
||||
lightRadiusMask = RadialAttenuation(toLight * lightData.RadiusInv, lightData.FalloffExponent);
|
||||
float3 worldLightVector = toLight * lightData.RadiusInv;
|
||||
float t = dot(worldLightVector, worldLightVector);
|
||||
lightRadiusMask = pow(1.0f - saturate(t), lightData.FalloffExponent);
|
||||
}
|
||||
|
||||
if (isSpotLight)
|
||||
{
|
||||
spotAttenuation = GetSpotAttenuation(lightData, L);
|
||||
// SpotAngles.x is CosOuterCone, SpotAngles.y is InvCosConeDifference
|
||||
spotAttenuation = Square(saturate((dot(normalize(-L), lightData.Direction) - lightData.SpotAngles.x) * lightData.SpotAngles.y));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user