Talking about Unity Shader, I will help you understand it from scratch

Preface

Shader, a thing called shader in Chinese, is a mysterious existence for many developers. They want to learn it but don’t know how to learn it, or after learning it for a while, they find that Get cannot get it. The feeling is that it is outside and there is no way to do it.

Yes, there is agame development exchange group. I hope everyone can click in to exchange development experiences

So today this article will explain to you how to get started with Shader from scratch. The purpose is to allow programmers or artists who are new to Shader to quickly enter this colorful world.

1. What is Shader

Shader is actually a technology specifically used to render graphics. Through shader, we can customize the algorithm of the graphics card to render the picture to achieve the effect we want. It ranges from every pixel to the entire screen, such as the following two common effects in games.

2. Shader classification

Vertex Shader (3D graphics are composed of triangular patches. Vertex Shader calculates the vertices on each triangular patch and prepares for final pixel rendering).

Pixel Shader, as the name suggests, is a series of algorithms that calculate lighting and color in pixels.

Several different graphics APIs have their own Shader languages. In DirectX, the vertex shader is called Vertex Shader, and the pixel shader is called Pixel Shader; in OpenGL, the vertex shader is also called Vertex Shader, but the pixel shader is called Fragment Shader, which is what we Often referred to as fragment Shader or fragment Shader.

To put it bluntly, Shader is actually a piece of code. The function of this code is to tell the GPU how to draw the color of each vertex of the model and ultimately the color of each pixel.

3. Shader programming language

Since Shader is a piece of code, it must be written in a language. There are currently three mainstream languages: OpenGL Shading Language based on OpenGL, referred to as GLSL. High Level Shading Language based on DirectX, referred to as HLSL. There is also NVIDIA's C for Graphic, referred to as Cg language.

GLSL and HLSL are interfaces based on OpenGL and Direct3D respectively, and the two cannot be mixed. The Cg language is a C language used for graphics. This actually illustrates the original intention of the designers at the time, which was to make programming based on graphics hardware as convenient and free as C language programming.

Just as the syntax of C++ and Java is based on C, the Cg language itself is also based on C language. If you have used any of C, C++, and Java, then the syntax of Cg is relatively easy to master. Cg language strives to retain most of the semantics of C language and strives to free developers from hardware details. Cg also has the benefits of high-level languages, such as easy reusability of code and high readability. The Cg language is a collaboration between Microsoft and NVIDIA to reach an agreement on the syntax and semantics of the standard hardware lighting language. Therefore, HLSL and Cg are actually the same language.

Generally speaking, for the convenience of cross-game platforms, CG language is generally learned.

4. What is Unity Shader

Graphics cards include NVIDIA, ATI, Intel, etc. Graphics APIs include OpenGL, DirectX, OpenglES, Vulkan, Metal, etc. Shader programming languages ​​include GLSL, HLSL, Cg, etc.

Are you a little dizzy, how should you choose? How should we do it in Unity?

In fact, everything becomes simpler in Unity. We only need to worry about how to achieve the effect we want, and Unity will handle all the rest automatically. Because the Shader we write in Unity will eventually be compiled into different shader languages ​​according to different platforms, so what language should we use to write Shader in Unity?

The official recommendation is to use Cg/HLSL to write Shader programs. Of course, you can also use GLSL. This is mainly because Cg/HLSL has better cross-platform features and we prefer to use Cg/HLSL to write Shader programs.

Strictly speaking, Unity Shader is not a traditional Shader, but an easy-to-write Shader encapsulated by Unity itself, also called ShaderLab. There are three types of Shaders in Unity (actually three different ways of writing): Surface Shaders; Vertex/Fragment Shaders; Fixed Function Shaders; Fixed Function Shaders have been eliminated completely. There is no need to study anymore. Surface Shader is actually another layer of Unity's packaging of Vertex/Fragment Shader, so that the production method of Shader is more in line with human thinking mode, and at the same time, different lighting models and things that need to be considered under different platforms can be completed with very little code. .

But Surface Shader also has its limitations, that is, the effects that Vertex/Fragment Shader can achieve may not be achieved by Surface Shader. The converse is true. Vertex/Fragment Shader that can be achieved by Surface Shader can definitely be achieved.

And Unity’s official visual Shader tool (Shader Graph) was launched in versions after Unity 2018. Judging from the generated code, all of them use Vertex/Fragment Shader. Can it be understood that in the future programmable rendering pipeline, Unity itself will abandon Surface Shader and all use Vertex/Fragment Shader?

In short, in the future learning process, some Surface Shader content will also be involved, but it will mainly focus on Vertex/Fragment.

In addition, learning Shader will also bring us many benefits: You must have encountered the situation where the model displays pink in the game. The Shader is missing, or the Shader does not meet the current platform, or there is a grammatical error in the Shader. Woolen cloth? If we understand and learn Shader, these questions will no longer be confusing.

The built-in Unity Shader is only a "universal" use case and is not enough to meet all our screen performance needs.

Once you master Shader, you can create a unique visual enjoyment for your game/application. Shader that implements specific functions for games and applications based on actual needs.

It can greatly help us optimize rendering performance, because Shader can control what and how to render.

The ability to write Shader is very important for game teams, and the development of Shader skills has always been a hot position. An indisputable fact now is that technical art will always be a scarce resource for major manufacturers.

5. Shader core knowledge

1. Rendering pipeline

Also called the rendering pipeline, it is an independent parallel processing unit inside the display chip that processes graphics signals. A pipeline is a sequence of stages that can be executed in parallel and in a fixed order, following a front-in, last-out process. By analogy, it is like a factory that produces BMW and Bentley cars at the same time. Each part of the two cars is produced at the same time. It can be said that there are two different rendering pipelines in this workshop.

Explain based on the picture above. When 3D software or games are running, they will call graphics API, OpenGL or DirectX. The vertex shader and fragment shader all work in the GPU operation box in the figure.

The picture above is aimed at the unity engine. The Geometry part can be understood as modeling. This process is to import Mesh data into unity, and the unity engine then calls the graphics API. The process of calling the graphics API is to drive the GPU to perform processing operations.

After entering the GPU, the first thing to do is the vertex shader corresponding to the vertex processor. The result of the vertex operation will be passed to the pixel processor (Pixel Processor is equivalent to the fragment processor), which corresponds to the pixel shader (that is, Fragment shader), and finally outputs pixel information that can be displayed on the screen, that is, Frame Buffer. Frame Buffer can not only store color information but also depth values.

2.shader material map

A shader is actually a small program. It is responsible for combining the input vertex data with the input texture or color in a specified way, and then outputs it. The drawing unit can use this output to draw images to the screen.

The input texture or color, etc., plus the corresponding shader, and specific parameter settings for the shader, are packaged and stored together. The result is a Material (material). In fact, there are also There are other things like vectors and matrices. After that, we assign the material to the 3D model for rendering (output).

Materials are like the products ultimately used by game engines, shaders are like the processing methods used to produce such products, and textures are the raw materials for such products. If we don't use Unity or other engines, to implement materials we need to use OpenGL or DirectX API calls to manually organize a shader, which is very troublesome, so the engine provides convenience.

6. The writing form of shader in unity

There are three types: surface shader vertex and Fragment shader fixed function shader. We know that the hardware only recognizes vertex shading and fragment shading, so why is there an additional surface shader (hereinafter referred to as SS) and Fixed Function shader (hereinafter referred to as FFS) in the unity engine.

For FFS, it is mainly for the operation of fixed pipeline hardware. It is a very conservative shader that can be supported by most hardware. As for SS, it is recommended by Unity. When creating a shader in Unity, the default code is SS. In fact, SS is a "package" of vertex and fragment shaders. After SS programming, unity will compile the SS code into hardware-recognizable vertex and fragment shader codes. The main structure of writing shader:

7. ShaderLab

ShaderLab is a syntax customized for Unity specifically for writing Shaders and can accommodate three other shaders. The main structure of shaderLab:

Three parts within curly braces: Properties, Subshaders, Fallback. What are Properties? Properties. We create a new shader and a shader in Unity, open the compiler and name the shader "111", then drag the 111shader to the shader, and then click on the shader to observe its display and the contents of the Properties in the code. We found that the Properties in shader programming will display the properties of the shader's material ball in the form of a list for parameter adjustment.

What is a subshader? The algorithm is a shader fragment written for GPU rendering. Remember here, there is at least one subshader in a shader. Every time the graphics card performs processing, it can only choose one of the subshaders to execute. So why are there multiple subshaders? This has to do with hardware.

When reading a shader, it will read from the first subshader first. If the first subshader can adapt to the current hardware, it will not be read further; if the hardware is too old and cannot keep up, the first subshader cannot be read. I will read the second one to see if it can be adapted to me. In other words, all subshader solutions will be simplified downwards. What if none of the listed subshaders can be used? That's the third Fallback.

FallBack means that when the subshader is no longer available, you can roll back to a shader that can be adapted to the specified unity. The commonly used built-in shaders in Unity are as follows:

More video tutorials

Unity3D tutorial www.bycwedu.com/promotion_channels/2146264125

Guess you like

Origin blog.csdn.net/Thomas_YXQ/article/details/134525982