Code to make octahedron and then make sphere breaks

My code creates a sphere mesh from a octahedron mesh it also creates. However it stops working when I increase the resolution from 126 to 127. This breaking gets worse as you increase the resolution is increased further. Maybe something is broken before 126, but I dont see anything wrong with the mesh.

The resolution should be as follows. Resolution 0 is a octahedron consisting of 6 vertices and 8 triangles.
Resoltion 1 means on each edge one more vertex is added, which leads to a octahedron with 18 vertices and 24 triangles. I made a crude picture of one side of the octahedron to hopefully clarify this.

My question is, why does it break at a resolution of 127? I dont necessarily want to know how to fix it, unless it is a complicated fix. If it is not too hard I would like to try and find it myself, but if the fix is complicated than please tell me how to fix it.

Please excuse the code for not being the shortest or cleanest, this is one of my first projects. Here is the code:

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEditor.Search;
using UnityEngine;

[RequireComponent(typeof(MeshFilter))]
public class Meshmaker : MonoBehaviour
{
    Mesh mesh;
    Vector3[] vertices;
    int[] triangles;
    public float radius = 1;
    public int resolution = 0;
    // Start is called before the first frame update
    void Start()
    {
        mesh = new Mesh();
        GetComponent<MeshFilter>().mesh = mesh;

        CreateShape();
    }

    // Update is called once per frame
    void Update()
    {

    }
    void CreateShape()
    {
        int totalvert = CalcTotalVert(resolution);
        Vector3[] vertices = new Vector3[totalvert];
        vertices = CalcPoints(resolution, vertices, radius);

        int totaltri = CalcTotalTri(resolution) * 3;
        int[] triangles = new int[totaltri];
        triangles = CalcTri(resolution, triangles);

        UpdateMesh(vertices, triangles);
    }
    void UpdateMesh(Vector3[] vertices, int[] triangles)
    {
        mesh.Clear();
        mesh.vertices = vertices;
        mesh.triangles = triangles;

        mesh.RecalculateNormals();
    }

    Vector3 RotVec(Vector3 input)
    {
        return Quaternion.AngleAxis(90, Vector3.up) * input;
    }
    int CalcTotalVert(int res)
    {
        int p = 0;
        int Vert = 2;
        while (p < res + 1)
        {
            p += 1;
            Vert += p * 8;
            if (p == res + 1)
            {
                Vert -= p * 4;
            }
        }
        return Vert;
    }
    int CalcTotalTri(int res)
    {
        int p = 0;
        int tri = 0;
        while (p < res + 1)
        {
            tri += (1 + 2 * p) * 8;
            p += 1;
        }
        return tri;
    }
    Vector3[] CalcPoints(int res, Vector3[] vert, float radius)
    {
        int p = 0;
        float dist = 1f / (Mathf.Sqrt(2) * (res + 1));
        float hight = 1f / (res + 1);
        int currentvert = 0;
        while (p < res + 2)
        {
            float spacing = dist * 2f;
            float layhight = hight * (res + 1 - p);
            if (p == 0)
            {
                vert[currentvert] = new Vector3(0, 1, 0);
                currentvert += 1;
                p += 1;
            }
            else
            {
                int n = 0;
                while (n < p)
                {
                    vert[currentvert] = new Vector3(spacing * p, layhight, spacing * p - spacing * 2 * n);
                    currentvert += 1;
                    n += 1;
                }
                int k = 0;
                while (k < p * 3)
                {
                    vert[currentvert] = RotVec(vert[currentvert - p]);
                    currentvert += 1;
                    k += 1;
                }
                p += 1;

            }
        }
        int c = 0;
        int totalvert = CalcTotalVert(res);
        int othervert = 0;
        while (c < res + 1)
        {
            if (c == 0)
            {
                vert[totalvert - 1] = vert[c] - 2 * Vector3.Dot(vert[c], Vector3.up) * vert[c];
                othervert += 1;
            }
            else
            {
                int a = 0;
                while (a < 4 * c)
                {
                    int v = CalcVert(c + 1) - 1 - a;
                    vert[totalvert - othervert - 1] = vert[v] - 2 * Vector3.Dot(vert[v], Vector3.up) * Vector3.up;
                    othervert += 1;
                    a += 1;
                }
            }
            c += 1;
        }

        int t = 0;
        while(t < totalvert)
        {
            vert[t] = vert[t].normalized * radius;
            t += 1;
        }
        return vert;
    }

    int[] CalcTri(int res, int[] tri)
    {
        int n = 0;
        int trin = 0;
        while (n < res + 1)
        {
            int currenttop = CalcVert(n);
            int currentbot = CalcVert(n + 1);
            int nextbot = CalcVert(n + 2);
            int k = 0;
            while (k < 1 + n * 2)
            {
                if(k % 2 == 0)
                {
                    tri[trin] = currenttop;
                    tri[trin + 1] = currentbot;
                    tri[trin + 2] = currentbot + 1;
                    currentbot += 1;
                    trin += 3;

                    tri[trin] = tri[trin - 3] + n;
                    tri[trin + 1] = tri[trin - 2] + n + 1;
                    tri[trin + 2] = tri[trin - 1] + n + 1;
                    trin += 3;

                    tri[trin] = tri[trin - 3] + n;
                    tri[trin + 1] = tri[trin - 2] + n + 1;
                    tri[trin + 2] = tri[trin - 1] + n + 1;
                    trin += 3;

                    if (tri[trin - 3] + n >= CalcVert(n + 1))
                    {
                        tri[trin] = CalcVert(n);
                    }
                    else
                    {
                        tri[trin] = tri[trin - 3] + n;
                    }
                    tri[trin + 1] = tri[trin - 2] + n + 1;
                    if (tri[trin - 1] + n + 1 >= nextbot)
                    {
                        tri[trin + 2] = CalcVert(n + 1);
                    }
                    else
                    {
                        tri[trin + 2] = tri[trin - 1] + n + 1;
                    }
                    trin += 3;
                }
                else
                {
                    tri[trin] = currenttop;
                    tri[trin + 1] = currentbot;
                    tri[trin + 2] = currenttop + 1;
                    currenttop += 1;
                    trin += 3;

                    tri[trin] = tri[trin - 3] + n;
                    tri[trin + 1] = tri[trin - 2] + n + 1;
                    tri[trin + 2] = tri[trin - 1] + n;
                    trin += 3;

                    tri[trin] = tri[trin - 3] + n;
                    tri[trin + 1] = tri[trin - 2] + n + 1;
                    tri[trin + 2] = tri[trin - 1] + n;
                    trin += 3;

                    tri[trin] = tri[trin - 3] + n;
                    tri[trin + 1] = tri[trin - 2] + n + 1;
                    if (tri[trin - 1] + n >= CalcVert(n + 1))
                    {
                        tri[trin + 2] = CalcVert(n);
                    }
                    else
                    {
                        tri[trin + 2] = tri[trin - 1] + n;
                    }
                    trin += 3;

                }
                k += 1;
            }
            n += 1;
        }
        int c = 0;
        while (c < CalcTotalTri(res) * 3 / 2)
        {
            tri[CalcTotalTri(res) * 3 / 2 + c] = CalcTotalVert(res) - tri[c] - 1;
            c += 1;
        }
        return tri;
    }

    int CalcVert(int lay)
    {
        int p = 0;
        int vert = 0;
        while (p < lay)
        {
            if (p == 0)
            {
                vert += 1;
            }
            else
            {
                vert += 4 * p;
            }
            p += 1;
        }
        return vert;
    }

    

}

At a resolution of 127 the mesh will require more than the maximum number of vertices. The maximum is 65535.

You can increase the maximum with:

mesh.indexFormat=UnityEngine.Rendering.IndexFormat.UInt32;
1 Like

That fixed it, thanks a lot.