This is driving me mad… I have a fragment shader that generates up to 4 ripples on the screen, but it doesn’t render correctly. The ripples go about halfway, then flash to the edge of the screen.
I used to have all the code (repeated 4 times) in main(). In that version, only one of my waves would render incorrectly. After moving the code to a function they all do. Also, performance is much worse with the function…
precision mediump float;
uniform vec4 u_Wave0;
uniform vec4 u_Wave1;
uniform vec4 u_Wave2;
uniform vec4 u_Wave3;
varying vec3 v_Position;
varying vec4 v_Color;
vec4 getColor(in vec4 wave, in vec3 coord, in vec4 vcolor) {
vec4 newColor = vec4(0.1, 0.2, 0.4, 1.0);
vec4 white = vec4(1, 1, 1, 1);
float distance = distance(coord.xy, wave.xy);
float wave1 = abs(distance - wave.z);
wave1 = clamp(wave1, 0.0, 25.0);
wave1 = (25.0 - wave1) / 35.0;
vec4 color = mix(vcolor, white, newColor.rrra);
color = mix(vcolor, color, wave1);
return color;
}
void main()
{
vec3 coord = gl_FragCoord.xyz;
vec4 color = getColor(u_Wave0, coord, v_Color);
vec4 color1 = getColor(u_Wave1, coord, v_Color);
vec4 color2 = getColor(u_Wave2, coord, v_Color);
vec4 color3 = getColor(u_Wave3, coord, v_Color);
color3 = max(color3, color);
color3 = max(color3, color1);
color3 = max(color3, color2);
gl_FragColor = color3;
}