This question was
closed Sep 19, 2017 at 10:14 AM by
Jenny_Wasja for the following reason:
I found the problem: Simple variable mistake!
Question by
Jenny_Wasja · Sep 13, 2017 at 10:51 PM ·
meshverticesindex
Creating Mesh - Vertices out of boundries
Hello everyone o/
I'm to make a program that initializes an Icosphere (A sphere that consists out of triangles that have the same size and distance) from the script, where I can set the number of iterations for the sphere. The first creation of the sphere with 20 triangles works fine. But if I try to add more triangles recursively, I get this error:
Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 240, VertexCount: 42
I'm confused about that: My vertices and my indices range from 0-41 and from 0-239. But there should be no further Vertex at the next position. Has anybody an idea, what I could have done wrong? What do I not see? :/
Here is my code:
public class GenerateIcosphere : MonoBehaviour {
public List<Vector3> newVertices;
public List<Vector2> newUV;
public List<int> newTriangles;
public Material wireframe;
public int recrusionLevel = 0;
private int triLevel = 0;
private Dictionary<Int64, int> middlePointIndexCache;
private Mesh iSphere;
private void createIcosphere()
{
middlePointIndexCache = new Dictionary<long, int>();
float t = (float)(1.0 + Mathf.Sqrt(5.0f) / 2.0f);
newVertices = new List<Vector3> { new Vector3(-1, t, 0), new Vector3(1, t, 0),
new Vector3(-1, -t, 0),new Vector3(1, -t, 0),
new Vector3(0, -1, t), new Vector3(0, 1, t),
new Vector3(0,-1, -t), new Vector3(0, 1, -t),
new Vector3(t, 0, -1), new Vector3(t, 0, 1),
new Vector3(-t,0, -1), new Vector3(-t, 0, 1)};
newTriangles = new List<int> {0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11,
1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7, 1, 8,
3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9,
4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1};
}
bool checkForDifference(int n)
{
if(n > triLevel || n < triLevel)
{
triLevel = n;
return true;
}
return false;
}
void iterateIcosphere()
{
for (int i = 0; i < recrusionLevel; i++)
{
List<int> addTriangles = new List<int> { };
for (int j = 0; j < newTriangles.Count; j = j+3)
{
int a = getMiddlePoint(newTriangles[j], newTriangles[j+1]);
int b = getMiddlePoint(newTriangles[j+1], newTriangles[j+2]);
int c = getMiddlePoint(newTriangles[j+2], newTriangles[j]);
addTriangles.Add(newTriangles[j]);
addTriangles.Add(a);
addTriangles.Add(c);
addTriangles.Add(newTriangles[j+1]);
addTriangles.Add(b);
addTriangles.Add(a);
addTriangles.Add(newTriangles[j+2]);
addTriangles.Add(c);
addTriangles.Add(b);
addTriangles.Add(a);
addTriangles.Add(b);
addTriangles.Add(c);
}
newTriangles = addTriangles;
}
}
private int addVertex(Vector3 p)
{
float length = Mathf.Sqrt(p.x * p.x + p.y * p.y + p.z * p.z);
newVertices.Add(new Vector3(p.x / length, p.y / length, p.z / length));
return newVertices.Count;
}
private int getMiddlePoint(int p1, int p2)
{
//check if the point already exists
bool firstIsSmaller = p1 < p2;
Int64 smallerIndex = firstIsSmaller ? p1 : p2;
Int64 greaterIndex = firstIsSmaller ? p2 : p1;
Int64 key = (smallerIndex << 32) + greaterIndex;
int ret;
if (middlePointIndexCache.TryGetValue(key, out ret))
{
return ret;
}
//if not calculate new point
Vector3 point1 = newVertices[p1];
Vector3 point2 = newVertices[p2];
Vector3 middle = new Vector3(((point1.x + point2.x) /2.0f),
((point1.y + point2.y) / 2.0f),
((point1.z + point2.z) / 2.0f));
int i = addVertex(middle);
middlePointIndexCache.Add(key, i);
return i;
}
void Start () {
createIcosphere();
iSphere = new Mesh();
gameObject.AddComponent<MeshFilter>().mesh = iSphere;
iSphere.vertices = newVertices.ToArray();
iSphere.uv = newUV.ToArray();
iSphere.triangles = newTriangles.ToArray();
iSphere.RecalculateNormals();
gameObject.AddComponent<MeshRenderer>();
gameObject.GetComponent<MeshRenderer>().material = wireframe;
}
// Update is called once per frame
void Update () {
if (checkForDifference(recrusionLevel))
{
iterateIcosphere();
iterateUVs();
iSphere = new Mesh();
gameObject.GetComponent<MeshFilter>().mesh = iSphere;
iSphere.Clear();
iSphere.vertices = newVertices.ToArray();
iSphere.uv = newUV.ToArray();
iSphere.triangles = newTriangles.ToArray();
iSphere.RecalculateNormals();
}
}
Comment