Mouse input in this GLSL shader?

This shader adapted from the Book of Shaders works in a shader image generator – but how can I use the workaround from this bug report to add mouse input? (That is, replace u_mouse with a port for mouse input)?

// Author @patriciogv - 2015
// https://thebookofshaders.com/edit.php#10/ikeda-03.frag
// Title: Ikeda Data Stream

float random (in float x) {
    return fract(sin(x)*1e4);
}

float random (in vec2 st) {
    return fract(sin(dot(st.xy, vec2(12.9898,78.233)))* 43758.5453123);
}

float pattern(vec2 st, vec2 v, float t) {
    vec2 p = floor(st+v);
    return step(t, random(100.+p*.000001)+random(p.x)*0.5 );
}

void main() {
    vec2 st = gl_FragCoord.xy/RENDERSIZE.xy;
    st.x *= RENDERSIZE.x/RENDERSIZE.y;

    vec2 grid = vec2(100.0,50.);
    st *= grid;

    vec2 ipos = floor(st);  // integer
    vec2 fpos = fract(st);  // fraction

    vec2 vel = vec2(TIME*2.*max(grid.x,grid.y)); // time
    vel *= vec2(-1.,0.0) * random(1.0+ipos.y); // direction

    // Assign a random value base on the integer coord
    vec2 offset = vec2(0.1,0.);
        
   // how can u_mouse be changed to mouse input port in Vuo?
	vec2 u_mouse = vec2(100.0,100.0);
	vec3 color = vec3(0.);
	
    color.r = pattern(st+offset,vel,0.5 + u_mouse.x/RENDERSIZE.x);
    color.g = pattern(st,vel,0.5 + u_mouse.x/RENDERSIZE.x);
    color.b = pattern(st-offset,vel,0.5 + u_mouse.x/RENDERSIZE.x);

    // Margins
    color *= step(0.2,fpos.y);

    gl_FragColor = vec4(1.0-color,1.0);
}

@jersmi,

One of our developers used these steps:

  • File > New Shader > Image Generator
  • pasted the source code
  • removed the vec2 u_mouse = vec2(100.0,100.0); line
  • renamed the other u_mouse identifiers to just mouse (since Vuo doesn’t allow underscores in published port names)
  • in the sidebar, clicked New Port > Single Value > 2D Point, and called it mouse

Now the shader can run standalone (attached).

Then you can put it in a Modules folder, and create a Vuo composition using it (optionally feeding actual mouse coordinates to it using Receive Mouse Moves).

jersmi.shaderTest.fs.zip (1.33 KB)