PDA

View Full Version : Problem making water, dudv image map keeps translating to the left.

hashbrown
06-18-2017, 03:11 PM
I'm attempting to create water using a dudv map, but having trouble with the waving effect. I managed to create the effect but as time goes by, the wave effect moves to the left of the quad until disappearing. I'll show you an example and if you look a carefully, you'll see what I'm talking about, here's a screen cap:

http://recordit.co/F0dt7H63px

I'm learning how to make water so please excuse me. I'm suspecting this has to do with the refractionTexCoords += totalDistortion.xy line, but I'm not sure how to avoid this visual bug. I'll share the portion of the frag shader that is in charge with movig the water. Thanks in advance!

// Oh and this is how I compute the clipSpace in the vertex shader: clipSpace = mvp * a_position;

vec2 ndc = (clipSpace.xy/clipSpace.w) / 2.0 + 0.5;

vec2 reflectionTexCoords = vec2(ndc.x, -ndc.y);
vec2 refractionTexCoords = vec2(ndc.x, ndc.y);
vec4 distortion1 = (texture2D(dudv, vec2(newTexCoords.x + moveFactor, newTexCoords.y).rg * 2.0 - 1.0)) * waveStr;
vec4 distortion2 = (texture2D(dudv, vec2(-newTexCoords.x + moveFactor, newTexCoords.y + moveFactor).rg * 2.0 - 1.0)) * waveStr;
vec4 totalDistortion = distortion1 + distortion2;

refractionTexCoords += totalDistortion.xy;
refractionTexCoords = clamp(refractionTexCoords, 0.001, 0.999);

reflectionTexCoords += totalDistortion.xy;
reflectionTexCoords.x = clamp(reflectionTexCoords.x, 0.001, 0.999);
reflectionTexCoords.y = clamp(reflectionTexCoords.y, -0.999, -0.001);

vec4 reflectionTex = texture2D(reflection, reflectionTexCoords);
vec4 refractionTex = texture2D(refraction, refractionTexCoords);

vec4 finalColor = mix(refractionTex, reflectionTex, 0.5);
finalColor = mix(finalColor, vec4(0.0, 0.3, 0.2, 1.0), 0.5);

gl_FragColor = finalColor;

Update:

I recorded another video but without the waving effect, if you notice the borders of the water texture tile moves, but initially matched. I'm starting to think I have two problems here :/

http://recordit.co/BqftsO0FCu

GClements
06-18-2017, 04:48 PM
vec4 distortion1 = (texture2D(dudv, vec2(newTexCoords.x + moveFactor, newTexCoords.y).rg * 2.0 - 1.0)) * waveStr;

Is applying the unsigned->signed mapping to the texture coordinates intentional? Or did you intend to apply it to the values retrieved from the texture instead?

vec2 distortion1 = (texture2D(dudv, vec2(newTexCoords.x + moveFactor, newTexCoords.y)).rg * 2.0 - 1.0) * waveStr;

hashbrown
06-18-2017, 04:51 PM
Is applying the unsigned->signed mapping to the texture coordinates intentional? Or did you intend to apply it to the values retrieved from the texture instead?

vec2 distortion1 = (texture2D(dudv, vec2(newTexCoords.x + moveFactor, newTexCoords.y)).rg * 2.0 - 1.0) * waveStr;

Hi GClements, if I understand correctly, I was planning on retrieving the values from the dudv texture for the distortion.

Oh I left out what newTexCoords is, my apologies: vec2(a_texCoords.x / 2.0 + 0.5, a_texCoords.y/2.0 + 0.5) * 1.0;

GClements
06-19-2017, 02:40 AM
Hi GClements, if I understand correctly, I was planning on retrieving the values from the dudv texture for the distortion.
But does the dudv texture use a signed format? Or is it intentional that the distortion vector is always in the first quadrant (X/Y both positive)?

hashbrown
06-19-2017, 07:07 PM
But does the dudv texture use a signed format? Or is it intentional that the distortion vector is always in the first quadrant (X/Y both positive)?

Sorry GC, it's intentional that the distortion vector is the first quadrant. The dudv texture i got provides unsigned values. My apologies if I don't seem very clear, I've been learning slowly with a tutorial.

hashbrown
06-20-2017, 09:58 PM
I forgot to mention, I'm starting to think that the problem with the dudv texture moving has to do with not tiling the texture properly. I tried to render only the dudv texture and as soon it moves out of it's original position, it looks stretched:

https://s22.postimg.org/c6ubafwj1/bug.png (https://postimg.org/image/c6ubafwj1/)

For water, I'd say it should be tiled so it waves repeatedly. But for some reason when I try to tile dudv image from the vertex shader, anything over 1 would look streched.