Unity Shader(表面着色器Surface Shader)

基本数据类型

float 32位浮点数

float2——float2(float,float)

float3——float3(float,float,float)

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

half 16位浮点数

half2——half2(half,half)

half3——half3(half,half,half)

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

int 32位整型

int2——int2(int,int)

int3——int3(int,int,int)

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

fixed 12位定点数

取值范围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

float2 uv_MainTex

纹理贴图UV

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

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

Surface Output

half3 Albedo 反射率,即颜色纹理rgb
Normal 法线,法向量(x,y,z)
Emission 自发光颜色rgb
half Specular 镜面反射度
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

猜你喜欢

转载自blog.csdn.net/qq_24977805/article/details/122930894