refract-liquid-glass
Refract liquid glass
Start with a visual test target. Place the panel across a thin diagonal, a bright window frame, and a dark surface. The line should stay recognizable through the face, curve inside the shoulder, and meet the undisturbed scene outside the rim.
Render the lens
-
Render the backdrop once to a texture before drawing glass. A Three.js scene can share its linear-color render target with the glass pass. For a DOM-only surface, use an accessible backdrop treatment unless the application controls a texture that represents the backdrop. Align the texture to the actual viewport and scroll offset.
-
Measure the DOM rect in CSS pixels. Convert fragment coordinates once to the same space, including the vertical axis. Evaluate a rounded-box signed distance and use its screen derivative for coverage. Reserve the high-curvature profile for the shoulder. A flat face preserves the detail inside a large tile.
-
Build a unit entrance normal from the rounded-box gradient and a smoothly varying tilt. Trace toward a backing plane with a modest glass index. These expressions assume an eye-facing normal, an incident ray along negative Z, and a thickness expressed in CSS pixels.
vec3 ray = refract(vec3(0., 0., -1.), normal, 1. / 1.46); vec2 offsetPx = thicknessPx * ray.xy / max(-ray.z, 0.15); vec2 sampleUV = (fragmentPx + offsetPx) / viewportPx; -
Use the displaced UV for transmission. Sample the surroundings farther along the rim normal for reflected color and increase its contribution toward grazing angles. Let hover shift the reflection and the apparent thickness locally. Inspect a dark and a bright background before tuning the rim intensity.
-
Render typography and focus indicators at display density. Size the drawing buffer to the CSS rectangle times the chosen device scale. Use linear filtering, derivative-based edge coverage and a filtered source for minified samples. If the room is expensive, reduce the atmospheric pass first. Increasing the final framebuffer cannot restore detail lost in the input texture.