Skip to content

Instantly share code, notes, and snippets.

@krupitskas
Created January 30, 2019 10:24
Show Gist options
  • Save krupitskas/b6080eec2cdf41fe1fef4af01f8cd36d to your computer and use it in GitHub Desktop.
Save krupitskas/b6080eec2cdf41fe1fef4af01f8cd36d to your computer and use it in GitHub Desktop.
float4 PSMain(float4 position : SV_POSITION, float2 uv : TEXCOORD0 ) : SV_Target
{
const float cExtinctionFactor = Params.Config.x;
const float cDensity = Params.Config.y;
const int cNumSamples = Params.Config.z;
const float cCutoffDist = Params.Config.w;
const float depth = Depth.Sample(PointSampler, uv).r;
const float4 vsPos = float4(screen_to_view(Params.Projection, uv, depth), 1.0f);
const float distToPos = length(vsPos);
const float sampleLength = distToPos / float(cNumSamples);
if ( distToPos > cCutoffDist )
return float4(0.0f.xxx, 1.0f);
float transmittance = 1.0;
float3 inScattering = 0.0f.xxx;
const float extinction = cExtinctionFactor * cDensity;
for (int i = 0; i < cNumSamples; ++i)
{
float beerLawIntegral = exp(-extinction * sampleLength);
transmittance *= beerLawIntegral;
inScattering += Params.FogColor; // Should be retrieved from 3D texture
}
inScattering /= cNumSamples;
return float4(inScattering * (1 - transmittance), transmittance);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment