Repository navigation
Multiple intersections between ray and mesh. #7350
|
Hi, I'm trying to get the intersection points between a mesh and a ray, which i'm basically doing like this:
However, when i get the intersection point (via the distanceAlongLine attrib on the result), it's only outputting me one intersection point, and the ray is intersecting the mesh in two points. I saw somewhere that passing the Any idea how to achieve this? Thanks in advance! |
Replies: 1 comment 1 reply
|
To get every intersection, use import meshlib.mrmeshpy as mm
mesh = mm.makeSphere(mm.SphereParams()) # unit sphere at the origin
ray = mm.Line3f(mm.Vector3f(-5, 0.1, 0.2), mm.Vector3f(1, 0, 0))
hits = []
def onHit(res):
hits.append((res.distanceAlongLine, res.proj.face.get(), res.proj.point))
return True # continue the search
mm.rayMeshIntersectAll(mesh, ray, onHit) # optional: rayStart, rayEnd
hits.sort(key=lambda h: h[0]) # intersections arrive in tree traversal order, not along the ray
for t, f, p in hits:
print(f"t={t:.4f} face={f} point={p}")Output: If the ray passes exactly through a vertex or along an edge shared by several triangles, the same point can be reported once for each of those triangles. Merge hits whose |
rayMeshIntersectalways returns at most one intersection.closestIntersect=Falsedoes not return more of them: it lets the search stop at the first intersection it finds, which may not be the closest one. That is faster when you only need to know whether the ray hits the mesh at all.To get every intersection, use
rayMeshIntersectAll. It calls your function once for each intersection. ReturnTrueto keep searching, orFalseto stop: