简体   繁体   English

将基于2D平铺的形状转换为简化多边形

[英]Converting 2D Tile based shapes to simplified Polygons

基于平铺的世界转换为多边形形状

As seen in the image above, I have a 2D array of tiles, each with 4 points in my game world. 如上图所示,我有一个2D阵列的瓷砖,每个瓷砖在我的游戏世界中有4个点。 I'm looking for a method to convert these shapes constructed from individual tiles into simplified (no unnecessary vertices, only those needed to form the outline) polygon shapes. 我正在寻找一种方法,将从单个瓷砖构造的这些形状转换为简化的(没有不必要的顶点,只有形成轮廓所需的那些)多边形。

I have been looking around, here and elsewhere and have had very little luck. 我一直在四处寻找,在这里和其他地方,运气很少。 But maybe I don't know the correct terminology to search for. 但也许我不知道要搜索的正确术语。 Any help is appreciated. 任何帮助表示赞赏。

Extra Info: I'm looking to use this to optimize dynamic lighting. 额外信息:我希望用它来优化动态照明。 If someone has a different approach for accomplishing fast dynamic shadows in a tile based world, that will also answer the question. 如果某人有一种不同的方法来在基于图块的世界中完成快速动态阴影,那么这也将回答这个问题。

I suggest next algorithm: 我建议下一个算法:

  1. Storing all edge positions into 2D array (edge position is center of edge). 将所有边缘位置存储到2D阵列中(边缘位置是边缘的中心)。
  2. Counting duplicate edges in this array (1 is no duplicates, 2 is intersecting with another edge. Other values are implossible). 计算此数组中的重复边(1表示没有重复,2表示与另一条边相交。其他值是不可能的)。
  3. Picking first unmarked edge from array with duplicating count 1 (no duplicates) and applying simple recursive fill algorithm in certaing direction (Clockwise for example) until first edge reaching. 从具有重复计数1(没有重复)的数组中挑选第一个未标记的边缘,并在确定方向(例如顺时针方向)中应用简单的递归填充算法,直到第一个边缘到达。 All this edges will form one simplified polygon. 所有这些边将形成一个简化的多边形。 If unmarked edge has not been founded, then GOTO 5. 如果尚未建立未标记的边缘,那么GOTO 5。
  4. Marking all this edges from step 3 as used. 标记所使用的步骤3中的所有边缘。 GOTO to step 3. GOTO到第3步。
  5. End. 结束。

For more intuitive representation of algorithm, I posted an image below. 为了更直观地表示算法,我在下面发布了一张图片。

在此输入图像描述

The naive approach would be to simply step through every single tile and mark any transition edges into a polygon but you could reuse a common edge detection routine for better performance. 天真的方法是简单地逐步遍历每个单独的图块并将任何过渡边缘标记为多边形,但您可以重复使用公共边缘检测例程以获得更好的性能。

After that you'd probably be interested in tessellating those polygons so as to convert them into collections of triangles (making the later shadow/lighting/intersection math a lot simpler), the only problem in this case is if you end up with a concave polygon, but a decent tessellator should allow you to break it up into convex polygons. 之后你可能有兴趣对这些多边形进行细分,以便将它们转换为三角形集合(使后面的阴影/光照/交叉点数学变得更加简单),在这种情况下唯一的问题是如果你最终得到一个凹面多边形,但一个体面的镶嵌器应该允许您将其分解为凸多边形。 I don't think XNA has anything built in for tessellation so you might need to find a utility library to do it for you. 我不认为XNA有任何内置的镶嵌细分,因此您可能需要找到一个实用程序库来为您完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM