/* Triangulate Open Water */
        private void TriangulateOpenWater(HexDirection direction, HexCell cell, HexCell neighbor, Vector3 center)
        {
            Vector3 c1 = center + (HexMeshUtility.GetCornerA(direction) * HexMeshUtility.SolidPercent);
            Vector3 c2 = center + (HexMeshUtility.GetCornerB(direction) * HexMeshUtility.SolidPercent);

            m_waterMesh.AddTriangle(center, c1, c2);

            if (direction <= HexDirection.BottomRight)
            {
                Vector3 bridge = HexMeshUtility.GetBridgeSimple(direction);
                Vector3 e1     = c1 + bridge;
                Vector3 e2     = c2 + bridge;

                m_waterMesh.AddQuad(c1, c2, e1, e2);

                if (direction <= HexDirection.Right)
                {
                    HexCell nextNeighbor = cell.GetNeighbor(direction.Next());
                    if (nextNeighbor == null || !nextNeighbor.IsUnderwater)
                    {
                        return;
                    }
                    m_waterMesh.AddTriangle(c2, e2, c2 + HexMeshUtility.GetBridgeSimple(direction.Next()));
                }
            }
        }
        /* Triangulate Shore */
        private void TriangulateWaterShore(HexDirection direction, HexCell cell, HexCell neighbor, Vector3 center)
        {
            Vector3 c1 = center + (HexMeshUtility.GetCornerA(direction) * HexMeshUtility.SolidPercent);
            Vector3 c2 = center + (HexMeshUtility.GetCornerB(direction) * HexMeshUtility.SolidPercent);

            HexEdgeVertices e1 = new HexEdgeVertices(c1, c2);

            m_waterMesh.AddTriangle(center, e1.v1, e1.v2);
            m_waterMesh.AddTriangle(center, e1.v2, e1.v3);
            m_waterMesh.AddTriangle(center, e1.v3, e1.v4);
            m_waterMesh.AddTriangle(center, e1.v4, e1.v5);

            Vector3         bridge = HexMeshUtility.GetBridgeSimple(direction);
            HexEdgeVertices e2     = new HexEdgeVertices(e1.v1 + bridge, e1.v5 + bridge);

            m_waterShoreMesh.AddQuad(e1.v1, e1.v2, e2.v1, e2.v2);
            m_waterShoreMesh.AddQuad(e1.v2, e1.v3, e2.v2, e2.v3);
            m_waterShoreMesh.AddQuad(e1.v3, e1.v4, e2.v3, e2.v4);
            m_waterShoreMesh.AddQuad(e1.v4, e1.v5, e2.v4, e2.v5);

            m_waterShoreMesh.AddQuadUV(0f, 0f, 0f, 1f);
            m_waterShoreMesh.AddQuadUV(0f, 0f, 0f, 1f);
            m_waterShoreMesh.AddQuadUV(0f, 0f, 0f, 1f);
            m_waterShoreMesh.AddQuadUV(0f, 0f, 0f, 1f);

            HexCell nextNeighbor = cell.GetNeighbor(direction.Next());

            if (nextNeighbor != null)
            {
                m_waterShoreMesh.AddTriangle(e1.v5, e2.v5, c2 + HexMeshUtility.GetBridgeSimple(direction.Next()));
                m_waterShoreMesh.AddTriangleUV(new Vector2(0f, 0f), new Vector2(0f, 1f), new Vector2(0f, nextNeighbor.IsUnderwater ? 0f : 1f));
            }
        }
        /* Awake */
        private void Awake()
        {
            // Performance setting
            QualitySettings.vSyncCount = 0;



            m_cellCountX = numberOfChunksX * hexesPerChunkX;
            m_cellCountZ = numberOfChunksZ * hexesPerChunkZ;

            /* Set properties to HexTerrainMaterial*/
            if (showGrid)
            {
                terrainMaterial.EnableKeyword("GRID_ON");
            }
            else
            {
                terrainMaterial.DisableKeyword("GRID_ON");
            }

            terrainMaterial.SetFloat(Shader.PropertyToID("_HexSize"), hexRadius);


            HexMeshUtility.SetUpUtility(hexRadius, blendPercent, elevationStep, terracesPerSlope, hexesPerChunkX, hexesPerChunkZ, waterElevationOffset, useTextures);

            GenerateChunks();
            GenerateCells();
            PlacePawn();
        }
        /* Triangulate Edge Terraces */
        private void TriangulateEdgeTerraces(Vector3 beginLeft, Vector3 beginRight, HexCell beginCell, Vector3 endLeft, Vector3 endRight, HexCell endCell)
        {
            Vector3 v3 = HexMeshUtility.TerraceLerp(beginLeft, endLeft, 1);
            Vector3 v4 = HexMeshUtility.TerraceLerp(beginRight, endRight, 1);

            Color c2;


            if (HexMeshUtility.UseTextures)
            {
                c2 = HexMeshUtility.TerraceLerp(m_red, m_green, 1);
                m_terrainMesh.AddQuadColor(m_red, c2);
                AddQuadType(beginCell.TerrainTypeIndex, endCell.TerrainTypeIndex);
            }
            else
            {
                c2 = HexMeshUtility.TerraceLerp(beginCell.Color, endCell.Color, 1);
                m_terrainMesh.AddQuadColor(beginCell.Color, c2);
            }

            m_terrainMesh.AddQuad(beginLeft, beginRight, v3, v4);


            for (int i = 2; i < HexMeshUtility.TerraceSteps; i++)
            {
                Vector3 v1 = v3;
                Vector3 v2 = v4;
                Color   c1 = c2;
                v3 = HexMeshUtility.TerraceLerp(beginLeft, endLeft, i);
                v4 = HexMeshUtility.TerraceLerp(beginRight, endRight, i);
                if (HexMeshUtility.UseTextures)
                {
                    c2 = HexMeshUtility.TerraceLerp(m_red, m_green, i);
                    AddQuadType(beginCell.TerrainTypeIndex, endCell.TerrainTypeIndex);
                }
                else
                {
                    c2 = HexMeshUtility.TerraceLerp(beginCell.Color, endCell.Color, i);
                }

                m_terrainMesh.AddQuad(v1, v2, v3, v4);
                m_terrainMesh.AddQuadColor(c1, c2);
            }



            if (HexMeshUtility.UseTextures)
            {
                m_terrainMesh.AddQuadColor(c2, m_green);
                AddQuadType(beginCell.TerrainTypeIndex, endCell.TerrainTypeIndex);
            }
            else
            {
                m_terrainMesh.AddQuadColor(c2, endCell.Color);
            }

            m_terrainMesh.AddQuad(v3, v4, endLeft, endRight);
        }
        /* Triangulate Boundary Triangle (using Color) */
        private void TriangulateBoundaryTriangle(Vector3 begin, HexCell beginCell, Vector3 left, HexCell leftCell, Vector3 boundary, Color boundaryColor)
        {
            Vector3 v2 = HexMeshUtility.TerraceLerp(begin, left, 1);
            Color   c2 = HexMeshUtility.TerraceLerp(beginCell.Color, leftCell.Color, 1);

            m_terrainMesh.AddTriangle(begin, v2, boundary);
            m_terrainMesh.AddTriangleColor(beginCell.Color, c2, boundaryColor);

            for (int i = 2; i < HexMeshUtility.TerraceSteps; i++)
            {
                Vector3 v1 = v2;
                Color   c1 = c2;
                v2 = HexMeshUtility.TerraceLerp(begin, left, i);
                c2 = HexMeshUtility.TerraceLerp(beginCell.Color, leftCell.Color, i);
                m_terrainMesh.AddTriangle(v1, v2, boundary);
                m_terrainMesh.AddTriangleColor(c1, c2, boundaryColor);
            }

            m_terrainMesh.AddTriangle(v2, left, boundary);
            m_terrainMesh.AddTriangleColor(c2, leftCell.Color, boundaryColor);
        }
        /* Triangulate Corner Terraces */
        private void TriangulateCornerTerraces(Vector3 begin, HexCell beginCell, Vector3 left, HexCell leftCell, Vector3 right, HexCell rightCell)
        {
            Vector3 v3 = HexMeshUtility.TerraceLerp(begin, left, 1);
            Vector3 v4 = HexMeshUtility.TerraceLerp(begin, right, 1);
            Color   c3, c4;

            Vector3 types;

            types.x = beginCell.TerrainTypeIndex;
            types.y = leftCell.TerrainTypeIndex;
            types.z = rightCell.TerrainTypeIndex;


            if (HexMeshUtility.UseTextures)
            {
                c3 = HexMeshUtility.TerraceLerp(m_red, m_green, 1);
                c4 = HexMeshUtility.TerraceLerp(m_red, m_blue, 1);
                m_terrainMesh.AddTriangleTerrainTypes(types);
            }
            else
            {
                c3 = HexMeshUtility.TerraceLerp(beginCell.Color, leftCell.Color, 1);
                c4 = HexMeshUtility.TerraceLerp(beginCell.Color, rightCell.Color, 1);
            }


            m_terrainMesh.AddTriangle(begin, v3, v4);
            m_terrainMesh.AddTriangleColor(beginCell.Color, c3, c4);

            for (int i = 2; i < HexMeshUtility.TerraceSteps; i++)
            {
                Vector3 v1 = v3;
                Vector3 v2 = v4;
                Color   c1 = c3;
                Color   c2 = c4;
                v3 = HexMeshUtility.TerraceLerp(begin, left, i);
                v4 = HexMeshUtility.TerraceLerp(begin, right, i);

                if (HexMeshUtility.UseTextures)
                {
                    c3 = HexMeshUtility.TerraceLerp(m_red, m_green, i);
                    c4 = HexMeshUtility.TerraceLerp(m_red, m_blue, i);
                    m_terrainMesh.AddQuadTerrainTypes(types);
                }
                else
                {
                    c3 = HexMeshUtility.TerraceLerp(beginCell.Color, leftCell.Color, i);
                    c4 = HexMeshUtility.TerraceLerp(beginCell.Color, rightCell.Color, i);
                }

                m_terrainMesh.AddQuad(v1, v2, v3, v4);
                m_terrainMesh.AddQuadColor(c1, c2, c3, c4);
            }



            if (HexMeshUtility.UseTextures)
            {
                m_terrainMesh.AddQuadColor(c3, c4, m_green, m_blue);
                m_terrainMesh.AddQuadTerrainTypes(types);
            }
            else
            {
                m_terrainMesh.AddQuadColor(c3, c4, leftCell.Color, rightCell.Color);
            }

            m_terrainMesh.AddQuad(v3, v4, left, right);
        }
        /* Construct fused blending regions with terraces */
        private void TriangulateSimpleWithTerraces(ref HexCell cell, HexDirection direction)
        {
            // Construct Triangle
            Vector3 center = cell.Center;
            Vector3 v1     = center + (HexMeshUtility.GetCornerA(direction) * HexMeshUtility.SolidPercent);
            Vector3 v2     = center + (HexMeshUtility.GetCornerB(direction) * HexMeshUtility.SolidPercent);

            m_terrainMesh.AddTriangle(center, v1, v2);


            if (HexMeshUtility.UseTextures)
            {
                m_terrainMesh.AddTriangleColor(m_red);              // Set splatmap colors
                AddTriangleTerrainType(cell.TerrainTypeIndex);
            }
            else
            {
                m_terrainMesh.AddTriangleColor(cell.Color);
            }


            // Create connection
            if (direction <= HexDirection.BottomRight)
            {
                HexCell neighbor = cell.GetNeighbor(direction);
                if (neighbor != null)
                {
                    // Create quad
                    Vector3 bridge = HexMeshUtility.GetBridgeSimple(direction);
                    Vector3 v3     = v1 + bridge;
                    Vector3 v4     = v2 + bridge;
                    v3.y = v4.y = neighbor.Elevation * HexMeshUtility.ElevationStep;

                    if (cell.GetEdgeType(direction) == HexEdgeType.Slope)
                    {
                        TriangulateEdgeTerraces(v1, v2, cell, v3, v4, neighbor);
                    }
                    else
                    {
                        m_terrainMesh.AddQuad(v1, v2, v3, v4);

                        if (HexMeshUtility.UseTextures)
                        {
                            m_terrainMesh.AddQuadColor(m_red, m_green);
                            AddQuadType(cell.TerrainTypeIndex, neighbor.TerrainTypeIndex);
                        }
                        else
                        {
                            m_terrainMesh.AddQuadColor(cell.Color, neighbor.Color);
                        }
                    }


                    // Create triangular connection
                    HexCell nextNeighbor = cell.GetNeighbor(direction.Next());
                    if (direction <= HexDirection.Right && nextNeighbor != null)
                    {
                        Vector3 v5 = v2 + HexMeshUtility.GetBridgeSimple(direction.Next());
                        v5.y = nextNeighbor.Elevation * HexMeshUtility.ElevationStep;


                        if (cell.Elevation <= neighbor.Elevation)
                        {
                            if (cell.Elevation <= nextNeighbor.Elevation)
                            {
                                TriangulateCorner(v2, cell, v4, neighbor, v5, nextNeighbor);
                            }
                            else
                            {
                                TriangulateCorner(v5, nextNeighbor, v2, cell, v4, neighbor);
                            }
                        }
                        else if (neighbor.Elevation <= nextNeighbor.Elevation)
                        {
                            TriangulateCorner(v4, neighbor, v5, nextNeighbor, v2, cell);
                        }
                        else
                        {
                            TriangulateCorner(v5, nextNeighbor, v2, cell, v4, neighbor);
                        }
                    }
                }
            }
        }
示例#8
0
 public HexEdgeType GetEdgeType(HexCell otherCell)
 {
     return(HexMeshUtility.GetEdgeType(Elevation, otherCell.Elevation));
 }
示例#9
0
 public HexEdgeType GetEdgeType(HexDirection direction)
 {
     return(HexMeshUtility.GetEdgeType(m_elevation, GetNeighbor(direction).m_elevation));
 }