Esfog_unityshader Tutorial _ Masking strokes (rationale)

Source: Internet
Author: User

Cough, there is a period of time has not been updated, recently a bit lazy! Put a lot of energy on C + + body. Gossip less, today to talk about can be different from the previous several, this is a comprehensive application. This content is different from the previous one mainly in the following points.

1. What we wrote earlier was to use only one shader to achieve some effect, and this time we were using multiple shader to work together.

2. Before we just write the pure shader code, there is no client-related C # script (You love to use JS can also). And this time also to use.

3. The amount of code involved in this tutorial is also several times before.

4. In general, the previous is relatively simple, and this article has some difficulty.

But do not be afraid, we first explain the implementation of the principle, because this tutorial content is more, so can only take a piece to separate theory, suggest to see this tutorial classmate, the best can have basic Unityshader Foundation, may wish to see my previous tutorials, in the back of the code I will not talk about everything, Go straight to the point where you are more focused.

Okay, get to the point, what is occlusion stroke? Go straight.

 For example, in many games, especially 3D games, when our character is blocked by some wall or other objects, in order to let the player clearly see the position of the character is currently in place, it is necessary to the role of the mask part of the outer contour (green border). This is the "occlusion stroke" we are going to implement. You can think of the game you have played to see if there is such a situation.

Important Concepts

before explaining the specific principles, we need to understand several more important concepts:

1. Post-processing: a bit similar to the audio and video aspects of post-processing meaning, in this case, when the camera has finished rendering the current frame, we do not directly map the image to the screen, but the use of our code to process the original image, the processing is displayed on the screen. Many of the effects in the game are achieved through post-processing, such as bloom effects, motion blur effects, and so on. Our "occlusion strokes" are also counted as post-processing.

2. Depth buffer: For a 3D game, the three-dimensional world in the camera to see the part of the two-dimensional screen, in addition to show the color of the object itself, but also to deal with the relationship between the object. And in our video memory there are two buffers (and specific graphics architecture, here is just an example) to save the game's picture and the object's relationship. We call them color buffers and depth buffers, respectively. Color buffers You can understand that he is a two-dimensional array in which an element corresponds to the color of one pixel of the screen (and some other information is included). The depth buffer can also be understood as a two-dimensional array that holds the distance from the camera to the point on the object in each point of the screen pixel. Deep buffering is important in the rendering process, and without it there is no relationship between the objects, and we can see a cluttered image on the screen.

3. Depth diagram: Depth map is suitable for deep buffer associated with a concept, depth buffer is controlled by the operating system through the graphics API, and we use the engine can only be used by the engine's limited API to read and write using the depth buffer to achieve some effect. We're going to freak out when we need to use the depth buffer to do some special effects. We can make a camera so that it does not display the rendered image on the screen, but instead displays it on a two-dimensional picture that we have set up. We're using the shader we've written to keep the depth of the object being captured by the camera in this two-dimensional image (which is now the depth map), not the color information. So we can use this depth map to do things in the next process.

Principle

with some of the above concepts we'll look at the principle of implementation (of course, there are many ways to implement occlusion strokes, and each game requires different effects, the method here is for reference only):

In general can be divided into four large steps: (The following two pictures, in the game players are not visible, developers are not see, all in the code to deal with, shown here just so that we can better understand)

1. Get a depth map of depth information that contains only the objects we want to do occlusion stroke processing (in our case, that soldier). For our example, the result of this step is that it looks like a red one, but you will see the soldier on the right. As for why the image is so red, you can think about it, I will explain in the next article.

  

2. Determine which parts of the object we are dealing with are blocked by comparing the depth information of the master camera (which is the camera you see in the game window) and the depth map information we obtained in the first step. and apply a stroke color to the occlusion. Such as

  

 3. Take the occluded part one at a time, the upper and lower direction of stretching one pixel. This doesn't have to be the same as the above figure, just up and down a pixel, so it doesn't look obvious.

4. Finally we use the original color (the color of the wall itself) for the masked part of the character in addition to the extra-pixel outline we drew in the third step. The effect is the picture I put in the beginning of this article.

Some of the APIs involved

This allows us to implement occlusion strokes, but the above is a more general generalization, with a lot of detail to note. Specific code implementation I will be in the next tutorial to explain in detail, here I first need to design some unity knowledge listed, a lot of content can be found through Unity's official API document, you can first understand and then try to implement the occlusion stroke. This will work better when you look at the next tutorial:

1. Some of the APIs involved in post-processing in unity are onrenderimage (...), rendertexture.gettemporary,rendertexture.releasetemporary, Graphics.blit,Renderwithshader.

2. Some of the APIs designed to shader in C # scripts, such as shader. setglobalxxx (), Shader.find,material.setxxx ().

3. Camera depth mode, and unity provides us with a default depth map. camera.depthtexturemode,_cameradepthtexture and so on.

Okay, about the principle of masking strokes basically finished, how is not and the content of the previous several different, this content is just to do a point, just hope you can understand the rendering of several important concepts, and then do some expansion, such as depth buffer and depth map, The effect they can achieve is far more than "occlusion strokes". More magical features await you to explore together. In addition, I will be completed in the near future "realization of the article", this time you might as well try to write and read, maybe before I send the next article, smart you AH has been done!!!

respect for the wisdom of others, welcome reprint, please specify the author Esfog, the original address   http://www.cnblogs.com/Esfog/p/CoverOutline_Shader.html

Esfog_unityshader Tutorial _ Masking strokes (rationale)

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.