最近研究了一下美式卡通效果,不过模型同学提供的贴图皮肤部分阴影过重,不太符合我清新透亮的审美,哈哈
衣服和皮质都区分了质感和纹理,金属部分依旧用的老办法,法线,边缘光,描边,头发做了两种效果,一个是基于切线空间的扰乱图的方法,
一个使用了算法实现的,不过第二种头发需要在画贴图的时候就尽量多的表现头发丝的效果,法线贴图需要比较细才可以,因此第二种头发目前不太好看。
下面展示下最终效果
代码展示:
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_MainColor ("MainColor",Color) = (1, 1, 1, 1)
_Cubemap ("Cube Map", CUBE) = "grey"{}
_BumpMap ("Normal Map", 2D) = "bump" {}
_ClothDetailMap ("ClothDetailMap", 2D) = "bump" {}
_LeatherDetailMap ("LeatherDetailMap", 2D) = "bump" {}
_MaskTex ("Mask(R=SpecLeat,G=Refl,B=Cloth,A=Skin)",2D) = "white"{}
_ReflectColor ("ReflectColor",Color) = (0.2, 0.2, 0.2, 1)
_Specular ("Specular Color", Color) = (1, 1, 1, 1)
_Gloss ("Gloss", Range(8.0, 256)) = 20
_RimColor ("Rim Color", Color) = (1, 1, 1, 1)
_RimWidth ("Rim Width", float) = 0.9
_ambientColor ("AmbientColor", Color) = (1, 1, 1, 1)
_BlurRadius("Blur Radius", Range(0, 10)) = 1
_BlurTex("BlurTex (RGB)", 2D) = "white" {}
_HairTex("HairTex (RGB)", 2D) = "white" {}
_MainHairSpecularColor ("MainHairSpecularColor", Color) = (1, 1, 1, 1)
_FuHairSpecularColor ("FuHairSpecularColor", Color) = (1, 1, 1, 1)
_MainHairSpecularSmooth ("MainHairSpecularSmooth", Range(0, 10)) = 1
_FuHairSpecularSmooth ("FuHairSpecularSmooth", Range(0, 10)) = 1
_MainHairSpecular ("MainHairSpecularOff", Range(0, 10)) = 1
_FuHairSpecular ("FuHairSpecularOff", Range(0, 10)) = 1
_Outline("Outline Width", Range(.0, 2)) = .5
_OutlineCol("OutlineCol", Color) = (1,0,0,1)
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 300
//先是描边操作了
Pass {
Tags { "LightMode"="ForwardBase" }
Cull Front
offset 1,1
CGPROGRAM
#pragma multi_compile_fwdbase
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform float _Outline;
fixed4 _OutlineCol;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
float3 norm = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);
float2 offset = TransformViewToProjection(norm.xy);
o.pos.xy += offset * _Outline * 0.5 ;
return o;
}
fixed4 frag(v2f i) : SV_Target {
return _OutlineCol;
}
ENDCG
}
Pass {
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
#pragma multi_compile_fog
#pragma multi_compile_fwdbase
#define USING_FOG (defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2))
float4 _MainTex_ST;
float4 _ClothDetailMap_ST;
samplerCUBE _Cubemap;
float4 _LeatherDetailMap_ST;
fixed3 _ambientColor;
fixed3 _Specular;
float _Gloss;
uniform fixed4 _RimColor;
float _RimWidth;
half _BlurRadius;
sampler2D _HairTex;
float4 _HairTex_ST;
float _MainHairSpecular;
float _FuHairSpecular;
fixed _MainHairSpecularSmooth;
fixed _FuHairSpecularSmooth;
struct appdata
{
float4 pos : POSITION;
float4 uv1 : TEXCOORD1;
//float3 uv0 : TEXCOORD0;
fixed3 normal : NORMAL;
float4 tangent : TANGENT;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
fixed4 color : COLOR0;
float4 uv1 : TEXCOORD0;
#if USING_FOG
fixed fog : TEXCOORD1;
#endif
fixed3 tangent : TEXCOORD2;
fixed3 vDir : TEXCOORD3;
float4 pos : SV_POSITION;
float4 TtoW0 : TEXCOORD4;
float4 TtoW1 : TEXCOORD5;
float4 TtoW2 : TEXCOORD6;
float4 uv2 : TEXCOORD7;
UNITY_VERTEX_OUTPUT_STEREO
};
v2f vert(appdata IN)
{
v2f o;
UNITY_INITIALIZE_OUTPUT(v2f, o);
UNITY_SETUP_INSTANCE_ID(IN);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
float3 eyePos = UnityObjectToViewPos(float4(IN.pos.xyz, 1));
o.uv1.xy = IN.uv1.xy * _MainTex_ST.xy + _MainTex_ST.zw;
o.uv1.zw = IN.uv1.xy * _ClothDetailMap_ST.xy + _ClothDetailMap_ST.zw;
o.uv2.xy = IN.uv1.xy * _LeatherDetailMap_ST.xy + _LeatherDetailMap_ST.zw;
o.uv2.zw = IN.uv1.xy * _HairTex_ST.xy + _HairTex_ST.zw;
#if USING_FOG
float fogCoord = length(eyePos.xyz);
UNITY_CALC_FOG_FACTOR_RAW(fogCoord);
o.fog = saturate(unityFogFactor);
#endif
o.pos = UnityObjectToClipPos(IN.pos);
o.vDir = normalize(WorldSpaceViewDir(float4(IN.pos.xyz, 1)));
float3 worldPos = mul(unity_ObjectToWorld, IN.pos);
//常规的法线操作
fixed3 worldNormal = UnityObjectToWorldNormal(IN.normal);
fixed3 worldTangent = UnityObjectToWorldDir(IN.tangent.xyz);
fixed3 worldBinormal = cross(worldNormal, worldTangent) * IN.tangent.w;
o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
//边缘光操作
float dotProduct = 1 - max(0,dot(worldNormal, o.vDir));
o.color = smoothstep(1 - _RimWidth, 1.0, dotProduct);
o.color *= _RimColor;
//求副切线方向
half4 p_tangent = mul(unity_ObjectToWorld, IN.tangent);
o.tangent = normalize(p_tangent).xyz;
o.tangent = cross(o.tangent, worldNormal);
return o;
}
sampler2D _LeatherDetailMap;
sampler2D _ClothDetailMap;
float4 _MainColor;
sampler2D _MainTex;
sampler2D _MaskTex;
float4 _ReflectColor;
sampler2D _BumpMap;
float _HeightFactor;
sampler2D _HeightMap;
sampler2D _BlurTex;
float4 _MainHairSpecularColor;
float4 _FuHairSpecularColor;
//头发的光照计算
float HairSpecular(fixed3 halfDir, float3 tangent, float specularSmooth)
{
float dotTH = dot(tangent, halfDir);//副切与半角响亮点积
float sqrTH =max(0.01,sqrt(1 - pow(dotTH, 2)));//限制高光范围
float atten = smoothstep(-1,0, dotTH);//计算高光衰减
float specMain = atten * pow(sqrTH, specularSmooth);
return specMain;
}
fixed4 frag(v2f IN) : SV_Target
{
fixed4 tex,uvanim;
float3 worldPos = float3(IN.TtoW0.w, IN.TtoW1.w, IN.TtoW2.w);
fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
half4 Mask = tex2D(_MaskTex, IN.uv1.xy);
//主图法线
fixed3 bump = normalize(UnpackNormal(tex2D(_BumpMap, IN.uv1.xy)));
bump = normalize(half3(dot(IN.TtoW0.xyz, bump), dot(IN.TtoW1.xyz, bump), dot(IN.TtoW2.xyz, bump)));
//衣服细节法线
fixed3 ClothDetail = normalize(UnpackNormal(tex2D(_ClothDetailMap, IN.uv1.zw))) ;
ClothDetail = normalize(half3(dot(IN.TtoW0.xyz, ClothDetail), dot(IN.TtoW1.xyz, ClothDetail), dot(IN.TtoW2.xyz, ClothDetail)))* Mask.b;
//皮革细节法线
fixed3 LeatherDetail = normalize(UnpackNormal(tex2D(_LeatherDetailMap, IN.uv2))) ;
LeatherDetail = normalize(half3(dot(IN.TtoW0.xyz, LeatherDetail), dot(IN.TtoW1.xyz, LeatherDetail), dot(IN.TtoW2.xyz, LeatherDetail)))* Mask.r;
fixed3 finalNormal = normalize(fixed3 (bump.x + ClothDetail.x + LeatherDetail.x,bump.y + ClothDetail.y + LeatherDetail.y,bump.z + ClothDetail.z + LeatherDetail.z));
fixed3 reflectDir = normalize(reflect(-IN.vDir,bump));
fixed3 reflect = texCUBE(_Cubemap , reflectDir) * Mask.g * _ReflectColor ;
tex = tex2D(_MainTex, IN.uv1.xy) ;
fixed4 col = tex2D(_BlurTex, IN.uv1.xy);
fixed3 blurcol = tex;
blurcol += col.rgb * 64;
blurcol += col.rgb * 32;
blurcol += col.rgb * 16;
blurcol += col.rgb * 8;
blurcol += col.rgb * 4;
blurcol += col.rgb * 2;
blurcol /= 256;
fixed3 diffuse = tex * _LightColor0.rgb * (dot(finalNormal, lightDir) * 0.5 + 0.2)* _MainColor ;
//fixed3 diffuse = tex * _LightColor0.rgb * max(0,(dot(bump, lightDir)))* _MainColor ;
fixed3 halfDir = normalize(lightDir + IN.vDir);
fixed3 halfDirHair = normalize(-lightDir + IN.vDir);
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(finalNormal, halfDir)), _Gloss) * (Mask.r+Mask.g);
float3 speTex = tex2D(_HairTex, IN.uv2.zw);
float3 Ts = IN.tangent + half3(IN.TtoW0.z,IN.TtoW1.z,IN.TtoW2.z) * _MainHairSpecular * speTex;
float3 Tf = IN.tangent + half3(IN.TtoW0.z,IN.TtoW1.z,IN.TtoW2.z) * _FuHairSpecular * speTex;
float specMain = HairSpecular(halfDirHair,Ts, _MainHairSpecularSmooth) * _MainHairSpecularColor;
float specFu = HairSpecular(halfDirHair,Tf, _FuHairSpecularSmooth) * _FuHairSpecularColor;
float4 color1;
color1.rgb= tex.rgb ;
color1.a = 1;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * tex.rgb * _ambientColor;
#if USING_FOG
color1.rgb = lerp(unity_FogColor.rgb, color1.rgb, IN.fog);
#endif
return half4(lerp(color1.rgb ,(blurcol * _BlurRadius + color1) , Mask.a) + diffuse + ambient + specular + reflect + (specMain + specFu) * col.a + IN.color * (1-col.a),1 );
}
ENDCG
}
}