Game Development Reference
In-Depth Information
The following are comments in code sample 5-9:
•
Lines 011-020
: The
CamerFader
class is responsible for cross fading between
Camera[0]
and
Camera[1]
. To achieve this, several variables are created.
The
Cameras
array maintains a list of cameras: two cameras in this case.
The
CamCols
array is linked to
Cameras
. It describes the color by which the
render from the camera will be multiplied; this allows the alpha value to
make the render transparent. The
FadeTime
variable defines the total time in
seconds for a camera fade in one direction, either fade-out or fade-in. Finally,
the
Mat
variable references any valid material that will be applied to the final
render from the main camera, that is, the pixels of the completed render,
including everything composited from all the other cameras.
•
Lines 023-038
: The
Start
method creates
RenderTexture
for each camera
that assigns the texture to its
TargetTexture
member. In essence, this
means each camera is assigned an internal texture to which its render
is locally composited.
•
Lines 033-052
: The
OnPostRender
event is called automatically by Unity
for any active camera objects in the scene, once for each frame and after the
camera has completed its render as normal. It gives the object an opportunity
to render additional cameras or elements on top of the normal rendered data.
Here, the
Render
method of each camera in the
Cameras
array is called; this
method manually renders the camera, not directly on screen but to its render
texture. Once rendered to the texture, the
Graphics.DrawTexture
function
draws
RenderTexture
for each camera onto the screen in the order of the
array, one atop the other. Notice that each
DrawTexture
call multiplies
the
CamCols
color to the texture; this also factors in the alpha component
for transparency.