从面的 4 个顶点获取 z 深度
如何仅使用 3d 对象每个面的 4 个顶点对 3d 对象的面进行 z 排序?我尝试过使用 z 缓冲区来存储每个面的平均 z 值;这在大多数情况下都有效,但当物体具有大面和小面时就会失败。
我正在 Flash 中构建一个小型 3D 引擎,只是为了学习的乐趣,所以我拥有的唯一数据就是这 4 个顶点和面部的法线。
谢谢!
How can I z-order the faces of a 3d object just using the 4 vertices of each of its faces? I've tried using a z-buffer where I store the average z value of each face; this works great most of the times, but fails when an object have large and small faces.
I'm building a small 3d-engine in flash, just for the fun of learning, so the only data i have is those 4 vertices and the normal of the face.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您遇到了可见性问题,并且没有简单的答案。 此页面更详细地描述了该问题,并且可能会给您一些帮助想法。 “真正的”3D 引擎解决这个问题的方法是,它们逐像素地绘制场景,并且持续跟踪迄今为止绘制到任何给定像素的最顶层对象的 Z 坐标。我们在 Flash 中无法使用这种方法,因此我们通常能做的最好的事情就是近似或概括。
一种常见的方法(在前面的链接中描述)是将面分组为凸多边形,因为对凸多边形的面进行深度排序很容易,并且对多边形本身进行深度排序也相对容易。如果这是可行的,这就是我要走的路。
You're butting up against the Visibility Problem, and there's no simple answer. This page describes the issue in a bit more detail, and may give you some thoughts. The way "real" 3D engines solve this is, they draw the scene pixel by pixel, and they keep running track of the Z coord of the topmost object drawn to any given pixel so far. This approach is not available to us in Flash, so the best we can generally do is approximate or generalize.
One common approach (described in the earlier link) is to group the faces into convex polygons, because it's easy to depth-sort the faces of a convex polygon, and relatively easy to depth-sort the polygons themselves. If this is feasible, it's the way I'd go.
当所有面的大小大致相同时,Z 缓冲效果最佳。如果你有任何比其他的大得多的东西,那么它们就会“覆盖”较小的那些,从而给你带来人工制品。
唯一的解决方案是将较大的面分解成更接近平均尺寸的较小面。
Z-buffering works best when all the faces are roughly the same size. If you have any that are very much larger than the rest then they're going to "overwrite" the smaller ones giving you artefacts.
The only solution is to break the larger faces down into smaller ones that are nearer the average size.
另一种实现起来非常简单的方法是BSP 树。它的一个问题是,在 3D 中,它具有一些非常糟糕的最坏情况性能特征。通常,您只会在一些非常奇怪的情况下遇到这种情况,因此可能值得尝试一下,看看它在您的情况下如何工作。
基本思想是构建一棵二叉树,其中每个内部节点都有一个平面方程,每个叶子都有一个多边形。在每个内部节点处,左子节点中的所有多边形都位于平面的一侧,右子节点中的所有多边形都位于平面的另一侧。您必须分割穿过平面的任何多边形。这就是糟糕的情况出现的地方。如果您的多边形的排列方式使得每个多边形的平面分割所有其他多边形,那么它就会变得丑陋。
Another approach which is pretty simple to implement is BSP trees. The one problem with it is that in 3D it has some pretty bad worst case performance characteristics. You'll usually only hit that with some pretty odd-ball cases, so it might be worth trying it and seeing how it works in your case.
The basic idea is that you build a binary tree where each internal node has a plane equation and each leaf has a polygon. At each internal node, all of the polygons in the left child lie on one side of the plane and all of the polygons in the right child lie on the other side. You have to split any polygons which cross the plane. That's where the bad cases come in. If you have polygons which are arranged so that each polygon's plane splits all of the other polygons, then it gets ugly.