Add smooth light brightness fade out at last 10% of ViewDistance (if used)
This commit is contained in:
@@ -18,11 +18,11 @@ void DirectionalLight::Draw(RenderContext& renderContext)
|
||||
{
|
||||
float brightness = Brightness;
|
||||
AdjustBrightness(renderContext.View, brightness);
|
||||
const Float3 position = GetPosition() - renderContext.View.Origin;
|
||||
Float3 position;
|
||||
if (Brightness > ZeroTolerance
|
||||
&& EnumHasAnyFlags(renderContext.View.Flags, ViewFlags::DirectionalLights)
|
||||
&& EnumHasAnyFlags(renderContext.View.Pass, DrawPass::GBuffer)
|
||||
&& (ViewDistance < ZeroTolerance || Float3::DistanceSquared(renderContext.View.Position, position) < ViewDistance * ViewDistance))
|
||||
&& CheckViewDistance(renderContext.View.Position, renderContext.View.Origin, position, brightness))
|
||||
{
|
||||
RenderDirectionalLightData data;
|
||||
data.Position = position;
|
||||
|
||||
@@ -29,7 +29,7 @@ public:
|
||||
float Brightness = 3.14f;
|
||||
|
||||
/// <summary>
|
||||
/// Controls light visibility range. The distance at which the light becomes completely faded. Use a value of 0 to always draw light.
|
||||
/// Controls light visibility range. The distance at which the light becomes completely faded (blend happens on the last 10% of that range). Use a value of 0 to always draw light.
|
||||
/// </summary>
|
||||
API_FIELD(Attributes="EditorOrder(35), Limit(0, float.MaxValue, 10.0f), EditorDisplay(\"Light\")")
|
||||
float ViewDistance = 0.0f;
|
||||
@@ -56,6 +56,19 @@ protected:
|
||||
// Adjust the light brightness used during rendering (called by light types inside SetupLightData callback)
|
||||
void AdjustBrightness(const RenderView& view, float& brightness) const;
|
||||
|
||||
FORCE_INLINE bool CheckViewDistance(const Float3& viewPosition, const Float3& viewOrigin, Float3& position, float& brightness) const
|
||||
{
|
||||
position = _transform.Translation - viewOrigin;
|
||||
if (ViewDistance > ZeroTolerance)
|
||||
{
|
||||
const float dst2 = Vector3::DistanceSquared(viewPosition, position);
|
||||
const float dst = Math::Sqrt(dst2);
|
||||
brightness *= Math::Remap(dst, 0.9f * ViewDistance, ViewDistance, 1.0f, 0.0f);
|
||||
return dst < ViewDistance;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public:
|
||||
// [Actor]
|
||||
void OnEnable() override;
|
||||
|
||||
@@ -82,13 +82,13 @@ void PointLight::Draw(RenderContext& renderContext)
|
||||
{
|
||||
float brightness = ComputeBrightness();
|
||||
AdjustBrightness(renderContext.View, brightness);
|
||||
const Float3 position = GetPosition() - renderContext.View.Origin;
|
||||
Float3 position;
|
||||
const float radius = GetScaledRadius();
|
||||
if (EnumHasAnyFlags(renderContext.View.Flags, ViewFlags::PointLights)
|
||||
&& EnumHasAnyFlags(renderContext.View.Pass, DrawPass::GBuffer)
|
||||
&& brightness > ZeroTolerance
|
||||
&& radius > ZeroTolerance
|
||||
&& (ViewDistance < ZeroTolerance || Vector3::DistanceSquared(renderContext.View.Position, position) < ViewDistance * ViewDistance))
|
||||
&& CheckViewDistance(renderContext.View.Position, renderContext.View.Origin, position, brightness))
|
||||
{
|
||||
RenderPointLightData data;
|
||||
data.Position = position;
|
||||
|
||||
@@ -130,7 +130,7 @@ void SpotLight::Draw(RenderContext& renderContext)
|
||||
{
|
||||
float brightness = ComputeBrightness();
|
||||
AdjustBrightness(renderContext.View, brightness);
|
||||
const Float3 position = GetPosition() - renderContext.View.Origin;
|
||||
Float3 position;
|
||||
const float radius = GetScaledRadius();
|
||||
const float outerConeAngle = GetOuterConeAngle();
|
||||
if (EnumHasAnyFlags(renderContext.View.Flags, ViewFlags::SpotLights)
|
||||
@@ -138,7 +138,7 @@ void SpotLight::Draw(RenderContext& renderContext)
|
||||
&& brightness > ZeroTolerance
|
||||
&& radius > ZeroTolerance
|
||||
&& outerConeAngle > ZeroTolerance
|
||||
&& (ViewDistance < ZeroTolerance || Vector3::DistanceSquared(renderContext.View.Position, position) < ViewDistance * ViewDistance))
|
||||
&& CheckViewDistance(renderContext.View.Position, renderContext.View.Origin, position, brightness))
|
||||
{
|
||||
RenderSpotLightData data;
|
||||
data.Position = position;
|
||||
|
||||
Reference in New Issue
Block a user