public void RenderSphere(Transform parentObject, Material outlineHex, Material outlinePent) { int sphereLayer = LayerMask.NameToLayer("Sphere");; foreach (SCoord tile in hexSphere.Coordinates) { // Setup new game object with generated mesh GameObject newTile = new GameObject(); MeshFilter mf = newTile.AddComponent <MeshFilter>(); MeshRenderer mr = newTile.AddComponent <MeshRenderer>(); MeshCollider mc = newTile.AddComponent <MeshCollider>(); HexIdentifier hider = newTile.AddComponent <HexIdentifier>(); //GameObjectUtility.SetStaticEditorFlags(newTile, //StaticEditorFlags.OccludeeStatic | StaticEditorFlags.OccluderStatic); // Set the standard material shader mr.material = new Material(Shader.Find("Diffuse")); // Make the mesh and render the tile RenderTile(mf.mesh, tile, hexSphere); // Move the tile to its new position and rotation newTile.transform.position += parentObject.transform.position; newTile.transform.Rotate(parentObject.transform.eulerAngles); // set hierarchy relationship newTile.transform.SetParent(parentObject); // Set Name of the tile newTile.name = "Lat " + Mathf.Round(tile.GetTheta() * Mathf.Rad2Deg * 100) / 100 + " Lon " + Mathf.Round(tile.GetPhi() * Mathf.Rad2Deg * 100) / 100; tileMap[tile] = newTile; hider.center = hexSphere.GetPoint(tile); hider.location = tile; CameraHider.AddObject(hider); newTile.layer = sphereLayer; if (hexSphere.GetDegree(tile) == 5) { mr.material = outlinePent; } else if (hexSphere.GetDegree(tile) == 6) { mr.material = outlineHex; } mc.sharedMesh = mf.mesh; } }
// Start is called before the first frame update void Start() { // Make the icosphere sphere = new Icosphere(transform.position, radius); for (int sub = 0; sub < subdivsions; sub++) { sphere = sphere.SubdivideSphere(); } // Make the faces of the icosphere foreach (SCoord coord in sphere.Coordinates) { // Get the 3d coordinate of the sphere Vector3 point = sphere.GetPoint(coord); // Get the rotation of the face to make it tangent to the sphere Vector3 rotation = SCoord.GetRotation(coord); // Get the kind of object GameObject newObj = null; int degree = sphere.GetDegree(coord); if (degree == 5) { newObj = Instantiate(pentagonPrefab); } else { newObj = Instantiate(hexagonPrefab); } // Place and rotate object tangent to the sphere newObj.transform.eulerAngles = rotation; newObj.transform.position = point; newObj.transform.SetParent(this.transform); // Rotate the face to be line up correclty IEnumerator <SCoord> neighbors = sphere.GetNeighbors(coord).GetEnumerator(); neighbors.MoveNext(); SCoord neighbor = neighbors.Current; Vector3 targetVec = Vector3.ProjectOnPlane(neighbor.ToEuclidian() - coord.ToEuclidian(), newObj.transform.up).normalized; float angle = Mathf.Acos(Vector3.Dot(newObj.transform.right.normalized, targetVec)); if (float.IsNaN(angle)) { angle = 180; } Vector3 cross = Vector3.Cross(newObj.transform.right.normalized, targetVec); if (Vector3.Dot(newObj.transform.up.normalized, cross) < 0) { angle *= -1; } angle *= 180 / Mathf.PI; Debug.Log(angle); newObj.transform.Rotate(0, angle, 0, Space.Self); tiles.Add(coord, newObj); } }