LYGIA Shader Library

pbrGlass (lygia/lighting/pbrGlass)

Simple glass shading model

Dependencies:

Use:

<vec4> glass(<Material> material)

Check it on Github






#ifndef IBL_LUMINANCE
#define IBL_LUMINANCE   1.0
#endif

#ifndef FNC_PBRGLASS
#define FNC_PBRGLASS

vec4 pbrGlass(const Material _mat) {

    // Cached
    Material M  = _mat;
    M.V         = normalize(CAMERA_POSITION - M.position);  // View
    M.R         = reflection(M.V, M.normal, M.roughness);   // Reflection
#if defined(SCENE_BACK_SURFACE)
    vec3 No     = normalize(M.normal - M.normal_back); // Normal out is the difference between the front and back normals
#else
    vec3 No     = M.normal;                            // Normal out
#endif
    M.NoV       = dot(No, M.V);                        // Normal . View

    vec3 eta    = ior2eta(M.ior);


    // Global Ilumination ( Image Based Lighting )
    // ------------------------
    vec3 E = envBRDFApprox(M.albedo.rgb, M);

    vec3 Gi = vec3(0.0, 0.0, 0.0);
    Gi  += envMap(M) * E;
    #if !defined(PLATFORM_RPI)
    // Gi  += fresnelReflection(M);

    #if defined(SHADING_MODEL_IRIDESCENCE)
    vec3 Fr = vec3(0.0);
    Gi  += fresnelIridescentReflection(M.normal, -M.V, M.f0, vec3(IOR_AIR), M.ior, M.thickness, M.roughness, Fr);
    #else
    vec3 Fr = fresnel(M.f0, M.NoV);
    Gi  += fresnelReflection(M.R, Fr) * (1.0-M.roughness);
    #endif

    #endif

    vec4 color  = vec4(0.0, 0.0, 0.0, 1.0);

    // Refraction
    color.rgb   += transparent(No, -M.V, Fr, eta, M.roughness);
    color.rgb   += Gi * IBL_LUMINANCE;

    // TODO: RaG
    //  - Add support for multiple lights
    // 
    {
        #if defined(LIGHT_DIRECTION)
        LightDirectional L = LightDirectionalNew();
        #elif defined(LIGHT_POSITION)
        LightPoint L = LightPointNew();
        #endif

        #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION)
        // lightResolve(diffuseColor, specularColor, M, L, lightDiffuse, lightSpecular);
        vec3 spec = vec3( specular(L.direction, M.normal, M.V, M.roughness) );

        color.rgb += L.color * spec;

        #endif
    }

    return color;
}



#endif

Dependencies:

Use:

<float4> glass(<Material> material)

Check it on Github






#ifndef CAMERA_POSITION
#if defined(UNITY_COMPILER_HLSL)
#define CAMERA_POSITION _WorldSpaceCameraPos
#else
#define CAMERA_POSITION float3(0.0, 0.0, -10.0)
#endif
#endif

#ifndef LIGHT_POSITION
#if defined(UNITY_COMPILER_HLSL)
#define LIGHT_POSITION _WorldSpaceLightPos0.xyz
#else
#define LIGHT_POSITION  float3(0.0, 10.0, -50.0)
#endif
#endif

#ifndef LIGHT_COLOR
#if defined(UNITY_COMPILER_HLSL)
#define LIGHT_COLOR     _LightColor0.rgb
#else
#define LIGHT_COLOR     float3(0.5, 0.5, 0.5)
#endif
#endif

#ifndef IBL_LUMINANCE
#define IBL_LUMINANCE   1.0
#endif

#ifndef FNC_PBRGLASS
#define FNC_PBRGLASS

float4 pbrGlass(const Material _mat) {

    // Cached
    Material M  = _mat;
    M.V         = normalize(CAMERA_POSITION - M.position);  // View
    M.R         = reflection(M.V, M.normal, M.roughness);   // Reflection
#if defined(SCENE_BACK_SURFACE)
    float3 No     = normalize(M.normal - M.normal_back); // Normal out is the difference between the front and back normals
#else
    float3 No     = M.normal;                            // Normal out
#endif
    M.NoV       = dot(No, M.V);                        // Normal . View

    float3 eta    = ior2eta(M.ior);


    // Global Ilumination ( Image Based Lighting )
    // ------------------------
    float3 E = envBRDFApprox(M.albedo.rgb, M);

    float3 Gi = float3(0.0, 0.0, 0.0);
    Gi  += envMap(M) * E;
    #if !defined(PLATFORM_RPI)
    // Gi  += fresnelReflection(M);

    #if defined(SHADING_MODEL_IRIDESCENCE)
    float3 Fr = float3(0.0, 0.0, 0.0);
    Gi  += fresnelIridescentReflection(M.normal, -M.V, M.f0, float3(IOR_AIR, IOR_AIR, IOR_AIR), M.ior, M.thickness, M.roughness, Fr);
    #else
    float3 Fr = fresnel(M.f0, M.NoV);
    Gi  += fresnelReflection(M.R, Fr) * (1.0-M.roughness);
    #endif

    #endif

    float4 color  = float4(0.0, 0.0, 0.0, 1.0);

    // Refraction
    color.rgb   += transparent(No, -M.V, Fr, eta, M.roughness);
    color.rgb   += Gi * IBL_LUMINANCE;

    // TODO: RaG
    //  - Add support for multiple lights
    // 
    {
        #if defined(LIGHT_DIRECTION)
        LightDirectional L = LightDirectionalNew();
        #elif defined(LIGHT_POSITION)
        LightPoint L = LightPointNew();
        #endif

        #if defined(LIGHT_DIRECTION) || defined(LIGHT_POSITION)
        // lightResolve(diffuseColor, specularColor, M, L, lightDiffuse, lightSpecular);
        float spec = specular(L.direction, M.normal, M.V, M.roughness);

        color.rgb += L.color * spec;

        #endif
    }

    return color;
}

#endif

Examples

Licenses

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