6.1.11 Debugging shaders in Unity

In Unity it is not possible to debug shaders in the same way as you do with traditional code. You can however use the output of the fragment shader to visualize the values you want to debug. You then have to interpret the image produced.

In following fragment shader, the output color has been replaced by the
normalized local corrected reflected vector:

return float4(normalize(localCorrReflDirWS), 1.0);

Instead of the reflected image, it visualizes the components of the
reflected vector normalized as colors.

The reddish color zone in the floor indicates that the reflected vector has a
strong X component, that is, it is mostly oriented to toward the X Axis. The reddish
part shows the reflection coming from that direction, that is, from the windowed
wall.

The blueish zone indicates a predominance of reflected vectors oriented to Z axis, that
is, the reflection from the right wall.

In the black zone the vectors are mainly oriented to –Z but the colors
can only have positive components because the negative components are clamped to 0.

The following figure shows the result of replacing the output color of the
fragment by the normalized local reflected vector:

Figure 6-7 Shader debugging with multiple colors

It might initially be difficult to interpret the meaning of the colors while
debugging so try to focus on a single color component. For example, you can return only
the Y component of the normalized local corrected reflected vector:

In this case, the output is only the reflections coming mainly from the roof
above the camera. That is, the part of the room oriented to the Y axis. The reflections
from the walls of the room are coming from X, Z and –Z directions, so they are rendered
in black.

The following figure shows shader debugging with a single color:

Figure 6-8 Shader debugging with a single color

Check that the magnitude you are debugging with color, is between 0 and 1
because any other value is automatically clamped. Any negative value is assigned zero
and any value greater than 1 is assigned 1.