- Home /
Unity subdividing icosohedron script problem
So I wrote this script to subdivide an icosohedron, but when I try to execute it unity crashes. Here is the code
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
[RequireComponent(typeof(MeshRenderer)), RequireComponent(typeof(MeshFilter))]
public class Icosohedron : MonoBehaviour {
public int recursionLevel;
private float t = (1 + Mathf.Sqrt(5)) / 2;
private MeshFilter mf;
// Use this for initialization
void Start () {
#region
List<Vector3> verticies = new List<Vector3>();
Vector3 aa = new Vector3(-1, t, 0);
Vector3 ab = new Vector3(1, t, 0);
Vector3 ac = new Vector3(-1, -t, 0);
Vector3 ad = new Vector3(1, -t, 0);
Vector3 ae = new Vector3(0, -1, t);
Vector3 af = new Vector3(0, 1, t);
Vector3 ag = new Vector3(0, -1, -t);
Vector3 ah = new Vector3(0, 1, -t);
Vector3 ai = new Vector3(t, 0, -1);
Vector3 aj = new Vector3(t, 0, 1);
Vector3 ak = new Vector3(-t, 0, -1);
Vector3 al = new Vector3(-t, 0, 1);
verticies.Add(aa);
verticies.Add(ab);
verticies.Add(ac);
verticies.Add(ad);
verticies.Add(ae);
verticies.Add(af);
verticies.Add(ag);
verticies.Add(ah);
verticies.Add(ai);
verticies.Add(aj);
verticies.Add(ak);
verticies.Add(al);
List<int> triangles = new List<int>();
triangles.AddRange(new 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
});
#endregion
mf = GetComponent<MeshFilter>();
mf.mesh = CreateIcosohedron(verticies, triangles);
print(mf.mesh.triangles.Length);
print(mf.mesh.vertices.Length);
for (int tri = 0; tri < triangles.Count/3; tri+=3)
{
Vector3 tempV1 = new Vector3();
Vector3 tempV2 = new Vector3();
Vector3 tempV3 = new Vector3();
Vector3 NewVS12 = new Vector3();
Vector3 NewVS23 = new Vector3();
Vector3 NewVS13 = new Vector3();
int[] TrianglesArray = triangles.ToArray();
Vector3[] VertsArray = verticies.ToArray();
tempV1 = VertsArray[tri];
tempV2 = VertsArray[tri + 1];
tempV3 = VertsArray[tri + 2];
triangles.Remove(0);
triangles.Remove(1);
triangles.Remove(2);
NewVS12 = Vector3.Slerp(tempV1, tempV2, 0.5f);
NewVS23 = Vector3.Slerp(tempV2, tempV3, 0.5f);
NewVS13 = Vector3.Slerp(tempV1, tempV3, 0.5f);
verticies.Add(NewVS12);
verticies.Add(NewVS23);
verticies.Add(NewVS13);
int VertNum1 = verticies.Count-2; //12
int VertNum2 = verticies.Count - 1; //23
int VertNum3 = verticies.Count; //13
triangles.Add(VertNum1);
triangles.Add(VertNum2); //Middle triangle
triangles.Add(VertNum3);
triangles.Add(VertNum1);
triangles.Add(tri); // adds 12
triangles.Add(tri + 1);
triangles.Add(VertNum2);
triangles.Add(tri + 2); // adds 23
triangles.Add(tri + 1);
triangles.Add(VertNum3);
triangles.Add(tri); // adds 13
triangles.Add(tri + 2);
}
/* Cycle through each triangle /
turn lists into arrays /
store old verts /
Delete the triangle /
make new verticies /
Slerp verticies /
Add Verts to list /
Get what number the verticies are /
Make 4 triangles ?
*/
mf.mesh = CreateIcosohedron(verticies, triangles);
}
Mesh CreateIcosohedron(List<Vector3> Verts, List<int> triangles)
{
Mesh m;
m = new Mesh();
float t = (1 + Mathf.Sqrt(5)) / 2;
Vector3[] array = new Vector3[99999];
array = Verts.ToArray();
m.vertices = array;
int[] arrayt = new int[99999];
arrayt = triangles.ToArray();
m.triangles = arrayt;
return m;
}
}
Comment
Best Answer
Answer by tanoshimi · May 06, 2016 at 02:40 PM
That's because you have an infinite loop:
for (int tri = 0; tri < triangles.Count/3; tri+=3)
In the body of that loop you're removing 3 elements from triangles, but adding 12 new ones, so your counter is never going to reach triangles.Count/3.
Your answer
Follow this Question
Related Questions
Find closest point in triangle to point? 1 Answer
How do I get the color of a certain mesh vertex? 0 Answers
Sketchup models import with 0 verts/tris? 0 Answers
Best way to optimize mesh updating through code? 1 Answer
Connecting flatshaded vertices 0 Answers