简体   繁体   English

如何用不同大小的四边形填充程序生成的网格?

[英]How to fill out a procedurally generated mesh with quads of varying sizes?

I'm generating meshes based of color coded areas on an image.我正在根据图像上的颜色编码区域生成网格。

In the following picture, the semi-transparent image is a representation of what a color coded section might look like, with the black dots being natural 2d vertex positions.在下图中,半透明图像表示彩色编码部分的外观,黑点是自然的 2d 顶点位置。

Representation表示

The way I currently create a mesh is by iterating through a nested x,y for loop and making a 1x1 quad.我目前创建网格的方法是迭代嵌套的 x,y for 循环并制作 1x1 四边形。

However, I would like to generate the meshes in a way in which I could specify the desired quad size, perhaps something like the following image.但是,我想以一种可以指定所需四边形大小的方式生成网格,可能类似于下图。 (Numbers are the order of generation) (数字是产生的顺序)

Proposed generation with desired 3x3 quads具有所需 3x3 四边形的建议生成

The generation doesn't have to follow this schema exactly, or even be consisted of quads.生成不必完全遵循此模式,甚至不必由四边形组成。 The only important thing is that I'm able to specify the desired size of the generated triangles, in order to make meshes of varying detail to use in a LOD system.唯一重要的是我能够指定生成三角形的所需大小,以便制作细节不同的网格以在 LOD 系统中使用。

Would you happen to know what kind of mathematical area I should look into in order to figure out how to write this logic, or better yet, of an algorithm or a library that can do the aforementioned?您是否碰巧知道我应该研究什么样的数学领域才能弄清楚如何编写这种逻辑,或者更好的是编写可以执行上述操作的算法或库?

I, of course, plan to texture the generated mesh, so I wanted to additionally ask if the UVs are going to be messed up with this kind of generation, and if fixing those UVs at runtime is going to be problematic and cpu intensive.我,当然,计划对生成的网格进行纹理处理,所以我想另外问一下 UV 是否会被这种生成弄乱,以及在运行时修复这些 UV 是否会出现问题和 CPU 密集型。

This doesn't seem incredibly difficult, and there is almost certainly branches of mathematics that deal with this kind of problem, but I don't think it's necessary to go down that route (edit: see addendum below) .这似乎并不难,几乎可以肯定有数学分支可以解决此类问题,但我认为没有必要走这条路(编辑:请参阅下面的附录)

I would treat it as a recursion problem, where you start with 2^nx 2^m sized rectangle, and then further analyze each of those blocks as four blocks of size 2^(n-1) x 2^(m-1).我会将其视为递归问题,从 2^nx 2^m 大小的矩形开始,然后将每个块进一步分析为四个大小为 2^(n-1) x 2^(m-1) 的块. And just progressively go from there until you reach a block of 2x2 or 1x1, or whatever size you think makes sense based on the starting size.并从那里逐步前进,直到达到 2x2 或 1x1 的块,或者根据起始大小您认为有意义的任何大小。

Essentially, would start at, eg 512x256 and split into two 256x256, and then split both of those into four 128x128 blocks.本质上,将从例如 512x256 开始并分成两个 256x256,然后将这两个分成四个 128x128 块。 If a block is completely filled (positive), then add that block as a quad to this list, otherwise break unfilled blocks into four smaller blocks that are 64x64.如果一个块被完全填充(正),则将该块作为四边形添加到此列表中,否则将未填充的块分成四个 64x64 的较小块。 Continue to either add quads or further break apart until you reach some minimum size that makes sense for the level of detail that's needed.继续添加四边形或进一步拆分,直到达到对所需细节级别有意义的最小尺寸。

Here's the concept quickly sketched out in MS Paint:这是在 MS Paint 中快速勾勒出的概念: 在此处输入图片说明


Addendum附录

If you want an algorithm--not sure why I didn't think of this before--there's a two-dimensional algorithm called Marching Squares , which is the lower dimensional version of Marching Cubes , the algorithm most commonly associated with Voxels .如果你想要一个算法——不知道为什么我之前没有想到这一点——有一个名为Marching Squares的二维算法,它是Marching Cubes的低维版本,该算法最常与Voxels相关联。


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

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