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

GPU Instancer Transparent Sorting error

Discussion in 'Shaders' started by o2co2, Nov 12, 2018.

  1. o2co2

    o2co2

    Joined:
    Aug 9, 2017
    Posts:
    45
    GPU Instancer Transparent Sorting error?
    upload_2018-11-12_20-46-29.png
    Is there a good solution? Thank you
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    It looks like you're using a couple of different meshes. When you're using instancing the draw order between different meshes is not guaranteed.

    For example, let's say you have two different meshes, a red cube and blue sphere, both evenly spread out over an area such that when sorted by distance they're roughly alternating between one mesh and the other. To draw these correctly when they're transparent requires drawing each mesh one by one in that order.

    Instancing works by drawing one mesh multiple times at one time. The performance optimal way to draw a lot of meshes is to draw all of one mesh, then all of the other, but this means the draw order is wrong for transparency. All of the red cubes will be drawn, and those can be properly sorted between themselves, then all of the blue spheres will be drawn, but they'll be drawing over all of the red cubes.

    The easiest solution to this is to not use instancing. This obviously isn't great for performance.

    The next option is to use some kind of OIT approximation, like weighted blend, but that comes with a lot of complications. This isn't something built in to Unity, do you'd have to find one of the existing implementations that are available on GitHub or on the asset store, but I don't think any of them support instancing so you'll have to modify or write this yourself.

    The next, and best option is to use a special kind of instancing which allows for multiple meshes. Or rather, uses a single mesh that is transformed in the shader to look like another mesh using a texture. That has the per vertex data stored in a texture. This is the most complex solution, and absolutely requires fully custom shaders, but it removes a lot of limitations from instancing.
     
    Gravesend likes this.
  3. o2co2

    o2co2

    Joined:
    Aug 9, 2017
    Posts:
    45
    Thanks to bgolus, OIT is more complicated, I can only choose ECS+GameObject.
     
  4. o2co2

    o2co2

    Joined:
    Aug 9, 2017
    Posts:
    45
    Thanks to bgolus, the explanation is very detailed and benefits a lot.