Esempio n. 1
0
    void GenerateStackMesh(int x, int z, CellStack stack)
    {
        int stackHeight = stack.Count();

        Vector2 worldOffset = new Vector2(x + chunk.offsetOrigin.x, z + chunk.offsetOrigin.y);

        Vector3 center = HexCoordinates.FromOffsetCoordinates(x, z).ToChunkPosition();

        center += new Vector3(0, transform.localPosition.y, 0);
        center += stackHeight * HexMetrics.heightVector;

        CellType   topCell            = stack.Peek();
        Material   topMaterial        = getTopMaterial(topCell);
        List <int> topMaterialSubmesh = getSubmesh(topMaterial);

        //Generates the horizontal part of the terrain (top of the stack)
        for (int i = 0; i < 6; i++)
        {
            AddTriangleToMesh(
                center,
                center + HexMetrics.corners[i],
                center + HexMetrics.corners[i + 1]
                );
            AddTriangleToSubmesh(
                topMaterialSubmesh,
                center,
                center + HexMetrics.corners[i],
                center + HexMetrics.corners[i + 1]
                );
        }

        HexCoordinates[] neighbors = stack.coordinates.GetNeighbors();

        Vector3 topCenter = center;

        //Generates the vertical part of the terrain (sides of the stack)
        for (int i = 0; i < 6; i++)
        {
            center = topCenter;

            //If we have a neighbor in this direction, check its height
            //If it is taller than us, ignore it (it will create the vertical wall)
            //If it is the same height as us, ignore it (we don't need a vertical wall)
            //If it is shorter than us, create a vertical wall down to its height

            CellStack neighbor       = chunk.GetCellStackFromWorldCoords(neighbors[i]);
            int       neighborHeight = 0;
            if (neighbor != null)
            {
                neighborHeight = neighbor.Count();
            }

            for (int elevation = stackHeight; elevation > neighborHeight; elevation--)
            {
                CellType   currentCell         = stack.PeekAt(elevation - 1);
                Material   sideMaterial        = getSideMaterial(currentCell);
                List <int> sideMaterialSubmesh = getSubmesh(sideMaterial);

                AddTriangleToMesh(
                    center + HexMetrics.corners[i] - HexMetrics.heightVector,
                    center + HexMetrics.corners[i + 1],
                    center + HexMetrics.corners[i]

                    );
                AddTriangleToSubmesh(
                    sideMaterialSubmesh,
                    center + HexMetrics.corners[i] - HexMetrics.heightVector,
                    center + HexMetrics.corners[i + 1],
                    center + HexMetrics.corners[i]

                    );
                AddTriangleToMesh(
                    center + HexMetrics.corners[i] - HexMetrics.heightVector,
                    center + HexMetrics.corners[i + 1] - HexMetrics.heightVector,
                    center + HexMetrics.corners[i + 1]
                    );
                AddTriangleToSubmesh(
                    sideMaterialSubmesh,
                    center + HexMetrics.corners[i] - HexMetrics.heightVector,
                    center + HexMetrics.corners[i + 1] - HexMetrics.heightVector,
                    center + HexMetrics.corners[i + 1]
                    );

                center -= HexMetrics.heightVector;
            }
        }
    }