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

Question I need help improving shader

Discussion in '2D' started by Tom-Kazansky, Oct 7, 2020.

  1. Tom-Kazansky

    Tom-Kazansky

    Joined:
    Mar 9, 2015
    Posts:
    58
    Hello!
    I'm making a 2D game and I want to simulate "shadow" of the sprites, I think I just use semi-transparent black-tinted sprite for that; but the "shadows" are not "blended" well (I'm not sure what's that called, please check my attached image).

    And after searching around, I found a solution that is using a "shader" and downloaded it (from a user on reddit), it works so far but the edges of the shadow is jagged, I would like to smooth out those edges but I'm not sure how to modify the shader.

    shadow_shader.png
    - on the left: not "blended" well shadows
    - on the right: this is what I need but I want the edges to be smoother

    can anyone help me with this? or point me to a direction to resolve this? (beware that I know nothing about shader :( )

    I also attach the shader file I downloaded.

    Thank you for reading.
     

    Attached Files:

  2. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    906
    Hello @Tom-Kazansky,

    If we take a look at the fragment shader, we see the following check
    Code (CSharp):
    1.  
    2.                 if (color.a == 0.0)
    3.                     discard;
    4.  
    Here we discard all pixels with the alpha value of 0. If we instead increase the value of which we discard to let's say < 0.1, you will see that the edges are rendered a bit smoother.

    Code (CSharp):
    1.             half4 frag(v2f i) : COLOR
    2.             {
    3.                 half4 color = tex2D(_MainTex, i.uv);
    4.                 if (color.a < 0.1)
    5.                     discard;
    6.  
    7.                 color = _Color;
    8.                 return color;
    9.             }
     
    Tom-Kazansky likes this.
  3. Tom-Kazansky

    Tom-Kazansky

    Joined:
    Mar 9, 2015
    Posts:
    58
    Thank you very much!
    it works.


    I think I should learn about shader (soon) :)
     
    Ted_Wikman likes this.