gkjohnson / three-mesh-bvh

A BVH implementation to speed up raycasting and enable spatial queries against three.js meshes.
https://gkjohnson.github.io/three-mesh-bvh/example/bundle/raycast.html
MIT License
2.37k stars 247 forks source link

Does the three-mesh-bvh library have any methods to distinguish between the cases where two meshes are just touching and when they are embedded within each other? #667

Open MYDJH opened 1 month ago

MYDJH commented 1 month ago

Hello, is there any method in the three-mesh-bvh library to ignore the situation where two mesh surfaces just come into contact? I tried to scale based on the centroid, but some meshes are too complex and scattered in different locations in space. I also found the method "distanceToGeometry", but whether they are just in contact or embedded in each other, I found that its minimum value is always 0, so I still can't distinguish these two situations. Is there any other way to achieve this point?

gkjohnson commented 1 month ago

Unfortunately there is no mathematical perfection with floating point values and matrix math and thus no reliable way to exactly determine if something is in a "just touch" configuration. However you could use a threshold value to check for distances or parallel faces being "close enough". This isn't something directly exposed by this project, though. You'll have to write your own detection using the shapecast function.

What is your use case?

MYDJH commented 1 month ago

hello, I apologize for the delayed response. You mentioned that I could use shapecast to write my own detection program, but I'm not entirely sure how to proceed as I'm not strong in reading technical documentation. Do you have any relevant demos that I could refer to? My current approach is to use intersectsGeometry to quickly check if meshes intersect, but this cannot exclude cases where two objects are just touching each other. To address this, I've tried using intersectsTriangle from three-mesh-bvh with the ExtendedTriangle and scaling each triangle down by 0.999 to exclude just touching cases. However, I've found that this becomes very computationally expensive when there are a large number of elements involved. Do you have any better approaches that could achieve this? I'm eagerly looking for your suggestions.

gkjohnson commented 4 weeks ago

Do you have any relevant demos that I could refer to?

Other functions like intersectsSphere and intersectsBox use this function internally. The characterMovement, clippedEdges, collectTriangles, and other examples all use shapecast internally but not exactly in the way you're trying to.

scaling each triangle down by 0.999 to exclude just touching cases.

Scaling a triangle down won't work to avoid just touch cases. Detecting what is considered "just touching" is not necessarily simple and one-size-fits all cases due to floating point error, as I mentioned, so you'll have to determine what solution is right for your use case. But consider cases where two opposite facing parallel triangle are overlapping. Simply scaling them down will not work. One solution is to shift the triangles along the plane but this still won't work for every case.

Do you have any better approaches that could achieve this?

You'll need to use an acceleration structure if you need higher performance - so shapecast is my best solution at the moment. If it's still not fast enough there are other solutions that require two BVHs but that's more complex.