当前位置: 首页> 游戏> 网游 > 2D图片的描边

2D图片的描边

时间:2025/7/13 0:45:46来源:https://blog.csdn.net/Er_nameSpace/article/details/139561171 浏览次数:0次
第一种 Sprite 外边框

原理是找到边缘像素,然后设置颜色,边缘像素的特点是像素的八个方向的邻像素不全是透明或者不全是不透明,这样的像素就是边缘上的像素,下面是找到的shader源码,很简单

Shader "Custom/Sprite Outline"
{Properties{_MainTex ("Sprite Texture", 2D) = "white" {}_Color ("Tint", Color) = (1,1,1,1)_OutlineThickness("Outline Thickness", Range (0.0, 0.1)) = 0.0_OutlineColor ("Outline Color", Color) = (1,1,1,1)}SubShader{Tags{ "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane""CanUseSpriteAtlas"="True"}Cull OffLighting OffZWrite OffBlend One OneMinusSrcAlphaPass{CGPROGRAM#pragma vertex vert#pragma fragment frag#pragma multi_compile _ PIXELSNAP_ON#include "UnityCG.cginc"struct appdata_t{float4 vertex   : POSITION;float4 color    : COLOR;float2 texcoord : TEXCOORD0;};struct v2f{float4 vertex   : SV_POSITION;fixed4 color    : COLOR;float2 texcoord  : TEXCOORD0;};fixed4 _Color;sampler2D _MainTex;fixed4 _OutlineColor;fixed _OutlineThickness;v2f vert(appdata_t IN){v2f OUT;OUT.vertex = UnityObjectToClipPos(IN.vertex);OUT.texcoord = IN.texcoord;OUT.color = IN.color * _Color;return OUT;}fixed4 SampleSpriteTexture (float2 uv){fixed4 color = tex2D (_MainTex, uv);return color;}fixed4 frag(v2f IN) : COLOR{fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;c.rgb *= c.a;fixed4 outlineC = _OutlineColor;outlineC.rgb *= outlineC.a;//outlineC.a *= ceil(c.a);if (c.a == 0.0){fixed upAlpha = SampleSpriteTexture ( IN.texcoord + fixed2(0, _OutlineThickness)).a;fixed downAlpha = SampleSpriteTexture ( IN.texcoord - fixed2(0, _OutlineThickness)).a;fixed rightAlpha = SampleSpriteTexture ( IN.texcoord + fixed2(_OutlineThickness, 0)).a;fixed leftAlpha = SampleSpriteTexture ( IN.texcoord - fixed2(_OutlineThickness, 0)).a;fixed upRightAlpha = SampleSpriteTexture ( IN.texcoord - fixed2(_OutlineThickness, _OutlineThickness)).a;fixed upLeftAlpha = SampleSpriteTexture ( IN.texcoord - fixed2(_OutlineThickness, -_OutlineThickness)).a;fixed downRightAlpha = SampleSpriteTexture ( IN.texcoord - fixed2(-_OutlineThickness, _OutlineThickness)).a;fixed downLeftAlpha = SampleSpriteTexture ( IN.texcoord - fixed2(-_OutlineThickness, -_OutlineThickness)).a;if (upAlpha != 0.0|| downAlpha != 0.0 || rightAlpha != 0.0 || leftAlpha != 0.0 || upRightAlpha != 0.0 || upLeftAlpha != 0.0 || downLeftAlpha != 0.0 || downRightAlpha != 0.0)return outlineC;    }return c;}ENDCG}}
}
第二种 UI图片外边框

UGUI已经开源了,特意扒了一下源码,outline组件中是通过ModifyMesh接口改变的顶点偏移,然后outline组件是继承自shadow组件,可以说是outline和shadow实现原理一样的,就是把图像进行偏移然后设置颜色,然后在渲染原图,也就是这个效果需要渲染两次,这面是顶点处理的回调函数

 public override void ModifyMesh(VertexHelper vh){if (!IsActive())return;var verts = ListPool<UIVertex>.Get();vh.GetUIVertexStream(verts);var neededCpacity = verts.Count * 5;if (verts.Capacity < neededCpacity)verts.Capacity = neededCpacity;var start = 0;var end = verts.Count;ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, effectDistance.x, effectDistance.y);start = end;end = verts.Count;ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, effectDistance.x, -effectDistance.y);start = end;end = verts.Count;ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, -effectDistance.x, effectDistance.y);start = end;end = verts.Count;ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, -effectDistance.x, -effectDistance.y);vh.Clear();vh.AddUIVertexTriangleStream(verts);ListPool<UIVertex>.Release(verts);}

关键字:2D图片的描边

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: