About combining reflections
The reflections based on local cubemaps technique enables rendering high quality, efficient reflections based on static local cubemaps. However, if the objects are dynamic, the static local cubemap is no longer valid and the technique does not work.
You can solve this by combining static reflections with dynamically generated reflections.
The following figure shows combining reflections from static and dynamic geometry:
Figure 5-27 Combining reflections from static and dynamic geometry
If the reflective surface is planar, you can generate dynamic reflections with a mirrored camera.
To create a mirrored camera, calculate the position and orientation of the main camera that renders the reflections at runtime.
Mirror the position and orientation of the main camera, relative to the reflective plane.
The following figure shows the mirrored camera technique:
Figure 5-28 The mirrored camera technique for rendering planar reflections
In the mirroring process, the new reflection camera ends up with its axis in the opposite orientation. In the same manner as a physical mirror, reflections from the left and right are inverted. This means the reflection camera renders the geometry with an opposite winding.
To render the geometry correctly you must invert the winding of the geometry before rendering the reflections. When you have finished rendering the reflections, restore the original winding.
The following figure shows the sequence of steps required to set up the mirrored camera and render the reflections:
Figure 5-29 Main steps for setting up the mirrored camera and rendering the reflections
Build the mirror reflection transformation matrix. Use this matrix to calculate the position, and the world-to-camera transformation matrix, of the reflection camera.
The following figure shows the mirror reflection transformation matrix:
Figure 5-30 The mirror reflection transformation matrix
Apply the reflection matrix transformation to the position and world-to-camera matrix of the main camera. This provides you with the position and world-to-camera matrix of the reflection camera.
The projection matrix of the reflection camera must be the same as the projection matrix of the main camera.
The reflection camera renders reflections to a texture.
For good results, you must set up this texture properly before rendering:
- Set the filtering mode to trilinear.
Ensure the texture size is proportional to the area of the reflective surface. The larger the texture is, the less pixelated the reflections are.
An example script for a mirrored camera is located at: http://wiki.unity3d.com/index.php/File:MirrorReflection.png.