2D sprite outlines

Shader "Sprites/Outline"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0

        // Add values to determine if outlining is enabled and outline color.
        [PerRendererData] _Outline ("Outline", Float) = 0
        [PerRendererData] _OutlineColor("Outline Color", Color) = (1,1,1,1)
    }

    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }

        Cull Off
        Lighting Off
        ZWrite Off
        Blend One OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile _ PIXELSNAP_ON
            #pragma shader_feature ETC1_EXTERNAL_ALPHA
            #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;
            float _Outline;
            fixed4 _OutlineColor;

            v2f vert(appdata_t IN)
            {
                v2f OUT;
                OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
                OUT.texcoord = IN.texcoord;
                OUT.color = IN.color * _Color;
                #ifdef PIXELSNAP_ON
                OUT.vertex = UnityPixelSnap (OUT.vertex);
                #endif

                return OUT;
            }

            sampler2D _MainTex;
            sampler2D _AlphaTex;
            float4 _MainTex_TexelSize;

            fixed4 SampleSpriteTexture (float2 uv)
            {
                fixed4 color = tex2D (_MainTex, uv);

                #if ETC1_EXTERNAL_ALPHA
                // get the color from an external texture (usecase: Alpha support for ETC1 on android)
                color.a = tex2D (_AlphaTex, uv).r;
                #endif //ETC1_EXTERNAL_ALPHA

                return color;
            }

            fixed4 frag(v2f IN) : SV_Target
            {
                fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;

                // If outline is enabled and there is a pixel, try to draw an outline.
                if (_Outline > 0 && c.a != 0) {
                    // Get the neighbouring four pixels.
                    fixed4 pixelUp = tex2D(_MainTex, IN.texcoord + fixed2(0, _MainTex_TexelSize.y));
                    fixed4 pixelDown = tex2D(_MainTex, IN.texcoord - fixed2(0, _MainTex_TexelSize.y));
                    fixed4 pixelRight = tex2D(_MainTex, IN.texcoord + fixed2(_MainTex_TexelSize.x, 0));
                    fixed4 pixelLeft = tex2D(_MainTex, IN.texcoord - fixed2(_MainTex_TexelSize.x, 0));

                    // If one of the neighbouring pixels is invisible, we render an outline.
                    if (pixelUp.a * pixelDown.a * pixelRight.a * pixelLeft.a == 0) {
                        c.rgba = fixed4(1, 1, 1, 1) * _OutlineColor;
                    }
                }

                c.rgb *= c.a;

                return c;
            }
            ENDCG
        }
    }

}

using UnityEngine;

[ExecuteInEditMode]
public class SpriteOutline : MonoBehaviour {
    public Color color = Color.white;

    private SpriteRenderer spriteRenderer;

    void OnEnable() {
        spriteRenderer = GetComponent<SpriteRenderer>();

        UpdateOutline(true);
    }

    void OnDisable() {
        UpdateOutline(false);
    }

    void Update() {
        UpdateOutline(true);
    }

    void UpdateOutline(bool outline) {
        MaterialPropertyBlock mpb = new MaterialPropertyBlock();
        spriteRenderer.GetPropertyBlock(mpb);
        mpb.SetFloat("_Outline", outline ? 1f : 0);
        mpb.SetColor("_OutlineColor", color);
        spriteRenderer.SetPropertyBlock(mpb);
    }
}
Completed Sprite

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
需要Unity 5.6.0或更高版本。 Shadero Sprite是Unity 5和2017的基于节点的实时着色器工具 描述 Shadero Sprite是一个实时的基于节点的着色器编辑器。 Shadero被认为是一个真正的生产时间节省。美丽而快速,它通过几个预制和完全优化的节点效果创造出许多令人惊叹的效果。 Shadero将在每次更新中增加预制效果的数量。无需代码。 特征 - 基于节点的编辑器 - 实时预览 - 包含源代码 - 加载/保存项目 - 优化着色器 - 可视化选择节点预览 - 直接预览场景 - 适用于Unity UI - 许多预制效果 - 编辑器中的帮助教程 - 项目示例 - Unity 5和2017兼容 版本1.1 - 添加节点:UV> FX(UV)> Sprite Sheet Animation - 添加节点:UV> FX(UV)>像素 - 添加节点:UV> FX(UV)>鱼眼 - 添加节点:UV> FX(UV)>捏 - 添加节点:UV> FX(UV)> Kaleiodoscope - 添加节点:生成>形状> XRay - 添加节点:生成>形状>甜甜圈 - 添加节点:生成>形状>线条 - 添加节点:生成>形状>噪声 - 添加节点:RGBA>颜色>转动金属 - 添加节点:RGBA>颜色>转金 - 添加节点:RGBA>颜色>打开透明 - 添加节点:RGBA>颜色> HDR控制值 - 添加节点:RGBA>颜色> HDR创建 - 将强制更改参数添加到构建着色器节点(实验) - 添加闪电支持 - 修复节点:UV> FX(UV)>像素XY精度 - 修复了保留预览材质问题的新着色器项目 - 修复添加同一节点时的自动变量订单问题
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值