Forum

Map(3d buffer) bug

[edit] initial report boiled down to a bug report

two supposed identical patches, cpu(white) and gpu(green), what could throw the second patch off-center?

walk 2d points onto sdf.v4p (1.1 MB)

map(buffer) nodes in wrap mode happen to return values outside of the wrapped range

map(buffer) bug.v4p (39.1 KB)

I really suck at math, but it seems the modulo operator % used inside the mapWrap function inside map.fxh in this case doesn’t do what it was expected to do.

The original looks like this:

float mapWrap(float Input, float InMin, float InMax, float OutMin, float OutMax)
{
	float range = InMax - InMin;
	float normalized = (Input - InMin) / range;	
	float output = OutMin + normalized * (OutMax - OutMin);
    if (normalized < 0) normalized = 1 + normalized;
	return  OutMin + (normalized % 1) * (OutMax - OutMin);
}

I changed it to:

float mapWrap(float Input, float InMin, float InMax, float OutMin, float OutMax)
{
	float range = InMax - InMin;
	float normalized = (Input - InMin) / range;	
	return  OutMin + zmod(normalized, 1) * (OutMax - OutMin);
}

and added a hacked together zmod function, that most likely can be optimized (or is there something like it already in HLSL?):

float zmod(float z, float d)
{
    float r = z;

    if (z >= d) 
    {
        r = z % d;
    }

    if (z < 0.0)
    {
        r = z % d;
        if (r != 0.0)
        {
            r += d;
        }
    }
    return  r;
}

Now it seems to work as expected.
map.fxh.7z (1.3 KB)
Maybe @everyoneishappy could have a look at it. Or open an issue on gihub.

1 Like

confirmed that it works now. will update on github. thank you::