Search Unity

  1. How can our website serve you better? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

Ignoring "folded" objects added opacity

Discussion in 'Shaders' started by xbures29, Jun 15, 2021.

  1. xbures29

    xbures29

    Joined:
    May 30, 2021
    Posts:
    2
    Hi,
    I'm trying to shape an object that is semi transpartent, but when I fold it on itself, the opacity adds together.
    Example, shaping the left object:
    upload_2021-6-15_21-46-41.png
    this works well, 2 objects add up.

    But I want to ignore this efect when shape is folded on itself:
    upload_2021-6-15_21-48-25.png
    Something similar is achiavable with shader from this post:
    https://www.reddit.com/r/Unity3D/comments/he8g81/transparency_on_a_sprite_rig/
    But that one acts wierd when put together with other objects.
    upload_2021-6-15_21-53-44.png
    Objects are rendered in wrong order and semi-trnasparent shape under, covers semi-transparent shape above him.(I dont mind the part that disapeared.)
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    10,610
    You either need a unique stencil ref for each object (which you can assign per material by adding a
    _StencilRef
    property to the shader's properties, and
    Stencil { Ref [_StencilRef]
    , or you can use a two pass system that renders the object twice with the second pass clearing the stencil buffer.

    Code (csharp):
    1. ColorMask 0 // render only to the depth and/or stencil buffer
    2. ZWrite Off
    3. Stencil {
    4.     Ref 0 // default "cleared" stencil buffer value
    5.     Comp Always // always render
    6.     Pass Replace // replace stencil buffer value with ref value
    7. }
     
  3. xbures29

    xbures29

    Joined:
    May 30, 2021
    Posts:
    2
    Thank you, the Frist part really helped, I have no idea what is going on, but it works :D
     
unityunity