Search Unity

Question Sprite has extruded edges after applying custom shader [Solved]

Discussion in '2D' started by Beastman632, Sep 10, 2021.

  1. Beastman632

    Beastman632

    Joined:
    Dec 12, 2016
    Posts:
    8
    Hey guys

    I made a Sprite lit shader graph that draws an outline around a sprite, but for some reason it creates these extruded edges around the sprite. How do I solve that? The default shader Sprite-Lit-Default doesn't seem to have the same issue.

    This is the Sprite-Lit-Default shader


    My custom shader





    Any help would be greatly appreciated. Thanks in advance :)
     

    Attached Files:

  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    Would be nice to see the shader because otherwise I'm just speculating.

    My guess is that you're not passing the alpha from the _MainTex to output / final color.

    What you're seeing there is the shape of the Sprite's mesh, which includes areas that are 100% transparent.
     
  3. Beastman632

    Beastman632

    Joined:
    Dec 12, 2016
    Posts:
    8
    Hi, thanks for the reply. I also thought it might be that. I am passing the alpha from the _MainTex though. There are a few ways to achieve the outline. All of the methods I tried have that same jagged mesh effect. Here is a screenshot of the URP
     

    Attached Files:

    • urp.PNG
      urp.PNG
      File size:
      99.2 KB
      Views:
      501
  4. MichaelH55

    MichaelH55

    Joined:
    Jan 19, 2014
    Posts:
    19
    Youre shader should work but youre sprite not.
    The alpha more visible looks like this:

    wrongAlpha.PNG

    Try this as sprite:

    alphaCutut.png
     
    Beastman632 likes this.
  5. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    The alpha is a separate node on the Fragment output, which from the pic is locked at value of 1 for all pixels. Use a split node and connect the alpha directly.

    It looks like you're interpreting the semi-alpha pixels to get an outline, which may not be possible unless you were to carefully tune the alpha of those pixels so that they're not too transparent along the edge of the rgb.
     
    Last edited: Sep 12, 2021
    Beastman632 likes this.
  6. Beastman632

    Beastman632

    Joined:
    Dec 12, 2016
    Posts:
    8
    @Lo-renzo Oh yes of course. Thank you. That fixed it!

    The tutorials I was following were using an older version of shader graph, which didn't have the alpha node exposed on the Fragment section. It is fixed by connecting the alpha of the sample with the alpha of the fragment. Thanks again :)
     

    Attached Files: