ThreeJS Ray 与隐形平面有交集吗?

发布于 2024-12-26 06:35:00 字数 282 浏览 1 评论 0原文

我一直在研究ThreeJS,但我无法弄清楚当前的问题。

当我从相机投射光线时,它有太多的交叉点,其中一些甚至不靠近我的实际平面。我应该找到交叉点,它实际上刺穿了我的可见平面。有什么想法如何做到这一点吗?

示例(双击平面,每个框代表交点。应该与 Chrome 和 FF 一起使用): http://concurio.com/webgl/thirdjs-plane.html

I've been studying ThreeJS and I can't figure out the current issue.

When I cast a ray from my camera it has too many intersections, and some of those are not even near of my actual plane. I should be finding the intersection, which is actually piercing my visible plane. Any ideas how to do that?

Example (Double click the plane, Each box represents the intersection. Should work with Chrome and FF):
http://concurio.com/webgl/threejs-plane.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美胚控场 2025-01-02 06:35:00

您需要在几何体上调用computeCentroids 和computeFaceNormals。即使如此,相交代码也仅适用于平面。如果您的几何体中需要有非平面四边形,则需要使用三角形。或者修复 Three.js 中的错误(在 https://github.com/mrdoob 提交问题/two.js/issues/1357 )

另外,将 renderer.domElement 移动到页面的左上角。

简而言之,试试这个:

var s = renderer.domElement.style;
s.left = s.top = '0px';
s.position = 'absolute';

var planeGeo = new THREE.PlaneGeometry(400, 200, 40, 40);
for ( var i = 0, l = planeGeo.vertices.length; i < l; i ++ ) {
  // planar quad faces 
  var p = planeGeo.vertices[i].position;
  p.z = Math.sin(p.y / 20)*40;
}
planeGeo.computeFaceNormals();
planeGeo.computeCentroids();

[更新]在 Three.js 的 dev 分支中,有 THREE.GeometryUtils.triangulateQuads 您可以将其用作解决方法。它已经计算了面法线和质心,因此您只需将这一行添加到代码中:

THREE.GeometryUtils.triangulateQuads(planeGeo);

You need to call computeCentroids and computeFaceNormals on your geometry. And even then, the intersection code only works on planar faces. If you need to have non-planar quads in your geometry, you need to use triangles instead. Or fix the bug in Three.js (filed issue at https://github.com/mrdoob/three.js/issues/1357 )

Also, move the renderer.domElement to the top-left of the page.

In short, try this:

var s = renderer.domElement.style;
s.left = s.top = '0px';
s.position = 'absolute';

var planeGeo = new THREE.PlaneGeometry(400, 200, 40, 40);
for ( var i = 0, l = planeGeo.vertices.length; i < l; i ++ ) {
  // planar quad faces 
  var p = planeGeo.vertices[i].position;
  p.z = Math.sin(p.y / 20)*40;
}
planeGeo.computeFaceNormals();
planeGeo.computeCentroids();

[update] In the dev branch of Three.js, there's THREE.GeometryUtils.triangulateQuads which you can use as a workaround. It already computes face normals and centroids, so you only need to add this one line to your code:

THREE.GeometryUtils.triangulateQuads(planeGeo);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文