Unity Shader Introduction to essential reading notes The third chapter Unity Shader Basics
Preface
Unity Shader The emergence of is to provide a place for developers to more easily manage shader code and rendering settings , It doesn't need to be the same as all kinds of shader Manage multiple files like files 、 Functions, etc .
3.1 Unity Shader summary
3.1.1 Materials and Unity Shader
stay Unity in , We need to use materials together (Material) and Unity Shader To achieve the desired effect .Unity Shader Define the code required for rendering 、 Properties and instructions , And materials allow us to adjust these properties by ourselves , And assign it to the corresponding model .
3.1.2 Unity Materials in
Unity The materials in need of a combination GameObject Of mesh perhaps Particle Systems( particle system ) Come to work .
3.1.3 Unity Medium Shader
Unity Medium shader The file is the same as that of the rendering pipeline shader Different , So the notes are called Unity Shader.
Creating Unity Shader when ,unity Four templates are provided for us :
Standard Surface Shader | Generate a surface shader template containing a standard lighting model |
---|---|
Unlit Shader | Does not include light ( But it contains fog effect ) The basic vertices of / Chip shader |
Image Effect Shader | Provide basic templates for various screen post-processing effects |
Compute Shader | utilize GPC To perform some calculations independent of the conventional rendering pipeline |
because 《 Introduction to topic 》 This book focuses on how to Unity Write vertices in / Chip shader , It is usually used in subsequent learning Unlit Shader To generate a basic vertex / Chip shader template .
A separate Unity Shader It doesn't work , Must be of the same material material Combine . It is essentially a text file ,Unity Shader The import settings panel of can see this shader Some relevant information ( For example, whether it is a fixed function shader (fixed function)、 Whether to cast shadows (cast shadows)、 The rendering queue used (Render queue)、 attribute (properties) List etc. ).
3.2 Unity Shader The basis of :ShaderLab
Unity It specially provides a kind of for Unity Shader The language of the service ——ShaderLab,Unity Shader It provides a layer of abstraction for controlling the rendering process , Help developers finish their work more easily .ShaderLab when Unity Provided by Unity Shader An illustrative language , Use some semantics nested within curly braces to describe a Unity Shader File structure . similar CgFX and Direct3D Effects(.FX) Language , Defines everything needed to display a material , and Not just shader code .
One Unity Shader The infrastructure of is as follows :
Shader "ShaderName"{
Properties{
// attribute
}
SubShader{
// The graphics card A Sub shaders used
}
SubShader{
// The graphics card B Sub shaders used
}
Fallback "VertexLit"
}
Unity These structures will be compiled into real code and Shader file , Developers just need to work with Unity Shader Just deal with it .
3.3 Unity Shader Structure
3.3.1 to Shader The name
use Shader "name"{ }
You can give Unity Shader name , It will also appear in the drop-down list of the material panel .
You can also add prefixes to orderly organize the location distribution , for example Shader "Custom/MyShader"{}
The effect is as shown in the picture :
3.3.2 Materials and Unity Shader The bridge :Properties
Properties Contains a series of properties , These attributes appear in the material panel .
It is defined as _ name (" Panel attribute name ", type )= The default value is
, Such as :
Properties{
Name("Display name",propertyType) = DefaultValue
Name("Display name",propertyType) = DefaultValue
// More properties
}
The name of an attribute is usually a Underline Start . The display name is the name that appears on the material panel , At the same time, you need to specify the type and default value .
Here are Properties Attribute types supported by semantic blocks :
Attribute types | Default value definition syntax | Example |
---|---|---|
Int | number | _Int("Int",Int) = 2 |
Float | number | _Float("Float",Float) = 1.5 |
Range(min,max) | number | _Range("Range",Range(0.0,5.0)) = 3.0 |
Color | (number,number,number,number) | _Color("Color",Color) = (1,1,1,1) |
Vector | (number,number,number,number) | _Vector("Vector",Vector) = (6,2,3,1) |
2D | "defaulttexture"{} | _2D("2D",2D) = ""{} |
Cube | "defaulttexture"{} | _Cube(“Cube”,Cube) = "white"{} |
3D | "defaulttexture"{} | _3D("3D",3D) = "black"{} |
Int、Float、Range: Properties of numeric type , The default value is a separate number .
Color、Vector: The default is a four-dimensional vector surrounded by parentheses .
2D、Cube、3D: The default values are strings and curly braces , The string is either empty , Or the built-in texture name ; Curly braces are used to specify some texture attributes , stay Unitu5.0 Then it's removed .
3.3.3 Heavyweight members :SubShader
One Unity Shader It can contain more than one SubShader Semantic chunk , At least one . load unity shader Will scan all SubShader, Choose the first one that can run on the target platform SubShader; If they don't support , So use Fallback designated UnityShader.
SubShader The structure is as follows :
SubShader{
// Optional
[Tags]
// Optional
[RenderSetup]
Pass{
}
// Other pass
}
Introduce one by one :
label Tags And status RenderSetup It's all optional , stay SubShader The settings in will apply to all pass.
label Tags It's a key-value pair , All strings , Represents the SubShader How would you like to 、 When to render this object .
state RenderSetup Sure Set various states of the graphics card , And apply to all pass.
Pass The semantic block is once Complete rendering process
Say it Pass:
Pass{ [Name] [Tags] [RenderSetup] //code }
Obviously we can define pass The name of , We can also quote other Unity Shader Of pass, Such as :
UsePass "MyShader/MYPASSNAME"
, It is worth noting that ,unity The interior will take all Pass Name converted to uppercase , So we also need to use capital letters here .Pass Labels can also be set inside / Render states , Used to tell the rendering engine how to render the object .
3.3.4 Stay in the back :Fallback
Fallback It stipulates that when all SubShader Can't be used when running on the graphics card Shader.
Developers can close Fallback, But this will affect the projection of the projection . because Fallback Use the built-in shader It includes a general Pass, This will cause the effect of shadow casting .
3.3.5 Other semantics
such as CustomEditor Language extension custom editing interface .
such as Category Semantic pair Unity Shader Command grouping in .
3.4 Unity Shader In the form of
stay Unity There are three forms to write Unity Shader:
Shader "MyShader"{
Properties{
// Various properties required
}
SubShader{
// In the real sense Shader The code will appear here
// Surface shaders (Subface Shader) perhaps
// The vertices / Chip shader (Vertex/Fragment Shader) perhaps
// Fixed function shader (Fixed Function Shader)
}
SubShader{
// ditto
}
}
3.4.1 Surface shaders
Surface shaders (Sruface Shader) yes Unity A shader code type created by yourself , Less code ; In essence, it is still the apex / Chip shader , It can be seen as Unity To the summit / High level abstraction of slice shaders .
The value of surface shaders lies in Unity Helped us deal with a lot of lighting details .
Shader "Custom/Simple Surface shader" {
Subshader{
Tags {"RenderType" = "opaque" }
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float4 color : COLOR;
};
void surf ( Input IN, inout SurfaceOutput o){
o.Albedo = 1;
)
ENDCG
}
Fallback "Diffuse"
}
Surface shaders are defined in SubShader Medium CGPROGRAM and ENDCG in . be not in Pass Because I don't care about how many pass,Unity Will do well Pass Things about .
CGPROGRAM and ENDCG The code in uses CG/HLSL To write .
3.4.2 The vertices / Chip shader
Basically the same as the surface shader , however CGPROGRAM and ENDCG It needs to be written in Pass Inside , Used to define each Pass Own code .
3.4.3 Fixed function shader
Older devices do not support programmable pipeline shaders , So use fixed function shaders .
Fixed function shaders use ShaderLab Grammar, not CG/HLSL.
Unity5.2 after , All fixed function shaders will be compiled into corresponding vertices at the bottom / Chip shader , The real fixed function shader no longer exists .
3.6 What to pay attention to
3.6.1 Unity Shader No Shader
Unity Shader It's actually a ShaderLab file , With .shader A file with a suffix name .
Unity Shader Highly packaged , There is no need to write specific types of vertices / fragment Shader, You can turn on rendering settings through instructions , It is convenient to modify some properties .
At the same time as Unity Shader The height of the package , some Shader Type and syntax limitations .
3.6.2 Unity Shader and CG/HLSL The relationship between
Unity Shader use ShaderLab Language writing , But for vertices / Slice shaders are nested CG/HLSL Code to implement .
3.7 Information
Official documents :http://docs.unity3d.com/Manual/SL-Reference.html
Official simple shader Writing Tutorial :http://docs.unity3d.com/Manual/ShaderTut1.htmlhttp://docs.unity3d.com/Manual/ShaderTut1.html
NVIDIA Provided CG file :http://http.developer.nvidia.com/CG
NVIDIA Series of tutorials provided :http://http.developer.nvidia.com/CGTutorial/cG_tutorial_chapter01.html