Author Topic: 3D UI Anchor Question/Problem  (Read 3679 times)

Ferazel

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 150
    • View Profile
3D UI Anchor Question/Problem
« on: December 18, 2013, 03:00:29 PM »
I'm trying to get a 3D UI setup in NGUI. However, I want to have static 2D UI on this 3D camera as well in the corners to allow me to have a 2D overlay in my 3D camera. I don't really need another 2D UI in the scene (at least I don't think I do). However, I'm basically just trying to figure out how to properly anchor sprites using a 3D UI system.

I have the following scene
UIRoot/Panel
--UICamera (perspective 3D camera)
----TopLeftPositionedSprite (Anchored to UICamera top left)

The problem appears to be that if the z depth isn't 0.5 of the camera's clip plane the calculations are off. It seems that the anchor system depends on this by the following code:
  1. Mathf.Lerp(cam.nearClipPlane, cam.farClipPlane, 0.5f)
  2.  

There seems to be a lot of this in the code for determine World or Viewport points. So I'm curious if we need to position anchors with 3D do we need to make sure that the UIWidgets are in a z-space that is exactly 1/2 of the camera's near/far clipping depth in order to get these elements positioned properly? Is this a known limitation? Wouldn't it make more sense to get the depth of the object by performing a target.position.z - camera.position.z?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: 3D UI Anchor Question/Problem
« Reply #1 on: December 18, 2013, 05:50:57 PM »
You need two UIs. One for 3D, another for 2D. It's the only way it's going to work properly.

Ferazel

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 150
    • View Profile
Re: 3D UI Anchor Question/Problem
« Reply #2 on: December 19, 2013, 03:11:35 PM »
I ended up just using the old UIAnchor script to anchor to the 3D camera and it didn't have this limitation. Which is fine, but felt like it might be worth looking into so the new anchoring system could work in a perspective camera. Unless I'm anchoring it incorrectly. I found if the panel's z-depth from the camera isn't 1/2 the clip difference it will not anchor to the camera's edges consistently as the aspect ratio changes.

mcarriere

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: 3D UI Anchor Question/Problem
« Reply #3 on: January 21, 2014, 02:03:11 PM »
I'd like to resurrect this thread to keep this information together.

I would like to anchor a sprite to another GameObject that is on my Game Camera (another perspective camera) when I mouse over it.

The effect I'm looking to achieve is the following: http://d.pr/i/Hg7w

Anchoring a sprite in a 3D UI to a transform on another camera simply doesn't work. If I create a 2D UI, it works like a charm. (For various reasons and UI effects we need to keep a 3D UI.)

Can you go into a bit of detail as to why this isn't working, and how we might overcome it without creating a separate 2D UI?

EDIT: One more small bug, if you change an anchor via script, it does not update the Inspector for that object.
« Last Edit: January 21, 2014, 02:10:30 PM by mcarriere »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: 3D UI Anchor Question/Problem
« Reply #4 on: January 22, 2014, 04:24:25 AM »
The reason why it's not working with 3D UIs is because 3D UIs have depth (Z), and position along XY differs depending on the Z. You can have two UIs at the same time -- 3D and 2D, allowing you to keep your 3D UI, and reticles can use 2D. I did that in Starlink.