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

Can't get textures with alpha to work?

Discussion in 'General Graphics' started by Kyrieru, Mar 31, 2021.

  1. Kyrieru

    Kyrieru

    Joined:
    Mar 2, 2015
    Posts:
    42
    When I use alpha blend, the material erases other parts of the mesh that uses the same material.
    This happens in both blender and unity, but I can't get any answers as to why it happens in either case.


    I'm just trying to do this sort of thing which seems common. What should I be doing?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Is this a custom shader you're using? The above looks like you're using an alpha blended shader that's writing to the depth buffer. Either it has
    ZWrite On
    or is missing
    ZWrite
    entirely. Generally you don't want to write the depth buffer with an alpha blended material for this exact reason.

    However if you turn use
    ZWrite Off
    you might notice another issue show up, which is that hair rendering "under" other hair will render on top of it. The reason why is because efficient and correct real time rendering of arbitrarily sorted transparent surfaces is an unsolved problem. For things like particle systems or multiple transparent objects placed in a scene Unity can sort them by distance from the camera. But for individual polygons of a single mesh you have to sort these yourself, manually. They need to be sorted in what's known as the Painter's Algorithm, or from furthest to nearest. The easiest solution I know of is to break the hair into separate meshes in Blender, then append them together back into a single mesh starting with the "bottom most" hair. This should sort them so the bottom layers are rendered first, and each layer above that renders over the previous hair.

    The other solution, is to use alpha tested hair instead of alpha blended. Or use a mix of the two and live with the bad sorting for the hair that's alpha blended.