SHARE
TWEET

Untitled

a guest Jun 12th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public void createWorld()
  2.     {
  3.         world = new GameObject();
  4.         world.transform.localScale = new Vector3(scale, scale, scale);
  5.         filter = world.AddComponent<MeshFilter>();
  6.        
  7.         meshRenderer = world.AddComponent<MeshRenderer>();
  8.         meshRenderer.material = mat;
  9.         hexGen = world.AddComponent<HexGridSphere>();
  10.  
  11.     }
  12.     void Start()
  13.     {
  14.         createWorld();
  15.         updateMesh(recursionLevel);
  16.     }
  17.  
  18.     public void updateMesh(float recursionLevel)
  19.     {
  20.         int i = Mathf.RoundToInt(recursionLevel);
  21.         filter.mesh = hexGen.fullMesh(i);
  22.         filter.mesh.RecalculateNormals();
  23.     }
  24.  
  25.  
  26.  
  27.     public Mesh fullMesh (int subdivision)
  28.     {
  29.         return meshFromHexMesh(hexSphere(icosphere(subdivision)));
  30.     }
  31.  
  32.     public Mesh meshFromHexMesh(hexMesh hex)
  33.     {
  34.         Vector3[] verts = hex.vertices.ToArray();
  35.         Mesh mesh = new Mesh();
  36.         List<int> poly;
  37.         List<triIndex> tris = new List<triIndex>();
  38.         for (int i = 0; i < hex.polys.Count; i++)
  39.         {
  40.             poly = hex.polys[i];
  41.             poly = sortPoly(poly, hex.vertices);
  42.             if (poly.Count == 6)
  43.             {
  44.                 tris.Add(new triIndex(poly[0], poly[1], poly[2]));
  45.                 tris.Add(new triIndex(poly[2], poly[3], poly[4]));
  46.                 tris.Add(new triIndex(poly[4], poly[5], poly[0]));
  47.                 tris.Add(new triIndex(poly[0], poly[2], poly[4]));
  48.                 drawFromTriIndex(tris[tris.Count - 1], verts);
  49.                 drawFromTriIndex(tris[tris.Count - 2], verts);
  50.                 drawFromTriIndex(tris[tris.Count - 3], verts);
  51.                 drawFromTriIndex(tris[tris.Count - 4], verts);
  52.             }else
  53.             {
  54.                 tris.Add(new triIndex(poly[0], poly[1], poly[2]));
  55.                 tris.Add(new triIndex(poly[2], poly[3], poly[4]));
  56.                 tris.Add(new triIndex(poly[0], poly[2], poly[4]));
  57.                 drawFromTriIndex(tris[tris.Count - 1], verts);
  58.                 drawFromTriIndex(tris[tris.Count - 2], verts);
  59.                 drawFromTriIndex(tris[tris.Count - 3], verts);
  60.             }
  61.         }
  62.         mesh.vertices = hex.vertices.ToArray();
  63.         mesh.triangles = new int[tris.Count * 3];
  64.         for (int i = 0; i < tris.Count * 3; i += 3)
  65.         {
  66.             mesh.triangles[i + 0] = tris[i / 3].v1;
  67.             mesh.triangles[i + 1] = tris[i / 3].v2;
  68.             mesh.triangles[i + 2] = tris[i / 3].v3;
  69.         }
  70.         Vector3[] normals = verts;
  71.  
  72.         mesh.normals = normals;
  73.         mesh.RecalculateTangents();
  74.         mesh.RecalculateBounds();
  75.         mesh.RecalculateNormals();
  76.         return mesh;
  77.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top