2

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.

Representation

The way I currently create a mesh is by iterating through a nested x,y for loop and making a 1x1 quad.

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

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.

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.

Vosheck
  • 29
  • 5

1 Answers1

2

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^n x 2^m sized rectangle, and then further analyze each of those blocks as four blocks of size 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.

Essentially, would start at, e.g. 512x256 and split into two 256x256, and then split both of those into four 128x128 blocks. 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. 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: enter image description here


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.


rodamn
  • 2,191
  • 19
  • 24
  • It might have got lost in the graphic, but I started with black squares to denote the largest dusbdivision of squares, then gray squares to denote the second subdivision, and then red to denote the third subdivision. – rodamn Aug 28 '20 at 20:29