首页 > 解决方案 > 在 Threejs 中找到网格上最接近其他点的点?

问题描述

你好 StackOverflow 社区!我的场景中有一个点漂浮在周围,还有一组复杂的网格。我需要在这些网格中的任何一个上找到离原始点最近的点。我想我可以用 SphereCast 之类的东西来做到这一点,但我在 Threejs 文档中没有找到任何东西。

这是我需要做的2D表示:

在此处输入图像描述

基本上,我有 P1 和一组中的所有其他表面,我需要找到 P2。

谢谢!

标签: three.js3d

解决方案


实际上,您似乎想重现光线投射中实现的内容。但我认为这取决于您在示例中考虑的形状,因为您需要表面方程来获得解析解。

我找到了基本形状(球体、三角形等)的光线投射的这个例子,但是没有形状的方程就没有一般的解决方案。例如,对于射线-球体相交,它们给出以下 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 中,因为大多数材质都是由小三角形组成的,所以每个光线投射演算都依赖于三角形光线投射。


推荐阅读