Unity-Shader - 2DSprite描边效果

文章介绍了如何在2D游戏中实现精灵图的描边效果,利用Alpha值检测边缘,使用BlendSrcAlphaOneMinusSrcAlpha混合模式,并通过顶点和片段着色器处理纹理边缘和颜色混合。
摘要由CSDN通过智能技术生成

实现一个简单的2D精灵图描边效果,效果如下
在这里插入图片描述
实现思路:
可以通过判断该像素周围是否有透明度为 0的值,如果有,则说明该像素位于边缘。
所以我们需要打开alpha blend,即: Blend SrcAlpha OneMinusSrcAlpha,并且加入渲染队列,

Tags{
     "Queue" = "Transparent"
}
Blend SrcAlpha OneMinusSrcAlpha

根据图片的Alpha值边缘判定,向内扩一段距离做边缘,颜色设置为描边颜色;
片元着色阶段,向上下左右四个方向做检测,有一个点的透明度为0,判定为边缘;

fixed4 frag(v2f i) : SV_Target
			{
				fixed4 col = tex2D(_MainTex, i.uv);
				// 采样周围4个点
				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;
				// 如果有一个点透明度为0 说明是边缘
				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;
			}

如果图片内容恰好铺满整张图,没有alpha值,方法不适用

outline

可以和原图做插值,根据边缘判断来混合线的颜色和原图颜色
完整shader如下

Shader "shader2D/outline"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}  // 主纹理属性,用于存储2D纹理
        _lineWidth("lineWidth",Range(0,10)) = 1  // 线宽属性,范围在0到10之间,默认值为1
        _lineColor("lineColor",Color)=(1,1,1,1)  // 线的颜色属性,RGBA格式,默认为白色
    }
    SubShader
    {
        // 渲染队列采用透明
        Tags{
            "Queue" = "Transparent"
        }
        Blend SrcAlpha OneMinusSrcAlpha  // 设置混合模式为源颜色乘以源透明度减去源透明度

        Pass
        {
            CGPROGRAM
            #pragma vertex vert  
            #pragma fragment frag  

            #include "UnityCG.cginc"  

            // 顶点着色器输入结构体 
            struct VertexInput
            {
                float4 vertex : POSITION;  // 顶点坐标
                float2 uv : TEXCOORD0;  // 纹理坐标
            };

            // 顶点着色器输出结构体 
            struct VertexOutput
            {
                float2 uv : TEXCOORD0;  // 纹理坐标
                float4 vertex : SV_POSITION;  // 顶点坐标
            };

           
            VertexOutput vert (VertexInput v)
            {
                VertexOutput o;
                o.vertex = UnityObjectToClipPos(v.vertex);  // 将顶点坐标转换到裁剪空间
                o.uv = v.uv;  // 传递纹理坐标
                return o;
            }

            sampler2D _MainTex;  // 主纹理
            float4 _MainTex_TexelSize;  // 主纹理的像素大小
            float _lineWidth;  // 线宽
            float4 _lineColor;  // 线的颜色

            fixed4 frag (VertexOutput i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);  // 获取纹理颜色

                // 采样周围4个点
                float2 up_uv = i.uv + float2(0,1) * _lineWidth * _MainTex_TexelSize.xy;
                float2 down_uv = i.uv + float2(0,-1) * _lineWidth * _MainTex_TexelSize.xy;
                float2 left_uv = i.uv + float2(-1,0) * _lineWidth * _MainTex_TexelSize.xy;
                float2 right_uv = i.uv + float2(1,0) * _lineWidth * _MainTex_TexelSize.xy;

                // 如果有一个点透明度为0,说明是边缘
                float w = tex2D(_MainTex,up_uv).a * tex2D(_MainTex,down_uv).a * tex2D(_MainTex,left_uv).a * tex2D(_MainTex,right_uv).a;

                // 和原图做插值,根据边缘判断来混合线的颜色和原图颜色
                col.rgb = lerp(_lineColor,col.rgb,w);

                return col;
            }
            ENDCG
        }
    }
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Shadero Sprite is a real time node-based shader tool for Unity 5, 2017 and 2018 NEW! Forum : https://forum.vetasoft.store/ Discuss with us about Shadero Sprite and more ! Description Shadero Sprite is a real time node-based shader editor. Shadero was made to be a real production time saver. Beautiful and fast, it creates many astonishing effects with the several premade and fully optimized node effects. Shadero will increase the number of the premade effects in every update. No code required. Features - Ultimate tools for 2D Shader Editor - Node based editor - Real time preview - Source code included - Load/Save project - Optimised shader - Fog Support - HDR Support - Visual selection node preview - Light Support - Blend Mode support - Normalmap with light Support - Preview directly the scene - Works with Unity UI - Many premade effects (+100) - Help Tutorial in editor - Project examples - Unity 5 and 2017 and 2018 compatible - Support Anima 2D - Support Render Texture - Support Mesh Renderer - Generate a Sprite - Create Morphing - And much much more ! http://www.shadero.com New version 1.7.0 see version history Version 1.1 - Add Node: UV > FX (UV) > Sprite Sheet Animation - Add Node: UV > FX (UV) > Pixel - Add Node: UV > FX (UV) > Fish Eye - Add Node: UV > FX (UV) > Pinch - Add Node: UV > FX (UV) > Kaleiodoscope - Add Node: Generate > Shape > XRay - Add Node: Generate > Shape > Donuts - Add Node: Generate > Shape > Lines - Add Node: Generate > Shape > Noise - Add Node: RGBA > Color > Turn Metal - Add Node: RGBA > Color > Turn Gold - Add Node: RGBA > Color > Turn Transparent - Add Node: RGBA > Color > HDR Control Value - Add Node: RGBA > Color > HDR Create - Add Force Change Parameters to the Build Shader Node (Experimental) - Add Lightning Support - Fix Node: UV > FX (UV) > Pixel XY precision - Fix new Shader Project that keep the preview material issue - Fix Auto variable order issue when you add the same node Any Feedback, error reports, questions or suggestions, please contact us at support@vetasoft.com If you like Shadero Sprite you can rate it on the asset store, it would be very helpful. Any idea or suggestions to improve Shadero Sprite? Feel free to contact us at : support@vetasoft.com
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值