求立方体内射线的长度

发布于 2024-10-03 10:26:25 字数 126 浏览 0 评论 0原文

我有一条光线,从相机向外发出,我需要找到立方体内光线的长度。请注意,相机可能位于立方体内部,在这种情况下,它应该返回到立方体边缘的距离。

它用于着色器,因此它应该相对较小并且涉及最少的分支。

有什么想法吗?

I have a ray, going outwards from a camera, I need to find the length of the ray inside the cube. Note that the camera could be inside the cube, in which case it should return the distance to the edge of the cube.

It's for a shader, so it should be relatively small and involve a minimum of branching.

Any ideas?

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

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

发布评论

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

评论(2

是你 2024-10-10 10:26:25

要将射线与凸多面体相交,请使射线与包含每个面的平面相交。

假设射线从 q 开始,方向为 r。那么对于标量参数 t,射线上的任何点都可以表示为 q + t r。现在假设我们想要将此射线与由方程 p · n = k 给出的平面相交(其中 n > 是垂直于面的向外指向单位)。当光线与平面相交时

(q + t r) · n = k

即,当

q · n + t r · n = k

等等

t = (kq · n) / r · n

r·n为负时,光线进入面;当r·n时,光线退出面是积极的。当r·n为零时,光线平行于面。您需要检查这种情况以避免被零除;在这种情况下,如果 q · n > k,光线完全错过了多面体。

因此,找到多面体所有面的t,并让tin为点的最大值t光线进入面的位置,tout 是光线离开面的点的最小值 t。那么多面体内射线的长度为

max(max(tout, 0) − max(tin, 0), 0 ) / |r|

如果 r 是单位向量,则可以避免此处的除法。两个内部 max(t, 0) 是处理相机位于立方体内部或立方体位于相机后面的情况所必需的。外部 max(..., 0) 用于处理光线完全错过立方体的情况(在这种情况下 tin 将大于 tout)。

恐怕我不知道如何用着色器语言表示此计算,更不用说如何无分支地执行此计算了。

To intersect a ray with a convex polyhedron, intersect the ray with the planes containing each face.

Suppose the ray starts at q and has direction r. Then any point on the ray can be represented as q + t r for a scalar parameter t. Now suppose we want to intersect this ray with the plane given by the equation p · n = k (where n is an outward-pointing unit normal to the face). The ray intersects the plane when

(q + t r) · n = k

That is, when

q · n + t r · n = k

and so

t = (kq · n) / r · n

The ray enters the face when r · n is negative and exits when r · n is positive. When r · n is zero, the ray is parallel to the face. You need to check for this case to avoid division by zero; and in this case, if q · n > k, the ray misses the polyhedron completely.

So find t for all the faces of your polyhedron and let tin be the maximum t for the the points where the ray enters a face, and tout be the minimum t for the points where the ray exits a face. Then the length of the ray within the polyhedron is

max(max(tout, 0) − max(tin, 0), 0) / |r|

If r is a unit vector you can avoid the division here. The two inner max(t, 0) are necessary to handle the case where the camera is inside the cube or the cube is behind the camera. The outer max(..., 0) is there to handle the case where the ray misses the cube entirely (in which case tin will be greater than tout).

I'm afraid I have no idea how to represent this computation in your shader language, let alone how to do so branchlessly.

南街九尾狐 2024-10-10 10:26:25

如果您告诉我们您到底需要这个做什么,那就太好了。您可以通过渲染立方体然后读取包含从相机到立方体每个可见点的距离的 z 缓冲区来轻松完成此操作。中间的点是相机正在观察的地方。

否则,您将需要对立方体的每个平面执行光线平面相交 ,检查交点是否在立方体的范围内,并检查它是否不在相机后面。您可以得到以下三种情况之一:

  1. 无交点
  2. 一个交点 - 您在立方体内部,计算交点与相机位置之间的距离
  3. 两个交点 - 您在立方体外部,计算两个交点之间的距离
  4. 无限多个交点 -视图向量是立方体的切线,只需丢弃切平面交点,只取两个相邻平面的交点即可。

您需要将立方体信息发送到着色器单元。所有着色器的计算都是相同的,因此我认为将其放入着色器中没有意义 - 最好在 CPU 上计算一次。此外,它还涉及分支(我不知道如何避免它)。

It would be fine if you told us what exactly you need this for. You can do it easily by rendering the cube and then reading the z-buffer which contains distances from camera to every visible point of the cube. The point in the middle is where the camera is looking.

Otherwise you will need to perform a ray-plane intersection for each of the planes of the cube, check that the intersection is within the range of the cube and also check that it is not behind the camera. You can get either of these three cases:

  1. No intersection
  2. One intersection - you are inside the cube, calculate the distance between the intersection and camera position
  3. Two intersections - you are outside the cube, calculate the distance between the two intersections
  4. Infinitely many intersections - the view vector is a tangent to the cube, just throw away the tangent plane intersections and take only two intersections to the two adjacent planes

You will need to send the cube information to the shader unit. The computation will be the same for all shaders so I don't see a point in putting it in a shader - it would be better to compute this once on the CPU. Also, it involves branching (I have no idea how to avoid it).

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