You copied the Doc URL to your clipboard.

# Preparing the cubemap

You must prepare the cubemap to be used in the refraction implementation:

To prepare the cubemap, do the following:

1. Place a camera in the center of the refractive geometry.
2. 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.
3. Bake the environment surrounding the refractive object into a static cubemap.
4. Determine the direction of the refraction vector, and find where it intersects with the bounding box of the local environment.
5. Apply the local correction in the same way as  Dynamic soft shadows based on local cubemaps.
6. 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.

`float eta=n2/n1;`

```float3Rrf = refract(D,N,eta);```

Find intersection point P

Find vector ```R'rf = CP;```

`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 Snell's law:

`R = refract( I, N, eta);`

Where:

• `I` is the normalized view or incident vector.

• `N` is the normalized normal vector.

• `eta` is 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 