Unity Shader示例 6: 卡渲基础 - 描边 + 着色
0 、获取原神模型:
【游戏开发实战】下载原神模型,PMX转FBX,导入到Unity中,卡通渲染,绑定人形动画(附Demo工程)-CSDN博客
《原神》公测视频征集计划
一、描边pass:Outline
1. 将顶点沿着法线方向向外扩
2. 剔除正面,只保留背面,得到外轮廓:Cull Front
3. 控制粗细随视距(顶点与摄像机距离)缩放:

v2f vert(appdata v)
{
v2f o;
//随视距缩放
float camDistance = length(mul(unity_ObjectToWorld, v.vertex) -
_WorldSpaceCameraPos);//顶点与摄像机的距离
float3 OutlineWidth = v.normal * _OutlineWidth * camDistance;
//顶点外扩
float3 pos = v.vertex + OutlineWidth;
o.pos = TransformObjectToHClip(pos);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}


选加:
4. 利用模板缓冲,仅获得最外轮廓
5. 利用法线平均化修复法线转折处的描边断裂
6. SRP合批优化:render object过滤绘制外轮廓的pass
二、着色Pass:Unlit
1. 兰伯特 + rampTexture ——二分
2. 高光 + 边缘光(菲涅尔)
3. 阴影:① 产生阴影的Pass:ShadowCaster,② 再在Unlit中接收阴影
可以在urp管线的配置中修改shadow的精度
half4 frag(v2f i) : SV_Target
{
half4 col = (0,0,0,0);
half3 l = _MainLightPosition;
half3 v = normalize(i.viewWS);
//漫反射
half nl = dot(l, i.normalWS) * 0.5 + 0.5 ;
//阴影
if(_UseShadow){
Light mainLight = GetMainLight(i.shadowCoord);
nl *= mainLight.shadowAttenuation * 0.5 + 0.5;//获取阴影衰减值
}
half4 baseColor = tex2D(_MainTex, i.uv);
col = baseColor;
//RampTex
if(_UseRampTex){
half4 rampColor = tex2D(_RampTex, nl);
col *= rampColor;
}
//镜面反射
if(_UseSpecular){
half3 h = normalize(l + v);
half nh = pow(max(0, dot(i.normalWS, h)), _SpecPower * 100);
col += nh * _SpecColor;
}
//边缘光(菲涅尔)
if(_UseRimLight){
half nv = 1 - max (0, dot (i.normalWS, v));
half f = pow(nv, _RimPower) * _SpecColor;
col += f;
}
return col ;
}