【ShaderLab】
Shader is the root command of a shader file. Each file must define one (and only one) Shader. It specifies how any objects whose material uses this shader are rendered.
1、Syntax
Shader "name" { [Properties] Subshaders [Fallback] } Defines a shader. It will appear in the material inspector listed under name. Shaders optionally can define a list of properties that show up as material settings. After this comes a list of SubShaders, and optionally a fallback.
2、Properties
Shaders can have a list of properties. Any properties declared in a shader are shown in the material inspector inside Unity. Typical properties are the object color, textures, or just arbitrary values to be used by the shader.
3、SubShaders & Fallback
Each shader is comprised of a list of sub-shaders. You must have at least one. When loading a shader, Unity will go through the list of subshaders, and pick the first one that is supported by the end user's machine. If no subshaders are supported, Unity will try to use fallback shader.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// colored vertex lighting Shader "Simple colored lighting" { // a single color property Properties { _Color ("Main Color", Color) = (1,.5,.5,1) } // define one subshader SubShader { Pass { Material { Diffuse [_Color] } Lighting On } } }
【Properties】
Syntax
- Properties { Property [Property ...] }
- Defines the property block. Inside braces multiple properties are defined as follows.
Each property a default value is given after equals sign:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Properties { // properties for water shader _WaveScale ("Wave scale", Range (0.02,0.15)) = 0.07 // sliders _ReflDistort ("Reflection distort", Range (0,1.5)) = 0.5 _RefrDistort ("Refraction distort", Range (0,1.5)) = 0.4 _RefrColor ("Refraction color", Color) = (.34, .85, .92, 1) // color _ReflectionTex ("Environment Reflection", 2D) = "" {} // textures _RefractionTex ("Environment Refraction", 2D) = "" {} _Fresnel ("Fresnel (A) ", 2D) = "" {} _BumpMap ("Bumpmap (RGB) ", 2D) = "" {} }
Later on in the shader’s fixed function parts, property values can be accessed using property name in square brackets:[name]. For example, you could make blending mode be driven by a material property by declaring two integer properties (say “SrcBlend“ and ”DstBlend”), and later on make Blend Command use them: Blend [_SrcBlend] [_DstBlend]
.
In front of any property, optional attributes in square brackets can be specified.
[HideInInspector]
- does not show the property value in the material inspector.[NoScaleOffset]
- material inspector will not show texture tiling/offset fields for texture properties with this attribute.[Normal]
- indicates that a texture property expects a normal-map.[HDR]
- indicates that a texture property expects a high-dynamic range (HDR) texture.[Gamma]
- indicates that a float/vector property is specified as sRGB value in the UI (just like colors are), and possibly needs conversion according to color space used. See Properties in Shader Programs.
【Fallback】
After all Subshaders a Fallback can be defined. It basically says "if none of subshaders can run on this hardware, try using the ones from another shader".
Syntax
Fallback "name"
Fallback to shader with a given name.
- Fallback Off
- Explicitly state that there is no fallback and no warning should be printed, even if no subshaders can run on this hardware.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Shader "example" { // properties and subshaders here... Fallback "otherexample" }
【Subshaders】
Syntax,实际上是Pass的容器,组合多个Pass。
Subshader { [Tags] [CommonState] Passdef [Passdef ...] }
- Defines the subshader as optional tags, common state and a list of pass definitions.
Each pass definition can be a regular Pass, a Use Pass or a Grab Pass.
Any statements that are allowed in a Pass definition can also appear in Subshader block. This will make all passes use this "shared" state.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// ... SubShader { Pass { Lighting Off SetTexture [_MainTex] {} } } // ...
【Subshader Tags】
Subshaders use tags to tell how and when they expect to be rendered to the rendering engine.
Syntax
- Tags { "TagName1" = "Value1" "TagName2" = "Value2" }
- Specifies TagName1 to have Value1, TagName2 to have Value2. You can have as many tags as you like.
In addition to built-in tags recognized by Unity, you can use your own tags and query them using Material.GetTag function.
You can determine in which order your objects are drawn using the Queue tag.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Shader "Transparent Queue Example" { SubShader { Tags {"Queue" = "Transparent" } Pass { // rest of the shader body... } } }
For special uses in-between queues can be used. Internally each queue is represented by integer index; Background
is 1000,Geometry
is 2000, AlphaTest
is 2450, Transparent
is 3000 and Overlay
is 4000. If a shader uses a queue like this: