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:
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();
