clamping the size of a bunck of vertices

i’ve just finished making a mesh, and have added a basic LOD system into it, but when I change the LOD scale the mesh loses detail like it should but also gets bigger, what i beleive i need to do is to change the xSize,ySize dynamically but i don’t know how to do it.


theses are the same mesh but on the left it’s LOD 1 and on the right its LOD 2.

public int LOD = 1;
public int ySize,xSize = 140;

for (int i = 0, y = 0; y <= ySizeLOD; y+=LOD)
{
for (int x = 0; x <= xSize
LOD; x+=LOD)
{
vertices = new Vector3(x, perlin, y);
i++;
}
}
I want to clamp the size so when the LOD changes the mesh stayes the same size.
If anyone could help i would appreciate it.
thanks

The way your code is written, each LOD level you make is going to have the same number of verts.

Instead, write your for loops like so:

for (int i = 0, y = 0; y <= ySize; y+=LOD)    // etc...

I’ll assume you’ve elided the code that generates the perlin height based on position in the 2D field…

Keep in mind that the LOD system swaps out the entire model, not portions of it the way the terrain system can do.

if i do what you suggested there when i change the LOD number i get this.4462771--409639--upload_2019-4-23_23-26-14.png

It looks like you did not change how the triangle indices are computed. Obviously that would be different because for a start, there’s way less triangles in each successive LOD.

for (int i = 0, y = 0; y <= YSize; y+=LOD)
{
for (int x = 0; x <= XSize; x+=LOD)
{
float rand = Perlin(x + xOffset * LOD , y + yOffset * LOD );
float per = PerlinTwo(x + xOffset * LOD , y + yOffset * LOD );
float pert = PerlinThree(x + xOffset * LOD , y + yOffset * LOD );
int b = 0;
/for(int g = 0; g <= Randoms; g++)
{
float rands = per * pert * randomNums
*;**
b++;
}*/
float rands = per * rand * pert;
vertices = new Vector3(x, rands, y);
i++;
}
}
int c = 0;
int v = 0;
for (int y = 0; y < YSize; y+=LOD)
{
for (int x = 0; x < XSize; x+=LOD)
{
triangles[v] = c;
triangles[v + 1] = triangles[v + 4] = c + xSize + 1;
triangles[v + 2] = triangles[v + 3] = c + 1;
triangles[v + 5] = c + xSize + 2;
v += 6;
c++;
}
c++;
}

are you meaning the loops that control the triangle making?

Yes. Think about the simplest two comparisons and how the triangles are going to lay out differently:

4462861--409657--lods.png

If the verts in the left are 0,1,2,3,4,5,6,7,8 and the verts on the right are 0,1,2,3, you’re going to need to change how the triangle indices are computed depending on your step rate.

It might be easier to control the number of steps on the side, and use that count to produce your triangle index. Then you can just adjust the reduced step count up to space apart an equivalent amount on any LOD.

ok if i’m not mistaken that shouldn’t matter, the reason why i say that is…

for (int i = 0, y = 0; y <= ySize; y+=LOD)
        {
            for (int x = 0; x <= xSize; x+=LOD)
            {
            vertices[i] = new Vector3(x, rands, y);

when i increment by LOD that sets the distance betwen the vertices so if LOD = 2 then when the loop is in process the x y iterate like this 0,2,4,6,8… but theses vertices are the same as if LOD =1, yes the iteration changes to 0,1,2,3,4,5… but in my Vector3[ ] vertices; array they are both = 0,1,2,3,4 so when i set

triangles[v] = c;
                triangles[v + 1] = triangles[v + 4] = c + xSize + 1;
                triangles[v + 2] = triangles[v + 3] = c + 1;
                triangles[v + 5] = c + xSize + 2;

it should be the same.

unless i’m missing something which is quit possible because i’m new to all this
:smile:

Check again! :slight_smile:

4462906--409663--lods2.png

If you want some sample code for generating geometry, here’s my ‘makegeo’ package.

It doesn’t have the exact use above, but it has other ways of winding geometry in code.

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo

oo are you saying to have the triangles on on something else

        for (int y = 0; y < somethingelse; y++)
        {
            for (int x = 0; x < somethingelse; x++)
            {
                triangles[v] = c;
                triangles[v + 1] = triangles[v + 4] = c + xSize + 1;
                triangles[v + 2] = triangles[v + 3] = c + 1;
                triangles[v + 5] = c + xSize + 2;
                v += 6;
                c++;
            }
            c++;
        }

so there aren’t some vertices hanging off the edge

1 Like

Actually I just updated the makegeo project to support making planes of varying sizes (dimensionally, vertex-count-wise, etc.), and you can supply a function to give it a height offset as well.

It’s super simple. It’s in the makegeo project, and it is the testmakequadplane scene.

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo