コード例 #1
0
    void Triangulate(HexDirection direction, HexCell cell)
    {
        bool    isDeep = cell.Elevation == ElevationLevel.Deep ? true : false;
        Vector3 center = cell.transform.localPosition;
        Vector3 v1     = center + HexMetrics.GetFirstSolidCorner(direction, isDeep);
        Vector3 v2     = center + HexMetrics.GetSecondSolidCorner(direction, isDeep);

        //Center Tris
        terrain.AddTriangle(center, v1, v2);
        terrain.AddTriangleColor(cell.Color);

        Vector3 edge   = HexMetrics.GetEdge(direction, isDeep);
        Vector3 tempV3 = center + HexMetrics.GetFirstEdgeCorner(direction, isDeep);
        Vector3 tempV4 = center + HexMetrics.GetSecondEdgeCorner(direction, isDeep);
        Vector3 v3     = tempV3 + edge;
        Vector3 v4     = tempV4 + edge;

        HexCell prevNeighbor = cell.GetNeighbor(direction.Previous()) ?? cell;
        HexCell neighbor     = cell.GetNeighbor(direction) ?? cell;
        HexCell nextNeighbor = cell.GetNeighbor(direction.Next()) ?? cell;

        //Edge Elevation
        if (cell.Elevation < neighbor.Elevation)
        {
            v3.y = v4.y = neighbor.Position.y;
        }

        //Edge Terrace Quads & Quads
        if (isDeep && neighbor.Elevation == ElevationLevel.Flat)
        {
            TriangulateEdgeTerraces(v1, v2, cell, v3, v4, neighbor);
        }
        else if (isDeep && neighbor.Elevation != ElevationLevel.Flat &&
                 cell.Elevation < neighbor.Elevation)
        {
            terrain.AddQuad(v1, v2, v3, v4);
            terrain.AddQuadColor(neighbor.Color);
        }
        else
        {
            terrain.AddQuad(v1, v2, v3, v4);
            terrain.AddQuadColor(cell.Color);
        }

        Vector3 v5 = center + HexMetrics.GetFirstCorner(direction);
        Vector3 v6 = center + HexMetrics.GetSecondCorner(direction);

        //Vertice Elevation Change
        if (cell.Elevation < neighbor.Elevation)
        {
            v5.y = v6.y = neighbor.Position.y;
        }
        if (cell.Elevation < nextNeighbor.Elevation)
        {
            if (nextNeighbor.Elevation < neighbor.Elevation)
            {
                v6.y = neighbor.Position.y;
            }
            else
            {
                v6.y = nextNeighbor.Position.y;
            }
        }
        if (cell.Elevation < prevNeighbor.Elevation)
        {
            if (prevNeighbor.Elevation < neighbor.Elevation)
            {
                v5.y = neighbor.Position.y;
            }
            else
            {
                v5.y = prevNeighbor.Position.y;
            }
        }

        //Corner Tris & Corner Terrace Tris
        if (isDeep)
        {
            if (neighbor.Elevation == ElevationLevel.Flat)
            {
                TriangulateCornerTerraces(v1, cell, v5, v3, neighbor);
                TriangulateCornerTerraces(v2, cell, v4, v6, neighbor);
            }
            else if (cell.Elevation == neighbor.Elevation)
            {
                if (nextNeighbor.Elevation == ElevationLevel.Flat &&
                    prevNeighbor.Elevation == ElevationLevel.Flat)
                {
                    TriangulateOuterCornerTerraces(v5, cell, v3, v1, prevNeighbor);
                    TriangulateOuterCornerTerraces(v6, cell, v2, v4, nextNeighbor);
                }
                else if (nextNeighbor.Elevation == ElevationLevel.Flat)
                {
                    TriangulateOuterCornerTerraces(v6, cell, v2, v4, nextNeighbor);
                    terrain.AddTriangle(v1, v5, v3);
                    if (cell.Elevation < prevNeighbor.Elevation)
                    {
                        terrain.AddTriangleColor(prevNeighbor.Color);
                    }
                    else
                    {
                        terrain.AddTriangleColor(cell.Color);
                    }
                }
                else if (prevNeighbor.Elevation == ElevationLevel.Flat)
                {
                    TriangulateOuterCornerTerraces(v5, cell, v3, v1, prevNeighbor);
                    terrain.AddTriangle(v2, v4, v6);
                    if (cell.Elevation < nextNeighbor.Elevation)
                    {
                        terrain.AddTriangleColor(nextNeighbor.Color);
                    }
                    else
                    {
                        terrain.AddTriangleColor(cell.Color);
                    }
                }
                else if (cell.Elevation < nextNeighbor.Elevation &&
                         cell.Elevation < prevNeighbor.Elevation)
                {
                    terrain.AddTriangle(v1, v5, v3);
                    terrain.AddTriangleColor(prevNeighbor.Color);
                    terrain.AddTriangle(v2, v4, v6);
                    terrain.AddTriangleColor(nextNeighbor.Color);
                }
                else if (cell.Elevation < nextNeighbor.Elevation)
                {
                    terrain.AddTriangle(v1, v5, v3);
                    terrain.AddTriangleColor(cell.Color);
                    terrain.AddTriangle(v2, v4, v6);
                    terrain.AddTriangleColor(nextNeighbor.Color);
                }
                else if (cell.Elevation < prevNeighbor.Elevation)
                {
                    terrain.AddTriangle(v1, v5, v3);
                    terrain.AddTriangleColor(prevNeighbor.Color);
                    terrain.AddTriangle(v2, v4, v6);
                    terrain.AddTriangleColor(cell.Color);
                }
                else
                {
                    terrain.AddTriangle(v1, v5, v3);
                    terrain.AddTriangleColor(cell.Color);
                    terrain.AddTriangle(v2, v4, v6);
                    terrain.AddTriangleColor(cell.Color);
                }
            }
            else
            {
                if (nextNeighbor.Elevation == ElevationLevel.Flat &&
                    prevNeighbor.Elevation == ElevationLevel.Flat)
                {
                    TriangulateNextCornerTerracesCliff(v6, cell, v2, v4, neighbor, nextNeighbor);
                    TriangulatePrevCornerTerracesCliff(v5, cell, v3, v1, neighbor, prevNeighbor);
                }
                else if (nextNeighbor.Elevation == ElevationLevel.Flat)
                {
                    TriangulateNextCornerTerracesCliff(v6, cell, v2, v4, neighbor, nextNeighbor);
                    terrain.AddTriangle(v1, v5, v3);
                    terrain.AddTriangleColor(neighbor.Color);
                }
                else if (prevNeighbor.Elevation == ElevationLevel.Flat)
                {
                    TriangulatePrevCornerTerracesCliff(v5, cell, v3, v1, neighbor, prevNeighbor);
                    terrain.AddTriangle(v2, v4, v6);
                    terrain.AddTriangleColor(neighbor.Color);
                }
                else
                {
                    terrain.AddTriangle(v1, v5, v3);
                    terrain.AddTriangleColor(neighbor.Color);
                    terrain.AddTriangle(v2, v4, v6);
                    terrain.AddTriangleColor(neighbor.Color);
                }
            }
        }
        else
        {
            terrain.AddTriangle(v1, v5, v3);
            terrain.AddTriangleColor(cell.Color);
            terrain.AddTriangle(v2, v4, v6);
            terrain.AddTriangleColor(cell.Color);
        }

        //Features
        if (cell.Elevation == ElevationLevel.Flat)
        {
            features.AddFeature(cell, (center + v1 + v5) * (1f / 3f));
        }
    }