LYGIA Shader Library

fresnelReflection (lygia/lighting/fresnelReflection)

Resolve fresnel coeficient and apply it to a reflection. It can apply iridescence to using a formula based on https://www.alanzucconi.com/2017/07/25/the-mathematics-of-thin-film-interference/

Dependencies:

Use:

<vec3> fresnelReflection(<vec3> R, <vec3> f0, <float> NoV)
<vec3> fresnelIridescentReflection(<vec3> normal, <vec3> view, <vec3> f0, <vec3> ior1, <vec3> ior2, <float> thickness, <float> roughness)
<vec3> fresnelReflection(<Material> _M)

Check it on Github



#ifndef FRESNEL_REFLECTION_RGB
#define FRESNEL_REFLECTION_RGB vec3(612.0, 549.0, 464.0)
#endif

#ifndef FNC_FRESNEL_REFLECTION
#define FNC_FRESNEL_REFLECTION

vec3 fresnelReflection(const in vec3 R, const in vec3 f0, const in float NoV) {
    vec3 frsnl = fresnel(f0, NoV);

    vec3 reflectColor = vec3(0.0);
    #if defined(FRESNEL_REFLECTION_FNC)
    reflection = FRESNEL_REFLECTION_FNC(R);
    #else
    reflectColor = envMap(R, 1.0, 0.001);
    #endif

    return reflectColor * frsnl;
}

vec3 fresnelReflection(const in vec3 R, const in vec3 Fr) {
    vec3 reflectColor = vec3(0.0);
    #if defined(FRESNEL_REFLECTION_FNC)
    reflection = FRESNEL_REFLECTION_FNC(R);
    #else
    reflectColor = envMap(R, 1.0, 0.001);
    #endif
    return reflectColor * Fr;
}

vec3 fresnelIridescentReflection(vec3 normal, vec3 view, float f0, float ior1, float ior2, float thickness, float roughness, 
                                 inout float Fr) {
    float cos0 = -dot(view, normal);
    Fr = fresnel(f0, cos0);
    float T = 1.0 - Fr;
    float a = ior1/ior2;
    float cosi2 = sqrt(1.0 - a * a * (1.0 - cos0*cos0));
    vec3 shift = 4.0 * PI * (thickness/FRESNEL_REFLECTION_RGB) * ior2 * cosi2 + HALF_PI;
    vec3 irid = Fr * ( 1.0 + T * ( T + 2.0 * cos(shift) ) );
    vec3 ref = envMap(reflect(view, normal), roughness, 0.0);
    return (ref + pow5(ref)) * irid;
}

vec3 fresnelIridescentReflection(vec3 normal, vec3 view, float f0, float ior1, float ior2, float thickness, float roughness) {
    float Fr = 0.0;
    return fresnelIridescentReflection(normal, view, f0, ior1, ior2, thickness, roughness, Fr);
}

vec3 fresnelIridescentReflection(vec3 normal, vec3 view, vec3 f0, vec3 ior1, vec3 ior2, float thickness, float roughness,
                                 inout vec3 Fr) {
    float cos0 = -dot(view, normal);
    Fr = fresnel(f0, cos0);
    vec3 T = 1.0 - Fr;
    vec3 a = ior1/ior2;
    vec3 cosi2 = sqrt(1.0 - a * a * (1.0 - cos0*cos0));
    vec3 shift = 4.0 * PI * (thickness/FRESNEL_REFLECTION_RGB) * ior2 * cosi2 + HALF_PI;
    vec3 irid = Fr * ( 1.0 + T * ( T + 2.0 * cos(shift) ) );
    vec3 ref = envMap(reflect(view, normal), roughness, 0.0);
    return (ref + pow5(ref)) * irid;
}

vec3 fresnelIridescentReflection(vec3 normal, vec3 view, vec3 f0, vec3 ior1, vec3 ior2, float thickness, float roughness) {
    vec3 Fr = vec3(0.0);
    return fresnelIridescentReflection(normal, view, f0, ior1, ior2, thickness, roughness, Fr);
}

vec3 fresnelIridescentReflection(vec3 normal, vec3 view, float ior1, float ior2, float thickness, float roughness) {
    float F0 = (ior2-1.)/(ior2+1.);
    return fresnelIridescentReflection(normal, view, F0 * F0, ior1, ior2, thickness, roughness);
}

vec3 fresnelIridescentReflection(vec3 normal, vec3 view, vec3 ior1, vec3 ior2, float thickness, float roughness) {
    vec3 F0 = (ior2-1.)/(ior2+1.);
    return fresnelIridescentReflection(normal, view, F0 * F0, ior1, ior2, thickness, roughness);
}

#ifdef STR_MATERIAL
vec3 fresnelReflection(const in Material _M) {
    #if defined(SHADING_MODEL_IRIDESCENCE)
    return fresnelIridescentReflection(_M.normal, -_M.V, _M.f0, vec3(IOR_AIR), _M.ior, _M.thickness, _M.roughness);
    #else
    return fresnelReflection(_M.R, _M.f0, _M.NoV) * (1.0-_M.roughness);
    #endif
}
#endif

#endif

Dependencies:

Use:

<float3> fresnel(const <float3> f0, <float> LoH)
<float3> fresnel(<float3> _R, <float3> _f0, <float> _NoV)

Check it on Github



#ifndef FNC_FRESNEL_REFLECTION
#define FNC_FRESNEL_REFLECTION

float3 fresnelReflection(float3 R, float3 f0, float NoV) {
    float3 frsnl = fresnel(f0, NoV);

    float3 reflectColor = float3(0.0, 0.0, 0.0);

    #if defined(FRESNEL_REFLECTION_FNC)
    reflection = FRESNEL_REFLECTION_FNC(R);

    #elif defined(ENVMAP_FNC) 
    reflectColor = ENVMAP_FNC(R, 0.001, 0.001);

    #elif defined(SCENE_CUBEMAP)
    reflectColor = SAMPLE_CUBE_FNC( SCENE_CUBEMAP, R, ENVMAP_MAX_MIP_LEVEL).rgb;

    #elif defined(SCENE_SH_ARRAY)
    reflectColor = sphericalHarmonics(R);

    #else
    reflectColor = fakeCube(R);
    #endif

    return reflectColor * frsnl;
}

#endif

Dependencies:

Check it on Github


fn fresnelReflection(R: vec3f, f0: vec3f, NoV: f32) -> {
    let frsnl = fresnel(f0, NoV);

    let reflectColor = vec3f(0.0);
    reflectColor = envMap(R, 1.0, 0.001);

    return reflectColor * frsnl;
}

License

LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.

Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).

It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.

Get the latest news and releases

Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository