Files
UnrealEngineUWP/Engine/Shaders/Private/LightFunctionPixelShader.usf
jon cain 40cb6a50ee Repurpose FauxOrtho resolving to compensate for the viewport being behind the camera location, causing artefacts.
Additionally, changing Ortho light calculations to use View.ViewForward instead of world position to camera position + correcting the distance as a result, leading to more accurate light ray angles and distance for ortho views. Non-Reflection passes only.

#jira UE-191798, FORT-641623
#rb Andrew.Lauritzen, Krzysztof.Narkowicz, Jason.Nadro

[CL 30014700 by jon cain in ue5-main branch]
2023-11-30 10:09:30 -05:00

75 lines
2.6 KiB
Plaintext

// Copyright Epic Games, Inc. All Rights Reserved.
/*=============================================================================
LightFunction.usf: Pixel shader for computing a light function.
=============================================================================*/
#include "Common.ush"
#include "/Engine/Generated/Material.ush"
#include "LightFunctionCommon.ush"
#include "DynamicLightingCommon.ush"
// used like this: mul(float4(SvPosition.xyz, 1), SvPositionToLight);
float4x4 SvPositionToLight;
/** Fade distance in x, disabled brightness in y, output for preview shadows mask in z, hair strands in w */
float4 LightFunctionParameters2;
Texture2D HairOnlyDepthTexture;
void Main(
// in float4 ScreenPosition : TEXCOORD0, // Not used and not output in VS
in float4 SvPosition : SV_Position, // after all interpolators
out float4 OutColor : SV_Target0
)
{
ResolvedView = ResolveView();
float2 ScreenUV = SvPositionToBufferUV(SvPosition);
// make SvPosition appear to be rasterized with the depth from the depth buffer
SvPosition.z = LookupDeviceZ(ScreenUV);
#if HAIR_STRANDS_SUPPORTED
BRANCH
if (LightFunctionParameters2.w > 0)
{
SvPosition.z = Texture2DSampleLevel(HairOnlyDepthTexture, SceneTexturesStruct_SceneDepthTextureSampler, ScreenUV, 0).r;
}
#endif
float3 LightVector;
{
float4 Hom = mul(float4(SvPosition.xyz, 1), SvPositionToLight);
LightVector = Hom.xyz / Hom.w;
}
float3 TranslatedWorldPosition = SvPositionToTranslatedWorld(SvPosition);
// Calculate radial view distance for stable fading
float ViewDistance = GetDistanceToCameraFromViewVector(PrimaryView.TranslatedWorldCameraOrigin - TranslatedWorldPosition);
float GreyScale;
{
float3 Color = GetLightFunctionColor(LightVector, TranslatedWorldPosition);
GreyScale = dot(Color, .3333f);
}
float DistanceFadeAlpha = saturate((LightFunctionParameters2.x - ViewDistance) / (LightFunctionParameters2.x * .2f));
// Fade to disabled based on LightFunctionFadeDistance
GreyScale = lerp(LightFunctionParameters2.y, GreyScale, DistanceFadeAlpha);
// Fade to disabled based on ShadowFadeFraction
GreyScale = lerp(LightFunctionParameters2.y, GreyScale, LightFunctionParameters.y);
#if FORWARD_SHADING
float LightInfluenceMask = GetLightInfluenceMask(TranslatedWorldPosition);
GreyScale = lerp(1, GreyScale, LightInfluenceMask);
OutColor = EncodeLightAttenuation(GreyScale);
#else
float EncodedLightAttenuation = EncodeLightAttenuation(GreyScale);
// Light function shadows write to the blue channel.
OutColor = lerp(float4(1, 1, EncodedLightAttenuation.xx), EncodedLightAttenuation.xxxx, LightFunctionParameters2.z);
#endif
}