Author Topic: Request For Enhancement: Shared depth with multiple atlases  (Read 9119 times)

magoghm

  • Guest
Request For Enhancement: Shared depth with multiple atlases
« on: April 12, 2012, 01:32:47 PM »
We are using NGUI to draw an isometric view of a city. The whole city, including static & dynamic sprites, is being drawn with NGUI. Yes, we know this isn't what NGUI was designed for, but it does handle it extremely well :)

The only problem we have right now it that we have some gameplay graphical elements that are the same across all levels of the game and some other graphical elements that change from level to level (building for different cities). So we would like to have one atlas for the shared gameplay elements and one extra atlas for each level of the game. Right now we can't do that because we need some of the gameplay elements (mostly vehicles) to be drawn sometimes behind a building and sometimes in front of it. Our code dynamically changes the depth of the gameplay element to handle that, but it only works if all the sprites come from the same atlas.

Unity can handle multiple materials on a single mesh, so I guess it might be possible to have NGUI put all the widgets from a single panel into a single mesh even if they use different atlases. This would solve a huge problem for us, because right now each atlas is twice as large as it should be due to having to place a copy of all the gameplay elements in each level atlas. It is a web based game and we need to reduce the download size as much as possible.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Request For Enhancement: Shared depth with multiple atlases
« Reply #1 on: April 12, 2012, 01:58:14 PM »
That has to be the most unusual usage of the UI system I've heard of to date. :)

Unfortunately using multi-materials won't help here, as they still end up as different draw calls.

What you want to do is split up the rendering here. First, all the background objects will be on one panel. Next you will draw the vehicles, followed by buildings. You may need to split this up further, possibly moving some buildings to a temporary panel if you want them to occlude other cars. You will also need to adjust the Z since you'll be working with different draw calls and Unity will need to know what's in wrong of what.

All in all, this is a fairly complicated task to do, and you might also find it easier to just use a different panel for each building / car and adjust Z instead of depth.

magoghm

  • Guest
Re: Request For Enhancement: Shared depth with multiple atlases
« Reply #2 on: April 12, 2012, 02:35:38 PM »
Than you for answering so fast!

You're right, I didn't think about the different draw calls. I guess the easiest solution will be to use different panels for each element.

I was so happy about having the whole city being drawn in a single draw call, it will be kind of sad to see that go away :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Request For Enhancement: Shared depth with multiple atlases
« Reply #3 on: April 12, 2012, 02:41:45 PM »
Well, it all comes down to what you have a budget for. If you still have a budget for more texture memory leftover, then just keep the duplicates. If not, then it gets more complicated. :)

magoghm

  • Guest
Re: Request For Enhancement: Shared depth with multiple atlases
« Reply #4 on: April 12, 2012, 02:56:40 PM »
I'm looking at the source code for the OnFill() method for UISprite. I think I will fool around with changing the z vertex coordinate to store the depth instead 0. I guess I will also have to modify the shaders to use the zbuffer (or enable the depth pass in NGUI). I'll let you know if I have any luck with that :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Request For Enhancement: Shared depth with multiple atlases
« Reply #5 on: April 12, 2012, 03:08:12 PM »
If you don't use alpha blending (alpha cutout shader for example), then you can certainly take advantage of the depth testing. That will indeed make it easier for you.

magoghm

  • Guest
Re: Request For Enhancement: Shared depth with multiple atlases
« Reply #6 on: April 12, 2012, 03:41:12 PM »
I do use alpha blending for some of the gameplay sprites (a glow effect to highlight them when they are selected) but none for the city sprites. So I guess I'll have to find a way to make sure the gameplay sprites are drawn after the city sprites.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Request For Enhancement: Shared depth with multiple atlases
« Reply #7 on: April 12, 2012, 03:42:32 PM »
If you place all your city sprites on one atlas (writing to depth) and gameplay sprites on another atlas (not writing to depth), then you're set.

magoghm

  • Guest
Re: Request For Enhancement: Shared depth with multiple atlases
« Reply #8 on: April 12, 2012, 03:57:05 PM »
OK. Thank you very much for all you help!