refract
Calculates the direction of a vector after passing through a transparent medium based on Snell's Law, used to create distortion effects like glass and water.
Core Advantages
Provides a physically accurate and high-performance GPU built-in function for creating realistic transparent materials. Its core parameter, `eta` (ratio of indices of refraction), offers direct control over the distortion level, balancing realism with creative flexibility.
Common Uses
Glass and liquid materials
Heatwave and water ripple effects
Simulating chromatic dispersion in gems like diamonds
How to adjust
Adjust the `eta` parameter to control the distortion intensity: a value closer to 1.0 results in less bending, while a smaller value creates a stronger effect. Replace the geometric normal `N` with a normal map to add detailed, irregular distortions to the surface, like ripples on water.
Code Examples
1// Define the ratio of indices of refraction (e.g., air to glass)
2const eta = float(1.0 / 1.5);
3
4// Get the incident vector I and normal vector N
5const I = positionWorld.sub(cameraPosition).normalize();
6const N = normalWorld.normalize();
7
8// Calculate the refraction vector
9const refractVec = refract(I, N, eta);
10
11// Use the refraction vector to sample an environment map, getting the distorted color
12const refractedColor = envMap.uv(refractVec);