Files
UnrealEngineUWP/Engine/Shaders/DynamicLightingCommon.usf
Ben Marsh 149375b14b Update copyright notices to 2015.
[CL 2379638 by Ben Marsh in Main branch]
2014-12-07 19:09:38 -05:00

94 lines
3.6 KiB
Plaintext

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
/*=============================================================================
DynamicLightingCommon.usf: Contains functions shared by dynamic light shaders.
=============================================================================*/
/**
* Returns 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 NormalizeDistanceSquared = dot(WorldLightVector, WorldLightVector);
// UE3 (fast, but now we not use the default of 2 which looks quite bad):
return pow(1.0f - saturate(NormalizeDistanceSquared), FalloffExponent);
// new UE4 (more physically correct but slower and has a more noticable cutoff ring in the dark):
// AttenFunc(x) = 1 / (x * x + 1)
// derived: InvAttenFunc(y) = sqrtf(1 / y - 1)
// FalloffExponent is ignored
// the following code is a normalized (scaled and biased f(0)=1 f(1)=0) and optimized
/*
// light less than x % is considered 0
// 20% produces a bright sphere, 5 % is ok for performance, 8% looks close to the old one, smaller numbers would be more realistic but then the attenuation radius also should be increased.
// we can expose CutoffPercentage later, alternatively we also can compute the attenuation radius from the CutoffPercentage and the brightness
const float CutoffPercentage = 5.0f;
float CutoffFraction = CutoffPercentage * 0.01f;
// those could be computed on C++ side
float PreCompX = 1.0f - CutoffFraction;
float PreCompY = CutoffFraction;
float PreCompZ = CutoffFraction / PreCompX;
return (1 / ( NormalizeDistanceSquared * PreCompX + PreCompY) - 1) * PreCompZ;
*/
}
/**
* Calculates attenuation for a spot light.
* L normalize vector to light.
* SpotDirection is the direction of the spot light.
* SpotAngles.x is CosOuterCone, SpotAngles.y is InvCosConeDifference.
*/
float SpotAttenuation(float3 L, float3 SpotDirection, float2 SpotAngles)
{
float ConeAngleFalloff = Square(saturate((dot(L, -SpotDirection) - SpotAngles.x) * SpotAngles.y));
return ConeAngleFalloff;
}
/** Calculates radial and spot attenuation. */
float CalcLightAttenuation(float3 WorldPosition, out float3 WorldLightVector)
{
WorldLightVector = DeferredLightUniforms.NormalizedLightDirection;
float DistanceAttenuation = 1;
#if RADIAL_ATTENUATION
WorldLightVector = DeferredLightUniforms.LightPosition - WorldPosition;
float DistanceSqr = dot( WorldLightVector, WorldLightVector );
// TODO Line segment falloff
// Sphere falloff (technically just 1/d2 but this avoids inf)
DistanceAttenuation = 1 / ( DistanceSqr + 1 );
float LightRadiusMask = Square( saturate( 1 - Square( DistanceSqr * DeferredLightUniforms.LightInvRadius * DeferredLightUniforms.LightInvRadius ) ) );
DistanceAttenuation *= LightRadiusMask;
#if !INVERSE_SQUARED_FALLOFF
DistanceAttenuation = RadialAttenuation(WorldLightVector * DeferredLightUniforms.LightInvRadius, DeferredLightUniforms.LightFalloffExponent);
#endif
#endif
float SpotFalloff = 1;
#if RADIAL_ATTENUATION
SpotFalloff = SpotAttenuation( normalize(WorldLightVector), -DeferredLightUniforms.NormalizedLightDirection, DeferredLightUniforms.SpotAngles);
#endif
return SpotFalloff * DistanceAttenuation;
}
float3 GetNormalizedLightVector(float3 WorldPosition)
{
// assumed to be normalized
float3 Ret = DeferredLightUniforms.NormalizedLightDirection;
#if RADIAL_ATTENUATION
Ret = normalize(DeferredLightUniforms.LightPosition - WorldPosition);
#endif
return Ret;
}