1.surface shader包含两个结构体和四个函数
两个结构体:
(1)Input(输入)
例:
struct Input{
float2 uv_MainTex;
}
【Input结构是允许我们自定义的,它可以包含一些纹理坐标和其他提前定义的变量,
例如view direction(float3 viewDir)、world space position(worldPos)、
world space reflection vector(float3 worldRefl)等】
(2)SurfaceOutput(输出)
例:
struct SurfaceOutput {
half3 Albedo;
half3 Normal;
half3 Emission;
half Specular;
half Gloss;
half Alpha;
};
【SurfaceOutput无法自定义这个结构体内的变量。
Albedo:我们通常理解的对光源的反射率;
Normal:即其对应的法线方向;
Emission:自发光;
Specular:高光反射中的指数部分的系数。影响一些高光反射的计算;
Gloss:高光反射中的强度系数;
Alpha:通常理解的透明通道。
】
四个函数:
(1)VertexFunction
例:
void vert(inout appdata_full v, out Input o)
{
o.vertColor = v.color;
}
【如果自定义了VertexFunction,Unity会在这里首先调用VertexFunction修改顶点数据;
然后分析VertexFunction修改的数据,最后通过Input结构体将修改结果存储到v2f_surf中。】
(2)surfaceFuntion
例:
void surf (Input IN, inout SurfaceOutput o)
{
float4 c;
c = pow((_EmissiveColor + _AmbientColor), _MySliderValue);
o.Albedo = c.rgb + tex2D(_RampTex, IN.uv_RampTex).rgb;
o.Alpha = c.a;
}
【做出一系列pos纹理坐标、normal(如果没有使用LightMap)、
vlight(如果没有使用LightMap)、lmap(如果使用LightMap)等操作】
(3)LightingModel(自定义光照函数)
例:
inline float4 LightingBasicDiffuse (SurfaceOutput s, fixed3 lightDir, fixed atten)
{
float difLight = max(0, dot (s.Normal, lightDir));
float hLambert = difLight * 0.5 + 0.5;
float3 ramp = tex2D(_RampTex, float2(hLambert)).rgb;
float4 col;
col.rgb = s.Albedo * _LightColor0.rgb * (ramp) * atten;
col.a = s.Alpha;
return col;
}
【自定义光照,也可用内置的一些光照函数——Lambert(diffuse)和Blinn-Phong(specular)】
(4)ColorFunction
例:
void final(Input IN, SurfaceOutput o, inout fixed4 color) {
color = color * 0.5 + 0.5;
}
【】
Unity shader随笔记(一)SurfaceShader
猜你喜欢
转载自blog.csdn.net/u012239432/article/details/46390329
今日推荐
周排行