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

Overdraw issue, potential fix, but missing knowledge.

Discussion in 'Shaders' started by AlexisTB, Feb 24, 2020.

  1. AlexisTB

    AlexisTB

    Joined:
    Jul 26, 2017
    Posts:
    20
    Hello, I just want to first say that I have limited expertise with shaders. We have a frame rate issue in some places on Switch since we have to much overdraw because our textures almost all have transparency.

    What I am trying to do is to split the rendering of the scene into two passes, one for the pixels of the texture with no transparency as front to back and the second would be back to front for the pixels with transparency.

    Is it possible to render the same object twice, one opaque pass and one transparent pass? If so, do I need two materials with different shaders or simple modify the current shader?

    The current shader we use is a pixel lit transparent based on Ferr2DTCommon shader, but I am not sure how I should change it if what I am trying to do is possible. I could post it here if it is relevant, but just an example would be helpful (again, if possible).
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    You would need two materials. While you can have both opaque and transparent passes in the same shader, they'll be rendered in the order of the material's queue dictates, which means any benefit of rendering as opaque would be negated by the transparent queue's back to front rendering.

    I've not used Ferr2D, so I can't really say how one would go about editing the existing system. It seems like most of the stuff is using a normal mesh renderer, which means you could assign multiple materials on it. Presumably you could split up the mesh between the edge and the solid interior and have them be separate materials and queues, if they aren't already. The problem with that is you'll need to be careful with the actual Z of your meshes and sprites so that the depth overlapping works as you expect.
     
  3. AlexisTB

    AlexisTB

    Joined:
    Jul 26, 2017
    Posts:
    20
    Thanks, is there a reliable way to add a 2nd material to a SpriteRenderer? I managed to remove my old material and replace it with my 2 new ones after making an editor script (assigning my mats to Renderer.sharedMaterials) and when I look in the editor it's fine, but when I save and start the scene, it only keep the first material of the two. For mesh renderers I know it works but we also use a lot of sprite renderers.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    AFAIK for opaque & alpha split sprites you just have to use two sprites.
     
  5. AlexisTB

    AlexisTB

    Joined:
    Jul 26, 2017
    Posts:
    20
    I see, one last question, when checking with the frame debugger I see that the opaque sprites are still rendered back to front. Is it because dynamic batching is enabled or something else? If I can't change that then it will still unfortunately do a lot of overdraw.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Well, you probably still have the original "order in layer" on the sprites. You probably want to zero that out so they're all the same sorting order so that Unity takes over. Also make sure the materials are using a material render queue of 2000 and the shader is using ZWrite On ZTest LEqual.

    But honestly I don't do 2D games or ever use sprites so there might be something I'm missing.