引子
Surface Shader
最简单的SurfaceShader
1 | Shader "Custom/T_3_0" { |
struct Input {
float2 uv_MainTex;
float3 viewDir;
float4 anyName:COLOR;
float4 screenPos;
float3 worldPos;
float3 worldRefl;
} ;1
2
3
4
5
6
7
8uv_MainTex: uv
viewDir: 入视角
screenPos:裁剪空间位置
worldPos:世界空间位置
### 输出
struct SurfaceOutput {
half3 Albedo;
half3 Normal;
half3 Emission;
half Specular;
half Gloss;
half Alpha;
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20Albedo:漫反射颜色
Normal: 法线
Emission:自发光颜色
Specular:镜面反射系数
Gloss:
Alpha:透明值
[参考](http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaders.html)
## Surface Shader参数
Surface Shader可以通过加参数实现额外的控制,例如加顶点处理函数,alpha混合等。
注意:
如果使用的光照模型是Unity自带的Lambert或者BlinnPhong:
alpha混合,alpha混合等功能必须使用Surface Shader参数。
### alpha混合
直接加参数Alpha即可
pragma surface surf BlinnPhong alpha
1 |
|
Shader “Custom/T_3_0” {
Properties {
_MainTex (“Base (RGB)”, 2D) = “white” {}
_Cutoff (“Alpha cutoff”, Range(0,1)) = 0.5
}
SubShader {
CGPROGRAM
#pragma surface surf Lambert alphatest:_Cutoff
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
}
}1
2
3
4
5
### 顶点函数
例:臃肿的模型
实现方法,将顶点向法线方向延伸一段距离
Shader “Example/Normal Extrusion” {
Properties {
_MainTex (“Texture”, 2D) = “white” {}
_Amount (“Extrusion Amount”, Range(-1,1)) = 0.5
}
SubShader {
Tags { “RenderType” = “Opaque” }
CGPROGRAM
#pragma surface surf Lambert vertex:vert
struct Input {
float2 uv_MainTex;
};
float _Amount;
void vert (inout appdata_full v) {
v.vertex.xyz += v.normal * _Amount;
}
sampler2D _MainTex;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
}
ENDCG
}
Fallback "Diffuse"
}1
2
3
4### finalcolor
finalcolor:ColorFunction
对颜色输出作最后的更改
Shader “Example/Tint Final Color” {
Properties {
_MainTex (“Texture”, 2D) = “white” {}
_ColorTint (“Tint”, Color) = (1.0, 0.6, 0.6, 1.0)
}
SubShader {
CGPROGRAM
#pragma surface surf Lambert finalcolor:mycolor
struct Input {
float2 uv_MainTex;
};
fixed4 _ColorTint;
void mycolor (Input IN, SurfaceOutput o, inout fixed4 color)
{
//在这里对最后输出的颜色color进行修改
color *= _ColorTint;
}
sampler2D _MainTex;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
}
ENDCG
}
}
```