Author Topic: Issue anchoring with non-standard cam rects  (Read 3821 times)

jarrah

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Issue anchoring with non-standard cam rects
« on: October 22, 2014, 04:00:28 AM »
Hi,

I hit an issue with NGUI 3.7.3 today. In my game sometimes the main camera is set to a "letterbox" screen ratio while the ui camera remains unchanged - thus the items in main cam 3d space are in a letterbox frame, while the ui cam can continue to draw the ui outside this frame.

When this happens UI elements anchored to 3d objects do not line up properly.  When translating between camera views, a change in the target camera's rect does not seem to be handled.

I think I managed to fix this by changing UIRect's GetLocalPos method (starting line 365) as shown below. Is there a better way to do this?

Thanks

  1. protected Vector3 GetLocalPos (AnchorPoint ac, Transform trans)
  2.         {
  3.                 if (anchorCamera == null || ac.targetCam == null)
  4.                         return cachedTransform.localPosition;
  5.        
  6.         Vector3 viewPos = ac.targetCam.WorldToViewportPoint(ac.target.position);
  7.         Vector3 pos = mCam.ViewportToWorldPoint(new Vector3((viewPos.x * ac.targetCam.rect.width) + ac.targetCam.rect.x, (viewPos.y * ac.targetCam.rect.height) + ac.targetCam.rect.y, viewPos.z));
  8.                 if (trans != null) pos = trans.InverseTransformPoint(pos);
  9.                 pos.x = Mathf.Floor(pos.x + 0.5f);
  10.                 pos.y = Mathf.Floor(pos.y + 0.5f);
  11.                 return pos;
  12.         }
  13.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Issue anchoring with non-standard cam rects
« Reply #1 on: October 22, 2014, 05:47:32 AM »
This seems odd to me... screen aspect ratio affects all cameras, unless you have a render texture set up. That's how it should be, so how are you even getting it so that one camera is affected by it and another isn't?

jarrah

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Issue anchoring with non-standard cam rects
« Reply #2 on: October 22, 2014, 06:32:36 AM »
It is not the screen ratio, it is the camera viewport ratio. Maybe I'm not using terms properly. I have attached an editor screenshot. The bit circled in red has changed for the main camera, but the ui cam has the original X & Y =0, H & W = 1. So the 3d world is in a narrow band across the center ("letter box") but the ui can use the whole screen.

Does that make more sense?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Issue anchoring with non-standard cam rects
« Reply #3 on: October 23, 2014, 03:10:27 PM »
Alright, sure, but they are different cameras. Whatever you do with your main camera shouldn't affect your UI camera or UI-anchored content.

jarrah

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Issue anchoring with non-standard cam rects
« Reply #4 on: October 23, 2014, 08:05:08 PM »
What happens with one camera can affect the other, if an object shown by one camera is anchored to an object shown by the other camera.

I have an object in the 3d scene shown by the main camera. It has a HUD element anchored to it shown by the UI camera. When the main camera has its view port changed then the anchored HUD is no longer in the right place. The HUD is positioned where the 3d scene object would be if the viewport was NOT modified. I fixed by changing the anchoring code as shown above (which takes into account the change in the viewport).

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Issue anchoring with non-standard cam rects
« Reply #5 on: October 25, 2014, 01:57:34 PM »
Fair enough, makes sense to me. I'll add similar changes to the repository, thanks!