Search Unity

Multiple Cameras for Depth Sorting & GFX.WaitForPresent Spikes

Discussion in 'Scripting' started by Nathan_D, Mar 6, 2018.

  1. Nathan_D

    Nathan_D

    Joined:
    Oct 10, 2013
    Posts:
    31
    Hi all,

    I'm building a pixel shooter game and I want my bullets to be rendered above/second to the reset of my assets as sometimes my bullets clip into other things they're not supposed to. Having read this thread I thought I could use multiple cameras in my scene to create custom depth. I did so and it worked as intended but it seems having multiple cameras render (I have 4) is causing the GFX.WaitForPresent to spike and cause insane slowdowns in my editor.

    I was just wondering, are there any other ways to achieve camera depth like this without using more than one camera? Even though no culling masks are rendered twice and clear flags of non-main cameras are set to Depth Only, GFX.WaitForPresent spikes to no end.

    I've been reading around for fixes, and I've come across things that sound useful. When people say they "render their game in stages" and sometimes "certain objects are rendered at a different resolutions", what does that mean? Different cameras? And what does depth buffers mean?
     
  2. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    429
    Is it a 2D game using SpriteRenderer's? You can use Sorting Layers and orderInLayer to achieve this
     
  3. Nathan_D

    Nathan_D

    Joined:
    Oct 10, 2013
    Posts:
    31
    Hey thanks for the reply!

    I'm not using SpriteRenderers, I'm using a variety of renderers including Tilemap and mesh renderers. I'm aware of Sorting Layers and orderInLayer, but it does not work in my case because my game uses real 3D depth in some instances.

    I'm still very curious what people mean when they render "certain objects at different resolution" or in "stages". I feel like I'm missing something about how to approach rendering. Any suggestions would really be appreciated!
     
  4. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    429
    In that case you can use the Render Queue depth on your materials to override what order things are rendered in.
    https://docs.unity3d.com/ScriptReference/Material-renderQueue.html

    When people are referring to rendering in different resolutions, you can use a RenderTexture and post processor to render camera's at different resolutions than the screen, then blit those textures together to create your final image. I'm not sure it's entirely necessary to go that route in your case though.
     
  5. Nathan_D

    Nathan_D

    Joined:
    Oct 10, 2013
    Posts:
    31
    Ah yes Render Queue, that's a good fix! Thanks for pointing that out to me. That works well.

    Thanks for your response to the second question I had! I'm noticing spikes of GFX.WaitForPresent when I first add another camera but when I close Unity and reopen the project it resolves itself. Weird.

    My pixel shooter game I have bullets that travel very quickly, so when they travel so fast it doesn't feel "smooth" or "frame-y", as in one frame the bullet flies 1/4 across the screen. Is there a way to make the movement feel more smooth while retaining the speed of the bullet? Maybe rendering it to a higher resolution RenderTexture?

    A final question about optimization. Is there a performance hit if I have multiple cameras but the Culling Mask never duplicates rendered layers? I read somewhere that cameras run frustum culling checks even before CullingMask layers are taken into account. Is there a way to optimize the use of multiple cameras?