3. Sdfs

Signed distance fields #

SDFs Primitivas #

Esfera Caja Tourus Cono

SDFs primitivas
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Esfera
float sdSphere( vec3 p, float s )
{
return length(p)-s;
}

//Caja
float sdBox( vec3 p, vec3 b )
{
vec3 q = abs(p) - b;
return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0);
}

//Tourus
float sdTorus( vec3 p, vec2 t )
{
vec2 q = vec2(length(p.xz)-t.x,p.y);
return length(q)-t.y;
}

//Cono
float sdCone( in vec3 p, in vec2 c, float h )
{
// c is the sin/cos of the angle, h is height
// Alternatively pass q instead of (c,h),
// which is the point at the base in 2D
vec2 q = h\*vec2(c.x/c.y,-1.0);

vec2 w = vec2( length(p.xz), p.y );
vec2 a = w - q*clamp( dot(w,q)/dot(q,q), 0.0, 1.0 );
vec2 b = w - q*vec2( clamp( w.x/q.x, 0.0, 1.0 ), 1.0 );
float k = sign( q.y );
float d = min(dot( a, a ),dot(b, b));
float s = max( k*(w.x*q.y-w.y*q.x),k*(w.y-q.y) );
return sqrt(d)\*sign(s);
}

SDFs y operaciones #

Script p5 raymarching.js
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function drawShader() {
push();

    shader(rayMarchingShader);

    rayMarchingShader.setUniform('cameraDistance', easycam.getDistance());
    rayMarchingShader.setUniform('dMatrix', dMatrix().mat3);
    rayMarchingShader.setUniform('join', checkbox.checked());

    quad(-1, -1, 1, -1, 1, 1, -1, 1);

    pop();

}
Fragment shader raymarching.frag
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
uniform bool join;

// Get the closest distance from p to world
float world(vec3 p) {
// Set point to initial camera distance
p /= 500.;

    // transform the point to simulate camera zoom
    p /= 1. / cameraDistance;

    // apply rotation and translation transformations from object
    p *= dMatrix;

    // sphere SDF translated on Y axis to see dMatrix effects
    float sphere1 = length(p - vec3(-.5, 0., 0.)) - .5;
    float sphere2 = length(p - vec3(0., 0., 0.)) - .5;

    if(join) return min(sphere1, sphere2);
    else return max(sphere1, sphere2);

}

SDFs en fractales #

Script p5 raymarching.js
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function drawShader() {
push();

    shader(rayMarchingShader);

    rayMarchingShader.setUniform('cameraDistance', easycam.getDistance());
    rayMarchingShader.setUniform('dMatrix', dMatrix().mat3);
    rayMarchingShader.setUniform('N', N);

    quad(-1, -1, 1, -1, 1, 1, -1, 1);

    pop();

}
Fragment shader raymarching.frag
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
uniform float N;

// Aux function
float map(float value, float istart, float istop, float ostart, float ostop) {
return ostart + (ostop - ostart) \* ((value - istart) / (istop - istart));
}

// Mandelbulb SDF
float mandelbulbSDF(vec3 pos) {
vec3 p = pos;

    float r = 0.;
    float dw = 1.0;
    vec3 w = p;

    for(int l = 0; l < MAX_ITERATIONS; l++) {
        // get derivative of w
        dw = pow(r, float(N) - 1.) * float(N) * dw + 1.;

        // cartesian to polar
        r = length(w);
        float f = atan(w.y, w.x);
        float t = acos(w.z / r);

        float zr = pow(r, float(N));
        t *= float(N);
        f *= float(N);

        // Nylander formula
        w = p + zr * vec3(sin(t) * cos(f), sin(t) * sin(f), cos(t));

        // Check if r bounds
        if(r > 2.)
            break;
    }

    // Detail in function of camera distance
    float bandFactor = map(cameraDistance, 500., 300., 0.5, 1.);

    // Hubbard-Douady potential see https://iquilezles.org/articles/distancefractals/
    float dist = bandFactor * log(r) * r / dw;

    return dist;

}

// Get the closest distance from p to world
float world(vec3 p) {
// Set point to initial camera distance
p /= 500.;

    // transform the point to simulate camera zoom
    p /= 1. / cameraDistance;

    // apply rotation and translation transformations from object
    p *= dMatrix;

    // Distance from p to mandelbulb
    float mandelbulb = mandelbulbSDF(p);

    return mandelbulb;

}

Referencias #