这一章,作者写的有点简短了,补几个链接。
https://www.cnblogs.com/freeblues/p/5738987.html
讲解了边缘检测的算子
https://blog.csdn.net/wenqiwenqi123/article/details/79248626
边缘检测原理
结合上面的博客,可以发现,旋转的角度是无关紧要的。
Shader "Custome/Chapter11/EdgeDection" {
Properties {
_MainTex("Base (RGB)",2D) = "White" {}
_EdgeOnly("Edge Only",Float) = 1.0
_EdgeColor ("Edge Color", Color) = (0, 0, 0, 1)
_BackgroundColor ("Background Color", Color) = (1, 1, 1, 1)
}
SubShader{
Pass {
//后处理标配
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
uniform half4 _MainTex_TexelSize;
fixed _EdgeOnly;
fixed4 _EdgeColor;
fixed4 _BackgroundColor;
struct v2f {
float4 pos : SV_POSITION;
half2 uv[9] : TEXCOORD0;
};
v2f vert(appdata_img v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
half2 uv = v.texcoord;
//这个是UV坐标中一个像素的大小1/512//计算9个相邻域坐标,类似方向向量
o.uv[0] = uv + _MainTex_TexelSize.xy * half2(-1, -1);
o.uv[1] = uv + _MainTex_TexelSize.xy * half2(0, -1);
o.uv[2] = uv + _MainTex_TexelSize.xy * half2(1, -1);
o.uv[3] = uv + _MainTex_TexelSize.xy * half2(-1, 0);
o.uv[4] = uv + _MainTex_TexelSize.xy * half2(0, 0);
o.uv[5] = uv + _MainTex_TexelSize.xy * half2(1, 0);
o.uv[6] = uv + _MainTex_TexelSize.xy * half2(-1, 1);
o.uv[7] = uv + _MainTex_TexelSize.xy * half2(0, 1);
o.uv[8] = uv + _MainTex_TexelSize.xy * half2(1, 1);
return o;
}
half Sobel(v2f i){
const half Gx[9] = {-1, 0, 1,
-2, 0, 2,
-1, 0, 1};
const half Gy[9] = {-1, -2, -1,
0, 0, 0,
1, 2, 1};
half texColor;
half edgex = 0;
half edgey = 0;
for(int it = 0;it < 9; it++){
//转化成灰度,对边界检查来说具体颜色 不是关键,
//关键是相邻颜色的差值
texColor = Luminance(tex2D(_MainTex,i.uv[it]));
edgex += texColor * Gx[it];
edgey += texColor * Gy[it];
}
//边缘部分会不为0,非边缘会为0
//进行颠倒,让边缘部分为0
half edge = 1 - abs(edgex) - abs(edgey);
return edge;
}
fixed4 frag(v2f i) : SV_Target {
half edge = Sobel(i);
//edge 边缘部分为0所以对应插值_EdgeColor
fixed4 withEdgeColor = lerp(_EdgeColor, tex2D(_MainTex, i.uv[4]), edge);//i.uv[4]和i.texcoord没什么区别
fixed4 onlyEdgeColor = lerp(_EdgeColor, _BackgroundColor, edge);
return lerp(withEdgeColor, onlyEdgeColor, _EdgeOnly);
return withEdgeColor;
return onlyEdgeColor;
}
ENDCG
}
}
FallBack off
}