Author Topic: UISprite Viewport distance to pixel width  (Read 8677 times)

boofcpw

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 30
    • View Profile
UISprite Viewport distance to pixel width
« on: February 23, 2014, 07:08:11 PM »
Hey,

Im currently trying to create a simple line renderer that connects two transforms (two other widgets), but I cant seem to get the conversion to the pixels correct.

Currently, I am doing the following:

// Setting the width of the sprite to be the distance between the two transforms
float width = Vector3.Distance(nguicamera.ViewportToScreenPoint(widgetTransform1.position),nguicamera.ViewportToScreenPoint(widgetTransform2.position));
lineSegmentSprite.SetDimensions((int)(width) , 32);

// Setting the sprite to be between the two transforms
lineSegment.transform.position = (transform1.position+transform2.position )/2.0f;

// Rotate the sprite

Using this method, the sprite is placed in the correct position between the widgets and rotated correctly, but the width is not correct (its sort of close, but off)

Any help with this would be great. Im probably doing something silly, but thanks :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UISprite Viewport distance to pixel width
« Reply #1 on: February 24, 2014, 02:02:10 PM »
transform.position is in world coordinates. NGUI elements work in virtual pixels. If the UIRoot is pixel perfect, then virtual pixels match actual pixels, but if it's fixed size then they don't.

You need to convert world position to local position via transform.InverseTransformPoint.

boofcpw

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 30
    • View Profile
Re: UISprite Viewport distance to pixel width
« Reply #2 on: February 24, 2014, 07:26:49 PM »
Excellent. Works great. Thanks a lot :)

pumuckli

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: UISprite Viewport distance to pixel width
« Reply #3 on: October 22, 2014, 04:52:03 AM »
I'm trying to do basically the same thing right now, and although i kind of understood what to do, i can't get it right - could you perhaps share a little piece of code? I am having a transform (sprite) and want to know the distance in "Ngui-Pixels" to another transform(sprite), so that i can build a line between these two.

I am using
  1.                
  2. float dist = Vector3.Distance(root.transform.InverseTransformPoint(transform.position),root.transform.InverseTransformPoint(target.position));
  3.  

, which is sort of right but off (same as boofcpw first post). Any Help would be very appreciated!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UISprite Viewport distance to pixel width
« Reply #4 on: October 22, 2014, 05:50:47 AM »
transform.position is the pivot point. If your widget is set to use a center pivot, this will be the widget's center. If it's top-left, it will be top-left, etc.

pumuckli

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: UISprite Viewport distance to pixel width
« Reply #5 on: October 22, 2014, 05:54:23 AM »
Thanks for your answer, but i think this is not the problem - i could also do this with two empty gameObjects, and the distance is not really right. So you think that the code postet above should work like it is?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UISprite Viewport distance to pixel width
« Reply #6 on: October 22, 2014, 06:04:30 AM »
Keep in mind, what you're doing is converting world space to local space. I'm guessing you want to convert to screen space instead.

camera.WorldToScreenPoint

pumuckli

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: UISprite Viewport distance to pixel width
« Reply #7 on: October 22, 2014, 06:47:06 AM »
D'oh - thanks, seem like Screen space is all i wanted. Thank you! This solution is working (connecting two Points with a single line-sprite, attatch this to a sprite with pivot set do "down"):

  1.  
  2.                 //Rotate towards Target;
  3.                 Quaternion newRotation = Quaternion.LookRotation(mTrans.position - target.position, Vector3.forward);
  4.                 newRotation.x = 0.0f;
  5.                 newRotation.y = 0.0f;
  6.                 mTrans.rotation = newRotation;
  7.  
  8.                 //Set Length
  9.                 float dist = Vector3.Distance(nguicam.WorldToScreenPoint(mTrans.position),nguicam.WorldToScreenPoint(target.position));
  10.  
  11.                 sprite.SetDimensions(width,(int)dist);