分析目标:Toony Colors Pro 2/Examples/Cat Demo/UnityChan/Style 1
项目地址:https://gitee.com/yichichunshui/ToonyColors.git
首先是surf函数:
void surf(Input IN, inout SurfaceOutputCustom o)
{
fixed4 mainTex = tex2D(_MainTex, IN.UV_MAINTEX);
//Shadow Color Texture
fixed4 shadowTex = tex2D(_STexture, IN.UV_MAINTEX);
o.ShadowColorTex = shadowTex.rgb;
o.Albedo = mainTex.rgb * _Color.rgb;
o.Alpha = mainTex.a * _Color.a;
//Normal map
half4 normalMap = tex2D(_BumpMap, IN.uv_BumpMap.xy);
o.Normal = UnpackScaleNormal(normalMap, _BumpScale);
o.DiffTintMask = mainTex.a;
//Rim
float3 viewDir = normalize(IN.viewDir);
half rim = 1.0f - saturate( dot(viewDir, o.Normal) );
rim = smoothstep(_RimMin, _RimMax, rim);
o.Rim = rim;
//VFace Register (backface lighting)
o.vFace = IN.vFace;
}
采样主纹理,采样阴影纹理,主纹理和_Color混合,采样法线贴图,多了一个DiffTintMask值,计算边缘因子,还有一个从来没有见过的vface。
这个参考:https://docs.unity3d.com/Manual/SL-ShaderSemantics.html
A fragment shader can receive a variable that indicates whether the rendered surface is facing the camera
, or facing away from the camera. This is useful when rendering geometry that should be visible from both sides – often used on leaves and similar thin objects. The VFACE semantic input variable will contain a positive value for front-facing triangles, and a negative value for back-facing ones.
This feature only exists from shader model 3.0 onwards, so the shader needs to have the #pragma target 3.0 compilation directive.
这个值能揭示一个三角形是朝着相机的还是背着相机的。朝着相机的正面,vface>0;背着相机的,vface<0,unity官网提供了一个例子:
Shader "Unlit/Face Orientation"
{
Properties
{
_ColorFront ("Front Color", Color) = (1,0.7,0.7,1)
_ColorBack ("Back Color", Color) = (0.7,1,0.7,1)
}
SubShader
{
Pass
{
Cull Off // turn off backface culling
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
float4 vert (float4 vertex : POSITION) : SV_POSITION
{
return UnityObjectToClipPos(vertex);
}
fixed4 _ColorFront;
fixed4 _ColorBack;
fixed4 frag (fixed facing : VFACE) : SV_Target
{
// VFACE input positive for frontbaces,
// negative for backfaces. Output one
// of the two colors depending on that.
return facing > 0 ? _ColorFront : _ColorBack;
}
ENDCG
}
}
}
运行结果:
ok,这个surf函数。
自定义灯光函数:
就这两个地方有差别,其他没有差别。
可以看到第一种风格的渲染结果。
而换成第二个风格的时候,其阴影更加平滑。
这个值其实就是那个smoothstep函数的设置的了。
第一种风格的设置:
分析其原因:
下载安装matlab。
RGB转为HSV模型:
http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
色调(H, Hue);饱和度(S,Saturation);明度(V, Value)。
用角度度量,取值范围为0°~360°
饱和度S表示颜色接近光谱色的程度,通常取值范围为0%~100%,值越大,颜色越饱和。
明度表示颜色明亮的程度,对于光源色,明度值与发光体的光亮度有关;对于物体色,此值和物体的透射比或反射比有关。通常取值范围为0%(黑)到100%(白)。