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 stark89 · Sep 24, 2012 at 10:45 AM · texturemeshrenderervertices

How to get the texture co-ordinate(x,y) from a 3D point on the mesh renderer?

Hi all,

I am trying to find a point x,y on the texture, corresponding to a 3D point on the mesh.

Thanks in Advance.

Comment
Add comment · Show 1
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 Fattie · Sep 24, 2012 at 10:46 AM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-textureCoord.html

1 Reply

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Bunny83 · Sep 24, 2012 at 10:52 AM

You have to raycast against the mesh to find the point on the surface. The raycast already returns the texture coordinates at this point in the RaycastHit struct.

The only alternative would be to iterate through all triangles manually and test if the point is inside it's bounds. If you really want to do this you have to project the point onto the triangle plane (unless you're sure it is exactly on the surface) and then convert the point into barycentric coordinates. Now you can interpolate the uv from the 3 vertices uvs.

edit

Ok so it seems you want the texture coordinates of a collision point, right?

In this case it's quite simple:

 // Attached to your "ball / projectile"
 
 void OnCollisionEnter(Collision collisionInfo)
 {
     foreach (ContactPoint cp in collisionInfo.contacts)
     {
         RaycastHit hit;
         float rayLength = 0.1f;
         Ray ray = new Ray(cp.point - cp.normal * rayLength * 0.5f, cp.normal);
         Color C = Color.white; // default color when the raycast fails for some reason ;)
         if (cp.otherCollider.Raycast(ray, out hit, rayLength))
         {
             Texture2D tex = (Texture2D)cp.otherCollider.renderer.material.mainTexture;
             C = tex.GetPixelBilinear(hit.textureCoord.x, hit.textureCoord.y);
         }
         // Instantiate your effect and
         // use the color C
     }
 }

Or, if you want to attach the script to the cube, it should look like this:

 // Attached to your cube object
 
 private Texture2D m_MainTexture;
 
 void Start()
 {
     m_MainTexture = (Texture2D)renderer.mainTexture;
 }
 
 void OnCollisionEnter(Collision collisionInfo)
 {
     foreach (ContactPoint cp in collisionInfo.contacts)
     {
         RaycastHit hit;
         float rayLength = 0.1f;
         Ray ray = new Ray(cp.point + cp.normal * rayLength * 0.5f, -cp.normal);
         Color C = Color.white; // default color when the raycast fails for some reason ;)
         if (cp.thisCollider.Raycast(ray, out hit, rayLength))
         {
             C = m_MainTexture.GetPixelBilinear(hit.textureCoord.x, hit.textureCoord.y);
         }
         // Instantiate your effect and
         // use the color C
     }
 }

ps: I haven't tried it, so maybe the normal points the other way, but usually it points away from the "other object".

Comment
Add comment · Show 3 · 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 stark89 · Sep 26, 2012 at 04:40 AM 0
Share

Thanks a ton bunny :)

avatar image Fattie · Sep 26, 2012 at 07:18 AM 3
Share

this question is a good example of where questions can sometimes take an astounding amount of time to answer, if, enough information is not given.

i think of it as the "misdirection to a difficult topic" issue.

"point near a mesh" problem is a huge, gargantuan topic in maths and program$$anonymous$$g and computer science.

Fortunately the answer to the question at hand is trivial, just "use this RaycastHit-textureCoord.html"

But it APPEARED TO BE a question regarding "point near a mesh" issues.

I have noticed on this list it often happens that there is "misdirection to a difficult topic" when in fact it's just a trivial issue.

I don't really know why I'm typing this or who will read it :) Cheers!

avatar image Millhow · Nov 19, 2018 at 10:01 AM 1
Share

Thank you for sharing! Just be careful whoever wants to use this technique, it only works with a $$anonymous$$esh Collider. If you use a predefined collider (Box, Sphere, Capsule), the hit.textureCoord will always be in (0, 0).

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

12 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

Related Questions

Skinned Cloth Editor Crashs 1 Answer

How to switch the texture being used by the material of the mesh renderer 1 Answer

Problem with generating a tube mesh 1 Answer

Do the Bounds of an Mesh Change When you Change its Vertices Positions? 1 Answer

Swapping out materials on a skinned mesh renderer? 3 Answers


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