procedural generation – Splitting the mesh of a plane into submeshes


I am struggling a bit with this. I am trying to split a plane recursively into subplanes, so that frustum culling is more efficient. I am doing that by dividing the indices into the 4 subplanes for every iteration. The Problem: I do not understand why it is working perfectly for two submeshes, but the other two are …something.

First iteration working as intended:
first iteration

Second iteration only working for submeshes 2 and 4, but their no.1 and 3 submeshes are not reproducing what is seen for no. 1 and 3 of iteration 1:

second iteration

Code:

Generating the plane:

private void GeneratePlane()
    {

        int indexpointer = 0;

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {

                int i = x + y * resolution;

                Vector2 percent = new Vector2(x, y) / (resolution - 1);

                Vector3 cubeVertex = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB;

                vertices[i] = cubeVertex;

                if (x != resolution - 1 && y != resolution - 1)
                {
                    indices[indexpointer] = i;
                    indices[indexpointer + 1] = i + resolution + 1;
                    indices[indexpointer + 2] = i + resolution;

                    indices[indexpointer + 3] = i;
                    indices[indexpointer + 4] = i + 1;
                    indices[indexpointer + 5] = i + resolution + 1;
                    indexpointer += 6;
                }
            }
        }

    }

Splitting the indices:

private void SubdividePlane(int[] indices, int subdivisionRecursions, int resolution)
    {
        if (subdivisionRecursions == 0 || indices.Length <= 6)
        {
           subdividedIndices.Add(indices);
           return;
        }
        else
        {

            List<int> sub1I = new List<int>();
            List<int> sub2I = new List<int>();
            List<int> sub3I = new List<int>();
            List<int> sub4I = new List<int>();



            int indexPointer = 0;
            for (int y = 0; y < resolution-1; y++)
            {
                for (int x = 0; x < resolution-1; x++)
                {
                    int pointer = x + y * resolution;

                    int[] vertexIndices = new int[6];
                    for (int i=0;i<6;i++){
                        vertexIndices[i] = indices[i+indexPointer];
                    }
                    indexPointer+=6;

                  
                    if (x <=(resolution / 2) - 1 && y <= (resolution / 2)-1)
                    {
                        sub1I.AddRange(vertexIndices);

                    }
                    else if (x >= (resolution / 2)  && y <= (resolution / 2)-1 )
                    {
                        sub2I.AddRange(vertexIndices);

                    }
                    else if (x <= (resolution / 2)-1  && y >= (resolution / 2) )
                    {
                        sub3I.AddRange(vertexIndices);

                    }
                    else if (x >= (resolution / 2) && y >= (resolution / 2))
                    {
                        sub4I.AddRange(vertexIndices);

                    }
                }
            }


            SubdividePlane(sub1I.ToArray(), subdivisionRecursions - 1, resolution / 2);
            SubdividePlane(sub2I.ToArray(), subdivisionRecursions - 1, resolution / 2);
            SubdividePlane(sub3I.ToArray(), subdivisionRecursions - 1, resolution / 2);
            SubdividePlane(sub4I.ToArray(), subdivisionRecursions - 1, resolution / 2);  
        }
    }

Could be simply be due to the way I am setting the split-thresholds in the if-conditions?



Source link

More To Explore

Share on facebook
Share on twitter
Share on linkedin
Share on email