- Home /

# How to convert pixel/UV coordinates to World Space?

I know it is possible to get pixel/uv coordinates from a raycasthit, but is there a way to find the world space position of a specific texture coordinate?

If you already have a RaycastHit you could use RaycastHit.point (?).

As efge says, you can use RaycastHit.point which will give you the world-space coordinates of the point the ray hits. A raycast like this is the only way to convert the 2d screen pixel into a set of 3d world coordinates.

The question is a little unclear though, as I'm not sure what you mean by the specific texture coordinate. The point where the ray hits the object is going to be the same world-space coordinates whether you're talking about textures, UVs, meshes etc.

**Answer** by Bunny83
·
Jan 05, 2013 at 01:18 AM

It is possible, but it doesn't produce a clear result. This is because a Texture (or the texture space in general) is defined per triangle. It's not guaranteed that each area of the texture is mapped to actual geometry. On the other hand it's also possible that the same area / pixel of the texture is mapped to multiple areas on a mesh. So for a single uv coordinate you can get 0 to (theoretically) infinity worldspace coordinates.

The best example is the Unity-default-cube. Each face is mapped to the same area (the whole texture). So a certain uv / pixel coordinate would translate to 6 different worldspace positions for this mesh.

However an easy way to transform a uv coordinate of a triangle into local space (and then into world space) is to convert the uv position into barycentric coordinates. This way it's very easy to get the local space coordinates.

Here's a helper function i've written some time ago that converts a uv coordinate into it's barycentric representation of the 3 triangle corners:

```
//C#
Vector3 GetBarycentric (Vector2 v1,Vector2 v2,Vector2 v3,Vector2 p)
{
Vector3 B = new Vector3();
B.x = ((v2.y - v3.y)*(p.x-v3.x) + (v3.x - v2.x)*(p.y - v3.y)) /
((v2.y-v3.y)*(v1.x-v3.x) + (v3.x-v2.x)*(v1.y -v3.y));
B.y = ((v3.y - v1.y)*(p.x-v3.x) + (v1.x - v3.x)*(p.y - v3.y)) /
((v3.y-v1.y)*(v2.x-v3.x) + (v1.x-v3.x)*(v2.y -v3.y));
B.z = 1 - B.x - B.y;
return B;
}
```

This function can be used to test the barycentric coordinate if the point is inside the triangle:

```
bool InTriangle(Vector3 barycentric)
{
return (barycentric.x >= 0.0f) && (barycentric.x <= 1.0f)
&& (barycentric.y >= 0.0f) && (barycentric.y <= 1.0f)
&& (barycentric.z >= 0.0f); //(barycentric.z <= 1.0f)
}
```

**Answer** by travelhawk
·
Mar 03, 2017 at 09:29 PM

I used this and it worked for me: http://answers.unity3d.com/questions/372047/find-world-position-of-a-texture2d.html.

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.