Skip to content

Instantly share code, notes, and snippets.

@float3
Last active January 24, 2022 12:01
Show Gist options
  • Save float3/d7fa7dc1ea59903c10ed27f09189d0eb to your computer and use it in GitHub Desktop.
Save float3/d7fa7dc1ea59903c10ed27f09189d0eb to your computer and use it in GitHub Desktop.
using nicer LUT dfg Fresnel in unity
// https://eheitzresearch.wordpress.com/240-2/
// Generating the dfg:
// https://github.com/google/filament/blob/main/tools/cmgen/src/cmgen.cpp
// make sure to generate the multiscatter dfg
[NonModifiableTextureData] [NoScaleOffset] _DFG ("DFG Lut", 2D) = "black" {}
Texture2D_float _DFG;
SamplerState sampler_DFG;
float3 F_Schlick(float3 f0, float f90, float VoH)
{
return f0 + (f90 - f0) * pow(1.0 - VoH, 5);
}
float3 fresnel(float3 f0, float LoH)
{
float f90 = saturate(dot(f0, float(50.0 * 0.33).xxx));
return F_Schlick(f0, f90, LoH);
}
// standardish fresnel
float3 fresnel;
fresnel = fresnel(f0, NoV);
fresnel *= length(indirectDiffuse);
...
// standardish way of calculating indirect specular
float3 indirectSpecular *= lerp(F, f0, perceptualRoughness) * horizon * horizon;
// standardish way of calculating direct specular
float3 specular = max(0, D * V * F * finalLight * UNITY_PI);
// dfg fresnel
float3 fresnel = fresnel(f0, NoV);
float2 dfguv = float2(NoV, perceptualRoughness);
float2 dfg = _DFG.Sample(sampler_DFG, dfguv).xy;
float3 energyCompensation = 1.0 + f0 * (1.0 / dfg.y - 1.0);
// dfg indirect specular
dfg.x *= saturate(length(indirectDiffuse)));
float3 indirectSpecular = indirectSpecular * lerp(dfg.xxx, dfg.yyy, f0) * horizon * horizon * energyCompensation;
// dfg direct specular
float3 specular = max(0, D * V * F * finalLight * energyCompensation * UNITY_PI);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment