mod() works like gdscript fposmod()

RattyRatty Posts: 7Member

Still new to shaders, I just spotted that mod(x, y) acts like gdscripts fposmod(x,y) rather than fmod(x,y).
This shader code shows this:

shader_type canvas_item;
render_mode blend_add;

void fragment() {
    vec4 c = vec4(1., 1., 1., 1.);
    float centredx = -0.5 + UV.x;
    float bright = abs(mod(centredx, 0.1)) * 10.;
    //float bright = (centredx - 0.1 * floor(centredx / 0.1)) * 10.;
    //x - y * floor(x/y)  - this from https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/mod.xhtml
    c *= bright;
    COLOR = c;
}

The stripes all face the same way, they would switch direction when negative if it acted like fmod(). This does match with the OpenGL reference (the commented out calculation above does it the long way).

You can check this against GDscript using this code:

for i in range(-20, 20, 1):
    var f : float = float(i) / 100.0
    print([f, fmod(f, 0.1), fposmod(f, 0.1), f - 0.1 * floor(f / 0.1)])    

Does anyone know if there is a quick way of getting the same results as fmod() in the shader language?
Cheers.

Comments

  • RattyRatty Posts: 7Member

    Nevermind, worked it out :) :

    float fmod(float x, float y) {
        return sign(x) * (abs(x) - y * floor(abs(x) / y));
    }
    

    Seems a bit messy though, anyone got something better?

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file