Curved Glass Lens Refraction Models in Games
Simulating light refraction through curved glass lenses in real-time game development requires mathematical models that balance optical realism with strict computational budgets. This article outlines the fundamental equations and approximations used by graphics programmers, ranging from foundational vector physics like Snell’s Law and Fresnel equations to screen-space distortion functions, the Lensmaker's equation, and chromatic dispersion models.
Snell’s Law in Vector Form
The physical foundation of any refraction simulation is Snell’s Law (\(n_1 \sin\theta_1 = n_2 \sin\theta_2\)), which governs how light bends across the interface of two media with different refractive indices (\(n_1\) and \(n_2\)). In modern real-time shaders (HLSL/GLSL), this is computed using the vector form to avoid expensive trigonometric functions:
\[\mathbf{T} = \eta \mathbf{I} + \left( \eta \cos\theta_1 - \sqrt{1 - \eta^2 (1 - \cos^2\theta_1)} \right) \mathbf{N}\]
Where:
- \(\mathbf{I}\) is the normalized incident light vector.
- \(\mathbf{N}\) is the normalized surface normal of the curved glass.
- \(\eta = \frac{n_1}{n_2}\) is the ratio of the refractive indices.
- \(\cos\theta_1 = -\mathbf{N} \cdot \mathbf{I}\).
- \(\mathbf{T}\) is the resulting transmitted (refracted) direction vector.
If the term under the square root becomes negative, total internal
reflection occurs, and the light reflects rather than transmits. This
equation forms the basis of the standard refract()
intrinsic function in graphics pipelines.
The Lensmaker's Equation and Paraxial Optics
For optical elements such as sniper scopes, camera lenses, or spectacles, calculating refraction at both the front and back surfaces of a 3D mesh is computationally expensive. Developers often utilize the Lensmaker’s Equation to approximate the net behavior of a curved lens:
\[\frac{1}{f} = (n - 1) \left[ \frac{1}{R_1} - \frac{1}{R_2} + \frac{(n - 1)d}{n R_1 R_2} \right]\]
Where:
- \(f\) is the focal length.
- \(n\) is the refractive index of the glass.
- \(R_1\) and \(R_2\) are the radii of curvature of the two lens surfaces.
- \(d\) is the thickness of the lens.
When simplified to the thin-lens approximation (\(d \approx 0\)), this allows shaders to compute magnification and virtual image displacement algebraically on a flat billboard or simple geometry rather than tracing internal volumetric paths.
Brown-Conrady Polynomial Distortion Model
To simulate the physical magnification, barrel, or pincushion distortion produced by curved glass without ray-tracing, games commonly deform screen-space UV coordinates using the Brown-Conrady radial distortion model:
\[x_{\text{distorted}} = x_c + (x - x_c)(1 + K_1 r^2 + K_2 r^4 + K_3 r^6)\] \[y_{\text{distorted}} = y_c + (y - y_c)(1 + K_1 r^2 + K_2 r^4 + K_3 r^6)\]
Where:
- \((x_c, y_c)\) is the optical center of the lens.
- \(r = \sqrt{(x - x_c)^2 + (y - y_c)^2}\) is the radial distance from the center.
- \(K_1, K_2, K_3\) are radial distortion coefficients (positive values produce pincushion distortion; negative values produce barrel distortion typical of thick convex lenses).
Cauchy’s Equation for Chromatic Aberration
Real curved glass disperses white light into its constituent spectral colors because the refractive index varies with wavelength (\(\lambda\)). To simulate this dispersion, shaders use Cauchy's empirical equation:
\[n(\lambda) = A + \frac{B}{\lambda^2}\]
In real-time implementations, this is simplified by evaluating Snell’s Law or the UV distortion function three separate times for discrete red, green, and blue wavelengths (\(n_{\text{red}} < n_{\text{green}} < n_{\text{blue}}\)). Sampling the background texture with these offset coordinates creates realistic color fringing along high-contrast curved edges.
Schlick's Approximation of Fresnel Equations
Refraction never occurs in isolation; the ratio of transmitted to reflected light changes based on the viewing angle relative to the surface curvature. The full Fresnel equations are replaced in games by Schlick's approximation:
\[R(\theta) = R_0 + (1 - R_0)(1 - \cos\theta)^5\]
Where \(R_0 = \left( \frac{n_1 - n_2}{n_1 + n_2} \right)^2\) is the reflectance at normal incidence (\(\theta = 0\)). The computed transmission coefficient \(T = 1 - R(\theta)\) determines the blend ratio between the refracted background image and the specular reflection on the curved glass.
Real-Time Pipeline Integration
In modern game engines (such as Unreal Engine and Unity), these mathematical models are applied within two primary architectures:
- Screen-Space Refraction (SSR): The curved normal vectors of the lens distort UV lookups into a pre-rendered background scene color buffer, scaled by thickness maps and the Snell-based offset.
- Ray Marching via Signed Distance Fields (SDF): For thick glass geometries where double-refraction (entering and exiting the lens) is required, rays are marched through the front interface, deflected by the entry normal, marched through the interior medium, and deflected again at the exit interface.