My Unity ShaderLab Study Notes
Recently reviewing Unity's colored knowledge.
Here, the main record is some of the highlights that individuals encountered in Shaderlab.
This will be recorded on an ongoing basis until this review is completed.
Basic structure of Shaderlab
Shaderlab was based on Cg (HLSL), but not equivalent to Cg. On that basis, it added a new set of things, higher than Shader.
The ShaderLab code can be equated to Unity material to some extent. It contains a whole set of things, conceptually like a D3D Effects based on HLSL.
Shader "ShaderName" {
Properties {
// 属性
}
SubShader {
Pass {
// 具体的Shader程序
}
Pass {
// 有时会用到多个Pass
}
}
SubShader {
// 因为硬件的差异,有的旧硬件不支持某些Shader
// 可能需要提供多个子Shader供Unity选择
// Unity会选择第一个能运行的子Shader
}
// 当所有子Shader都不能运行时
// 提供一个应急方案,双引号里是Shader名称
// 或者关掉这个功能
// 代码是:Fallback off
Fallback "Diffuse"
}
Note that Fallback has a hidden effect here, and that when Unity expects shadows to be counted, it will look for the associated Pass in SubShader, which will be provided by Fallback.
Propertiers Properties Settings
If Shader is seen as a set of functions, or as a function in a broad sense, Propertiers is the parameter that Shader was submitted to GPU for execution.
...
Properies {
// 形式:
// Name("DisplayName", PropertyType) = DefaultValue
// 例如:
_pos("Position", Vector) = (1, 1, 1, 1)
}
...
Here._posYou can use it as a variable name in the Shader code in the back.
PropertyType SupportInt、Float、Range(min,max)、Color、Vector、2D、Cube、3DThese types, the last three of which are texture types.
The type of stickers can be like this:_tex("Texture", 2D) = "white"{}, specify the default texture. "white"is the default texture name.
The attribute of the code type should be in the Unity material property setting panel.
General structure of the SubShader
...
SubShader {
// 不一定要有
Tags {
// 标签块
// 键值对形式
}
// 不一定要有
RenderSetup {
// 状态设置块
}
Pass {
// Pass的名字,不一定要给
Name "Pass1"
// Pass的Tags
// 注意和和SubRender的不一样
// 也可以有Pass自己的RenderSetup
// CGPROGAM/ENDCG 表示该块是Cg代码
// GLSLPROGAM/ENDGLSL 表示该块是glsl代码
// 但是不推荐使用glsl,因为Unity能够将Cg交叉编译给glsl,反之不能
CGPROGRAM
// 指定具体的功能函数入口
#pragma vertex vert
#pragma fragment frag
float4 vert(float4 v : POSITION) : SV_POSITION {
return mul(UNITY_MATRIX_MVP, v);
}
fixed4 frag() : SV_Target {
return fixed4(1.0, 0.0, 0.0, 1.0);
}
ENDCG
}
}
...
Tags and RederSetup were placed behind the record.
In fact, ShaderLab supports fixed rendering lines, but, after all, there is almost no hardware for non-programmable pipes, and no record is made.
RenderSetup RenderSetup RenderSite Status Settings
Common status tables:
| Status Name | Set Items | Explanation |
|---|---|---|
| Cull | Back, Front, Off | Excerpt mode: back, front, off |
| ZTest | Less, Greater, LEqual, GEqual, Equal, NotEqual, Always | Depth test: <、>、<=、>♪ ♪ I'll always ♪ |
| ZWrit | On, Off | Depth Writing Switches |
| Blend | Original mode, destination mode | Open and set hybrid mode |