Author Topic: [Bug] Panel with lower z may be drawn under Panel with higher z  (Read 4206 times)

blechowski

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Panel with lower z may be drawn under Panel with higher z. Reverse is expected as Panel closer to the camera should be drawn first.

UIPanel.cs line line 713
void UpdateTransformMatrix ()
worldToLocal = cachedTransform.worldToLocalMatrix;
worldToLocal takes localPosition z of UIPanel. Later worldToLocal is used to calculate widget geometry.
The result is that UIPanel localPosition Z may not behave as expected.
Panel with lower z may be drawn under Panel with higher z(reverse is expected).

To fix when worldToLocal is assigned to make sure that Panel's localPosition z is always 0.0f.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [Bug] Panel with lower z may be drawn under Panel with higher z
« Reply #1 on: August 17, 2013, 01:31:35 AM »
I don't get your "fix". Forcing the position's Z is a bad idea. I frequently have multiple windows, some of which must be drawn before others, and I do that by adjusting the Z on the panel's transforms.

blechowski

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: [Bug] Panel with lower z may be drawn under Panel with higher z
« Reply #2 on: August 17, 2013, 10:34:44 AM »
My fix ensures that Panel position Z is not taken into account when building widgets geometry.

What I do is:
1. Store Panel localPosition
2. Zero Panel localPosition.z
3. worldToLocal = cachedTransform.worldToLocalMatrix;
4. Restore Panel localPosition from 1.

This way I make sure that only Panel z position is taken into account when determining which panel should be drawn first as it is closer to the Camera and not widgets' geometry(that is currently based on Panel worldToLocal matrix hence also on Panel z position).

According [1] It is Panel's Z position that determines which Panel should be drawn first. But right now this is not always true as widgets's geometry is based on Panel Z position.

Example of the problem:
1. Panel1 has Z localPosition = -1
2. Panel2 has Z localPosition = 0
3. Panel1 widgets mesh is generated based on Panel1 worldToLocal matrix, therefor it is moved in z direction also by unknown value(I noticed that the value changes).
4. Panel2 widgets mesh in generated but not moved as Panel2 z is 0;
5. Set Panel1 Z localPosition = -0.5
6. Set Panel2 Z localPosition = -0.75

You would expect Panel2 to be drawn before Panel1(because it is close to camera), but this is not the case as Panel1 mesh is internally moved in z so Panel1 is before Panel2.

With the fix Panel2 will always be drawn before Panel1 as Panel's z position will not be taken into account when building widgets mesh.

[1] http://www.tasharen.com/forum/index.php?topic=1858.0

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [Bug] Panel with lower z may be drawn under Panel with higher z
« Reply #3 on: August 18, 2013, 07:45:19 AM »
Unity determines what's drawn first based on the distance from the center of the geometry to the eye point of the camera.

All widgets offset themselves slightly based on their "Depth" property. Offsetting the panel adds a larger offset value.

So no, it's not accurate that "the panel's Z position determines the draw order". It does, but so do widgets. If you move one widget within a panel to -1000 Z, you can guess what happens to the panel that draws it -- the geometry will have a different center, thus the draw order will differ.

As I said, your "fix" isn't a "fix" at all.

shokinhan

  • Guest
Re: [Bug] Panel with lower z may be drawn under Panel with higher z
« Reply #4 on: August 19, 2013, 10:23:23 AM »
The trouble is that , the different altas must use the z for render order, and the samealtas must use the depth.