Unity Shader (Surface Shader Surface Shader)

basic data type

float 32-bit floating point number

float2——float2(float,float)

float3——float3(float,float,float)

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

half 16-bit floating point number

half2——half2(half,half)

half3——half3(half,half,half)

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

int 32-bit integer

int2——int2(int,int)

int3——int3(int,int,int)

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

fixed 12-bit fixed point

A decimal between 0-1, or an integer

bool Boolean value
string string
sampler2D texture object

pragma

CGPROGRAM (beginning)

#pragma surface surfaceFunction lightModel[optionalparams]

||

#pragma surface surf Lambert

#compiler directive shader name function name lighting model special settings

surfaceFunction function name

lightModel light type - Lambert diffuse reflection

void surf(Input IN,inout SurfaceOutput o)

ENDCG (end)

Input Structure

float2 uv_MainTex

Texture Map UVs

The naming method must be uv+map variable name (_MainTex)

float3 viewDir view direction
worldPos world position
float4 screenPos screen coordinates
color:COLOR (semantic binding) Interpolated color for each vertex

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

Guess you like

Origin blog.csdn.net/qq_24977805/article/details/122930894