LYGIA Shader Library

sphericalHarmonics (lygia/lighting/sphericalHarmonics)

Return the spherical harmonic value facing a normal direction

Use:

sphericalHarmonics( <vec3> normal)

Check it on Github


#ifndef SPHERICALHARMONICS_BANDS
#if defined(PLATFORM_RPI) 
#define SPHERICALHARMONICS_BANDS           1
#elif defined(TARGET_MOBILE) || defined(PLATFORM_WEBGL)
#define SPHERICALHARMONICS_BANDS           2
#else
#define SPHERICALHARMONICS_BANDS           3
#endif
#endif

// #ifndef SCENE_SH_ARRAY
// #define SCENE_SH_ARRAY u_SH
// #endif

#ifndef SPHERICALHARMONICS_TONEMAP 
#define SPHERICALHARMONICS_TONEMAP
#endif

#ifndef FNC_SPHERICALHARMONICS
#define FNC_SPHERICALHARMONICS

vec3 sphericalHarmonics(const vec3 sh[9], const in vec3 n) {
    return SPHERICALHARMONICS_TONEMAP ( max(
           0.282095 * sh[0]
#if SPHERICALHARMONICS_BANDS >= 2
        + -0.488603 * sh[1] * (n.y)
        +  0.488603 * sh[2] * (n.z)
        + -0.488603 * sh[3] * (n.x)
#endif
#if SPHERICALHARMONICS_BANDS >= 3
        +  1.092548 * sh[4] * (n.y * n.x)
        + -1.092548 * sh[5] * (n.y * n.z)
        +  0.315392 * sh[6] * (3.0 * n.z * n.z - 1.0)
        + -1.092548 * sh[7] * (n.z * n.x)
        +  0.546274 * sh[8] * (n.x * n.x - n.y * n.y)
#endif
        , 0.0) );
}

vec3 sphericalHarmonics(const in vec3 n) {
#ifdef SCENE_SH_ARRAY
    return sphericalHarmonics(SCENE_SH_ARRAY, n);
#else
    return vec3(1.0);
#endif
}

#endif

Use:

sphericalHarmonics( <float3> normal)

Check it on Github


#ifndef SPHERICALHARMONICS_BANDS
#if defined(TARGET_MOBILE) || defined(PLATFORM_RPI) || defined(PLATFORM_WEBGL)
#define SPHERICALHARMONICS_BANDS           2
#else
#define SPHERICALHARMONICS_BANDS           3
#endif
#endif

// #ifndef SCENE_SH_ARRAY
// #define SCENE_SH_ARRAY u_SH
// #endif

#ifndef SPHERICALHARMONICS_TONEMAP 
#define SPHERICALHARMONICS_TONEMAP
#endif

#ifndef FNC_SPHERICALHARMONICS
#define FNC_SPHERICALHARMONICS

float3 sphericalHarmonics(const float3 n) {
#ifdef SCENE_SH_ARRAY
    return SPHERICALHARMONICS_TONEMAP ( max(
           0.282095 * SCENE_SH_ARRAY[0]
#if SPHERICALHARMONICS_BANDS >= 2
        + -0.488603 * SCENE_SH_ARRAY[1] * (n.y)
        +  0.488603 * SCENE_SH_ARRAY[2] * (n.z)
        + -0.488603 * SCENE_SH_ARRAY[3] * (n.x)
#endif
#if SPHERICALHARMONICS_BANDS >= 3
        +  1.092548 * SCENE_SH_ARRAY[4] * (n.y * n.x)
        + -1.092548 * SCENE_SH_ARRAY[5] * (n.y * n.z)
        +  0.315392 * SCENE_SH_ARRAY[6] * (3.0 * n.z * n.z - 1.0)
        + -1.092548 * SCENE_SH_ARRAY[7] * (n.z * n.x)
        +  0.546274 * SCENE_SH_ARRAY[8] * (n.x * n.x - n.y * n.y)
#endif
        , 0.0) );
#else
    return float3(1.0, 1.0, 1.0);
#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