Hello StackOverflow社区!我的场景中有一个点在漂浮,并且有一组复杂的网格。我需要在这些网格中找到最接近原始点的点。我想可以用SphereCast之类的东西来做到这一点,但我在Threejs文档中找不到任何东西。
这里是我需要做的2D表示:
基本上,我有P1和一个组中的所有其他曲面,我需要找到P2。
谢谢!
实际上,您似乎想复制在光线投射中实现的内容。但是我认为这取决于您在示例中考虑的形状,因为您将需要表面方程式来获得解析解。
我发现基本形状(球体,三角形等)的射线投射的this example,但没有形状的方程式,就没有通用的解决方案。例如,对于射线-球面相交,它们给出以下C ++:
{
float t0, t1; // solutions for t if the ray intersects
#if 0
// geometric solution
Vec3f L = center - orig;
float tca = L.dotProduct(dir);
// if (tca < 0) return false;
float d2 = L.dotProduct(L) - tca * tca;
if (d2 > radius2) return false;
float thc = sqrt(radius2 - d2);
t0 = tca - thc;
t1 = tca + thc;
#else
// analytic solution
Vec3f L = orig - center;
float a = dir.dotProduct(dir);
float b = 2 * dir.dotProduct(L);
float c = L.dotProduct(L) - radius2;
if (!solveQuadratic(a, b, c, t0, t1)) return false;
#endif
if (t0 > t1) std::swap(t0, t1);
if (t0 < 0) {
t0 = t1; // if t0 is negative, let's use t1 instead
if (t0 < 0) return false; // both t0 and t1 are negative
}
t = t0;
return true;
}
我认为在Three.js中,因为大多数材料都是由小三角形组成的,所以每个射线投射演算都依赖于三角形射线投射。