Jessy
Jessy

Reputation: 11

ScreenToWorldPoint in Unity with the real camera

I am making an AR application using Unity.

Using a "real camera", it matches the specific object of which picture is already in database, using feature matching.

After feature matching, I stored one of the values of key points in the instance below:

   KeyPoint key_point;

Then I want to have the position of real world using that key_point.

   //let's assume that 
   //'x' is x-coordinate of key_point
   //'y' is y-coordinate of key_point

   Vector3 p = camera.ScreenToWorldPoint (Vector3 (x, y, camera.nearClipPlane));

I'm not sure if it is possible, since other people who are using "camer.nearClipPlane" were using it on the virtual 3D world.

If not possible, is there any other way that I can change 2d position to 3d position in the real world(I mean, the real camera world)?

Upvotes: 0

Views: 1000

Answers (1)

Everts
Everts

Reputation: 10701

I would think your solution has the problem of not considering the FOV of the camera. So your screen point will project to the near clip plane perpendicular. But that is correct only for the center of the screen.

One easy solution is to use ray object:

Ray ray = Camera.main.ScreenPointToRay(x,y);

https://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html

According to the docs:

"Resulting ray is in world space, starting on the near plane of the camera and going through position's (x,y) pixel coordinates on the screen (position.z is ignored)."

So your point would be:

   Vector3 point =  ray.origin;

Upvotes: 1

Related Questions