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:
-
for some values of d the sphere gets only partialy generated.
-
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();
}
}