Three.js Wave Interference (Double-Slit Ripple Tank)

GPU ripple tank in Three.js showing constructive and destructive interference from multiple coherent point sources, with adjustable wavelength, separation, phase, damping and an intensity view that reveals nodal lines.

Key topics: three.js wave interference, ripple tank, double slit, superposition, physics simulation, shader

What this code does

- Superposition: the vertex shader sums `A/(1+γr) · sin(kr − ωt + φ)` from up to six sources and displaces the plane.
- Nodal Lines: where path differences are half a wavelength the waves cancel; switch to intensity mode to see them clearly.
- Wavelength vs Separation: more fringes appear as source separation grows relative to λ, the same relation as Young's experiment.
- Phase Steps: shifting each source's phase steers the interference pattern, which is how phased-array antennas work.
- Performance: a 320×320 `PlaneGeometry` is animated entirely on the GPU with a `ShaderMaterial`.
- Controls: sources, separation, phase step, wavelength, speed, amplitude, damping, color mode and wireframe.

Continue Learning

JavaScript Implementation

ES6+
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 200)
camera.position.set(0, 16, 20)
camera.lookAt(0, 0, 0)
const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setSize(width, height)
document.querySelector('#app').appendChild(renderer.domElement)

const uniforms = {
  uTime: { value: 0 },
  uA: { value: new THREE.Vector2(-3, 0) },
  uB: { value: new THREE.Vector2(3, 0) }
}
const material = new THREE.ShaderMaterial({
  uniforms,
  vertexShader: `
    uniform float uTime; uniform vec2 uA; uniform vec2 uB;
    varying float vH;
    void main () {
      vec3 p = position;
      float k = 6.2831853 / 2.0;
      float h = sin(k * length(p.xy - uA) - 4.0 * uTime) + sin(k * length(p.xy - uB) - 4.0 * uTime);
      p.z += h * 0.4;
      vH = h * 0.5;
      gl_Position = projectionMatrix * modelViewMatrix * vec4(p, 1.0);
    }`,
  fragmentShader: `
    varying float vH;
    void main () {
      vec3 c = vH > 0.0 ? mix(vec3(0.04), vec3(1.0, 0.45, 0.25), vH) : mix(vec3(0.04), vec3(0.15, 0.45, 1.0), -vH);
      gl_FragColor = vec4(c, 1.0);
    }`
})
const plane = new THREE.Mesh(new THREE.PlaneGeometry(40, 40, 300, 300), material)
plane.rotation.x = -Math.PI / 2
scene.add(plane)

function animate (t) {
  requestAnimationFrame(animate)
  uniforms.uTime.value = t / 1000
  renderer.render(scene, camera)
}
animate(0)