Vertex Shader supported cartoon stroke rendering
Cell Style Rendering Use Vertex Shader Pan Li Liang 2003-10-4 Stanly Lee 2003-10-4 Email: XheartBlue@tang.com HomePage GameHunter.3322.Net/xpertsoft/ Usually, we have seen 3D scenes in real light The model to illuminate rendering, although the real-time light model is only an approximate to the real world. But this model has already had a good acceptable real feeling. While people think about the results of the scene rendering, other non-real sense rendering techniques are also very useful. Comparable is the rendering of cartoon style, which is similar to our common cartoon painting. This article will introduce to use Vertex Shader to implement a cartoon style rendering.
1. What is a cartoon style rendering
First we have to figure out what is the rendering result of cartoon style, that is, what kind of effect we need. Investigate the results of the teapot rendering below, we got the following conclusions: Figure 1, teapot model rendered in cartoon style.
1. There is a more obvious edge black line, that is, the obvious contour feeling.
2, there must be a very sharp shadow contrast, that is, the color change is relatively fast.
3. When considering changes in viewpoint, we should also get the same effect.
2, solution. For the third point of demand mentioned above, we must establish a light direction in the view space. The direction of this light should not be affected by the transformation matrix. At the same time, this light direction should be consistent with our observation direction, only this can get the bright and dark effect shown in the figure. There are many ways to generate edge contour, and it is easier to think that it is to draw two times. The first time is drawn according to the normal illumination model, and the second pass is drawn, and the Z-Buffer detection is set to It passes equal time. But this shortcoming is that it is obvious: first you have to draw two times, seriously affect performance. Second, you can't set the width of the line in D3D, you can only get a relatively thin contour. The second method is the method of rotating the triangular triangle after the direction of viewing direction (direction of Camera). The result of the point represents a triangular degree close to the edge, and the edge must be a common side of a triangular triangle and a triangular side towards a triangle. If the absolute value of a triangular method and the point in the direction of Camera, the closer it is, the closer it is from the edge, and if it is negative, it indicates that it is from the observer. We can rendering the triangles near the edges into black so that you can create a heavy edge effect to the object. Figure 2, the influence of the light and triangular normal angle of the light, the longitudinal coordinate is the light intensity, the abscissa is the angle of the angle, how to create a sharp bright and dark change problem, the normal source direction and the angle of the normal light The impact is shown in Figure 2. Normal illumination is shown in blue, usually it is a string cosine curve. In order to achieve the effect we need, the curve we want is shown in the green image of the above figure. When the angle is less than a certain value, the curve is flat, indicating that most of the area we have seen has a brighter coloring effect. After a sharp transformation, it has become very flat in a place near 90 degrees (red in Figure 2). Curves in the circle), this is mainly because we want to make a relatively strong edge line effect. 3. Implementing the method In the usual practice, there are two ways to map the normal direction of the viewing space to the brightness. One is a one-dimensional texture in advance, and the change in the value between 0-256 is defined according to the brightness curve of FIG. 2. A vertex texture coordinates are determined according to its method, which requires us to specify a certain texture coordinate automatic generation algorithm for D3D / OpenGL. The second method is to use Cube Map. The Cube Map is a pixel that uses a vector (the normal vector of the vertex) to index a texture. This approach requires us to generate a special Cube texture, and also require a specific automatic texture coordinate generation method. The above two methods are used to illuminate the entire model with texture, and they need to take up a texture level. And it is not very intuitive (relatively, the first method is slightly intuitive). Since the observer's orientation (Camera's orientation) is certain, we can construct a desired cartoon rendering when the method can be converted to the viewing space. The effect, that is, as long as we control the T & L process. This is easy to implement in the current 3D API and graphics card. Vertex Shader provides full support to this. 4. Method for constructor of light curve
Because Vertex Shader is processed in Per-Vertex, we should not establish a very complex calculation code. The easiest way to reach the curve shown in Figure 2 is to calculate the calculated normal line and the point of the Camera direction, and then subtract a value. The specific curve relationship is shown in Figure 3. Pink is a normal illumination model curve, let's zoom in to a certain multiple to the green curve, then move down to the blue curve position, because the 3D API will be greater than 1 The cut to 1, less than 0 to 0. In the end, we got the illumination curve as shown in the curve of the red. This method can be implemented in the version of the D3D VS2.0, as long as one instruction (MAD) is added. However, since the D3D supports the creation of light having a brightness greater than 1, the rendering result of this method has a significant metal shin. There is a SLT instruction in VS2.0, which can be selected according to the different size of the two operands, is set to set the destination operation register to 1 or 0. We can use this instruction to make a curve as shown in Figure 4. Figure 3 Figure 45, code