If you have tried recreating Apple's Liquid Glass aesthetic with CSS backdrop-filter: blur(), you already know the result: it looks like frosted plastic, not solid optical glass.
The reason is physical, not stylistic. backdrop-filter applies a uniform Gaussian blur across a flat rectangle. Real glass bends light selectively along its curved bezel, subtly magnifies whatever passes under its core, splits RGB wavelengths near the rim, and catches sharp specular highlights at grazing angles.
While building Dinggao (a library of 126 single-file, zero-dependency UI motion references), we needed a floating Liquid Glass Dock that:
- Runs locked at 60fps on desktop and mobile.
- Uses zero external dependencies (no Three.js, no React runtime—just one standalone HTML file).
- Responds instantaneously to pointer/touch movement and returns smoothly to a deterministic loop.
Here is the exact architecture and WebGL2 fragment shader we used.
1. Split the Work: Offscreen 2D Canvas + Single-Pass WebGL2
A common mistake is trying to render crisp vector icons or typography directly inside WebGL geometry. Instead, we separate static crispness from optical distortion:
- Offscreen Canvas 2D: Draw the monochrome tray and vector icons once at
Math.min(devicePixelRatio, 2)and upload the canvas as a static GPU texture (texImage2D). - WebGL2 Fullscreen Triangle: Run a single fragment shader pass per frame that samples that texture through an analytical capsule lens.
Because the background texture only rebuilds on viewport resize, per-frame GPU cost stays negligible.
2. Aspect-Locked Layout Math (Avoiding Mobile SDF Distortion)
If you scale a capsule's width by viewport width and its height by viewport height, any portrait viewport (aspect < 1) makes halfSize.y > halfSize.x and pinches the rounded box into a distorted oval.
Always derive both dimensions from a single uniform scale fitW and guard the corner radius in GLSL:
function getLayout(w, h) { const safeW = Math.max(2, w); const safeH = Math.max(2, h); const aspect = safeW / safeH; const portraitBoost = aspect < 1 ? clamp(1.0 + (1.0 - aspect) * 0.55, 1.0, 1.28) : 1.0; const fitW = Math.min(safeW * portraitBoost, safeH * 1.6); const sc = fitW / 640; return { fitW, sc, cx: safeW * 0.5, cy: safeH * 0.52 }; } 3. Analytical Capsule SDF + Convex Rim Refraction
Inside the #version 300 es fragment shader, we compute the signed distance d to the floating glass pill and its outward normal grad:
float r = min(u_halfSize.x, u_halfSize.y); float halfLen = max(0.0, u_halfSize.x - r); float d = sdRoundedBox(p, u_halfSize, r); vec2 grad = gradCapsule(p, halfLen); float bezelW = r * 0.74; float edgeT = clamp(1.0 + d / bezelW, 0.0, 1.0); // Convex lens interior magnification + strong optical rim refraction float rimProfile = pow(edgeT, 2.05); vec2 rimRefract = -grad * rimProfile * (25.5 * u_scale) / u_res; vec2 coreMag = -p * 0.115 / u_res; vec2 baseDisp = rimRefract + coreMag; Notice how rimProfile = pow(edgeT, 2.05) keeps the center clean and legible while bending pixels strongly along the curved outer 74% bevel—matching the curvature of a physical glass lens.
4. Subtle RGB Chromatic Dispersion (Without Rainbow Slop)
Heavy rainbow aberration immediately looks cheap. Splitting the UV displacement by just 1.12 / 1.00 / 0.88 across Red, Green, and Blue creates crisp optical prism edges over high-contrast monochrome icons without turning neon:
vec2 uvR = clamp(v_uv + baseDisp * 1.12, 0.001, 0.999); vec2 uvG = clamp(v_uv + baseDisp * 1.00, 0.001, 0.999); vec2 uvB = clamp(v_uv + baseDisp * 0.88, 0.001, 0.999); vec3 refracted; refracted.r = sampleScene(uvR).r; refracted.g = sampleScene(uvG).g; refracted.b = sampleScene(uvB).b; 5. Dual-Direction Fresnel Rim Highlights
Finally, two opposing directional Fresnel terms give the pill unmistakable studio-lit depth: a crisp white top-left key highlight (dirA) and a restrained cold-sky bottom-right bounce (dirB):
float rimBand = pow(edgeT, 3.3); vec2 dirA = normalize(vec2(-0.62, 0.78)); vec2 dirB = normalize(vec2( 0.62, -0.78)); float fresnelA = pow(max(0.0, dot(grad, dirA)), 1.5) * rimBand; float fresnelB = pow(max(0.0, dot(grad, dirB)), 1.9) * rimBand; glass = mix(glass, vec3(0.99, 0.995, 1.0), clamp(fresnelA * 0.95, 0.0, 1.0)); glass += vec3(0.220, 0.741, 0.973) * fresnelB * 0.56; Try It Live & Grab the Full Single-File Source (MIT)
- Live interactive demo + AI Rebuild Pack: WebGL2 Liquid Glass Floating Dock
- All 12 free open-source single-file components (MIT): github.com/VaneAi888/dinggao-ui
- Full library (126 interactive 60fps finals): dinggao.design
If you use Grok Build (formerly Cursor), Gemini, or Claude Code, try pasting the single-file HTML straight into your editor and asking it to port the dock into your React or Vue layout—keeping the GLSL uniforms and getLayout() math intact preserves 90%+ of the optical feel on the first pass.