Shader tutorial on Unity3d development (introduction to GPU rendering)

Source: Internet
Author: User

This essay, not according to a variety of professional explanations to describe, completely see yourself play it, write where to calculate where. If there is a place to say wrong, please crossing to speak frankly no harm!

When it comes to game development, it is inevitable to mention graphics, and the study of graphics will involve a variety of mathematical knowledge, vectors, matrices and the like! And here, let's start with shader, what is shader? We usually say that writing a shader, is actually writing a function of a program, and you write a common program principle of the same, but the difference is that the shader is written by a GPU execution, When it comes to GPU execution, you have to talk about the characters in the 3D game, ah, birds and flowers, how to see, or say a professional point, called rendering out!

If it is on the 2D, it is very simple and can be seen as a picture affixed to the screen is OK. On the 3D above, it is a bit more complicated, first of all, our screen 2D, unlike the real world, is three-dimensional space! So how to draw something on the screen to look like a cubic effect, you can try to draw a cube on paper, learn math can be drawn, you can look at the first glance that this is 3D, rather than a simple rectangle!

Model coordinate system: in the 3D world of computers, the 3D effect is achieved by such a thing. If the 3D model is drawn on the computer screen, then we first make this model on the 3D software, the model will have a origin, to build a coordinate system, whether it is the left hand or the right hand is similar, this coordinate system, we are called model coordinate system, then each part of the model has a coordinate with respect to the origin.

World coordinate system: Since each model has a coordinate system, it is equivalent to holding your hand, your hands in front of the face, then relative to the person next to you, where your hands. Then there will be a concept called the world coordinate system, in the world coordinate system, each model coordinates are relative to the world coordinate system origin, then unless overlapping, it is impossible to appear the same situation of coordinates. It's the same as where you stand, no one else stands. And our lighting calculations will be done in the world coordinate system.

Photographic coordinate system: Everyone is looking at the world in their own eyes, such as when you turn your eyes and see things differently. Similarly, in the computer world, each time only from the unique perspective to render objects, 3D games will provide the function of the camera roaming, the screen display will change with the camera, because the GPU transferred the object's vertex coordinates from the world coordinate system to the camera coordinate system. The so-called camera coordinate system, is the camera as the origin, from the line of sight direction, angle of view and near and far plane, composed of a ladder form of three-dimensional space, called viewing frustum (cones) such as

Since you can see the area has been determined, then the GPU will have to do a screening, as you face the sea, even if the spring, you can only see the face you are facing, you are behind the landscape you cannot see, but that is objectively. At this time, the GPU has to do a cut, in this area of the things will be rendered, not in this area, it can only be discarded, this step is called cutting (clip), how to judge the process of the object is not in this area, called the clipping algorithm.

As for how to crop, the process algorithm is more complex, please refer to the data (this process can also be done in the application). After cropping, you are still in the concept of 3D space, and always have to show on the two-dimensional screen above, then there is a process called screen mapping, that is, the three things on the 2D plane to display the same as the cube on the paper, and you see a piece of paper drawing, Or you can see the 3D effect.

The next step, called an element assembly, is to compose lines and polygons based on the vertex's position and index relationships, and then crop the triangles out of the screen, just like a triangle has a vertex outside the screen, and when it is cut off, the triangle becomes a quadrilateral. The next process, called rasterization, the so-called rasterization, is to give the display pixel point assignment, the screen is composed of a pixel point, to each point assigned to the corresponding color, connected to a whole screen, the result of the process before, we can get each point of screen coordinate values, also know what we want to draw into what kind (point, line, surface) There's one more question,

After that, we need to know the color of each vertex, the color is also in the vertex information, which involves a situation called process color, is based on the current color and the color of the calculation is a transparent value, mixed to a color output, that is, we often say the alpha value, commonly called alpha mixing technology, Each pixel is associated with an RGB color value and a value of a, which is used to describe the transparency of an object.

The calculation formula is newcolor=curcolor*a+ (1-a) *nextcolor. After blending, the color is written to the frame cache. So if there are two vertices in the same position, if there are two colors and they are opaque, then what is the color of the drawing? Here is a concept, called Z buffer is generally called Z-buffers, or called depth buffer, to determine the occlusion relationship, the value of the lower (0,1) value, closer to the cutting screen, imagine that in real life, two pieces of wood stacked in front of you, of course, you can only see the block near. Then, the GPU by comparison to determine the occlusion between vertices, this value is actually only a relative size, is completely used to compare.

Forget it, just stop for a moment, and say

  

Shader tutorial on Unity3d development (introduction to GPU rendering)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.