Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Overlapping Shadows: Achieving Consistent Alpha Values in 2D Sprites

Discussion in '2D' started by wagenheimer, Apr 9, 2024.

  1. wagenheimer

    wagenheimer

    Joined:
    Jun 1, 2018
    Posts:
    326
    Is there a technique or shader setting that can maintain a constant alpha value for overlapping shadow sprites, ensuring that the shadow appearance remains consistent and does not accumulate when multiple sprites overlap?


    upload_2024-4-9_17-5-25.png
     
  2. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    404
    sorry i'm late, but you can use
    Code (CSharp):
    1.             BlendOp Min
    2.  
    in hlsl shaders so it only chooses the lowest value of shadow. this means you can also bake penumbras into shadows or use soft shadows and it will still look nice

    alternatively, you can use stencil buffer to prevent drawing on the same fragment twice.
     
    Last edited: May 28, 2024
    wagenheimer likes this.
  3. wagenheimer

    wagenheimer

    Joined:
    Jun 1, 2018
    Posts:
    326
    Thank you for the response! I attempted both methods, but neither worked perfectly. Could you please review my shaders to see what is wrong?

    Method 1 appears to be better, although the shadows are no longer semi-transparent. Method 2 brought additional problems and did not work as well.


    Method 1
    upload_2024-6-20_16-11-11.png





    Method 2

    upload_2024-6-20_16-8-51.png
     

    Attached Files:

  4. wagenheimer

    wagenheimer

    Joined:
    Jun 1, 2018
    Posts:
    326
    It appears that eliminating BlendOp Min and implementing Blend One OneMinusSrcAlpha has resolved the issue.

    upload_2024-6-20_16-50-54.png
     

    Attached Files:

    DragonCoder likes this.