Unity Shader 入门精要里提到一种基于几何方法的描边方式。
https://blog.csdn.net/candycat1992/article/details/45577749
Shader "Unity Shaders Book/Chapter 14/Toon Shading" {
Properties {
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_MainTex ("Main Tex", 2D) = "white" {}
_Ramp ("Ramp Texture", 2D) = "white" {}
_Outline ("Outline", Range(0, 1)) = 0.1
_OutlineColor ("Outline Color", Color) = (0, 0, 0, 1)
_Specular ("Specular", Color) = (1, 1, 1, 1)
_SpecularScale ("Specular Scale", Range(0, 0.1)) = 0.01
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Geometry"}
Pass {
NAME "OUTLINE"
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float _Outline;
fixed4 _OutlineColor;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
};
v2f vert (a2v v) {
v2f o;
float4 pos = mul(UNITY_MATRIX_MV, v.vertex);
float3 normal = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);
normal.z = -0.5;
pos = pos + float4(normalize(normal), 0) * _Outline;
o.pos = mul(UNITY_MATRIX_P, pos);
return o;
}
float4 frag(v2f i) : SV_Target {
return float4(_OutlineColor.rgb, 1);
}
ENDCG
}
Pass {
Tags { "LightMode"="ForwardBase" }
Cull Back
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
#include "UnityShaderVariables.cginc"
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _Ramp;
fixed4 _Specular;
fixed _SpecularScale;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
float4 tangent : TANGENT;
};
struct v2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
float3 worldPos : TEXCOORD2;
SHADOW_COORDS(3)
};
v2f vert (a2v v) {
v2f o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(_Object2World, v.vertex).xyz;
TRANSFER_SHADOW(o);
return o;
}
float4 frag(v2f i) : SV_Target {
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
fixed3 worldHalfDir = normalize(worldLightDir + worldViewDir);
fixed4 c = tex2D (_MainTex, i.uv);
fixed3 albedo = c.rgb * _Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
fixed diff = dot(worldNormal, worldLightDir);
diff = (diff * 0.5 + 0.5) * atten;
fixed3 diffuse = _LightColor0.rgb * albedo * tex2D(_Ramp, float2(diff, diff)).rgb;
fixed spec = dot(worldNormal, worldHalfDir);
fixed w = fwidth(spec) * 2.0;
fixed3 specular = _Specular.rgb * lerp(0, 1, smoothstep(-w, w, spec + _SpecularScale - 1)) * step(0.0001, _SpecularScale);
return fixed4(ambient + diffuse + specular, 1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}
思路就是将模型面片背面沿着法线方向沿伸,之后将背面颜色变为线条颜色。
但这里一直有个疑问,既然背面沿法线方向延伸了,那么为什么没有把面片正面给遮住,而是部分显示了。
首先要知道这里说的背面是面片的背面,而不是光线照不到的地方。
请看这篇文章:https://www.cnblogs.com/MrZivChu/p/shader3.html
之后说下个人理解吧。
第一个Pass:
我们将一个面片的背面延伸了,并且对其着色了,那么这部分的一定在着Unity界面里存在的,不管后面的Pass有没有关闭背面(Cull Back)。但是要注意的是,延伸的这部分面片仍然属于背面,因此视角看向某个面片(假设是面片A)正面时,膨胀的那部分是看不见的,至于能看见的部分应该是模型其它面片的背面,绝对不是面片A的背面。
并且,对于第二个pass ,如果你将 Cull Back 改为Cull Off,你会发现,你通过旋转的方式能看见三个面,一个是延伸的背面,另外两个是面片本身的正面和背面。下面图片文字显示的面片背面只是告诉你,面片背面能看见,并且面片背面可没有真的沿法线方向延伸哦,延伸的那部分我也不知道它属于什么(飞出去的一块颜色吧,看代码就知道了,就是对一块区域着色嘛),但只能从面片背面视角看到。下面的第二张图是为了更好的说明只能从面片背面的视角看到那块飞出的色块。
你还可以将第二个Pass,保持原来的Cull Back,会发现上图中面片背面立马消失了,只剩下那块飞出的色块,因为背面剔除啦。
这里只使用了一个plane来说明我的理解,其实也不知道是否正确。希望能得到比较专业的说明。