最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Unity實(shí)現(xiàn)2D邊緣檢測(cè)

 更新時(shí)間:2022年04月11日 15:47:25   作者:小紫蘇  
這篇文章主要介紹了如何利用Unity實(shí)現(xiàn)2D邊緣檢測(cè),從而達(dá)到人物描邊效果。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

一、ShaderLab

1.Alpha值邊緣檢測(cè)

根據(jù)圖片的Alpha值邊緣判定,向內(nèi)擴(kuò)一段距離做邊緣,顏色設(shè)置未描邊顏色;

片元著色階段,向上下左右四個(gè)方向做檢測(cè),有一個(gè)點(diǎn)的透明度為0,判定為邊緣;

Shader "2DOutline"
{
	Properties
	{
		_MainTex("Texture", 2D) = "white" {}
		_LineWidth("Width",Range(0,0.4)) = 1.0
		_LineColor("LineColor",color) = (1,1,1,1)
		_Intensity("Intensity",Range(1,10)) = 1.0
	}

	SubShader
	{
		Tags { "RenderType" = "Opaque" "Queue" = "Transparent"}
		Blend SrcAlpha OneMinusSrcAlpha
		
		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;
			fixed _LineWidth;
			float4 _LineColor;
			fixed _Intensity;

			v2f vert(appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				return o;
			}

			fixed4 frag(v2f i) : SV_Target
			{
				fixed4 col = tex2D(_MainTex, i.uv);
				// 采樣周圍4個(gè)點(diǎn)
				float2 up_uv = i.uv + float2(0, 1) * _LineWidth * 1 / 10 * _MainTex_ST.xy;
				float2 down_uv = i.uv + float2(0,-1) * _LineWidth * 1 / 10 * _MainTex_ST.xy;
				float2 left_uv = i.uv + float2(-1,0) * _LineWidth * 1 / 10 * _MainTex_ST.xy;
				float2 right_uv = i.uv + float2(1,0) * _LineWidth * 1 / 10 * _MainTex_ST.xy;
				// 如果有一個(gè)點(diǎn)透明度為0 說(shuō)明是邊緣
				float w = tex2D(_MainTex,up_uv).a * tex2D(_MainTex,down_uv).a * tex2D(_MainTex,left_uv).a * tex2D(_MainTex,right_uv).a;

				if (w == 0) {
					col.rgb = lerp(_LineColor * _Intensity, col.rgb, w);
				}

				return col;
			}
		ENDCG
		} 
	}
}

如果圖片內(nèi)容恰好鋪滿整張圖,沒(méi)有alpha值,方法不適用;下圖底部邊緣消失了;

2.卷積邊緣檢測(cè)

在屏幕后處理階段,使用卷積做邊緣檢測(cè);

卷積:根據(jù)像素周圍八個(gè)方向的像素的計(jì)算出新的像素值;

邊緣檢測(cè)卷積算子,都包含水平和豎直兩個(gè)方向的卷積核;

梯度公式:G = sqrt(Gx*Gx + Gy*Gy);

考慮性能問(wèn)題,使用:G = |Gx|+|Gy|;

頂點(diǎn)著色器計(jì)算卷積紋理采樣坐標(biāo),減少計(jì)算量(片元數(shù)量更多);

片元著色階段Sobel卷積計(jì)算,插值獲得片元像素顏色;

Sobel計(jì)算結(jié)果和梯度Gradient比較,大于梯度和EdgeColor做插值;

屏幕后效調(diào)用OnRenderImage接口;

Shader "EdgeDetection" 
{
	Properties{
		_MainTex("Base (RGB)", 2D) = "white" {}
		_EdgeColor("Edge Color", Color) = (0, 0, 0, 1)		
        //卷積梯度
		_Gradient("Gradient",float) =0.0
	}
	SubShader{
		Pass 
		{
			ZTest Always Cull Off ZWrite Off

			CGPROGRAM

			#include "UnityCG.cginc"

			#pragma vertex vert  
			#pragma fragment frag

			sampler2D _MainTex;
			uniform half4 _MainTex_TexelSize;
			//fixed _EdgeOnly;
			fixed4 _EdgeColor;
			//fixed4 _BackgroundColor;
			fixed _Gradient;

			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;

				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;
			}

			fixed luminance(fixed4 color) {
				return  0.2125 * color.r + 0.7154 * color.g + 0.0721 * color.b;
			}

			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];
				}

				half edge = 1 - abs(edgeX) - abs(edgeY);

				return edge;
			}

			fixed4 frag(v2f i) : SV_Target {
				half edge = Sobel(i);

				fixed4 col = tex2D(_MainTex, i.uv[4]);

				if(edge> _Gradient)
					col = lerp(_EdgeColor, tex2D(_MainTex, i.uv[4]), edge);				
				
				return col;
			}

			ENDCG
		}
	}
	FallBack Off
}

二、ShaderGraph

抓取圖片緩沖,上下左右四個(gè)方位平移,乘以描邊顏色;

四張圖合并,減去原圖范圍的像素,只剩邊緣;

最后將原圖和邊緣合并(可插值使邊緣柔和);

升級(jí)項(xiàng)目到URP,修改projectsetting-graphic-pielinesettings;

導(dǎo)入ShaderGraph包,開始拖拖拽拽,真的香,效果好,速度快,思路清晰;

到此這篇關(guān)于基于Unity實(shí)現(xiàn)2D邊緣檢測(cè)的文章就介紹到這了,更多相關(guān)Unity邊緣檢測(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

镇宁| 冷水江市| 留坝县| 樟树市| 洛隆县| 南陵县| 闽清县| 齐齐哈尔市| 浦县| 沅江市| 湖南省| 襄城县| 桦川县| 武鸣县| 申扎县| 北安市| 德钦县| 阿拉尔市| 安溪县| 宁明县| 德惠市| 和林格尔县| 高阳县| 高邮市| 天津市| 宜宾县| 乌鲁木齐县| 三亚市| 女性| 德州市| 康定县| 通山县| 伊宁县| 葫芦岛市| 会宁县| 类乌齐县| 容城县| 北辰区| 陆丰市| 丰原市| 张家川|