127 lines
4.9 KiB
Plaintext
127 lines
4.9 KiB
Plaintext
Shader "Hidden/PostProcessing/DeferredFog"
|
|
{
|
|
HLSLINCLUDE
|
|
|
|
#pragma multi_compile __ FOG_LINEAR FOG_EXP FOG_EXP2
|
|
|
|
// Start: LuxWater
|
|
#pragma multi_compile __ LUXWATER_DEFERREDFOG
|
|
|
|
#if defined(LUXWATER_DEFERREDFOG)
|
|
sampler2D _UnderWaterMask;
|
|
float4 _LuxUnderWaterDeferredFogParams; // x: IsInsideWatervolume?, y: BelowWaterSurface shift, z: EdgeBlend
|
|
|
|
inline float DecodeFloatRG( float2 enc ) {
|
|
float2 kDecodeDot = float2(1.0, 1/255.0);
|
|
return dot( enc, kDecodeDot );
|
|
}
|
|
#endif
|
|
// End: LuxWater
|
|
|
|
#include "../StdLib.hlsl"
|
|
#include "Fog.hlsl"
|
|
|
|
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
|
|
TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
|
|
|
|
#define SKYBOX_THREASHOLD_VALUE 0.9999
|
|
|
|
float4 Frag(VaryingsDefault i) : SV_Target
|
|
{
|
|
half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoordStereo);
|
|
|
|
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoordStereo);
|
|
depth = Linear01Depth(depth);
|
|
float dist = ComputeFogDistance(depth);
|
|
half fog = 1.0 - ComputeFog(dist);
|
|
|
|
// Start: LuxWater
|
|
#if defined(LUXWATER_DEFERREDFOG)
|
|
half4 fogMask = tex2D(_UnderWaterMask, UnityStereoTransformScreenSpaceTex(i.texcoordStereo));
|
|
float watersurfacefrombelow = DecodeFloatRG(fogMask.ba);
|
|
|
|
// Get distance and lower it a bit in order to handle edge blending artifacts (edge blended parts would not get ANY fog)
|
|
float ldist = (watersurfacefrombelow - depth) + _LuxUnderWaterDeferredFogParams.y * _ProjectionParams.w;
|
|
// Fade fog from above water to below water
|
|
float fogFactor = saturate ( 1.0 + _ProjectionParams.z * _LuxUnderWaterDeferredFogParams.z * ldist ); // 0.125
|
|
// Clamp above result to where water is actually rendered
|
|
fogFactor = (fogMask.r == 1) ? fogFactor : 1.0;
|
|
// Mask fog on underwarter parts - only if we are inside a volume (bool... :( )
|
|
if(_LuxUnderWaterDeferredFogParams.x) {
|
|
fogFactor *= saturate( 1.0 - fogMask.g * 8.0);
|
|
if (ldist < -_ProjectionParams.w * 4 && fogMask.r == 0 && fogMask.g < 1.0) {
|
|
fogFactor = 1.0;
|
|
}
|
|
}
|
|
// Tweak fog factor
|
|
fog *= fogFactor;
|
|
#endif
|
|
// End: LuxWater
|
|
|
|
return lerp(color, _FogColor, fog);
|
|
}
|
|
|
|
float4 FragExcludeSkybox(VaryingsDefault i) : SV_Target
|
|
{
|
|
half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoordStereo);
|
|
|
|
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoordStereo);
|
|
depth = Linear01Depth(depth);
|
|
float skybox = depth < SKYBOX_THREASHOLD_VALUE;
|
|
float dist = ComputeFogDistance(depth);
|
|
half fog = 1.0 - ComputeFog(dist);
|
|
|
|
// Start: LuxWater
|
|
#if defined(LUXWATER_DEFERREDFOG)
|
|
half4 fogMask = tex2D(_UnderWaterMask, UnityStereoTransformScreenSpaceTex(i.texcoordStereo));
|
|
float watersurfacefrombelow = DecodeFloatRG(fogMask.ba);
|
|
|
|
// Get distance and lower it a bit in order to handle edge blending artifacts (edge blended parts would not get ANY fog)
|
|
float ldist = (watersurfacefrombelow - depth) + _LuxUnderWaterDeferredFogParams.y * _ProjectionParams.w;
|
|
// Fade fog from above water to below water
|
|
float fogFactor = saturate ( 1.0 + _ProjectionParams.z * _LuxUnderWaterDeferredFogParams.z * ldist ); // 0.125
|
|
// Clamp above result to where water is actually rendered
|
|
fogFactor = (fogMask.r == 1) ? fogFactor : 1.0;
|
|
// Mask fog on underwarter parts - only if we are inside a volume (bool... :( )
|
|
if(_LuxUnderWaterDeferredFogParams.x) {
|
|
fogFactor *= saturate( 1.0 - fogMask.g * 8.0);
|
|
if (ldist < -_ProjectionParams.w * 4 && fogMask.r == 0 && fogMask.g < 1.0) {
|
|
fogFactor = 1.0;
|
|
}
|
|
}
|
|
// Tweak fog factor
|
|
fog *= fogFactor;
|
|
#endif
|
|
// End: LuxWater
|
|
|
|
return lerp(color, _FogColor, fog * skybox);
|
|
}
|
|
|
|
ENDHLSL
|
|
|
|
SubShader
|
|
{
|
|
Cull Off ZWrite Off ZTest Always
|
|
|
|
Pass
|
|
{
|
|
HLSLPROGRAM
|
|
|
|
#pragma vertex VertDefault
|
|
#pragma fragment Frag
|
|
|
|
ENDHLSL
|
|
}
|
|
|
|
Pass
|
|
{
|
|
HLSLPROGRAM
|
|
|
|
#pragma vertex VertDefault
|
|
#pragma fragment FragExcludeSkybox
|
|
|
|
ENDHLSL
|
|
}
|
|
}
|
|
}
|