2.5 Sample Application: Cartoon Rendering (Part)
Read this article shows that you have agreed to the statement
As an example of the second vertex shader, let's write two vertex shaders, which are colorful to grid coloring (OUTLINE) with cartoon style. Figure 17.2 shows this:
Figure 2.2: (a) Use the cartoon colored object (attention to the sharp transition between the color). (B) Enhance the cartoon effect, the silhouette Edge is tick. (C) Use standard scattering light coloring objects
Note: Cartoon rendering is a specific type of non-realistic rendering, sometimes referred to as stylish rendering.
Although cartoon rendering does not apply to all games, such as fierce first person shooting games, it still enhances some atmosphere that wants to express cartoon feel type. In addition, cartoon rendering is quite easy to implement and allows us to have a beautiful demonstration vertex shader.
We divide cartoon rendering for two steps:
1. Cartoon drawing is typically rarely colored intensity of coloring between two shades; we call Cartoon Shading. In Figure 2.2 (a), we see only three colored intensity (bright, middle, dark) to color the grid, and the transition between them is not coherent - unlike Figure 2.2 (c), The bright and dark transition is smooth.
2. Cartoon drawing also typically has its own silhouette of the edge, as shown in Figure 2.2 (b).
These two steps require their respective vertex shaders.
2.5.1 Cartoon Coloring
To achieve cartoon coloring, we use Lander to publish the methods described in Game Developer Magazine in March 2000. "Shades of Disney: Opaquing A 3D World". It works like this: We created a grayscale texture with intensity level, which contains different colored intensities we need. Figure 2.3 shows the texture we used in the sample program.
Figure 2.3: Coloring texture used to store colored intensity. Note that the discontinuous coloring between transitions and texture coloring strength must be increased from left to right.
Then in the vertex shader, we perform a standard scattering dot product to determine the cosine between the angle between the vertex method n and the light vector L, to determine how many light received by the vertex:
s = l · n
If S <0, the angle between the light vectors and the vertex sheet is greater than 90 degrees, and it means that the surface is not received in the light. Therefore, if S <0, we let s = 0. So s ∈ [0, 1].
Now, in the usual scatter light model, we use S to mark color vectors, so that the vertex color is dimmed according to the amount of light received:
DiffuseColor = S (R, G, B, A)
However, this will result in a smoothed transition from brightness between darkness. This is the opposite of the cartoon colors we expect. We want a unconnected transition in some different coloring rooms for cartoon rendering in two to four shades.
Anti-road, do not use s to mark color vector, we are ready to use S as a U-texture coordinate of the strength texture mentioned earlier - as shown in Figure 2.3. Note: Scarar S must be a valid texture coordinate because s ∈ [0, 1], this is a range of usual texture coordinates.
In this way, the vertex will not be smoothed, but discontinuous. For example, the strength texture may be divided into three colors, as shown in Figure 2.4:
Figure 2.4: So, the value of s ∈ [0, 0.33] uses Shader0 coloring, s ∈ [0.33, 0.66] using shader1 coloring, s ∈ [0.66, 1] using shader2 coloring. Of course, a transition from these coloring to another is discontinuous, which gives us the desired effect.
Note: We also close the texture filtration for cartoon coloring, because this filter will try to make the colored transition smooth. This is redundant for the discontinuous transition we have requested.
2.5.2 Cartoon Coloring Vertex Coloring Code
We now present cartoon colored vertex shaders. The main task of this shader is only calculated according to S = L · N and sets texture coordinates. Pay attention to the observation of the output structure, we have added a data member to store the textured coordinates that have been calculated. At the same time, it is necessary to pay attention to it, we still output the vertex color, although we do not modify it, but when the color is combined with the intensity texture, it is presented as colored.
// file: toon.txt
// Desc: Vertex Shader That Lights Geometry So It Appears To BE
// DRAWN IN A CARTOON STYLE.
//
// globals
//
Extern Matrix WorldViewMatrix;
Extern Matrix WorldViewProjmatrix;
Extern vector color;
EXTERN Vector Lightdirection;
Static Vector Black = {0.0F, 0.0F, 0.0F, 0.0F};
//
// structures
//
Struct vs_INPUT
{
Position: position;
VECTOR NORMAL: NORMAL;
}
Struct vs_output
{
Position: position;
FLOAT2 UVCOORDS: TEXCOORD;
Vector Diffuse: Color;
}
//
// main
//
Vs_output main (vs_input input)
{
// Zero Out Each MEMBER in OUTPUT
Vs_output output = (vs_output) 0;
// Transform Vertex Position To Homogenous Clip Space
Output.position = MUL (Input.position, WorldViewProjmatrix);
//
// Transform Lights and Normals to View Space. Set W
// Components to Zero Since We're Transforming Vectors.
// Assume There no scalings in the world
// Matrix as well.
//
LightDirection.w = 0.0f;
Input.NORMAL.W = 0.0F;
LightDirection = MUL (LightDirection, WorldViewMatrix); Input.Normal = MUL (Input.Normal, WorldViewMatrix);
//
// compute the 1d texture coordinate for toon rendering.
//
Float u = DOT (LightDirection, Input.Normal);
//
// Clamp to Zero if u is negative Because U
// NEGATIVE IMPLIES THE Angle Between THE LIGHT
// and Normal Is Greater Than 90 DegRees. And
// if this is true kiln Surface Receives
// No Light.
//
IF (u <0.0f)
u = 0.0f;
//
// set Other
TEX
Coord to Middle.
//
Float v = 0.5F;
Output.uvcoords.x = u;
Output.uvcoords.y = v;
// Save Color
Output.diffuse = color;
Return Output;
}
Two points:
n We assume that the world matrix does not perform any zoom. Because if it is executed, it will run in the length and direction of its vertices.
n We always set the midpoint of the v texture coordinates for textures. This means that we only use a single line in the texture, that is, we can use the 1D intensity texture instead of 2D texture. No matter what, 1D and 2D textures work. In this example, we used 2D textures instead of 1D texture, this is not a special reason.
[Declaration]: Introduction to 3D Game Programming With DirectX 9.0 "in this article, it is limited to the translator level, and it is inevitable that there is a mistake in the text. Welcome all netizen criticism; this article is only used for learning exchange and reference usage, not to use In any form of commercial use; if you need to reprint the author's consent of the author and the translator, maintain the integrity of the article, indicate the author, translator and source, the author retains all rights to translation. For the consequences of violating the above terms, the translator is not responsible for this. My MSN is raymond_king123@hotmail.com, welcome 3D graphics and games, and friends with a certain graphic programming exchange.