Add dithering to Volumetric Fog to reduce aliasing
This commit is contained in:
@@ -27,8 +27,6 @@ struct ExponentialHeightFogData
|
||||
float VolumetricFogMaxDistance;
|
||||
float DirectionalInscatteringStartDistance;
|
||||
float StartDistance;
|
||||
|
||||
float4 VolumetricFogGrid;
|
||||
};
|
||||
|
||||
float4 GetExponentialHeightFog(ExponentialHeightFogData exponentialHeightFog, float3 posWS, float3 camWS, float skipDistance, float sceneDistance)
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
META_CB_BEGIN(0, Data)
|
||||
GBufferData GBuffer;
|
||||
ExponentialHeightFogData ExponentialHeightFog;
|
||||
VolumetricFogData VolumetricFog;
|
||||
float4 TemporalAAJitter;
|
||||
META_CB_END
|
||||
|
||||
DECLARE_GBUFFERDATA_ACCESS(GBuffer)
|
||||
@@ -46,7 +48,7 @@ float4 PS_Fog(Quad_VS2PS input) : SV_Target0
|
||||
|
||||
#if VOLUMETRIC_FOG
|
||||
// Sample volumetric fog and mix it in
|
||||
float4 volumetricFog = SampleVolumetricFog(VolumetricFogTexture, ExponentialHeightFog.VolumetricFogGrid, worldPos - GBuffer.ViewPos, input.TexCoord);
|
||||
float4 volumetricFog = SampleVolumetricFog(VolumetricFogTexture, VolumetricFog, worldPos - GBuffer.ViewPos, input.TexCoord, TemporalAAJitter);
|
||||
fog = CombineVolumetricFog(fog, volumetricFog);
|
||||
#endif
|
||||
|
||||
|
||||
@@ -3,8 +3,18 @@
|
||||
#ifndef __VOLUMETRIC_FOG__
|
||||
#define __VOLUMETRIC_FOG__
|
||||
|
||||
#include "./Flax/Noise.hlsl"
|
||||
|
||||
#define VOLUMETRIC_FOG_GRID_Z_LINEAR 1
|
||||
|
||||
// Structure that contains information about volumetric fog
|
||||
struct VolumetricFogData
|
||||
{
|
||||
float4 GridSliceParameters;
|
||||
float2 ScreenSize;
|
||||
float2 VolumeTexelSize; // Scaled for dithering
|
||||
};
|
||||
|
||||
float GetDepthFromSlice(float4 gridSliceParameters, float zSlice)
|
||||
{
|
||||
#if VOLUMETRIC_FOG_GRID_Z_LINEAR
|
||||
@@ -23,11 +33,19 @@ float GetSliceFromDepth(float4 gridSliceParameters, float sceneDepth)
|
||||
#endif
|
||||
}
|
||||
|
||||
float4 SampleVolumetricFog(Texture3D volumetricFogTexture, float4 gridSliceParameters, float3 viewVector, float2 uv)
|
||||
float4 SampleVolumetricFog(Texture3D volumetricFogTexture, VolumetricFogData volumetricFogData, float3 viewVector, float2 uv, float4 temporalAAJitter = 0)
|
||||
{
|
||||
// Project view vector to get 3D frustum UVW coordinates
|
||||
float sceneDepth = length(viewVector);
|
||||
float zSlice = GetSliceFromDepth(gridSliceParameters, sceneDepth) * gridSliceParameters.w;
|
||||
float zSlice = GetSliceFromDepth(volumetricFogData.GridSliceParameters, sceneDepth) * volumetricFogData.GridSliceParameters.w;
|
||||
float3 volumeUV = float3(uv, zSlice);
|
||||
|
||||
// Dither to reduce banding artifacts
|
||||
float2 noiseUV = volumeUV.xy + temporalAAJitter.xy;
|
||||
float2 noise = rand2dTo2d(noiseUV * volumetricFogData.ScreenSize) * 2.0f - 1.0f;
|
||||
volumeUV.xy += noise * volumetricFogData.VolumeTexelSize;
|
||||
|
||||
// Sample 3D texture
|
||||
return volumetricFogTexture.SampleLevel(SamplerLinearClamp, volumeUV, 0);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user