Understanding Unity Shader

The Unity engine is a very powerful game engine that supports cross-platform development. It is designed based on Mono, an open source .Net framework. ShaderLab is defined in Unity to organize Shader content and compile for different platforms. After understanding some basic knowledge of Shader and Cg, the next thing we have to do is: learn how to use Cg to write Shader in Unity and achieve some simple Shader effects.

Unity Shader:

In the final analysis, Shader is actually just a program that specifies the input (color, texture, etc.) and output (the correspondence between points and colors that the renderer can understand). Then, the process of designing a Shader is actually to calculate and transform according to the input to generate the output.

1. Classification:

Shaders in Unity are divided into two categories:

Surface Shader: It has already done most of the work for us, and only needs simple operations to get good results;

Fragment Shader: You can design a lot of things yourself, because there are more content that can be set by yourself, but it is also more difficult to write. The purpose of using a fragment shader is to allow more complex (or more efficient for the target device) development at a lower level.

2. Basic structure of Shader program:

Using the framework in Unity to write Shader programs is actually simpler than other game engines. In Cocos2d, you have to start writing logic from the OpenGL level, but in Unity you only need to fill in the content that needs to be controlled in the framework. One The basic structure of the Shader program is shown in the figure below:

Shader (shader)

Property Definition

SubShader (subshader: start)

Pass

Pass

......

SubShader (subshader: end)

Fallback

First, define some properties that specify which inputs the code will have;

Secondly, there will be one or more sub-shaders, but which sub-shader is used in actual operation is determined by the running platform;

The sub-shader is the main body of the code, each sub-shader contains one or more Pass;

Finally, specify a rollback to handle the situation where all Subshaders cannot run (such as the device is too old).

When performing shading, the platform selects the sub-shader that can be used with the highest priority, then executes the passes in the sub-shader in sequence, and then outputs the result.

3.Unity creates the first Shader:

In Unity's Project panel, right-click-Create-Shader, named Diffuse_Texture, use VS to open to view the content of the newly created Shader as follows:

Shader "Custom/Diffuse_Texture" {

Properties {

_MainTex ("Base (RGB)", 2D) = "white" {}

}

SubShader {

Tags { "RenderType"="Opaque" }

LOD 200

CG PROGRAM

#pragma surface surf Lambert

sampler2D _MainTex;

struct Input {

float2 uv_MainTex;

};

void surf (Input IN, inout SurfaceOutput o) {

half4 c = tex2D (_MainTex, IN.uv_MainTex);

o.Albedo = c.rgb;

o.Alpha = c.a;

}

ENDCG

}

FallBack "Diffuse"

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

The next thing we have to do is to analyze the meaning and function of each line in this Shader, including attributes, Tags, LOD, lighting model, etc.

Reference link:

Understand the basics of Shader: a guide for getting started with Unity3D Shader that cats can learn (1)

Unity's official information about Shader: Materials, Shaders & Textures

Understand the mechanism of Shader: [Unity Shaders] Explore the mechanism behind Surface Shader

Recommended books:

"Unity Shader and Effect Cookbook", Chinese version: "Unity Shader and Screen Effects Development Tips"

Guess you like

Origin blog.csdn.net/mr_five55/article/details/129332889