Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Drop shadow shader for sprites

Discussion in 'Shaders' started by realvasion, Jan 3, 2017.

  1. realvasion

    realvasion

    Joined:
    Oct 25, 2014
    Posts:
    21
    Hi there,

    I am looking for a drop shadow shader (just like photoshop) for my sprite, I searched forum/google for almost a week and find nothing except (TextMesh on asset store) but it’s only for fonts not sprite.

    Does anyone with shader experience now how to create one like these image.



    Any help is much appreciated.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    Last edited: Jun 11, 2020
    realvasion likes this.
  3. realvasion

    realvasion

    Joined:
    Oct 25, 2014
    Posts:
    21
  4. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,663
  5. realvasion

    realvasion

    Joined:
    Oct 25, 2014
    Posts:
    21
    thanks, but this one is not given any smooth outline its like it was made for pixel art sprites, because the outcome is very edgy outline.
    any other suggestion plz?
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Thick and smooth outlines are very expensive to do in real time on sprites, which is why you won't find any shaders for this, plus the way Unity packs sprites you can only really have 1 pixel wide outlines as any larger will start to clip on the edges and bleed from other sprites in the atlas. So really there is no good shader only solution!

    The only way I can think of to do this 100% with a shader would be to render the sprite with multiple passes as a solid color (the outline color) with a slight offset each time after rendering the shadow pass but before the sprite. However this will be quite bad for performance and won't look that good for thicker outlines with out a lot more passes.

    The answer really is ... do it outside of Unity.

    If you want to do it in shader so you can have a variable width based on scale or something like that, you probably still want to do most of the work outside of Unity, but using the same technique as some of the text rendering assets use: distance fields.

    I posted an example for doing a variable glow on sprites using a cheap approximation of distance fields which could be modified to be an outline.
    https://forum.unity3d.com/threads/l...-sprites-mobile-friendly.427824/#post-2791841
     
    fengkx likes this.
  7. CredixYt

    CredixYt

    Joined:
    Mar 5, 2018
    Posts:
    1
  8. vambier

    vambier

    Joined:
    Oct 1, 2012
    Posts:
    102
  9. blakesnake100

    blakesnake100

    Joined:
    Feb 24, 2018
    Posts:
    2
    Here you go:
    Code (CSharp):
    1. v2f vert(appdata_t IN) {
    2.     v2f OUT;
    3.     OUT.vertex = mul(unity_ObjectToWorld, IN.vertex);
    4.     OUT.vertex += _ShadowOffset;
    5.     OUT.vertex = mul(unity_WorldToObject, OUT.vertex);
    6.  
    7.     OUT.texcoord = IN.texcoord;
    8.     OUT.color = IN.color *_ShadowColor;
    9.     #ifdef PIXELSNAP_ON
    10.     OUT.vertex = UnityPixelSnap (OUT.vertex);
    11.     #endif
    12.         OUT.vertex = UnityObjectToClipPos(OUT.vertex);
    13.     return OUT;
    14. }
    Just replace the vert function with the one above to fix the shadow's position. I figured out how to do it after looking at the comments in the answer to this question: https://answers.unity.com/questions/1184469/shader-vert-offsets-in-local-space.html
     
    Anders_Pe likes this.
  10. arkogelul

    arkogelul

    Joined:
    Nov 14, 2016
    Posts:
    105
    I was wondering if this would work with UI text.
    I know there is the Shadow component, but I'm trying to make a shadow with a specific solid color (no a multiply blend), and I'm using a custom colored font. So all I'm getting is tinted characters.
     
  11. RustyFlash

    RustyFlash

    Joined:
    Oct 8, 2014
    Posts:
    88
  12. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Any shader based technique will mean no batching. There's no way around that. If you want drop shadows and batching, you'll need to do the drop shadows manually as their own individual sprites.