Problems with creating mesh with script

hi

Im trying to learn how to create meshes with scripts
in this example i was trying to create a dome mesh and apply a texure to it.
I build the the following code from scratch

my problems:

  1. for some values of d the sphere gets only partialy generated.

  2. the generated uv map isn’t working.

Im a c# beginner and i’ve got no idea why the script is not working

please help me

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class halbkugel : MonoBehaviour {

    public int d = 2;
    public int r = 10;

    void Start () {

        Mesh mesh = GetComponent<MeshFilter>().mesh;

        int w = 180 / d;

        Vector3[] v = new Vector3[(d + 1) * (d + 1)];

        for (int i = 0; i <= d; i++)
        {
            float s = Mathf.Sin(w * i * Mathf.PI / 180) * r;
            for (int j = 0; j <= d; j++)
            {
                v[(i * (d + 1)) + j] = new Vector3(
                Mathf.Cos(w * j * Mathf.PI / 180) * s,
                Mathf.Cos(w * i * Mathf.PI / 180) * r,
                Mathf.Sin(w * j * Mathf.PI / 180) * s
                );

            }
        }

        int[] t = new int[d * d * 6];

        for (int i = 0; i < d; i++)
        {
            for (int j = 0; j < d; j++)
            {
                int a = (i * d) + j;
                int b = (i * (d + 1)) + j;

                t[(a * 6) + 0] = b;
                t[(a * 6) + 1] = b + (d + 1);
                t[(a * 6) + 2] = b + (d + 2);
                t[(a * 6) + 3] = b + (d + 2);
                t[(a * 6) + 4] = b + 1;
                t[(a * 6) + 5] = b;
            }
        }

        Vector2[] u = new Vector2[(d + 1) * (d + 1)];

        for (int i = 0; i <= d; i++)
        {
            for (int j = 0; j <= d; j++)
            {
                u[(i * (d + 1)) + j] = new Vector2(j * (1 / d), i * (1 / d));
            }
        }

        mesh.Clear();
        mesh.vertices = v;
        mesh.triangles = t;
        mesh.uv = u;
        mesh.RecalculateNormals();

    }

}

I have a little “makegeo” repository I started that has some procedural geometry generation examples in it. I intend to grow it over time, but it already has a sphere (with UV) maker in it.

You can find all the source here and sorta see how I did it, compare it to yours:

https://bitbucket.org/kurtdekker/makegeo

or here:

If I had to guess, your code might not be winding all the triangles correctly: some may be degenerate (two or three identical verts), the triangle facing the wrong way, etc.

1 Like

I looked at you code but its way to complex for me :frowning:

here is an image of my generated dome
I always get the same error

The code you posted does not modify or interact with the MeshRenderer, so I presume you are either adding that or recycling another object and changing its mesh. The error above is pretty literal: the MeshRenderer on your object has more materials than your mesh has submeshes. Generally, submeshes map to materials. By using the Mesh.triangles shortcut property, you will only have one submesh. If you want to have multiple submeshes (each one would refer to a separate material in the renderer), you use the Mesh.SetTriangles() call instead, to set each submesh’s triangles.

Relevant documentation:

and

You are using the latter, but your MeshRenderer appears to be expecting multiple submeshes because it has multiple materials.