I'm having trouble with a shader in my Phaser game. I've applied the shader as a post pipeline, hoping it would cover the whole screen with a water effect. But when I move my character around, the effect moves too. It's like the shader is stuck to the camera instead of staying put on the screen.
I've put together a quick demo so you can see what I mean. You can use the arrow keys to move around and check out the issue: https://phaser.io/sandbox/vFDT9xXy
Here's the relevant shader code:
const waterFragShader = `
#ifdef GL_ES
precision mediump float;
#endif
uniform float uTime;
uniform vec2 uResolution;
uniform sampler2D uMainSampler;
varying vec2 outTexCoord;
void main(void)
{
vec2 uv = gl_FragCoord.xy / uResolution.xy;
vec2 distortion = vec2(
sin(uv.y * 10.0 + uTime) * 0.002,
cos(uv.x * 10.0 + uTime) * 0.002
);
vec4 texture_color = texture2D(uMainSampler, uv + distortion);
vec4 k = vec4(uTime)*0.6;
k.xy = uv * 7.0;
float val1 = length(0.5-fract(k.xyw*=mat3(vec3(-2.0,-1.0,0.0), vec3(3.0,-1.0,1.0), vec3(1.0,-1.0,-1.0))*0.5));
float val2 = length(0.5-fract(k.xyw*=mat3(vec3(-2.0,-1.0,0.0), vec3(3.0,-1.0,1.0), vec3(1.0,-1.0,-1.0))*0.2));
float val3 = length(0.5-fract(k.xyw*=mat3(vec3(-2.0,-1.0,0.0), vec3(3.0,-1.0,1.0), vec3(1.0,-1.0,-1.0))*0.5));
float pattern = pow(min(min(val1,val2),val3), 7.0) * 2.0;
vec4 pattern_color = vec4(1, 1, 1, pattern);
vec4 color = vec4(pattern, pattern, pattern, pattern);
gl_FragColor = mix(texture_color, pattern_color, pattern_color.a);
}
`;
class WaterPipeline extends Phaser.Renderer.WebGL.Pipelines
.PostFXPipeline {
constructor(game) {
super({
game,
name: "Water",
fragShader: waterFragShader,
});
}
onPreRender() {
this.set1f("uTime", this.game.loop.time / 1000);
}
onDraw(renderTarget) {
this.set2f("uResolution", renderTarget.width, renderTarget.height);
this.bindAndDraw(renderTarget);
}
}
Any ideas on how I can get this shader to stay put and cover the whole screen, no matter where the camera moves?