Friday, 12 April 2013

Toon Shading


                One of the most interesting looking shaders that were shown in our classes was the Toon Shader. This shader gives the models an interesting look that gives the models a more comic look. The Toon Shader uses cell shading and edge detection to create these comic like characters.

                In the program, cell shading happens before the edge detection program starts. The Toon Shader takes in 2 uniforms. These 2 uniforms are the qMap, and the image of the toon texture. The qMap shows the values of how much lighting calculations should be done on a section. It is a monochrome gradient that ranges from white to black. The toon texture is the texture that is to be applied on the model. This texture is usually simple, and uses bright colours.

                In the cell shader 2 varyings are needed in the vertex shader, the texture coordinates and the normal. The position, colour, texture coordinate and normal are calculated and given to the fragment shader for further processing.


The fragment shader uses the uniforms from the initial input and has a new constant, the light to eye direction which is a vec3 (x, y, z). In the main function the normal is normalized and the diffuse is calculated by taking the max value of the dot product of the normalized normal and the light to eye direction. The qMap is used with the diffuse lighting to calculate for the blocky value which only takes the ‘x’ value of the vector. The colour is calculated with the sampled texture image and texture coordinates. The shader then exports the fragment data that it has calculated.


                The second program after cell shading is the edge detection program. This program takes in the colour, normal, depth and pixel size to calculate for the edges of an object and to outline them. The edge detection program only has a fragment shader, no vertex shader. The edges are calculated using the Sobel operator which calculates the horizontal edges and vertical edges. Horizontal and vertical passes should be check separately because it is much more convenient. In the main the fragment shader calculates the fragment colour by multiplying the colour with the final edge of the algorithm. And now Toon shading is completed!



                Some of my favourite games that utilize the Toon Shader is a popular game known as Boarderlands. This game uses Toon Shading rather than to create a more realistic look, since their art style is more towards a comic book style game rather than military like game such as Battlefield. Another game that uses Toon Shading is Elsword which is a Korean made game but they also have an art style to look like a comic book.
                


1 comment: