Implementing Vertex Distortion in GLSL Shaders with Three.js
In this article, we'll walk through how to implement noise-based vertex distortion in GLSL shaders using Three.js. This effect can be used to create dynamic, wave-like movements for 3D objects by manipulating their vertex positions with noise functions, resulting in a highly interactive and visually appealing animation.
Setting up the Scene
To start, we first need to set up a basic Three.js scene, complete with a camera, renderer, and canvas. We'll also import several libraries to enhance the scene's functionality, including GSAP for animations and Lenis for smooth scrolling.
import * as THREE from 'three';
import GUI from 'lil-gui';
import gsap from 'gsap';
import Lenis from '@studio-freight/lenis';
import vertexShader from './shaders/vertex.glsl';
import fragmentShader from './shaders/fragment.glsl';
// Set up scene, camera, and canvas
const scene = new THREE.Scene();
const canvas = document.querySelector('canvas.webgl');
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 5000);
const renderer = new THREE.WebGLRenderer({ canvas: canvas, antialias: true });
Creating the Shader Material
Next, we need to define a custom shader material. In our case, we are working with a ShaderMaterial that combines both a vertex and fragment shader. The vertex shader is where we will apply our noise-based distortion.
// Define the geometry and texture for the mesh
const geometry = new THREE.PlaneGeometry(480 * 1.5, 800 * 1.5, 480, 800);
const material = new THREE.ShaderMaterial({
vertexShader: vertexShader,
fragmentShader: fragmentShader,
uniforms: {
uTime: { value: 0 },
uDistortion: { value: 0 },
uTexture: { value: textureImg }
}
});
// Create a mesh and add it to the scene
const mesh = new THREE.Points(geometry, material);
scene.add(mesh);
Adding Noise-Based Distortion to the Vertex Shader
The core of this effect lies in the vertex shader. We utilize Perlin noise and curl noise to distort the vertex positions dynamically. The curl_noise function generates a turbulence-like effect, which is then applied to the position of each vertex.
Here’s the vertex shader code that applies noise-based distortion:
uniform float uTime;
uniform float uDistortion;
varying vec2 vUv;
vec3 curl_noise(vec3 p) {
const float step = 0.01;
float ddx = cnoise(p+vec3(step, 0.0, 0.0)) - cnoise(p-vec3(step, 0.0, 0.0));
float ddy = cnoise(p+vec3(0.0, step, 0.0)) - cnoise(p-vec3(0.0, step, 0.0));
float ddz = cnoise(p+vec3(0.0, 0.0, step)) - cnoise(p-vec3(0.0, 0.0, step));
const float divisor = 1.0 / ( 2.0 * step );
return ( vec3(ddy - ddz, ddz - ddx, ddx - ddy) * divisor );
}
void main() {
vec3 distortion = vec3(position.x * 2., position.y, 1.) *
curl_noise(vec3(position.x * 0.02 + uTime * 0., position.y * 0.009, uTime * 0.1)) * uDistortion;
vec3 finalPosition = position + distortion;
vec4 modelViewPosition = modelViewMatrix * vec4(finalPosition, 1.);
gl_Position = projectionMatrix * modelViewPosition;
gl_PointSize = 1.5;
vUv = uv;
}
This shader works by applying the noise function to the vertex positions and adding the resulting distortion. The amount of distortion is controlled by the uDistortion uniform, which can be manipulated to create different visual effects.
Animating the Distortion
Now, let's animate the distortion based on the scroll position. To do this, we use the Lenis library for smooth scrolling and GSAP for smooth interpolation of the distortion effect. As the user scrolls, the distortion increases, creating a dynamic effect on the mesh.
// Set up Lenis for smooth scrolling
const lenis = new Lenis({ smooth: true, lerp: 0.1 });
// Update distortion based on scroll position
let targetDistortion = 0;
lenis.on('scroll', ({ progress }) => {
targetDistortion = progress;
});
// Smoothly interpolate the distortion
function animate() {
targetDistortion = gsap.utils.interpolate(targetDistortion, targetDistortion, 0.1);
material.uniforms.uDistortion.value = targetDistortion;
requestAnimationFrame(animate);
}
animate();
Conclusion
With this setup, we’ve created a mesh whose vertex positions are dynamically distorted based on noise functions and smooth scrolling. This effect can be further enhanced by adjusting the parameters of the noise functions or the shader itself. It’s an excellent way to add interactivity and depth to your 3D scenes!
Feel free to experiment with the code, tweak the noise parameters, and see how you can adapt the effect for your own projects. The possibilities are endless!
Happy coding!