unity-表面shader需要具备的基本知识

表面着色器包含四个函数入口

(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;
        }

初学,如有错误请见谅,欢迎旨在指出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值