Author Topic: Screen Coordinate Conversion  (Read 9332 times)

heliosprime

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Screen Coordinate Conversion
« on: August 23, 2014, 05:39:41 PM »
I'm trying to draw a 3D line (using Vectrosity, if it matters) from a NGUI sprite to an object in the world.

I need a starting  world Vector3 and an end world Vector3.   Starting Vector3 is the transform.position of the world object, which works fine.

Now, I'm trying to convert the position from my 3D NGUI UISprite widget to a world coordinate, given the current position of the camera/view.  For the life of me I can't figure out the correct conversion.  Without any conversion, the line is drawn to the actual NGUI object in-world, but that obviously doesn't match up with the camera projection.

Any help would be much appreciated.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Screen Coordinate Conversion
« Reply #1 on: August 23, 2014, 08:12:01 PM »
Look in NGUIMath. There is a function called OverlayPosition. It does all the necessary math inside.

heliosprime

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Screen Coordinate Conversion
« Reply #2 on: August 23, 2014, 08:44:50 PM »
Thanks for the reply, I know you're busy.

I don't think I explained it right, or I'm not understanding how OverlayPosition would help me.  It looks like it does the opposite of what I need.

I see there's a function called WorldToLocalPoint.  I think what I need is LocalPointToWorld, which doesn't seem to exist.

In the end, I need to convert a UISprite.transform.position to a World Position.  Looking at NGUIMath I thought I could

  1. Vector3 targetLoc = uiCam.ViewportToWorldPoint(sprite.transform.position);
  2. targetLoc = worldCam.WorldToViewportPoint(targetLoc );
  3.  

That totally did not work.



« Last Edit: August 23, 2014, 08:52:43 PM by heliosprime »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Screen Coordinate Conversion
« Reply #3 on: August 24, 2014, 07:25:07 PM »
Opposite of transform.WorldToLocalPoint is transform.LocalToWorldPoint. That's not NGUI, that's just Unity.

UISprite.transform.position is already world position, but "world" is different depending on the camera that sees it. If you have a UI camera, you need to take that world point and convert it to view or screen space. Then you need to use the main camera to do the inverse -- convert from view/screen space to world space. That's what that Overlay function does.

Paul

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Screen Coordinate Conversion
« Reply #4 on: June 21, 2017, 04:38:58 PM »
+1 Came here to say NGUIMath.cs OverlayPosition() did the job, thanks again ArenMook!