예제 #1
0
    //Build the mesh for the base
    private static Mesh CreateBoardBaseMesh(int width, int height, float unitSize, List <int>[] allPorts)
    {
        //Create the mesh
        Mesh           mesh      = new Mesh();
        List <Vector3> vertices  = new List <Vector3>();
        List <int>     triangles = new List <int>();

        //Create base
        var h    = height * unitSize;
        var w    = width * unitSize;
        var quad = new RectangleQuad(0, 0, w, h);

        quad.Build(vertices, triangles);


        #region  Create Borders

        float offset = 0.0f;

        //horizontal
        var topStrip    = new Strip();
        var bottomStrip = new Strip();

        var topPorts    = allPorts[1];
        var bottomPorts = allPorts[3];

        var edgei = new Edge(0, h, 0, h + BordersWidth);
        topStrip.AddEdge(edgei);

        edgei = new Edge(0, -BordersWidth, 0, 0);
        bottomStrip.AddEdge(edgei);

        for (var i = 0; i < width; i++)
        {
            //top
            offset = (topPorts.Exists(u => u == i))? BordersOffset : 0.0f;

            var edge0 = new Edge(unitSize * i + 0.1f, h, unitSize * i + 0.1f, h + BordersWidth + offset);
            var edge1 = new Edge(unitSize * i + 1.9f, h, unitSize * i + 1.9f, h + BordersWidth + offset);
            topStrip.AddEdge(edge0);
            topStrip.AddEdge(edge1);

            //bottom
            offset = (bottomPorts.Exists(u => u == i))? BordersOffset : 0.0f;

            edge0 = new Edge(unitSize * i + 0.1f, -BordersWidth - offset, unitSize * i + 0.1f, 0);
            edge1 = new Edge(unitSize * i + 1.9f, -BordersWidth - offset, unitSize * i + 1.9f, 0);
            bottomStrip.AddEdge(edge0);
            bottomStrip.AddEdge(edge1);
        }

        edgei = new Edge(unitSize * width, h, unitSize * width, h + BordersWidth);
        topStrip.AddEdge(edgei);

        edgei = new Edge(unitSize * width, -BordersWidth, unitSize * width, 0);
        bottomStrip.AddEdge(edgei);

        var top    = topStrip.Build(vertices, triangles);
        var bottom = bottomStrip.Build(vertices, triangles);


        //vertical
        var leftStrip  = new Strip();
        var rightStrip = new Strip();

        var leftPorts  = allPorts[0];
        var rightPorts = allPorts[2];

        edgei = new Edge(0, 0, -BordersWidth, 0);
        leftStrip.AddEdge(edgei);

        edgei = new Edge(w + BordersWidth, 0, w, 0);
        rightStrip.AddEdge(edgei);

        for (var i = 0; i < height; i++)
        {
            //left
            offset = (leftPorts.Exists(u => u == i)) ? BordersOffset : 0.0f;

            var edge0 = new Edge(0, unitSize * i + 0.1f, -BordersWidth - offset, unitSize * i + 0.1f);
            var edge1 = new Edge(0, unitSize * i + 1.9f, -BordersWidth - offset, unitSize * i + 1.9f);
            leftStrip.AddEdge(edge0);
            leftStrip.AddEdge(edge1);

            //right
            offset = (rightPorts.Exists(u => u == i)) ? BordersOffset : 0.0f;

            edge0 = new Edge(w + BordersWidth + offset, unitSize * i + 0.1f, w, unitSize * i + 0.1f);
            edge1 = new Edge(w + BordersWidth + offset, unitSize * i + 1.9f, w, unitSize * i + 1.9f);
            rightStrip.AddEdge(edge0);
            rightStrip.AddEdge(edge1);
        }

        edgei = new Edge(0, unitSize * height, -BordersWidth, unitSize * height);
        leftStrip.AddEdge(edgei);

        edgei = new Edge(w + BordersWidth, unitSize * height, w, unitSize * height);
        rightStrip.AddEdge(edgei);

        var left  = leftStrip.Build(vertices, triangles);
        var right = rightStrip.Build(vertices, triangles);

        #endregion

        #region Create Corners

        //left-top corner
        var v1 = left[1] - 1;
        var v2 = left[1];
        var v3 = top[0] + 1;
        triangles.Add(v1);
        triangles.Add(v2);
        triangles.Add(v3);

        //top-right corner
        v1 = top[1] - 1;
        v2 = top[1];
        v3 = right[1] - 1;
        triangles.Add(v1);
        triangles.Add(v2);
        triangles.Add(v3);

        //right-bottom corner
        v1 = right[0];
        v2 = bottom[1] - 1;
        v3 = right[0] + 1;
        triangles.Add(v1);
        triangles.Add(v2);
        triangles.Add(v3);

        //bottom-left corner
        v1 = bottom[0];
        v2 = left[0] + 1;
        v3 = bottom[0] + 1;
        triangles.Add(v1);
        triangles.Add(v2);
        triangles.Add(v3);

        #endregion

        #region Extrude

        //find outer edges
        var outerEdges = new List <Vector2Int>();

        //left
        for (var i = left[1]; i >= left[0]; i--)
        {
            if (i % 2 == 0)
            {
                continue;
            }
            if (i - 2 < left[0])
            {
                break;
            }
            outerEdges.Add(new Vector2Int(i, i - 2));
        }

        //top
        for (var i = top[1]; i >= top[0]; i--)
        {
            if (i % 2 == 0)
            {
                continue;
            }
            if (i - 2 < top[0])
            {
                break;
            }
            outerEdges.Add(new Vector2Int(i, i - 2));
        }

        //right
        for (var i = right[0]; i <= right[1]; i++)
        {
            if (i % 2 != 0)
            {
                continue;
            }
            if (i + 2 > right[1])
            {
                break;
            }
            outerEdges.Add(new Vector2Int(i, i + 2));
        }

        //bottom
        for (var i = bottom[0]; i <= bottom[1]; i++)
        {
            if (i % 2 != 0)
            {
                continue;
            }
            if (i + 2 > bottom[1])
            {
                break;
            }
            outerEdges.Add(new Vector2Int(i, i + 2));
        }

        //corners
        outerEdges.Add(new Vector2Int(top[0] + 1, left[1]));
        outerEdges.Add(new Vector2Int(right[1] - 1, top[1]));
        outerEdges.Add(new Vector2Int(bottom[1] - 1, right[0]));
        outerEdges.Add(new Vector2Int(left[0] + 1, bottom[0]));

        //extrude
        for (var i = 0; i < outerEdges.Count; i++)
        {
            var n = vertices.Count;

            var edge = outerEdges[i];
            var a0   = edge.x;
            var a1   = edge.y;

            //duplicate edge
            var v = vertices[edge.x];
            v.y -= BoardThickness;
            vertices.Add(v);
            var a2 = n;

            v    = vertices[edge.y];
            v.y -= BoardThickness;
            vertices.Add(v);
            var a3 = n + 1;

            triangles.Add(a0);
            triangles.Add(a1);
            triangles.Add(a2);
            triangles.Add(a2);
            triangles.Add(a1);
            triangles.Add(a3);
        }

        #endregion

        //set mesh properties
        mesh.vertices  = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.RecalculateNormals();

        return(mesh);
    }