Preparing the cubemap
You must prepare the cubemap to be used in the refraction implementation:
To prepare the cubemap, do the following:
- Place a camera in the center of the refractive geometry.
- Hide the refractive object and render the surrounding static environment to a cubemap in the six directions. You can use this cubemap for implementing both refraction and reflection.
- Bake the environment surrounding the refractive object into a static cubemap.
- Determine the direction of the refraction vector, and find where it intersects with the bounding box of the local environment.
- Apply the local correction in the same way as Dynamic soft shadows based on local cubemaps.
- Build a new vector from the position where the cubemap was generated, to the intersection point. Use this final vector to fetch the texel from the cubemap, to render what is behind the refractive object.
Instead of fetching the texel from the cubemap using the refracted vector Rrf, you find the point P where the refracted vector intersects the bounding box and build a new vector R'rf from the center of the cubemap C to the intersection point P. Use this new vector to fetch the texture color from the cubemap.
Find intersection point P
Float4 col = texCube(Cubemap, R'rf);
The following figure shows a scene with a cubemap and the refraction vectors:
Figure 6-42 The local correction to refraction vector
The refraction produced by this technique is accurately physically based, because the direction of the refraction vector is calculated using Snell’s Law.
There is also a built-in function that you can use in your shader to find
the refraction vector
R strictly according to the
R = refract( I, N, eta);
Iis the normalized view or incident vector.
Nis the normalized normal vector.
etais the ratio of indices of refractions n1/n2.
The following figure shows the flow of shaders that implement refraction based on a local cubemap:
Figure 6-43 Shader implementations of refraction based on local cubemap