Back to ArticlesArchitecture Blog
2026-08-2812 min read

The Mathematics of WebGL: Parametric Manifolds, Fourier Waves & 60fps GPU Shaders

How computer graphics algorithms leverage trigonometric parametric equations, Fourier series decomposition, and quaternions in GLSL shaders to render real-time 3D manifolds at 60fps.

Marco Romero
Frontend Architect & Mathematician
MathematicsWebGLThree.jsComputer GraphicsGLSL

At the intersection of pure mathematics and high-performance frontend engineering lies WebGL Computer Graphics. While naive web animations evaluate frame coordinates on CPU threads using JavaScript loops, production-grade 3D graphics offload mathematical equations directly onto the Graphics Processing Unit (GPU) via custom GLSL (OpenGL Shading Language) shaders.

Below is an exploration of how advanced mathematical concepts—such as Parametric Torus Knots, Fourier Harmonics, and Quaternion Rotations—are translated into real-time 60fps web graphics.


📐 1. Parametric Manifolds & Torus Knot Curves

In differential geometry, a Torus Knot is a closed spatial curve that lies on the surface of an unknotted 3D torus. It is defined mathematically by coprime integer winding parameters $(p, q)$, where $p$ represents the number of longitudinal wraps and $q$ represents the number of meridional wraps around the torus.

The 3D Cartesian coordinates $(x, y, z)$ of a $(p, q)$-torus knot as a function of parameter $t in [0, 2pi]$ are given by:

ascii
x(t) = [R + r * cos(q * t)] * cos(p * t)
y(t) = [R + r * cos(q * t)] * sin(p * t)
z(t) = r * sin(q * t)

Where:

  • $R$ is the major radius (distance from center to tube midpoint).
  • $r$ is the minor radius (radius of the tube).
  • $p, q$ determine the topological knot winding invariant (e.g. $p=2, q=3$ yields a classic Trefoil Knot).

⚡ GPU Offloading via GLSL Shaders

Rather than generating static 3D vertex buffers on the CPU main thread, graphics pipelines compute vertex positions dynamically inside GLSL shaders:

glsl
// GLSL Vertex Shader
uniform float uTime;
uniform float uP;
uniform float uQ;
varying vec3 vNormal;
varying vec3 vPosition;

void main() {
  float t = position.x * 6.28318 + uTime * 0.2;
  float R = 4.2;
  float r = 0.95 + 0.2 * sin(position.y * 10.0 + uTime);
  
  vec3 knotPos;
  knotPos.x = (R + r * cos(uQ * t)) * cos(uP * t);
  knotPos.y = (R + r * cos(uQ * t)) * sin(uP * t);
  knotPos.z = r * sin(uQ * t);
  
  vNormal = normalize(normalMatrix * normal);
  vPosition = knotPos;
  gl_Position = projectionMatrix * modelViewMatrix * vec4(knotPos, 1.0);
}
glsl
// GLSL Fragment Shader - Cosmic Fresnel Glow & Specular Shading
uniform float uTime;
varying vec3 vNormal;
varying vec3 vPosition;

void main() {
  vec3 viewDir = normalize(-vPosition);
  float fresnel = pow(1.0 - dot(viewDir, vNormal), 3.0);
  
  vec3 baseColor = mix(vec3(0.01, 0.52, 0.78), vec3(0.96, 0.62, 0.04), fresnel);
  float glow = sin(uTime * 2.0 + vPosition.x) * 0.15 + 0.85;
  
  gl_FragColor = vec4(baseColor * glow, 0.4 + fresnel * 0.5);
}

🌊 2. Fourier Series & Wave Superposition

Fourier Analysis states that any periodic continuous function $f(t)$ can be decomposed into an infinite sum of sinusoidal harmonics:

ascii
f(t) = a0/2 + ∑ [ an * cos(n * ω * t) + bn * sin(n * ω * t) ]

In web graphics, ocean wave rendering (Gerstner waves) combines multiple Fourier frequencies to render realistic fluid distortion in fragment shaders without main-thread CPU overhead:

ts
// Three.js Custom Shader Material Integration
const waveMaterial = new THREE.ShaderMaterial({
  uniforms: {
    uTime: { value: 0 },
    uColor: { value: new THREE.Color(0x0284c7) },
  },
  vertexShader: myVertexShader,
  fragmentShader: myFragmentShader,
  transparent: true,
  wireframe: true,
});

🔄 3. Quaternion Rotations Without Gimbal Lock

Representing 3D rotations using Euler angles $(phi, heta, psi)$ leads to the mathematical singularity known as Gimbal Lock, where a degree of freedom is lost when two axes align.

Computer graphics solves this using Unit Quaternions $mathbf{q} in mathbb{H}$:

ascii
q = w + x*i + y*j + z*k,  where i² = j² = k² = i*j*k = -1

Spherical Linear Interpolation (Slerp) allows smooth 60fps rotation between two orientation quaternions $mathbf{q}_0$ and $mathbf{q}_1$:

ascii
Slerp(q0, q1; t) = [ sin((1-t)*θ) / sin(θ) ] * q0 + [ sin(t*θ) / sin(θ) ] * q1

[!TIP] Performance Rule: Always use quaternions (THREE.Quaternion) for camera and mesh rotations in Three.js scenes to guarantee smooth vector math across all 3D rotational planes.


💡 Best Practices for High-Performance Graphics

  1. Keep Main Thread Idle: Never run 60fps coordinate math loops in JavaScript; pass uniform time counters (uTime) into WebGL shaders.
  2. Minimize Draw Calls: Group geometric meshes into instanced buffer geometries (InstancedMesh).
  3. Responsive Mobile Fallbacks: Automatically detect mobile viewports (window.innerWidth < 768) to scale down fragment shader precision or render 2D parametric canvas curves.
Explore All Articles
Written by Marco Romero • Frontend Architect