LYGIA Shader Library

kochSDF (lygia/v1.1.6/sdf/kochSDF)

Returns a Koch curve SDF. From https://www.shadertoy.com/view/Mlf3RX

Dependencies:

Use:

<vec2> kochSDF(<vec2> st, <int> iterations)

Check it on Github



#ifndef FNC_KOCHSDF
#define FNC_KOCHSDF
float kochSDF( vec2 st, vec2 center, int N ) {
    st -= center;
    st *= 3.0;
    float r3 = sqrt(3.);  
    st = abs(st);
    st += r3*vec2(-st.y,st.x); // 60° rotation, scale 2
    st.y -= 1.;   
    float w = .5;    
    mat2 m = mat2(r3,3,-3,r3)*.5;
    #ifdef PLATFORM_WEBGL
    for (int i = 0; i< 20; i++) {
        if (i >= N) break;
    #else
    for (int i = 0; i< N; i++) {
    #endif
        st = vec2(-r3,3)*.5 - m*vec2(st.y,abs(st.x));
        w /= r3;
    }
    float d = sign(st.y)*length(vec2(st.y,max(0.,abs(st.x)-r3)));  
    return (d*w);
}

float kochSDF( vec2 st, int N ) {
    #ifdef CENTER_2D
        return kochSDF(st, CENTER_2D, N);
    #else
        return kochSDF(st, vec2(0.5), N);
    #endif
}
#endif

Dependencies:

Use:

<float2> kochSDF(<float2> st, <int> iterations)

Check it on Github



#ifndef FNC_KOCHSDF
#define FNC_KOCHSDF
float kochSDF( float2 st, float2 center, int N ) {
    st -= center;
    st *= 3.0;
    float r3 = sqrt(3.);  
    st = abs(st);
    st += r3*float2(-st.y,st.x); // 60° rotation, scale 2
    st.y -= 1.;   
    float w = .5;    
    float2x2 m = float2x2(r3,3,-3,r3)*.5;
    for (int i = 0; i< N; i++) {
        st = float2(-r3,3)*.5 - m*float2(st.y,abs(st.x));
        w /= r3;
    }
    float d = sign(st.y)*length(float2(st.y,max(0.,abs(st.x)-r3)));  
    return (d*w);
}

float kochSDF( float2 st, int N ) {
    #ifdef CENTER_2D
        return kochSDF(st, CENTER_2D, N);
    #else
        return kochSDF(st, float2(0.5, 0.5), N);
    #endif
}
#endif

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