Author Topic: Perspective Camera, Dynamic Font, Force Draw Order?  (Read 10149 times)

eucryptic

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Perspective Camera, Dynamic Font, Force Draw Order?
« on: August 22, 2013, 04:53:32 AM »
I'm making a 3D GUI with a perspective camera.
I have a bunch of UI elements that are on one Atlas, and some labels that use a dynamic font texture.

I've read about the issues involved with multiple atlases, but as I want all the labels to be on top of the UI elements, it seems like it doesn't affect me. I've set the Z positions of the labels so that they're in front of the UI elements.

The problem is, as I tilt and pan the camera by small amounts, the text labels appear and disappear.
This happens in the Scene view too, as I pan/rotate around. I can't work out what's causing it, but there's a magic angle/position where the draw order seems to flip.

Is there a way to force the draw order?
I want the labels to be on top of all UI elements.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Perspective Camera, Dynamic Font, Force Draw Order?
« Reply #1 on: August 22, 2013, 10:18:51 AM »
Create a material for your labels that will use a higher draw order. That's the only way to hack it. Look at the Overlay shader -- see how it has Transparent+1? Do the same thing.

eucryptic

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: Perspective Camera, Dynamic Font, Force Draw Order?
« Reply #2 on: August 22, 2013, 08:38:08 PM »
Thanks for the quick reply!

So I'd duplicate the Font Material for my dynamic font, but instead of using GUI/Text Shader, I'd write my own that has Transparent+1?


Also I experimented, making each UI element have its own UIPanel. I figured I could sacrifice draw calls for simplicity, but each of my elements seems to draw over the other in a seemingly random order, nothing to with Z position.
It seems my understanding of the draw order isn't quite right.

Depth changes drawing order of things with the same material/atlas, within the same UIPanel for an orthographic camera.
Z position sets drawing order for things with different materials/atlases, within the same UIPanel for an orthographic camera.

But then for different UIPanels, perspective cameras...? It seems all bets are off. It'd be nice if this was summarised somewhere in a table...


Edit: It seems I can hack it to draw in the right order by placing extra UISprite elements off-screen, far enough away in the Z order to force things using a certain atlas to draw first. Worst hack of the century but it seems to get the job done for now.
« Last Edit: August 22, 2013, 08:58:30 PM by eucryptic »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Perspective Camera, Dynamic Font, Force Draw Order?
« Reply #3 on: August 23, 2013, 02:48:25 PM »
That's a pretty bad hack, yeah! But hey, as long as it works... :P

eucryptic

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: Perspective Camera, Dynamic Font, Force Draw Order?
« Reply #4 on: August 25, 2013, 09:17:21 PM »
Sorry to beat a dead horse, but I've still been trying to get a 3D interface working with NGUI.
With 2D, the way that Depth, Z, UIPanels and Atlases interact is understandable and it's possible to get the order you want.

With 3D, you almost always want a moving camera (why else bother with 3D).
I've seen "draw order is based on Z (position?)" but more accurately draw order is calculated based on distance from the camera (correct me if I'm wrong).

The problem is as soon as you start panning the camera, the distance between your GUI elements and the camera starts changing, and the draw order varies wildly. Things pop behind and in front as the camera pans past them.

Am I missing something? Is there a way around this? It's kind of a dealbreaker here.

Simie

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: Perspective Camera, Dynamic Font, Force Draw Order?
« Reply #5 on: September 06, 2013, 10:27:56 AM »
I was also having trouble with items clipping around in a 3D gui, I found setting the UI Camera transparency sort mode to Orthographic helped a lot in getting it right. My drag panel was clipping behind other elements when not moving, and setting this fixed that.

http://docs.unity3d.com/Documentation/ScriptReference/Camera-transparencySortMode.html