Thank you for the thorough explanation - finding the closest facet on a mesh is a critical function for what I am doing. I am trying to think of ways to make this more accurate (projecting to a plane within the bounding box etc.). If you know about something I can do, let me know.
It is currently replacing the following function, which as you may imagine is very slow:
/**
* Finds the point that is closest to the part's mesh.
* @param {Vector3} point
* @return {Vector3} Closest point on the Part Mesh
*/
closestPointOnPartMeshTo(point) {
var positions = this._partMesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
var minSquaredDistance = Math.pow(10, 9);
var closestVertex = BABYLON.Vector3.Zero;
var numberOfVertices = positions.length / 3;
for (var i = 0; i < numberOfVertices; i = i + 1) {
var vertPosition = new BABYLON.Vector3(positions[i*3], positions[i*3+1], positions[i*3+2]);
var dsq = BABYLON.Vector3.DistanceSquared(point, vertPosition);
if (minSquaredDistance > dsq) {
minSquaredDistance = dsq;
closestVertex = vertPosition;
}
}
return closestVertex;
}