Unity 各向异性头发效果
3D建模技术文章教学modeling技术综合
显示全部 7
9794 0
实名

通过了实名认证的内容创造者

发布于 2019-12-16 09:15:07

您需要 登录 才可以下载或查看,没有账号?注册

x
一 概览
本文讲解了各向异性头发效果的原理,Shader的具体实现,以及部分注意事项。
社会你坤弟,人怂话又多,这次先上Demo: AnisotropicHair




二 原理
1 面片模型配合基本颜色贴图来模拟发丝的效果。




2 基本贴图



3 高光走向Kajiya-Kay模型:利用切线(tangent)方向来计算高光强度。




4 两层高光Marschner模型:两层高光,并为第二层高光添加噪点。




三 Shader实现
1 Diffuse Lightingfixed4 albedo = tex2D(_MainTex, i.uv);half3 diffuseColor = albedo.rgb * _MainColor.rgb;
2 两层高光//获取头发高光fixed StrandSpecular ( fixed3 T, fixed3 V, fixed3 L, fixed exponent){    fixed3 H = normalize(L + V);    fixed dotTH = dot(T, H);    fixed sinTH = sqrt(1 - dotTH * dotTH);    fixed dirAtten = smoothstep(-1, 0, dotTH);    return dirAtten * pow(sinTH, exponent);}            //沿着法线方向调整Tangent方向fixed3 ShiftTangent ( fixed3 T, fixed3 N, fixed shift){    return normalize(T + shift * N);}fixed3 spec = tex2D(_AnisoDir, i.uv).rgb;//计算切线方向的偏移度half shiftTex = spec.g;half3 t1 = ShiftTangent(worldBinormal, worldNormal, _PrimaryShift + shiftTex);half3 t2 = ShiftTangent(worldBinormal, worldNormal, _SecondaryShift + shiftTex);//计算高光强度        half3 spec1 = StrandSpecular(t1, worldViewDir, worldLightDir, _SpecularMultiplier)* _SpecularColor;half3 spec2 = StrandSpecular(t2, worldViewDir, worldLightDir, _SpecularMultiplier2)* _SpecularColor2;
3 结合起来fixed4 finalColor = 0;finalColor.rgb = diffuseColor + spec1 * _Specular;//第一层高光finalColor.rgb += spec2 * _SpecularColor2 * spec.b * _Specular;//第二层高光,spec.b用于添加噪点finalColor.rgb *= _LightColor0.rgb;//受灯光影响finalColor.a += albedo.a;
四 注意事项
1 半透明渲染次序错乱头发挽起来的部位出现在了前面:




通过添加一个写入深度的Pass可解决此问题:
Pass{    ZWrite On //写入深度,被遮挡的像素将不能通过深度测试    ColorMask 0 //不输出颜色}
2 半透穿透但随之而来却产生了另外一个问题:下层头发被剔除之后,上层头发直接和背景色进行了混合




除了调整贴图alpha值之外,还可在通过在新添加的Pass中添加一个基本的颜色来解决此问题:
half4 finalColor = half4(0, 0, 0, albedo.a);finalColor.rgb += (albedo.rgb * _MainColor.rgb) * _LightColor0.rgb;return finalColor;
3 高光方向为确保头发的高光走向为沿着发根到发尖,需要设置模型的轴向为Y轴朝上(和Unity一致)




同时UV方向也要和模型方向一致








9476896-afc6bb13d1bf08ab.png

评分

参与人数 1元素币 +10 活跃度 +12 展开 理由
源支始 + 10 + 12

查看全部评分

本帖被以下画板推荐:

接U3D外包,V 695907593
使用道具 <
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表