Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MibZ · Jan 04, 2013 at 11:36 PM · uv coordinatesworld space

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image efge · Jan 05, 2013 at 12:02 AM 0
Share

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

avatar image Davidovich · Jan 05, 2013 at 12:26 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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)
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# PackTextures & UV Mapping 2 Answers

Unknown input semantics TEXCOORD/4 1 Answer

CustomRenderTexture ignores "ComputeScreenPos" 1 Answer

Particles don't appear in world space mode 2 Answers

Can I use the local x,y transforms of a plane? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges