LYGIA Shader Library

equirect (lygia/sample/equirect)

sample an equirect texture as it was a cubemap

Dependencies:

Use:

sampleEquirect(<SAMPLER_TYPE> texture, <vec3> dir)

Check it on Github



#ifndef FNC_SAMPLEEQUIRECT
#define FNC_SAMPLEEQUIRECT
vec4 sampleEquirect(SAMPLER_TYPE tex, vec3 dir) { 
    vec2 st = xyz2equirect(dir);
    #ifdef SAMPLEEQUIRECT_FLIP_Y
    st.y = 1.0-st.y;
    #endif
    return SAMPLER_FNC(tex, st); 
}

vec4 sampleEquirect(SAMPLER_TYPE tex, vec3 dir, float lod) { 

    #if defined(SAMPLEEQUIRECT_ITERATIONS)
    vec4 color = vec4(0.0);
    vec2 st = xyz2equirect(dir);
    #ifdef SAMPLEEQUIRECT_FLIP_Y
        st.y = 1.0-st.y;
    #endif

    vec2 r = vec2(1.0+lod);
    const float f = 1.0 / (1.001 - 0.75);
    mat2 rot = mat2( cos(GOLDEN_ANGLE), sin(GOLDEN_ANGLE), 
                    -sin(GOLDEN_ANGLE), cos(GOLDEN_ANGLE));
    vec2 st2 = vec2( dot(st + st - r, vec2(.0002,-0.001)), 0.0 );

    float counter = 0.0;
    #ifdef PLATFORM_WEBGL
    for (float i = 0.0; i < float(SAMPLEEQUIRECT_ITERATIONS); i++) {
    #else
    for (float i = 0.0; i < float(SAMPLEEQUIRECT_ITERATIONS); i += 2.0/i) {
    #endif
        st2 *= rot;
        color += gamma2linear( SAMPLER_FNC(tex, st + st2 * i / vec2(r.x * 2.0, r.y))) * f;
        counter++;
    }
    return linear2gamma(color / counter);

    #else

    dir += srandom3( dir ) * 0.01 * lod;
    vec2 st = xyz2equirect(dir);
    #ifdef SAMPLEEQUIRECT_FLIP_Y
        st.y = 1.0-st.y;
    #endif
    return SAMPLER_FNC(tex, st);

    #endif
}


#endif

Dependencies:

Use:

sampleEquirect(<SAMPLER_TYPE> texture, <float3> dir)

Check it on Github



#ifndef FNC_SAMPLEEQUIRECT
#define FNC_SAMPLEEQUIRECT
float4 sampleEquirect(SAMPLER_TYPE tex, float3 dir) { 
    float2 st = xyz2equirect(dir);
    #ifdef SAMPLEEQUIRECT_FLIP_Y
    st.y = 1.0-st.y;
    #endif
    return SAMPLER_FNC(tex, st); 
}

float4 sampleEquirect(SAMPLER_TYPE tex, float3 dir, float lod) { 

    #if defined(SAMPLEEQUIRET_ITERATIONS)
    float4 acc = float4(0.0, 0.0, 0.0, 0.0);
    float2 st = xyz2equirect(dir);
    #ifdef SAMPLEEQUIRECT_FLIP_Y
    st.y = 1.0-st.y;
    #endif
    float2x2 rot = float2x2(cos(GOLDEN_ANGLE), sin(GOLDEN_ANGLE), -sin(GOLDEN_ANGLE), cos(GOLDEN_ANGLE));
    float r = 1.;
    float2 vangle = float2(0.0, lod * 0.01);
    for (int i = 0; i < SAMPLEEQUIRET_ITERATIONS; i++) {
        vangle = mul(rot, vangle);
        r++;
        float4 col = SAMPLER_FNC(tex, st + random( float3(st, r) ) * vangle );
        acc += col * col;
    }
    return float4(acc.rgb/acc.a, 1.0); 

    #else
    dir += srandom3( dir ) * 0.01 * lod;
    float2 st = xyz2equirect(dir);
    #ifdef SAMPLEEQUIRECT_FLIP_Y
    st.y = 1.0-st.y;
    #endif
    return SAMPLER_FNC(tex, st);

    #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