How to make smooth transition in Three.js with shaders?

Viewed 1118

I have a basic Three.js app which just adds two textures to the plane. I pass them as uniforms to my shaders where I transform one to another when my mouse enters the plane.

And my question is is there a way to add a transition to this process of changing one texture to another? So they change more smoothly than right now:

enter image description here

My fragment shader:

uniform vec2 u_resolution;
uniform float u_time;
uniform vec2 u_mouse;
uniform float u_progress;

uniform sampler2D image;
uniform sampler2D displacementTexture;

uniform int mouseIntersects;

varying vec2 vUv;

void main() {
    vec2 st = gl_FragCoord.xy/u_resolution.xy;
    vec4 mainTexture = texture2D(image, vUv);
    vec4 displacementTexture = texture2D(displacementTexture, vUv.yx);
    vec2 disaplacedUv = vec2(
        vUv.x, 
        vUv.y
    );

    if (mouseIntersects == 1) {
        disaplacedUv.y = mix(vUv.y, displacementTexture.r + 0.2, 0.2);

        gl_FragColor = texture2D(image, disaplacedUv);
    } else {

        gl_FragColor = mainTexture;
    }
}

Three.js setup

const setup = () => {
  ...scene, camera etc

  uniforms = {
    image: { type: 't', value: new THREE.TextureLoader().load(whale) },
    displacementTexture: { type: 't', value: new THREE.TextureLoader().load(texture) },
    mouseIntersects: { type: 'f', value: 0 },
  };

  material = new THREE.ShaderMaterial({
    side: THREE.DoubleSide,
    uniforms: uniforms,
    vertexShader: vertex,
    fragmentShader: fragment,
  });

  const geometry = new THREE.PlaneGeometry(1, 1, 32, 32);

  mesh = new THREE.Mesh(geometry, material);

  scene.add(mesh);

  raycaster = new THREE.Raycaster();

  document.addEventListener('mousemove', (event) => updateMousePositions(event), false);
};

const updateMousePositions = (event) => {
  event.preventDefault();
  uniforms.u_mouse.value.x = (event.clientX / window.innerWidth) * 2 - 1;
  uniforms.u_mouse.value.y = -(event.clientY / window.innerHeight) * 2 + 1;
};

const animate = () => {
  requestAnimationFrame(animate);
  render();
};

const render = () => {
  uniforms.u_time.value += 0.05;
  camera.lookAt(scene.position);

  camera.updateMatrixWorld();
  raycaster.setFromCamera(uniforms.u_mouse.value, camera);
  const intersects = raycaster.intersectObject(mesh);
  
  if (intersects.length === 1) {
    uniforms.mouseIntersects.value = 1;
  } else {
    uniforms.mouseIntersects.value = 0;
  }

  renderer.render(scene, camera);
};

setup();
animate();
1 Answers

Instead of using uniform int mouseIntersects; make it a float so you can transition it from 0.0 to 1.0 in a smooth gradient instead of a hard discrete step. Then you could use the mix() GLSL command in your shader to smoothly transition from one texture to the other:

uniform sampler2D image;
uniform sampler2D displacementTexture;
uniform int mouseIntersects;
varying vec2 vUv;

void main() {
    vec4 mainTexture = texture2D(image, vUv);
    vec4 altTexture = texture2D(displacementTexture, vUv);

    // 0.0 outputs mainTexture, 1.0 outputs altTexture
    gl_FragColor = mix(mainTexture, altTexture, mouseIntersects);
}

Finally, in JavaScript, you can interpolate this value with MathUtils.lerp() when the mouseover takes place:

var targetValue = 0;

const render = () => {

  // ...

  if (intersects.length === 1) {
    targetValue = 1;
  } else {
    targetValue = 0;
  }

  // This will smoothly transition from its current value to the targetValue on each frame:
  uniforms.mouseIntersects.value = THREE.MathUtils.lerp(uniforms.mouseIntersects.value, targetValue, 0.1);

  renderer.render(scene, camera);
};

... or you could use an animation library like GSAP that might give you more control

Related