可能是由于边缘检测渲染纹理的方式不正确导致的。可以尝试在着色器中使用Sobel算子进行边缘检测,然后调整边缘线条的位置和宽度。以下是示例代码:
Shader "Custom/Outline" {
Properties {
_LineWidth("Line Width", Range(0, 1)) = 0.1
_MainTex("Sprite Texture", 2D) = "white" {}
_OutlineColor("Outline Color", Color) = (1, 1, 1, 1)
}
SubShader {
Pass {
Tags { "LightMode"="ForwardBase" }
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;
float _LineWidth;
float4 _OutlineColor;
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);
fixed edge = 1 - tex2D(_MainTex, i.uv).a;
col *= 1 - smoothstep(0, _LineWidth, edge);
fixed4 outlineColor = _OutlineColor;
outlineColor *= _MainTex.a * (1 - edge);
return col + outlineColor;
}
ENDCG
}
}
FallBack "Diffuse"
}
同时,需要在材质上使用该着色器并将_MainTex设置为精灵纹理。