最近公司要启动新项目,所以研究了一下卡通渲染,参考崩坏3与罪恶装备的效果
通过backface做的轮廓描边,接着使用绘制的轮廓内贴图通过调整UV的方式呈现,原因是为了防止马赛克的出现
阴影是通过阴影图加定点颜色来控制,比较灵活,顶点颜色甚至可以使用Unity来绘制
以下是代码分享:
Shader "BaseTest" {
Properties {
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_Darken ("Darken Color", Color) = (1, 1, 1, 1)
_RimPower ("RimPower", Range(-1, 10)) = 0
_MainTex ("Main Tex", 2D) = "white" {}
_DarkenInnerLineColor("Darken Inner Line Color", Range(0, 1)) = 0.2
_ILMTex("ILM (RGB)", 2D) = "white" {}
_Outline("Outline Width", Range(.0, 2)) = .5
_LightMap ("LightMap", 2D) = "white" {}
_LightArea ("LightArea", Range(-1, 1)) = 0
_LightArea1 ("LightArea1", Range(-1, 1)) = 0
_SecondShadow ("SecondShadow", Range(-1, 1)) = 0
_OutlineCol("OutlineCol", Color) = (1,0,0,1)
_FirstShadowMultColor("FirstShadowMultColor", Color) = (1,1,1,1)
_SecondShadowMultColor("SecondShadowMultColor", Color) = (1,1,1,1)
_Shininess("Shininess", Range(0, 256)) = 0
_LightSpecColor("LightSpecColor", Color) = (1, 1, 1, 1)
_SpecMulti("SpecMulti", Range(0, 256)) = 0
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Geometry"}
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" }
Cull Back
CGPROGRAM
#pragma multi_compile_fwdbase
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
uniform float _Outline;
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
//fixed4 _Specular;
float _Shininess;
sampler2D _LightMap;
sampler2D _ILMTex;
float _LightArea;
fixed4 _FirstShadowMultColor;
fixed4 _SecondShadowMultColor;
uniform float _DarkenInnerLineColor;
float _SecondShadow;
float _RimPower;
fixed4 _Darken;
float _LightArea1;
fixed4 _LightSpecColor;
float _SpecMulti;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
float4 color : COLOR;
};
struct v2f {
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
float3 worldPos : TEXCOORD1;
float3 worldNormal : TEXCOORD2;
float4 color : COLOR;
SHADOW_COORDS(5)
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.color = v.color;
TRANSFER_SHADOW(o);
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed3 lightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
fixed3 albedo = tex2D(_MainTex, i.uv.xy).rgb * _Color.rgb;
fixed4 cILM = tex2D(_ILMTex, i.uv.xy);
float clampedLineColor = cILM.a;
if (clampedLineColor < _DarkenInnerLineColor)
clampedLineColor = _DarkenInnerLineColor;
half3 InnerLineColor = half3(clampedLineColor,clampedLineColor,clampedLineColor);
float rim = max(0, dot(viewDir, i.worldNormal));
fixed3 rimColor = _Darken * pow(rim, 1 / _RimPower);
rimColor*=1-InnerLineColor;
rimColor = 1-rimColor;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 lightMapColor = tex2D(_LightMap, i.uv.xy).rgb;
fixed mask = lightMapColor.r * i.color.r;
half halfLambert = max(0, dot( i.worldNormal, lightDir))*0.5+0.5;
mask += saturate(halfLambert);
mask = mask *0.5 +(-_LightArea) + 1;
int lightStep = step(1,mask); //1>mask 1 : 0
fixed3 firstShadow = albedo.xyz * _FirstShadowMultColor.rgb;
if(lightStep!=0)//1>mask用固有色
firstShadow = albedo.xyz;
else //1<mask用阴影
firstShadow = firstShadow ;
fixed3 secondShadow = albedo.xyz * _SecondShadowMultColor.rgb;
fixed secMask = i.color.r * lightMapColor.y + saturate(halfLambert);
secMask = secMask *0.5 + (-_SecondShadow)+1;
lightStep = step(1, secMask);
if(lightStep !=0)
secondShadow= albedo.xyz;
else
secondShadow= secondShadow;
fixed sep = i.color.r * lightMapColor.y + _LightArea1;
int sepMask = step(1,sep);
fixed3 finalColor;
if(sepMask != 0) //1>mask 1:0
finalColor= firstShadow;
else //1<mask
finalColor= secondShadow;
//fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(bump, lightDir));
//fixed3 halfDir = normalize(lightDir + viewDir);
//fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(bump, halfDir)), _Gloss);
float3 halfView = normalize(viewDir + normalize(_WorldSpaceLightPos0.xyz));
float shinPow = pow(max(dot(normalize(i.worldNormal.xyz), halfView), 0), _Shininess);
float oneMinusSpec = 1 - lightMapColor.z;
oneMinusSpec = oneMinusSpec - shinPow;
int specMaslk = step(0,oneMinusSpec);
fixed3 specColor = _SpecMulti * _LightSpecColor.xyz;
specColor = lightMapColor.x * specColor;
if(specMaslk!=0)
specColor = 0;
else
specColor = specColor;
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
//return fixed4( albedo /** InnerLineColor *//*+finalColor*/, 1.0);
//return fixed4( albedo*(1-rimColor)+finalColor, 1.0);
return fixed4( (albedo * rimColor +finalColor )/2 + specColor, 1.0);
//return fixed4(ambient + (diffuse + specular) * atten /*+ reflectColor*/, 1.0);
}
ENDCG
}
}
}