Unity Shader(表面着色器Surface Shader)

基本数据类型

float32位浮点数

float2——float2(float,float)

float3——float3(float,float,float)

float4——float4(float,float,float,float)

half16位浮点数

half2——half2(half,half)

half3——half3(half,half,half)

half4——half4(half,half,half,half)

int32位整型

int2——int2(int,int)

int3——int3(int,int,int)

int4——int4(int,int,int,int)

fixed12位定点数

取值范围0-1之间的小数,或整数

bool布尔值
string字符串
sampler2D纹理对象

编译指令

CGPROGRAM(开头)

#pragma surface surfaceFunction lightModel[optionalparams]

||

#pragma surface surf Lambert

#编译指令  着色器名称  函数名称  光照模型  特殊设置

surfaceFunction函数名称

lightModel光照类型———Lambert漫反射

void surf(Input IN,inout SurfaceOutput o)

ENDCG(结尾)

Input Structure

float2uv_MainTex

纹理贴图UV

命名方式必须为uv+贴图变量名称(_MainTex)

float3viewDir视图方向
worldPos世界坐标位置
float4screenPos屏幕坐标位置
color:COLOR (语义绑定)每个顶点的内插值颜色

Surface Output

half3Albedo反射率,即颜色纹理rgb
Normal法线,法向量(x,y,z)
Emission自发光颜色rgb
halfSpecular镜面反射度
Gloss光泽度
Alpha透明度

 常用CG函数

UnpackNormal

fixed4输入

返回fixed3

返回对应的法线

saturate限制值函数
限制0-1
dot点乘
tex2D通过uv查找贴图纹理的当前顶点颜色值

案例(SubShader中不用写Pass通道)

        surface Color

固定颜色

struct Input {float4 color:COLOR};

o.Albedo = float3(0.5,0.8,0.3);

可调颜色

struct Input {float4 color:COLOR};

与properties同名——float4_Color;

o.Albedo = _Color.rgb;

o.Alpha = _Color.a;前提:#pragma surface surf Lambert alpha

         surface Texture

与properties同名——sampler2D _MainTex;
定义图片的uv坐标——struct Input {float2 uv_MainTex};
o.Albedo = tex2D(_MainTex,IN.uv_MainTex).rgb;

         suface Normal

struct Input {float2 uv_MainTex;};

struct Input {float2 uv_BumpMap;};

sampler2D _MainTex;

sampler2D _BumpMap;

o.Normal = UnpackNormal (tex2D(_BumpMap,IN.uv_BumpMap));

         suface Texture + Color + Normal

struct Input {float2 uv_MainTex;};

struct Input {float2 uv_BumpMap;};

struct Input {fixed4 _MainColor;};

sampler2D _MainTex;

sampler2D _BumpMap;

fixed4 _MainColor;

o.Albedo = tex2D(_MainTex,uv_MainTex).rgb;

o.Albedo += _MainColor.rgb;

o.Normal = UnpackNormal (tex2D(_BumpMap,IN.uv_BumpMap));

        suface Texture + Color + Normal + RimEmission 

struct Input {half3 viewDir;};
half rim = 1-saturate(dot(normalize(IN.viewDir),o.Normal));

o.Emission = _RimColor.rgb * pow(rim,_RimPower);

-----------------------------------------------------------------------

o.Emission = _RimColor.rgb * rim * _RimPower

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值