2.27
Generic Refraction

- 2.27.1. Overview
- 2.27.2. Algorithm
- 2.27.3. Sources
- 2.27.4. Vectors
- 2.27.5. Colors
- 2.27.6. Masking
- 2.27.7. Types

2.27.1
Overview
1
The r2 package implements the generic refraction effect described in GPU Gems 2. The technique lends itself to a huge range of effects such as lenses, glass, heat haze, and water - simply by varying the meshes and textures used when performing refraction.
2.27.2
Algorithm
1
For a given instance, the process to render the instance is as follows:
2.27.2.2 Algorithm

1. Produce a mask, if necessary.
2. Render the instance using a given source image, vector texture, color, and mask image.

3
The actual rendering technique is very simple: Given a screen-space position (x, y), sample the color from a source image at (x + s, y + t), where (s, t) are signed per-pixel offset values that are sampled from textures or derived from an associated vector texture.
2.27.3
Sources
1
The refraction effect typically uses a (possibly downsized) image of the scene as a source image. The r2 allows for use of an arbitrary image.
2.27.4
Vectors
1
Refraction vectors are sampled from the red and green components of a delta texture. The sampled values are scaled by the material's scale factor and used directly to calculate (x + s, y + t). For example, a simple noisy red/green delta texture applied to a quad results in the following effect:
2.27.4.2 Noise quad
2.27.4.3 Noise quad (texture)
2.27.5
Colors
1
The sampled scene colors used to perform the refraction effect are multiplied by a constant color, specified by each material. This allows for simple colored glass effects (shown here with a specular-only instance rendered over the top of the refractive instance to provide specular highlights):
2.27.5.2 Color 0
3
Using pure RGBA white (1.0, 1.0, 1.0, 1.0) results in a clear glass material:
2.27.5.4 Color 1
2.27.6
Masking
1
Because refractive instances are translucent, they are normally rendered after having already rendered all of the opaque objects in the scene. Because rendering of translucent instances occurs with depth testing enabled, it is therefore possible for opaque instances to occlude refractive instances. This poses a problem for the implementation of refraction described above, because the pixels of an occluding object may be sampled when performing the refraction, as shown in the following image:
2.27.6.2 Refraction bleeding
3
Note how the pixels of the opaque instances are bleeding into the refracting object, despite being conceptually in front of it. This is because the refraction effect is implemented in screen space and is just sampling pixels from the surrounding area to simulate the bending of light rays. Using a mask prevents this:
2.27.6.4 Refraction without bleeding
5
A mask is produced by rendering a monochrome silhouette of the refracting object, and then using the values of this mask to linearly interpolate between the colors c at (x, y) and the colors r at (x + s, y + t). That is, a value of m = 0 sampled from the mask yields mix c r m = mix c r 0 = c, and a value of m = 1 sampled from the mask yields mix c r m = mix c r 1 = r. This has the effect of preventing the refraction simulation from using pixels that fall outside of the mask area.
2.27.6.6 Mask
7
The mask image can also be softened with a simple box blur to reduce artifacts in the refracted image.
2.27.7
Types
1
In the r2 package, the refraction effect is provided by rendering a translucent instance with a refraction shader such as R2RefractionMaskedDeltaShaderSingle.
2
Masks can be produced via implementations of the R2MaskRendererType interface.