Author Topic: Mixing different atlases and UITexture  (Read 33262 times)

N3uRo

  • Guest
Mixing different atlases and UITexture
« on: June 19, 2012, 04:09:31 PM »
I know that the perfect scenario is with only one atlas because it's only a drawcall and you don't have any problem with "Z" and you can use de UIWidget inspector with the depth.

But sometimes it's not possible because:

1. You don't want to have textures that exceed from 2048x2048
2. Also you want to load textures dinamically so in that case your only option is to use UITexture.

With this scenario it's very hard to order them:

1. Sometimes some objects appear above others despite of being correctly ordered with Z.
2. Sometimes you change the Z of an object and other one dissapear with no reason.

I mean anyone has come across this problem?

It's really annoying...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Mixing different atlases and UITexture
« Reply #1 on: June 19, 2012, 05:16:28 PM »
You need to understand how NGUI works. Everything under a single UIPanel that uses the same material (ie: atlas) gets grouped into the same draw call. If you have something like this:

Background Sprite (Atlas A)
Label (Atlas B)
Highlight (Atlas A)

...this means that background and highlight will be grouped into the same draw call -- they will be drawn at the same time. This means that your label will either be drawn before or after the background+highlight combo. In order to split this up, you'd have to ensure that the highlight has a separate panel, essentially turning 2 draw calls into 3.

There is no way around it. When the buffer is submitted to the videocard, it's submitted as one material at a time. You can't say render 1 with material A, render 2 with material B, then 3 with material A again without explicitly creating 3 separate draw calls. It's just how all graphics hardware works.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Mixing different atlases and UITexture
« Reply #2 on: June 19, 2012, 06:25:25 PM »
You think that's bad? Try putting UITextures with their own panel into another panel that has clipping that also has to clip the UItexture. I aged 5 years the day I made that. ;)

There's an implied hierarchy in NGUI that you need to understand, like ArenMook says.

1) Within a UIPanel, within the same atlas the "depth" of the widget determines which is on op.

2) Within a UIPanel in different atlases, the whole atlas will be either ontop of or below all widgets from the other atlas. The widget with the lowest (can be negative) z-position is put on top, and all widgets from that atlas are on top as well.

If depth or z-position in those instances are the same, there's undefined behavior, ie. it may be different in editor and final build, aka, don't ever do that.

3) In two different UIpanels, from the same atlas, the z-position of the panel counts first and within the individual panels widget depth counts.


So, if you want to force a grouping of widget from atlas A under a widget from atlas B under a widget from atlas A (ABA), you have to group it in panels like:
UIpanel1 with z = 0: AB
UIPanel2 with Z = -1: A

or

UIPanel1, z=0: A
UIPanel2, z=-1: BA


If you wanna get into even further shenannigans to put 3D models in your menu or something, you can mix in multiple cameras with their own unity layer, group by that as well. But then you are getting into Serious ShitTM8)

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Mixing different atlases and UITexture
« Reply #3 on: June 20, 2012, 02:13:51 AM »
Oh, and something I forgot earlier. UITextures are practically their own atlas, each is their own that is. This can make it a bit tricky to mix properly, but certainly possible.

N3uRo

  • Guest
Re: Mixing different atlases and UITexture
« Reply #4 on: June 20, 2012, 11:29:33 AM »
Ok, thank you both!!

Now I understand better how it works. The key are the panels!

unity1.ftb

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Mixing different atlases and UITexture
« Reply #5 on: November 12, 2012, 09:58:27 AM »
Ok, I think my question is related to this one, so lets make a reply.

I have a similar scenario to the one described by Nicki => Trying to make a panel that contains a list of items, with drag and clip so that you have to scroll to see the full list. Each item itself has a hierarchy like this:
Background (Atlas A) in z=0
Icon (UITexture, loaded from server) in z=-1
Watermark (Atlas A again) in z=-2

Working with that, the UITexture sometimes gets behind the background as you scroll the panel. I tried to check the "depth pass" in that panel but it doesn't work ok with the clipping. So I guess I have to split all the content in 2 (or more) panels and coordinate the drags so all of them scroll at the same time... Is that the way to go? Isn't there a simpler solution with the current version of NGUI that I may try first?

Thanks for the help anyway.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile

DuckOfDoom

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 10
    • View Profile
Re: Mixing different atlases and UITexture
« Reply #7 on: November 30, 2012, 06:15:27 AM »
Also, when you add nested UIPanel, you have to switch the parent panel off and on so the nested panel will start handling its widgets' drawcalls instead of parent panel. Took me some time to realize.

madmat3001

  • Guest
Re: Mixing different atlases and UITexture
« Reply #8 on: December 03, 2012, 08:36:05 AM »
2) Within a UIPanel in different atlases, the whole atlas will be either ontop of or below all widgets from the other atlas. The widget with the lowest (can be negative) z-position is put on top, and all widgets from that atlas are on top as well.

I am having big problems getting the quoted part to work.

I have a panel with three atlasses. One is for background textures, one is the font and one is for foreground textures. The text renders on top of the background (though this may just be luck, as I did not need to adjust anything to achieve this) but no matter how I adjust the z-values I cannot get any sprite from the foreground atlas to render in front of the background.

Am I missing something?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Mixing different atlases and UITexture
« Reply #9 on: December 03, 2012, 11:14:47 AM »
Not sure how else to explain it that wasn't already explained in the linked post.

madmat3001

  • Guest
Re: Mixing different atlases and UITexture
« Reply #10 on: December 03, 2012, 11:30:41 AM »
And I realize, that the explanation isn't very complicated, but it doesn't seem to work for me.

I have now simplified the setup to the bare minimum and it still does't work:
UIRoot
- Panel
--Anchor
---BGSprite(z = 0)
---FGSprite(z = -1)

Result: FGSprite is rendered behind BGSprite in GameView. In the SceneView it depends on the way the camera is positioned.

Is there anything that has not been mentioned yet, that might have an effect on this? (Shaders, Atlases, God knows what...?)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Mixing different atlases and UITexture
« Reply #11 on: December 04, 2012, 02:41:58 AM »
Is it a 3D UI? In 3D UI the order depends on proximity to the camera, which changes as you tilt the window. For this reason, 3D UIs should only use one atlas. With a 2D UI setup, assuming FGSprite is on a separate atlas, Z of -1 will cause it to be in front. If it uses the same atlas as the background sprite, then the "depth" property will determine the draw order.

madmat3001

  • Guest
Re: Mixing different atlases and UITexture
« Reply #12 on: December 05, 2012, 04:55:03 AM »
No, it's a 2D UI, it's just the Scene-View that's 3D as usual. And Yes FGSprite is in a different atlas.

So I took a day off yesterday and when I started Unity this morning everything seemed OK. The FGSprites were correctly in front and I was quite happily blaming some stupid Unity bug for monday's problems. Well I changed some stuff around and as soon as I hit "Apply" on my Prefab, the fricking FGSprites moved into the back again.

I investigated this a little further and I can reproducibly make the FGSprites "disappear" by hitting apply on the Prefab. To get them to show up in front again, I have to reload the scene.

But on the bright side, they seem to be in front during game play now. So while this is still a little annoying during the design phase, it does not seem to affect the end result.