I have a tiled XZ world comprised of separate meshes. Additionally, I've implemented a water shader that draws patterns based on world space coordinates. Furthermore, I have a camera that can both move and rotate.
The issue I'm encountering is that the rotation of UV coordinates occurs around the world origin rather than around the camera. However, the movement functions correctly by simply adding the camera's XZ position to the UV coordinates.
I use https://www.shadertoy.com/view/3tKBDz
here is the code of vertex shader
vec3 camera_position = -mtx_view[3].xyz;
vec4 p = mtx_worldview * vec4(position.xyz , 1.0);
var_position = p + vec4(camera_position.xyz , 1.0);
vec2 uv = p.xz + camera_position.xz;
var_texcoord0 = uv ;
var_normal = normalize(( mtx_view * vec4(normal, 0.0)).xyz);
gl_Position = mtx_proj * p;
and fragment shader
void main()
{
gl_FragColor = vec4(water(var_texcoord0, vec3(0,1,0)),1);
}
// Procedural texture generation for the water
vec3 water(vec2 uv, vec3 cdir)
{
uv = vec2(0.25);
float iTime = time_resolution.x;
// Parallax height distortion with two directional waves at
// slightly different angles.
vec2 a = 0.025 cdir.xz / cdir.y; // Parallax offset
float h = sin(uv.x + iTime); // Height at UV
uv += a * h;
h = sin(0.841471 * uv.x - 0.540302 * uv.y + iTime);
uv += a * h;
// Texture distortion
float d1 = mod(uv.x + uv.y, M_2PI);
float d2 = mod((uv.x + uv.y + 0.25) * 1.3, M_6PI);
d1 = iTime * 0.07 + d1;
d2 = iTime * 0.5 + d2;
vec2 dist = vec2(
sin(d1) * 0.15 + sin(d2) * 0.05,
cos(d1) * 0.15 + cos(d2) * 0.05
);
vec3 ret = mix(WATER_COL, WATER2_COL, waterlayer(uv + dist.xy));
ret = mix(ret, FOAM_COL, waterlayer(vec2(1.0) - uv - dist.yx));
return ret;
}