0
\$\begingroup\$

I am fairly new in GLSL and only recently discover Signed Distance Field. I am trying to figure out how SDF can perform so well when you use mod() function to repeat but badly when you use for loop function. I got a feeling that in for loop function, the ray marching is repeat as many times as the loop itself, but when using mod(), it only perform once. Can somebody explain in more detail how this works?

UPDATE1:

This is the mod() function:

float map( vec3 p )
{
    p.x=mod(p.x+3.0,6.0)-3.0;
    p.y=mod(p.y+3.0,6.0)-3.0;
    p.z=mod(p.z+3.0,6.0)-3.0;
    return box( p, vec3(1.,1.,1.) );
}

and Here is the for() loop function:

float map( vec3 p )
{
    p.yz = rotate(p.yz, mouse.y*10.);

    float b = box( p, vec3(1.,1.,1.) );
    float aa = b;

    for(int i=0; i<5; i++)
    {
    float off = float(i);
    vec3 q = p+off*.5;
    q.xz = rotate(q.xz, -mouse.x*off*12.);
    float c = box( q, vec3(off,1.,1.) );
    aa = fOpUnionRound(aa,c,.5);
    }
    return aa;
}
\$\endgroup\$
  • \$\begingroup\$ Can you post actual code samples? Without code it's very speculative. (But from what you did say, yes, doing something more-than-once will be slower than doing something once...) \$\endgroup\$ – david van brink Sep 11 '16 at 6:52
  • \$\begingroup\$ Code Posted. Please check Update1. \$\endgroup\$ – sooon Sep 14 '16 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.