1

I have an issue where I made a marching cubes algorithm and it works perfectly, up until rendering an arbitrary cube n, where n is dependant on the dimensions of the render area, where any cube after n renders in a seemingly random location.

It works fine on smaller scales. The coordinates of the cubes seem to be fine. The nodes in the picture represent where the polygons should be drawn. Thank you! Picture of the Issue.

Code used for rendering:

 void Update()
    {
        List<Vector3> vertices = new List<Vector3>();   
        List<int> triangles = new List<int>();
        int numCubesRendered = 0;  //keeps track of the number of cubes and their polygons rendered for access
        foreach (Cube cube in createPoints.cubeHandler.cubeList)
        {

            short cubeBinary = CalculateBinaryCubeIndex(cube);  //i.e 1000 0000 0000 1100 has edges 2, 3 and 15 being intersected

            /* Cycles through all the possible edges
             * and adds a triangle wherever is needed
             * based upon the tri table
             */ 
            for (int i = 0; i < 16; i++)
            {
                if (triTable[cubeBinary, i] != -1) //tritable is an array that showcases all the configurations possible
                {
                    //adds the triangles in clockwise order in order to prevent improper face culling
                    triangles.Add(triTable[cubeBinary, i + 2] + (numCubesRendered * 12));
                    triangles.Add(triTable[cubeBinary, i + 1] + (numCubesRendered * 12));
                    triangles.Add(triTable[cubeBinary, i] + (numCubesRendered * 12));
                    i += 2;
                }
                    
                
            }

            //Cycles through all vertices that make up a cube
            for (int j = 0; j < 12; j++)
            {
                //finds the vertices that make up the edge
                int indexA = cornerIndexFromEdge[j, 0];
                int indexB = cornerIndexFromEdge[j, 1];

                Vector3 vertexPos = (cube.vertices[indexA].GetPosition() + cube.vertices[indexB].GetPosition()) / 2;

                vertices.Add(vertexPos);
            }
            ++numCubesRendered;
        }
        drawTriangles(vertices, triangles);
    }

    //Calculates the proper index of the cube configuration by using binary in order to represent all edges as a single digit in a short
    public short CalculateBinaryCubeIndex(Cube cube)
    {
        Vector3[] vertList = new Vector3[12];
        short cubeIndex = 0;
        float surfaceRance = createPoints.surfaceRange;
        if (cube.vertices[0].surfaceLevel >= surfaceRance) cubeIndex |= 1;
        if (cube.vertices[1].surfaceLevel >= surfaceRance) cubeIndex |= 2;
        if (cube.vertices[2].surfaceLevel >= surfaceRance) cubeIndex |= 4;
        if (cube.vertices[3].surfaceLevel >= surfaceRance) cubeIndex |= 8;
        if (cube.vertices[4].surfaceLevel >= surfaceRance) cubeIndex |= 16;
        if (cube.vertices[5].surfaceLevel >= surfaceRance) cubeIndex |= 32;
        if (cube.vertices[6].surfaceLevel >= surfaceRance) cubeIndex |= 64;
        if (cube.vertices[7].surfaceLevel >= surfaceRance) cubeIndex |= 128;

        /* Cube is entirely in/out of the surface */
        if (edgeTable[cubeIndex] == 0)
            return 0;

        return cubeIndex;
    }

   public Mesh drawTriangles(List<Vector3> vertices, List<int> triangles)
    {
        Mesh mesh = new Mesh();
        mesh = GetComponent<MeshFilter>().mesh;

        mesh.vertices = vertices.ToArray();
        mesh.triangles = triangles.ToArray();

        return mesh;
    }
JimMcTim
  • 11
  • 2

0 Answers0