- Home /
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.
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".
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!
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
Follow this Question
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