I'm basically following this Codrops example to add a displacement map effect for my website. However I want to make it a slideshow with direction, e.g. clicking "next" would make the displacement map move right and transition the image, and the opposite for clicking "previous". Below is the shader code:
Fragment Shader:
varying vec2 vUv;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D disp;
uniform float dispFactor;
uniform float effectFactor;
void main() {
vec4 disp = texture2D(disp, vUv);
vec2 distortedPosition1 = vec2(uv.x + dispFactor * (disp.r*effectFactor), uv.y);
vec2 distortedPosition2 = vec2(uv.x - (1.0 - dispFactor) * (disp.r*effectFactor), uv.y);
vec4 _texture1 = texture2D(texture1, distortedPosition1);
vec4 _texture2 = texture2D(texture2, distortedPosition2);
vec4 finalTexture = mix(_texture1, _texture2, dispFactor);
gl_FragColor = finalTexture;
}
Right now I can only get the direction to be alternating right / left. To do this, I keep track of the current texture I'm displaying on the slideshow. To transition, I set the other texture uniform (texture2
if texture1
is being displayed and vice versa) and then tween the dispFactor
back and forth between 0
and 1
. So if I keep clicking "next", the animation would be swiping right, left, right, left, and so on.
I'm trying to wrap my head around this and figure out a way to input a direction but I think that's where I hit the ceiling of my Three.js knowledge.