public void Awake() { Mesh mesh = new Mesh(); this.meshFilter = this.GetComponent <MeshFilter>(); this.meshFilter.mesh = mesh; HexCorners hex = HexCorners.Create(Vector3.zero, Vector3.up, this.edgeLength); this.vertices = new Vector3[hex.Length]; this.triangles = new int[12]; this.texCoords = new Vector2[6]; for (int vertexIdx = 0; vertexIdx < this.vertices.Length; vertexIdx++) { this.vertices[vertexIdx] = hex[vertexIdx]; // TODO: assign sane texture coordinates. this.texCoords[vertexIdx] = Vector2.zero; } for (int triangleIdx = 0; triangleIdx < 4; triangleIdx++) { this.triangles[triangleIdx * 3] = 0; this.triangles[triangleIdx * 3 + 1] = triangleIdx + 1; this.triangles[triangleIdx * 3 + 2] = triangleIdx + 2; } mesh.vertices = this.vertices; mesh.triangles = this.triangles; mesh.uv = this.texCoords; }
public void Awake() { HexCorners centerHex = HexCorners.Create(Vector3.zero, Vector3.up, this.edgeLength); HexTile[] hexes = new HexTile[this.hexCount]; float centerToCenterDistance = 2 * this.edgeLength * Mathf.Cos(Mathf.Deg2Rad * 30f); for (int hexIdx = 0; hexIdx < this.hexCount; hexIdx++) { HexCorners hex; if (hexIdx == 0) { hex = centerHex; } else { Vector3 direction = centerHex.GetNeighborDirection((hexIdx - 1) % 6); Vector3 center = direction * (centerToCenterDistance + this.space); hex = HexCorners.Create(center, Vector3.up, this.edgeLength); } hexes[hexIdx] = new HexTile(hex); } this.GetComponent <HexGridMesh>().GenerateMesh(hexes); }
public void RebuildGrid() { float sphereRadius = this.edgeLength * 3; // TODO: compute this. HexCorners top = HexCorners.Create(Vector3.up * sphereRadius, Vector3.up, this.edgeLength); HexCorners bottom = HexCorners.Create(Vector3.down * sphereRadius, Vector3.down, this.edgeLength); // for now we know it's 8 tiles around HexTile[] hexes = new HexTile[1 + 6 + 8 + 6 + 1]; float anglePerLayer = 360f / 5; hexes[0] = new HexTile(top); int tileIdx = 1; for (int i = 0; i < 6; i++) { float anglePerTile = 360f / 6; Vector3 center = Quaternion.Euler(0f, anglePerTile * i, 180f - (anglePerLayer * 1f)) * Vector3.right * sphereRadius; hexes[tileIdx] = new HexTile(HexCorners.Create(center, center.normalized, this.edgeLength)); tileIdx++; } for (int i = 0; i < 8; i++) { float anglePerTile = 360f / 8; Vector3 center = Quaternion.Euler(0f, anglePerTile * i, 0f) * Vector3.right * sphereRadius; hexes[tileIdx] = new HexTile(HexCorners.Create(center, center.normalized, this.edgeLength)); tileIdx++; } for (int i = 0; i < 6; i++) { float anglePerTile = 360f / 6; Vector3 center = Quaternion.Euler(0f, anglePerTile * i, 180f - (anglePerLayer * 4f)) * Vector3.right * sphereRadius; hexes[tileIdx] = new HexTile(HexCorners.Create(center, center.normalized, this.edgeLength)); tileIdx++; } hexes[tileIdx] = new HexTile(bottom); this.GetComponent <HexGridMesh>().GenerateMesh(hexes); }
public HexTile(HexCorners _corners) { this.corners = _corners; }