 # Texture coordinates of a random point on a mesh

How can I get texture coordinates of a random point on a mesh?

Hi @rhinor,

Can you provide some background on why you want to do this? What problem are you trying to solve?

Thanks,

– Dale

Hi,

I think what you are looking for is the Barycentric Coordinates of a point on the mesh and multiply those with the UV coordinates of the corresponding vertex texturecoordinates.

I made a quick setup to test t and it seems this is working.
Since I’m short in time it’s rather minimal:

If you run the python code in the attached file you can move the point around and see that the point coordinates equal the UV found on this 1x1 mesh with normalized mapping.
*I did no test with a triangulated mesh

Does this make sense?
-Willem

bary_test.3dm (459.4 KB)

``````import rhinoscriptsyntax as rs
import Rhino

mesh_id= [id for id in rs.NormalObjects() if rs.IsMesh(id)]
mesh = rs.coercemesh(mesh_id)

pt_id= [id for id in rs.NormalObjects() if rs.IsPoint(id)]
pt = rs.coerce3dpoint(pt_id)

mesh_point = mesh.ClosestMeshPoint(pt, 0)

mesh_face = mesh.Faces[mesh_point.FaceIndex]
uvA = mesh.TextureCoordinates[mesh_face.A]
uvB = mesh.TextureCoordinates[mesh_face.B]
uvC = mesh.TextureCoordinates[mesh_face.C]
uvD = mesh.TextureCoordinates[mesh_face.D]

bary = mesh_point.T

U = 0
V = 0
for b,uv in zip(bary, [uvA,uvB,uvC,uvD]):

print b ,' : ',uv

U += uv*b
V += uv*b

uv_coord = (U,V)

print uv_coord
print pt
``````
1 Like

Thank you, Willem. The script works very well.

1 Like

Hi Dale,

I was trying to use RayShoot method for placing points on a mesh, and retrieve hit points’ info. Any update on Whether RayShoot method works with meshes?

Hi,

You might want to use MeshRay for that: