LYGIA Shader Library

starSDF (lygia/sdf/starSDF)

Returns a star-shaped sdf with V branches

Dependencies:

Use:

starSDF(<vec2> st, <int> V, <float> scale)

Check it on Github



#ifndef FNC_STARSDF
#define FNC_STARSDF
float starSDF(in vec2 st, in int V, in float s) {
#ifdef CENTER_2D
    st -= CENTER_2D;
#else
    st -= 0.5;
#endif
    st *= 2.0;
    float a = atan(st.y, st.x) / TAU;
    float seg = a * float(V);
    a = ((floor(seg) + 0.5) / float(V) +
        mix(s, -s, step(0.5, fract(seg))))
        * TAU;
    return abs(dot(vec2(cos(a), sin(a)),
                   st));
}

float starSDF(in vec2 st, in int V) {
    return starSDF( scale(st, 12.0/float(V)), V, 0.1);
}
#endif

Dependencies:

Use:

starSDF(<float2> st, <int> V, <float> scale)

Check it on Github


#ifndef FNC_STARSDF
#define FNC_STARSDF
float starSDF(in float2 st, in int V, in float s) {
#ifdef CENTER_2D
    st -= CENTER_2D;
#else
    st -= 0.5;
#endif
    st *= 2.0;
    float a = atan2(st.y, st.x) / TAU;
    float seg = a * float(V);
    a = ((floor(seg) + 0.5) / float(V) +
        lerp(s, -s, step(0.5, frac(seg))))
        * TAU;
    return abs(dot(float2(cos(a), sin(a)),
                   st));
}

float starSDF(in float2 st, in int V) {
    return starSDF( scale(st, 12.0/float(V)), V, 0.1);
}
#endif

Dependencies:

Use:

starSDF(<float2> st, <int> V, <float> scale)

Check it on Github



#ifndef FNC_STARSDF
#define FNC_STARSDF
float starSDF(float2 st, int V, float s) {
#ifdef CENTER_2D
    st -= CENTER_2D;
#else
    st -= 0.5;
#endif
    st *= 2.0;
    float a = atan(st.y, st.x) / TAU;
    float seg = a * float(V);
    a = ((floor(seg) + 0.5) / float(V) +
        mix(s, -s, step(0.5, fract(seg))))
        * TAU;
    return abs(dot(float2(cos(a), sin(a)),
                   st));
}

float starSDF(float2 st, int V) {
    return starSDF( scale(st, 12.0/float(V)), V, 0.1);
}
#endif

Examples

97
 
1
#ifdef GL_ES
2
precision mediump float;
3
#endif
4
5
uniform vec2        u_resolution;
6
uniform vec2        u_mouse;
7
uniform float       u_time;
8
9
// By default all 2D shapes and space functions asume
10
// the center is at vec2(0.5, 0.5), this can be overloaded
11
// by defining CENTER_2D to be something else, like vec2(0.0)
12
// before the functions are include
13
14
// #define CENTER_2D vec2(0.0)
15
16
#include "lygia/draw/fill.glsl"
17
#include "lygia/space/aspect.glsl"
18
#include "lygia/space/ratio.glsl"
19
#include "lygia/space/center.glsl"
20
#include "lygia/space/uncenter.glsl"
21
#include "lygia/space/rotate.glsl"
22
23
#include "lygia/sdf/circleSDF.glsl"
24
#include "lygia/sdf/crossSDF.glsl"
25
#include "lygia/sdf/flowerSDF.glsl"
26
#include "lygia/sdf/gearSDF.glsl"
27
#include "lygia/sdf/heartSDF.glsl"
28
#include "lygia/sdf/hexSDF.glsl"
29
#include "lygia/sdf/polySDF.glsl"
30
#include "lygia/sdf/rectSDF.glsl"
31
#include "lygia/sdf/raysSDF.glsl"
32
#include "lygia/sdf/spiralSDF.glsl"
33
#include "lygia/sdf/starSDF.glsl"
34
#include "lygia/sdf/triSDF.glsl"
35
#include "lygia/sdf/vesicaSDF.glsl"
36
#include "lygia/sdf/rhombSDF.glsl"
37
38
void main(void) {
39
    vec4 color = vec4(vec3(0.0), 1.0);
40
    vec2 pixel = 1.0/u_resolution.xy;
41
    vec2 st = gl_FragCoord.xy * pixel;
42
43
    // Option 1
44
    st = ratio(st, u_resolution.xy);
45
46
    // // Option 2 
47
    // st = center(st);
48
    // st = aspect(st, u_resolution.xy);
49
    // st = uncenter(st);
50
51
    float cols = 4.0; 
52
    st *= cols;
53
    vec2 st_i = floor(st);
54
    vec2 st_f = fract(st);
55
    // st_f = center(st_f);
56
57
    st_f = rotate(st_f, u_time * 0.4);
58
59
    float sdf = 0.0;
60
    float index = ( st_i.x + (cols-st_i.y - 1.0) * cols);
61
    
62
    if (index == 0.0)
63
        sdf = circleSDF( st_f );
64
    else if (index == 1.0)
65
        sdf = vesicaSDF( st_f, 0.25 );
66
    else if (index == 2.0)
67
        sdf = rhombSDF(st_f);
68
    else if (index == 3.0)
69
        sdf = triSDF( st_f );
70
    else if (index == 4.0)
71
        sdf = rectSDF( st_f, vec2(1.0) );
72
    else if (index == 5.0)
73
        sdf = polySDF( st_f, 5);
74
    else if (index == 6.0)
75
        sdf = hexSDF( st_f );
76
    else if (index == 7.0)
77
        sdf = starSDF(st_f, 5);
78
    else if (index == 8.0)
79
        sdf = flowerSDF(st_f, 5);
80
    else if (index == 9.0)
81
        sdf = crossSDF(st_f, 1.0);
82
    else if (index == 10.0)
83
        sdf = gearSDF(st_f, 10.0, 10);
84
    else if (index == 11.0)
85
        sdf = heartSDF(st_f);
86
    else if (index == 12.0)
87
        sdf = raysSDF(st_f, 14);
88
    else if (index == 13.0)
89
        sdf = spiralSDF(st_f, 0.1);
90
    else
91
        sdf = 1.0;
92
93
    color.rgb += fill(sdf, 0.5);
94
    
95
    gl_FragColor = color;
96
}
97

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