Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void createWorld()
- {
- world = new GameObject();
- world.transform.localScale = new Vector3(scale, scale, scale);
- filter = world.AddComponent<MeshFilter>();
- meshRenderer = world.AddComponent<MeshRenderer>();
- meshRenderer.material = mat;
- hexGen = world.AddComponent<HexGridSphere>();
- }
- void Start()
- {
- createWorld();
- updateMesh(recursionLevel);
- }
- public void updateMesh(float recursionLevel)
- {
- int i = Mathf.RoundToInt(recursionLevel);
- filter.mesh = hexGen.fullMesh(i);
- filter.mesh.RecalculateNormals();
- }
- public Mesh fullMesh (int subdivision)
- {
- return meshFromHexMesh(hexSphere(icosphere(subdivision)));
- }
- public Mesh meshFromHexMesh(hexMesh hex)
- {
- Vector3[] verts = hex.vertices.ToArray();
- Mesh mesh = new Mesh();
- List<int> poly;
- List<triIndex> tris = new List<triIndex>();
- for (int i = 0; i < hex.polys.Count; i++)
- {
- poly = hex.polys[i];
- poly = sortPoly(poly, hex.vertices);
- if (poly.Count == 6)
- {
- tris.Add(new triIndex(poly[0], poly[1], poly[2]));
- tris.Add(new triIndex(poly[2], poly[3], poly[4]));
- tris.Add(new triIndex(poly[4], poly[5], poly[0]));
- tris.Add(new triIndex(poly[0], poly[2], poly[4]));
- drawFromTriIndex(tris[tris.Count - 1], verts);
- drawFromTriIndex(tris[tris.Count - 2], verts);
- drawFromTriIndex(tris[tris.Count - 3], verts);
- drawFromTriIndex(tris[tris.Count - 4], verts);
- }else
- {
- tris.Add(new triIndex(poly[0], poly[1], poly[2]));
- tris.Add(new triIndex(poly[2], poly[3], poly[4]));
- tris.Add(new triIndex(poly[0], poly[2], poly[4]));
- drawFromTriIndex(tris[tris.Count - 1], verts);
- drawFromTriIndex(tris[tris.Count - 2], verts);
- drawFromTriIndex(tris[tris.Count - 3], verts);
- }
- }
- mesh.vertices = hex.vertices.ToArray();
- mesh.triangles = new int[tris.Count * 3];
- for (int i = 0; i < tris.Count * 3; i += 3)
- {
- mesh.triangles[i + 0] = tris[i / 3].v1;
- mesh.triangles[i + 1] = tris[i / 3].v2;
- mesh.triangles[i + 2] = tris[i / 3].v3;
- }
- Vector3[] normals = verts;
- mesh.normals = normals;
- mesh.RecalculateTangents();
- mesh.RecalculateBounds();
- mesh.RecalculateNormals();
- return mesh;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement