Search code examples
javascriptshader

How to translate this Shader function to JS?


So I found this very useful peace of code in Shader vec2 cmp = step(t_max.xy, t_max.yx);. I need to translate it to JavaScript. But no idea how.

Vec2 I simply assume as 2d array, where index 0 is x and 1 is y.

var cmp = step([t_max[0], t_max[1]], [t_max[1], t_max[0]]);

But the step part hard for me. Even after checking https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/step.xhtml


Solution

  • step, as defined in GLSL, simply takes two parameters: edge and x (or input). It returns 0.0 when x < edge and 1.0 when the opposite is true. To use it with a vec2 (an array of size 2 - not 2d array) you can define a function like this:

    function step(edge, input) {
        x = edge[0] > input[0] ? 0.0 : 1.0;
        y = edge[1] > input[1] ? 0.0 : 1.0;
        return [x, y]
    }