Procedural GLSL Vertex & Fragment Waves.
Deforming 3D geometry dynamically on the GPU using custom GLSL trigonometric sine wave displacement and chromatic aberration.
View code
Run the JavaScript as a module. Keep the import map from the HTML panel.
<script type="importmap">{"imports":{"three":"https://cdn.jsdelivr.net/npm/three@0.180.0/build/three.module.js","three/addons/":"https://cdn.jsdelivr.net/npm/three@0.180.0/examples/jsm/"}}</script>
<canvas aria-label="Animation demo"></canvas>:root { color-scheme: dark; font: 15px system-ui, sans-serif; color: #dbff55; }
* { box-sizing: border-box; }
body { margin: 0; height: 100dvh; display: grid; place-items: center; overflow: hidden; background: transparent; }
.tile { display: grid; place-items: center; width: 68px; aspect-ratio: 1; border-radius: 18px; background: currentColor; }
.tile { background: #dbff55; color: #202415; font-size: 30px; }
canvas { display: block; width: 100%; height: 100%; min-width: 0; min-height: 0; }
button { font: inherit; color: #f3f1e9; background: #191919; border: 1px solid #ffffff66; padding: 8px 14px; border-radius: 24px; cursor: pointer; }
:focus-visible { outline: 2px solid #dbff55; outline-offset: -3px; }
.notice { max-width: 34ch; padding: 16px; color: #fff; }
@media (prefers-reduced-motion: reduce) { .sprite { animation-play-state: paused; } }import * as THREE from 'three';
// The gallery pauses this loop when the preview leaves the viewport.
let active = !matchMedia('(prefers-reduced-motion: reduce)').matches;
if (new URLSearchParams(location.search).has('embed')) active = false;
addEventListener('message', event => {
if (event.source === parent && event.origin === location.origin && event.data?.type === 'motion-play') {
active = event.data.active;
}
});
function loop(draw) {
let time = 0;
let previous;
function frame(now) {
const dt = previous === undefined ? 0 : Math.min((now - previous) / 1000, .05);
previous = now;
if (active && !document.hidden) {
time += dt;
draw(time, dt);
}
requestAnimationFrame(frame);
}
draw(0, 0);
requestAnimationFrame(frame);
}
const canvas = document.querySelector('canvas');
let renderer;
try {
renderer = new THREE.WebGLRenderer({canvas, antialias:true, alpha:true});
} catch {
canvas.replaceWith(Object.assign(document.createElement('p'), {
className: 'notice', textContent: 'This demo needs WebGL 2. Try a browser with hardware acceleration enabled.'
}));
throw new Error('WebGL 2 unavailable');
}
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(42, 1, .1, 120);
camera.position.z = 5;
function resize() {
renderer.setPixelRatio(Math.min(devicePixelRatio || 1, 2));
const width = Math.max(1, canvas.clientWidth);
const height = Math.max(1, canvas.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.render(scene, camera);
}
resize();
addEventListener('resize', resize);
const uniforms = {uTime:{value:0}};
const material = new THREE.ShaderMaterial({
uniforms, wireframe:true,
vertexShader: 'uniform float uTime; varying float vHeight; void main(){ vec3 p=position; p.z=.28*sin(5.0*p.x+uTime*2.0)*cos(3.0*p.y-uTime); vHeight=p.z; gl_Position=projectionMatrix*modelViewMatrix*vec4(p,1.0); }',
fragmentShader: 'varying float vHeight; void main(){ gl_FragColor=vec4(mix(vec3(.19,.46,.91),vec3(.83,1.0,.42),vHeight+0.5),1.0); }'
});
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(3.5, 2.45, 43, 31), material);
mesh.rotation.x = -.73;
scene.add(mesh);
loop(t => {
uniforms.uTime.value = t;
mesh.rotation.z = .14 * Math.sin(t);
renderer.render(scene, camera);
});