LYGIA Shader Library

torusSDF (lygia/sdf/torusSDF)

generate the SDF of a torus

Use:

<float> torusSDF( in <vec3> pos, in <vec2> h [, in <float> ra, in <float> rb] )

Check it on Github


#ifndef FNC_TORUSSDF
#define FNC_TORUSSDF
float torusSDF( vec3 p, vec2 t ) { return length( vec2(length(p.xz)-t.x,p.y) )-t.y; }

float torusSDF(in vec3 p, in vec2 sc, in float ra, in float rb) {
    p.x = abs(p.x);
    float k = (sc.y*p.x>sc.x*p.y) ? dot(p.xy,sc) : length(p.xy);
    return sqrt( dot(p,p) + ra*ra - 2.0*ra*k ) - rb;
}
#endif

Use:

<float> torusSDF( in <float3> pos, in <float2> h [, in <float> ra, in <float> rb] )

Check it on Github


#ifndef FNC_TORUSSDF
#define FNC_TORUSSDF
float torusSDF( float3 p, float2 t ) { return length( float2(length(p.xz)-t.x,p.y) )-t.y; }

float torusSDF(in float3 p, in float2 sc, in float ra, in float rb) {
    p.x = abs(p.x);
    float k = (sc.y*p.x>sc.x*p.y) ? dot(p.xy,sc) : length(p.xy);
    return sqrt( dot(p,p) + ra*ra - 2.0*ra*k ) - rb;
}
#endif

Use:

<float> torusSDF( <float3> pos, <float2> h [, <float> ra, <float> rb] )

Check it on Github


#ifndef FNC_TORUSSDF
#define FNC_TORUSSDF
float torusSDF( float3 p, float2 t ) { return length( float2(length(p.xz)-t.x,p.y) )-t.y; }

float torusSDF(float3 p, float2 sc, float ra, float rb) {
    p.x = abs(p.x);
    float k = (sc.y*p.x>sc.x*p.y) ? dot(p.xy,sc) : length(p.xy);
    return sqrt( dot(p,p) + ra*ra - 2.0*ra*k ) - rb;
}
#endif

Check it on Github


fn torusSDF(p: vec3f, t: vec2f ) -> f32 { 
    return length( vec2f(length(p.xz) - t.x, p.y) ) - t.y;
}

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