ANALYZING THE TV NOISE SHADER

  Introduction

Play Movie

The TV Noise shader produces the effect of common television noise, complete with randomly colored pixels and alternating black scan lines.


  Section 1

Open the scene file:

tvNoise.ma

Graph the tvNoise material.

This shader produces the effect of television noise by using an expression to assign color to a Surface Shader depending on the result from a Sampler Info node.


  Section 2

When Maya renders an image, the renderer scans the frame, pixel by pixel, to determine what color the pixel should be.

For each pixel, a number of shading samples are taken to calculate this color. In general, for each of those samples, the renderer determines the closest point visible, and uses the properties of the surface, of the environment, and of the lighting conditions to determine the color of the sample.

Unlike most materials however, the tvNoise material gets its color as part of the sampling process. As the render is carried out, any point being shaded that contains a surface that has the tvNoise material assigned to it is checked by the Sampler Info node for its value in the Y screen direction. That information, known as Pixel Center Y, is then passed on to an Expression node which uses it to determine what color will be assigned to the tvNoise material at that point.

    (Note that the size of the rendered pixels has been greatly exaggerated to illustrate the process.)


  Section 3

PIXEL CENTER

In any image, the pixels of the image can be identified by their X and Y co-ordinates. If you imagine an image as consisting of rows and columns of pixels, and if X,Y are the co-ordinates of a pixel, then X corresponds to the column in which the pixel resides, and Y corresponds to its row. A row of pixels is commonly referred to as a scan line. For example, an image with a resolution of 640 by 480 would have columns of pixels numbered from 0 to 639, and scan lines numbered from 0 to 479.

The Pixel Center X and Pixel Centre Y parameters of a Sampler Info node correspond to the X and Y co-ordinates of the pixel being shaded.

In the tvNoise shader, the Pixel Center Y parameter is used to supply an expression with information regarding the point being shaded within the given pixel.

If the Sampler Info node finds that the pixel being rendered is on an evenly numbered row (in the Y direction of the frame) then it assigns a random color to the pixel, but if the pixel is on an oddly numbered line it assigns the color black to the pixel.

Note that in the image above, the pixel being sampled is not on an evenly numbered line in the Y direction so the expression will assign the color black to the pixel.


  Section 4

Once the Pixel Center Y value has been determined, an expression is used assign color to the tvNoise shader.

This is the expression:

    //TVnoise

    if (samplerInfo.pixelCenterY % 2)

    {

    tvNoise.outColorR = 0;

    tvNoise.outColorG = 0;

    tvNoise.outColorB = 0;

    }

    else

    {

    tvNoise.outColorR = rand (1);

    tvNoise.outColorG = rand (1);

    tvNoise.outColorB = rand (1);

    }

In Other words, the expression says:

Check the pixel currently being rendered. If it is on an oddly numbered line in the Y direction go to the first set of instructions, which say to assign a value of 0 to Red, Green, and Blue (the color black) for the tvNoise shader at the pixel being rendered. BUT, if the pixel currently being rendered is NOT on an oddly numbered line then go to the next set of instructions, which say to assign random numbers between 0 and 1 to Red, Green, and Blue.


  Section 5

CONDITIONAL EXPRESSIONS.

The TVnoise Expression contains a conditional expression that uses the "if/else" format to control the behaviour of the shading network. In short this means that the expression will execute one set of commands "if" a given condition is true, "else" another set of commands will be executed.

The "if" statement is used to check if a given condition is true:

    if (samplerInfo.pixelCenterY % 2)

and an "else" statement to be performed if the condition is not true.

In this case, the conditional expression is equivalent to the statement "Pixel Center Y is an odd number".

Maya considers the condition to be true if a value other than 0 is returned from the operation, and false if the operation returns the value 0.


  Section 6

EVALUATING THE CONDITION.

The "if" statement described above uses the mathematical operation Modulus to determine wether or not the Pixel Center Y value is an odd number.

Modulus divides one integer value (a number with no decimal places) by another integer and then returns the value of the remainder as the smallest non-negative integer posible.

For example:

    2 % 2 = 0

When 2 is divided by 2 the answer equals 1 with nothing left over, therefore the Modulus is 0.

Here's another example:

    213 % 2 = 1

213 divided by 2 equals 106.5, but note that the Modulus DOES NOT equal 0.5. The modulus of 213 divided by 2 is 1. This is how the operation is performed with modulus.


  Section 7

ACTING ON THE CONDITION

In the condition:

    if (samplerInfo.pixelCenterY % 2)

Maya is asking, "is the modulus 2 of the value for pixelCenterY 0, or something else?". If the answer is something other than 0 (such as 1) then Maya considers the condition to be true and proceeds to the first set of commands:

    {

    tvNoise.outColorR = 0;

    tvNoise.outColorG = 0;

    tvNoise.outColorB = 0;

    }

But if the answer is 0, then Maya considers the condition to be false, and performs the commands listed after the "else" statement:

    else

    {

    tvNoise.outColorR = rand (1);

    tvNoise.outColorG = rand (1);

    tvNoise.outColorB = rand (1);

    }

The first example, 2 % 2 would evaluate as "Not True" since the result is 0, and the pixel would be rendered with a random value for Red, Green, and Blue.

The second example 213 % 2 would evaluate as "True" since the result is 1, and the pixel would be rendered black.


  Conclusion

Play movie

The TV Noise shader is a good example of how an expression can evaluate a given condition, in this case wether the Pixel Center Y value was evenly divisable by 2, to determine what color should be used at the point being rendered.


Your use of this file confirms your agreement to the
Terms and Conditions set out in the Terms and Conditions page.