Properties支持的属性类型
| 属性类型 |
默认值定义语法 |
例子 |
| 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)=(1,1,1,1) |
| 2D |
“defaulttexture”{} |
_2D(“2D”, 2D)=””{} |
| Cube |
“defaulttexture”{} |
_Cube(“Cube”, Cube)=”white”{} |
| 3D |
“defaulttexture”{} |
_3D(“3D”, 3D)=”black”{} |
- defaulttexture
- MaterialPropertyDrawer
- [KeywordEnum(None, Add, Multiply)] ` ( [KeywordEnum(None, Add, Multiply)] _Overlay(“Overlay mode”, Float) = 0) `
- [Toggle] ` ([Toggle] _Invert(“Invert color?”, Float) = 0) `
- [HideInInspector] ` does not show the property value in the material inspector. `
- [Normal] ` indicates that a texture property expects a normal-map. `
SubShader语义块
// unity shader
SubShader{
// optional
[Tags]
// optional
[RenderSetup]
Pass{
}
// other passes
}
SubShader渲染状态设置选项
| 状态名称 |
设置指令 |
解释 |
| Cull |
Cull Back|Front|Off |
设置剔除模式:剔除背面/正面/关闭 |
| ZTest |
ZTest Less|Greater|LEqual|GEqual|Equal|NotEqual|Always |
设置深度测试时使用的函数 |
| ZWrite |
ZWrite On|Off |
开启/关闭深度写入 |
| Blend |
Blend SrcFactor DstFactor |
开启并设置混合模式 |
- BlendMode
- Zero
Blend factor is (0,0,0,0)
- One
Blend factor is (1,1,1,1)
- DstColor
Blend factor is (Rd, Gd, Bd, Ad)
- SrcColor
Blend factor is (Rs, Gs, Bs, As)
- OnMinusDstColor
Blend factor is (1 - Rd, 1 - Gd, 1 - Bd, 1 - Ad)
- SrcAlpha
Blend factor is (As, As, As, As)
- OneMinusSrcColor
Blend factor is (1 - Rs, 1 - Gs, 1 - Bs, 1 - As)
- DstAlpha
Blend factor is (Ad, Ad, Ad, Ad)
- OneMinusDstAlpha
Blend factor is (1 - Ad, 1 - Ad, 1 - Ad, 1 - Ad)
- SrcAlphaSaturate
Blend factor is (f,f,f,1) where f = min(As, 1 - Ad)
- OneMinusSrcAlpha
Blend factor is (1 - As, 1 - As, 1 - As, 1 - As)
SubShader的标签类型
Tags { "TagName1" = "Value1" "TagName2" = "Value2" }
| 标签类型 |
说明 |
例子 |
| Queue |
控制渲染顺序,指定该物体属于哪一个渲染队列,通过这种方式可以保证所有的透明物体可以在所有不透明物体后面被渲染,我们也可以自定义使用的渲染队列来控制物体的渲染顺序 |
Tags { “Queue” = “Transparent” } |
| RenderType |
对着色器进行分类:不透明着色器 or 透明着色器 etc |
Tags { “RenderType” = “Transparent” } |
| DisableBatching |
指明是否对该 SubShader 使用批处理 |
Tags { “DisableBatching” = “True” } |
| ForceNoShadowCasting |
控制使用该 SubShader 的物体是否会投射阴影 |
Tags { “ForceNoShadowCasting” = “True” } |
| IgnoreProjector |
控制使用该 SubShader 的物体是否受 Projector 的影响,通常用于半透明物体 |
Tags { “IgnoreProjector” = “True” } |
| CanUseSpriteAtlas |
当该 SubShader 是用于sprites时,将该标签设为“False” |
Tags { “CanUseSpriteAtlas” = “False” } |
| PreviewType |
指明材质面板预览模式,默认为球形 |
Tags { “PreviewType” = “Plane”|”SkyBox” } |
- Queue
- Background
- Geometry (default)
- AlphaTest
- Transparent
- Overlay
- RenderType
- Opaque
- Transparent
- TransparentCutout
- Background
- Overlay
- TreeOpaque
- TreeTransparentCutout
- TreeBillboard
- Grass
- GrassBillboard
- 上述标签只能在 SubShader 中申明
Pass语义块
// unity shader
Pass{
[Name]
[Tags]
[RenderSetup]
// other code
}
Pass的标签类型
| 标签类型 |
说明 |
例子 |
| LightMode |
光照模型 |
Tags { “LightMode” = “ForwardBase” } |
| RequireOptions |
满足条件才渲染该Pass |
Tags { “RequireOptions” = “SoftVegetation” } |
- LightMode
- Always
- ForwardBase
- ForwardAdd
- Deferred
- ShadowCaster
- MotionVectors
- PrepassBase
- PrepassFinal
- Vertex
- VertexLMRGBM
- VertexLM
一个简单的 Surface Shader
// unity shader
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"
}
- Cg/HLSL语法结构
- 定义在 SubShader(非 Pass )语义块
一个简单的 Vertex/Fragment Shader
// unity shader
Shader "Custom/Simple VertexFragment Shader"{
SubShader{
Pass{
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
}
}
}
自定义输入结构体
// unity shader
struct StructName {
Type Name : Semantic;
}
- Semantic
+ POSITION (模型空间顶点)
+ TANGENT (切线方向)
+ NORMAL (法线方向)
+ TEXCOORD0 (模型纹理1)
+ TEXCOORD1 (模型纹理2)
+ TEXCOORD2 (模型纹理3)
+ TEXCOORD3 (模型纹理4)
+ COLOR (顶点颜色)
固定函数着色器
// unity shader
Shader "Custom/Fixed Function Shader"{
Properties{
_Color("Main Color", Color) = (1, 1, 1, 1)
}
SubShader{
Pass{
Material{
Diffuse [_Color]
}
Lighting On
}
}
}