Unity shader随笔记(一)SurfaceShader

来源:互联网 发布:mysql 存储过程 性能 编辑:程序博客网 时间:2024/05/18 12:33
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;   
}  
【】
0 0
原创粉丝点击