Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

How is the reverse painter algorithm used by Unity?

Discussion in 'General Graphics' started by YJack, Jan 20, 2021.

  1. YJack

    YJack

    Joined:
    Apr 7, 2010
    Posts:
    35
    When two transparent materials are rendered using the exact same render queue, how Unity sort what is in front and what is in the back? I mean, I know that they will be sorted back to front by the painter algorithm but doesn't seems to be based on the transform position so what is used instead? Putting in other words, if I have two transparent planes using two different materials with the same render queue and with the exact same transformations, what Unity will use to sort what should be rendered first?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,804
    I believe the render order for transparency is determined like this:
    1. Material Queue
    2. Render Sorting Layer
    3. Renderer Sorting Order
    4. Sorting Group
    5. Renderer Bounds Center Distance or Depth (depending on camera projection or sorting mode)
    6. something to do with the Material / Renderer that's hidden to the user
    For two quads with exactly the same transform and different transparent materials in an otherwise empty scene, I don't think there's a way to know for sure before hand what the order is going to be. It is consistent though, and appears to be controlled by the order in which the renderer components or materials are created.

    If you create add multiple quads to a scene in the same spot and apply the same transparent material to all of them, the order they render in appears to be the order they were added, even if you rearrange their order in the scene hierarchy after the fact. My best guess is their internal game object or component identifier is being used to sort them at some point, likely in some internal cached list of "all objects" rather than explicitly for rendering purposes.

    If you create individual transparent materials for each quad, the oldest material appears to render on top, regardless of which order they are assigned. Likely again based on some internally cached list of assets or material definitions. It's roughly stable between runs of Unity, but I wouldn't rely on it being the same between different systems since I don't actually know how this order is determined. Material sorting is for sure done for rendering reasons though, as Unity's dynamic batching system is going to be looking for objects to batch together, and the opaque sorting may actually choose to draw objects "out of order" if they share materials.

    If you want to ensure the render order, use the material queue or renderer component's
    .sortingOrder
    . The later of which isn't exposed to the inspector for most renderer components, nor is it serialized (saved by the component in the scene) but can be set via c# with a custom script.
     
    YJack likes this.
unityunity