Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Looking for glow effect for sprites (mobile friendly)

Discussion in 'Image Effects' started by Silverlode, Aug 26, 2016.

  1. Silverlode

    Silverlode

    Joined:
    Apr 9, 2013
    Posts:
    41
    Hi Guys,

    I'm looking for a material/shader I can apply to sprites (ideally both UI and non-UI sprites), that mimics Photoshop's outer-glow effect. (Understandably it won't recreate Photoshop's outer glow exactly!)

    2DxFX doesn't appear to have a glow effect, just solid outlines. Same for Sprite Color FX.

    Blend Mode does this apparently, but its $75 - looking for something cheaper. Also, only works on some mobile devices.

    Glow Effect appears to be full-screen only, and I'm looking for per-sprite.

    I haven't tried MK Glow - but looks to be mesh-based.

    Advanced Glow doesn't have much of a description and some poor reviews. Doesn't mention sprites anyway.

    This Unity Answers post ("Make Sprites Glow" reference s"glow" provides a different (a nice shine) effect. Not what I'm looking for.

    This Unity Forums post ("Create a Glowing effect for 2D sprites) hasn't helped me, the Glow Per Object asset has a tiny description and a 2 star rating, so I've avoided it so far. Not obvious it setup for sprites or mobile.

    This Unity forums thread ("free sprite flow shader") refers to a Unity Technologies Bit Bucket 2D demo repo, but the link doesn't work (I'm also new to BitBucket). Suspect it the relevant files may not be available now.

    .... ....so I'm asking a common question, looking for new answers. I need something that
    • Works with sprites
    • Provides an outer glow (soft, faded edge, expanding from the edge of the sprite (not a solid stroke))
    • Works on mobile (I will be applying it to 128x128 sprites, so the per-pixel cost should be fairly low)
    • Doesn't need to be free, but preferable <$25
    Does anyone have any experience of existing assets or techniques to help me? If I've been unfair or misunderstood any of the assets linked above, please correct me!
     
  2. macdude2

    macdude2

    Joined:
    Sep 22, 2010
    Posts:
    686
    Glow effect is actually per object so it will probably be your best bet.
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    The problem with outer glow is it's a very expensive effect to reproduce in real time, and worse the way sprites work the glow can only extend as far as the sprite's mesh, which is usually fairly tight to the sprite, and can bleed glows from the surrounding sprites packed in the sprite atlas too close, hence why the outline effect that is common is usually only a pixel or two wide.

    The easiest way to do a outer glow like Photoshop is going to be to an outer glow in Photoshop. It wouldn't be too hard to just have the glow just in the alpha channel and a fairly simple shader to use that alpha in a way to allow for it to show as a glow. Obviously requires some setup, but doesn't have the issues a purely shader based approach would have.
     
    Oshigawa and jjbish like this.
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    Here's a quick shader and example sprite (grabbed from this thread, don't know the original author http://forum.unity3d.com/threads/2d-samurai-and-ninja-character-sprite.240425/ )

    Code (CSharp):
    1. Shader "Sprites/Cheap Outer Glow"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         _GlowScale ("Glow Scale", Range(0,1)) = 1
    8.         _GlowColor ("Glow Color", Color) = (1,1,1,1)
    9.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Tags
    15.         {
    16.             "Queue"="Transparent"
    17.             "IgnoreProjector"="True"
    18.             "RenderType"="Transparent"
    19.             "PreviewType"="Plane"
    20.             "CanUseSpriteAtlas"="True"
    21.         }
    22.  
    23.         Cull Off
    24.         Lighting Off
    25.         ZWrite Off
    26.         Blend One OneMinusSrcAlpha
    27.  
    28.         Pass
    29.         {
    30.         CGPROGRAM
    31.             #pragma vertex vert
    32.             #pragma fragment frag
    33.             #pragma target 2.0
    34.             #pragma multi_compile _ PIXELSNAP_ON
    35.             #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
    36.             #include "UnityCG.cginc"
    37.            
    38.             struct appdata_t
    39.             {
    40.                 float4 vertex   : POSITION;
    41.                 float4 color    : COLOR;
    42.                 float2 texcoord : TEXCOORD0;
    43.             };
    44.  
    45.             struct v2f
    46.             {
    47.                 float4 vertex   : SV_POSITION;
    48.                 fixed4 color    : COLOR;
    49.                 float2 texcoord  : TEXCOORD0;
    50.             };
    51.            
    52.             fixed4 _Color;
    53.  
    54.             v2f vert(appdata_t IN)
    55.             {
    56.                 v2f OUT;
    57.                 OUT.vertex = UnityObjectToClipPos(IN.vertex);
    58.                 OUT.texcoord = IN.texcoord;
    59.                 OUT.color = IN.color * _Color;
    60.                 #ifdef PIXELSNAP_ON
    61.                 OUT.vertex = UnityPixelSnap (OUT.vertex);
    62.                 #endif
    63.  
    64.                 return OUT;
    65.             }
    66.  
    67.             sampler2D _MainTex;
    68.             sampler2D _AlphaTex;
    69.  
    70.             fixed _GlowScale;
    71.             fixed4 _GlowColor;
    72.  
    73.             fixed4 SampleSpriteTexture (float2 uv)
    74.             {
    75.                 fixed4 color = tex2D (_MainTex, uv);
    76.  
    77. #if ETC1_EXTERNAL_ALPHA
    78.                 // get the color from an external texture (usecase: Alpha support for ETC1 on android)
    79.                 color.a = tex2D (_AlphaTex, uv).r;
    80. #endif //ETC1_EXTERNAL_ALPHA
    81.  
    82.                 return color;
    83.             }
    84.  
    85.             fixed4 frag(v2f IN) : SV_Target
    86.             {
    87.                 fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;
    88.  
    89.                 // Use alpha assuming glow is a gradient from 0.0 to ~0.75% alpha, and the rest is the sprite.
    90.                 fixed spriteAlpha = saturate(c.a * 4.0 - 3.0);
    91.                 fixed glowAlpha = saturate(1.0 - (1.0 - c.a / 0.75) / max(_GlowScale, 0.01)) * saturate(_GlowScale * 32.0);
    92.                 c.a = max(spriteAlpha, glowAlpha * _GlowColor.a);
    93.                 c.rgb = lerp(_GlowColor.rgb, c.rgb, 1.0 - (1.0 - saturate(spriteAlpha / max(_GlowColor.a, 0.01))) * saturate(_GlowScale * 32.0));
    94.  
    95.                 c.rgb *= c.a;
    96.                 return c;
    97.             }
    98.         ENDCG
    99.         }
    100.     }
    101. }
    102.  
    glowSprite.png

    The sprite was generated by using an outer glow with precise settings and an alpha of 75%. That value is important for the shader to work properly. A softer outer glow will work too, but it won't scale as nicely.
     
    chrusty, Falasby, DireDay and 3 others like this.
  5. ArtyUwU

    ArtyUwU

    Joined:
    Dec 27, 2018
    Posts:
    15
    is it possible to use this with lighweight pipeline?
     
  6. AcademyOfFetishes

    AcademyOfFetishes

    Joined:
    Nov 16, 2018
    Posts:
    219
    What does that actually mean?
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    Umm that’s kind of what the whole post after that is about ...
     
  8. ManiaCCC

    ManiaCCC

    Joined:
    Aug 15, 2015
    Posts:
    41
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    That draws a super bright outline around the sprite, then lets the bloom post process do the glow. Post processing can be very slow on mobile. The above technique doesn't require any post processing.
     
    yugurlu72 likes this.