LYGIA Shader Library

directionalEvaluate (lygia/lighting/light/directionalEvaluate)

Calculate directional light

Dependencies:

Use:

<void> lightDirectionalEvaluate(<LightDirectional> L, <Material> mat, inout <ShadingData> shadingData)

Check it on Github



#ifndef FNC_LIGHT_DIRECTIONAL_EVALUATE
#define FNC_LIGHT_DIRECTIONAL_EVALUATE

void lightDirectionalEvaluate(LightDirectional L, Material mat, inout ShadingData shadingData) {

    shadingData.L = L.direction;
    shadingData.H = normalize(L.direction + shadingData.V);
    shadingData.NoL = saturate(dot(shadingData.N, L.direction));
    shadingData.NoH = saturate(dot(shadingData.N, shadingData.H));

    #ifdef FNC_RAYMARCH_SOFTSHADOW    
    float shadow = raymarchSoftShadow(mat.position, L.direction);
    #else
    float shadow = 1.0;
    #endif

    float dif  = diffuse(shadingData);
    vec3 spec = specular(shadingData);

    vec3 lightContribution = L.color * L.intensity * shadow * shadingData.NoL;
    shadingData.directDiffuse  += max(vec3(0.0, 0.0, 0.0), shadingData.diffuseColor * lightContribution * dif);
    shadingData.directSpecular += max(vec3(0.0, 0.0, 0.0), lightContribution * spec) * shadingData.energyCompensation;

    #ifdef SHADING_MODEL_SUBSURFACE
    float scatterVoH = saturate(dot(shadingData.V, -L.direction));
    float forwardScatter = exp2(scatterVoH * mat.subsurfacePower - mat.subsurfacePower);
    float backScatter = saturate(shadingData.NoL * mat.subsurfaceThickness + (1.0 - mat.subsurfaceThickness)) * 0.5;
    float subsurface = mix(backScatter, 1.0, forwardScatter) * (1.0 - mat.subsurfaceThickness);
    shadingData.directDiffuse += mat.subsurfaceColor * (subsurface * diffuseLambertConstant());
    #endif
}

#endif

Dependencies:

Use:

<void> lightDirectionalEvaluate(<LightDirectional> L, <Material> mat, inout <ShadingData> shadingData)

Check it on Github



#ifndef FNC_LIGHT_DIRECTIONAL_EVALUATE
#define FNC_LIGHT_DIRECTIONAL_EVALUATE

void lightDirectionalEvaluate(LightDirectional L, Material mat, inout ShadingData shadingData) {

    shadingData.L = L.direction;
    shadingData.H = normalize(L.direction + shadingData.V);
    shadingData.NoL = saturate(dot(shadingData.N, L.direction));
    shadingData.NoH = saturate(dot(shadingData.N, shadingData.H));

    #ifdef FNC_RAYMARCH_SOFTSHADOW    
    float shadow = raymarchSoftShadow(mat.position, L.direction);
    #else
    float shadow = 1.0;
    #endif

    float dif  = diffuse(shadingData);
    float3 spec = specular(shadingData);

    float3 lightContribution = L.color * L.intensity * shadow * shadingData.NoL;
    shadingData.directDiffuse  += max(float3(0.0, 0.0, 0.0), shadingData.diffuseColor * lightContribution * dif);
    shadingData.directSpecular += max(float3(0.0, 0.0, 0.0), lightContribution * spec) * shadingData.energyCompensation;

    #ifdef SHADING_MODEL_SUBSURFACE
    float scatterVoH = saturate(dot(shadingData.V, -L.direction));
    float forwardScatter = exp2(scatterVoH * mat.subsurfacePower - mat.subsurfacePower);
    float backScatter = saturate(shadingData.NoL * mat.subsurfaceThickness + (1.0 - mat.subsurfaceThickness)) * 0.5;
    float subsurface = lerp(backScatter, 1.0, forwardScatter) * (1.0 - mat.subsurfaceThickness);
    shadingData.directDiffuse += mat.subsurfaceColor * (subsurface * diffuseLambertConstant());
    #endif
}

#endif

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