表面着色器包含四个函数入口
(1)顶点变换函数
(2)表面着色函数
(3)光照模型
(4)最终颜色修改函数
(1)(3)(4)这三个函数可选
如何定义入口函数
格式:
#pragma surface 入口函数名称 光照模型[Options]
(1)Options代表的是可选的参数
Suface后面跟着的是表面着色器的入口函数,例如:
surf(Input IN,inout SurfaceOutput o)
(2)光照模型:
1.系统内置的Lamber(漫反射光照)、BlinnPhong(高光光照)
2.自定义光照:名字为Name
四种定义方式
half4 Lighting(SurfaceOutput s,halff3 lightDir, half atten);
half4 Lighting(SurfaceOutput s,half3 lightDir, half3 viewDir, half atten);
half4 Lighting(SurfaceOutput s, half4 light);
(3)[Options]
1.vertex:name
vertex 是入口函数名称
然后下面的函数定义有两种
void(inout appdata_full v)参数的作用是,如果该顶点着色器中的输入顶点数据 使用这个
half4(inout appdata_full v,out input o)修改输入顶点数据,以及为表面着色器转递数据
这是顶点入口函数的两种定义方式
2.finalcolor:name最终颜色修改函数
void (input IN,SurfaceOutput o,inout fixed4 color);
举例使用:
...
#include "UnityCG.cginc"
#pragma surface surf Standard vertex:vert
...
//顶点函数入口,一共有两种声明的方式
//第一种
void vert(inout appdata_full v) {
//其中的appdata_full是在Cg...那个库中定义的,所以需要导入库文件
}
//表面着色器的入口函数 Input必须是大写的 IN也必须是大写的
void surf (Input IN, inout SurfaceOutputStandard o)//对应的是SurfaceOutput
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
...
3.Options中其他可选参数
使用方式
#pragma surface surf Standard fullforwardshadows
其中的Standard 是标准光照,同样可以修改为上面两种光照模型的任意一种(Lamber(漫反射光照)、BlinnPhong(高光光照))
但是需要注意的是,如果是使用的标准光照(Standard),那么在表面着色器入口函数定义的方式为
void surf (Input IN, inout SurfaceOutputStandard o)
然后在SurfaceOutput结构体中才能具备相应的参数如下图
如果是SurfaceOutput,则函数定义中只能由1-6参数
如果是SurfaceOutputStandard则多加上7-8
如果是SurfaceOutputStandardSpecular则全部都可以
如果上面的光照模式为Lamber(漫反射光照)或者BlinnPhong(高光光照)则表面着色器入口函数的参数必须将Stand去掉(不再是标准
void surf (Input IN, inout SurfaceOutput o)
然后需要将函数中不能存在的参数去掉
void surf (Input IN, inout SurfaceOutputSpecular o)//对应的是SurfaceOutput
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
//o.Metallic = _Metallic;
//o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
初学,如有错误请见谅,欢迎旨在指出