Unity shader随笔记(一)SurfaceShader

这篇博客详细介绍了Unity中的Surface Shader,包括Input和SurfaceOutput结构体以及VertexFunction、surf、LightingModel和ColorFunction四个关键函数。通过实例解释了如何自定义顶点数据、表面输出属性以及光照模型,帮助读者理解Unity中Shader的基础应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;   
}  
【】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值