sooon
sooon

Reputation: 4878

Math differences between GLSL and Metal

I play around with GLSL and got this effect. And I tried to convert it to metal but I got some funky result for y-axis when it is smaller than 0:

enter image description here

There are these funny curvy crop off for most of the cubes above the horizon(<0). This is my Metal code:

static float mod(float x, float y)
{
    return x - y * floor(x/y);
}

static float vmax(float3 v) {
    return max(max(v.x, v.y), v.z);
}

float fBoxCheap(float3 p, float3 b) { //cheap box
    return vmax(abs(p) - b);
}

static float map( float3 p )
{   
    p.x = mod(p.x + 5,10)-5;
    p.y = mod(p.y + 5 ,10)-5;
    p.z = mod(p.z + 5 ,10)-5;

    float box = fBoxCheap(p-float3(0.0,3.0,0.0),float3(4.0,3.0,1.0));

    return box;
}

It is almost the same code in GLSL:

float vmax(vec3 v) {
    return max(max(v.x, v.y), v.z);
}

float box(vec3 p, vec3 b) { //cheap box
    return vmax(abs(p) - b);
}

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.) );
}

How can I resolve this? I am fairly new to both GLSL and Metal but I find Metal is more tricky because of these math issue.

Upvotes: 3

Views: 784

Answers (1)

warrenm
warrenm

Reputation: 31782

I don't think there's a difference here. You can create similar artifacts in the GL version by applying all of the same modifications you do in the Metal version. The problem is that offsetting the point after you fold space with mod violates the requirement that the SDF be Lipschitz continuous (i.e., the gradient must be <= 1 everywhere). If you want to translate the box, translate p before applying mod.

Upvotes: 2

Related Questions