Log in or register to post comments

GUI Point to world position

January 26, 2012 - 7:25am #1

Hi,

First, thanks for this fantastic unity3d extension.

Situation:
I am using qcar ( the new beta ) for an ar project with unity3d on iPad.
I want to connect a gui element with a linerenderer to an object in the scene.

I have the gui texture position and want to convert this to a position in space. ( startPosition of my linerenderer. It is not so important if this is a linerenderer or whatever, I just need that one position)

Problem:
The startPosition of my line is not as expected.

What did I try:
I tested in unity editor with a modified camera matrix and my code worked for that. Testing my scene on an iPad, the line did not connect to the startPosition. It points to positions far off, more or less, depending on how I point to the marker.

The world center mode is set to user.

I use this code in a loop:

Vector3 tPos = guiLabel.transform.position;
float tX = (tPos.x * Screen.width);
float tY = (tPos.y * Screen.height);

// the 10 for the z val is hardcoded, I have no idea how get the nearClipping for the matrix manipulated camera
Vector3 screenPos = new Vector3(tX ,tY, 10);
Vector3 StartPos = Camera.mainCamera.ScreenToWorldPoint(screenPos);
StartPos = Camera.mainCamera.worldToCameraMatrix * StartPos;

// update my linerenderer
lineComponent.SetPosition (0, StartPos);
// targettrans is a 3d object in the world, this is no problem
lineComponent.SetPosition (1, targetTrans.position);

So, any hints, ideas?

Thank you

pictofun

Re: GUI Point to world position

January 26, 2012 - 9:32pm #2

You can use the camera.nearClipPlane value, QCAR actually uses the near and far values set in the Inspector to construct its projection matrix.

This code worked for me:

Vector3 tPos = guiLabel.transform.position;
float tX = (tPos.x * Screen.width);
float tY = (tPos.y * Screen.height);

Vector3 screenPos = new Vector3(tX ,tY, Camera.mainCamera.nearClipPlane);
Vector3 StartPos = Camera.mainCamera.ScreenToWorldPoint(screenPos);

lineComponent.SetPosition (0, StartPos);
lineComponent.SetPosition (1, targetTrans.position);

You may want to make the line narrower at the end near the screen:

lineComponent.SetWidth(0.01f, 1.0f);

- Kim

Log in or register to post comments