Esempio n. 1
0
    /// <summary>
    /// builds a flattened 2D array of vertices like the following:
    ///
    /// 15--16--17--18--19
    /// |   |   |   |   |
    /// 10--11--12--13--14
    /// |   |   |   |   |
    /// 5---6---7---8---9
    /// |   |   |   |   |
    /// 0---1---2---3---4
    ///
    /// NOTE: <see cref="AddNeighbors"/> must be called after this method to
    /// ensure that we properly detect the correct neighbors
    /// </summary>
    private void GenerateVertices()
    {
        // start from the passed in _bottomLeft Vector3 and build list of QuadVerts
        int   rows          = Subdivisions + 2;
        int   cols          = rows;
        float colVertOffset = 0F;
        float rowVertOffset = 0F;

        colVertOffset = Size / (Subdivisions + 1F);
        rowVertOffset = Size / (Subdivisions + 1F);
        float uvScalar = 1 / (4 * Face.Size);

        float   xBottomLeftOffset = -Size / 2F;
        float   yBottomLeftOffset = -Size / 2F;
        float   zBottomLeftOffset = 0F;
        Vector3 bottomLeftCorner  = new Vector3(xBottomLeftOffset, yBottomLeftOffset, zBottomLeftOffset);

        for (int row = 0; row < rows; row++)
        {
            for (int col = 0; col < cols; col++)
            {
                int index = FlatArray.GetIndexFromRowCol(row, col, cols);

                Vector3 v = new Vector3((col * colVertOffset) + bottomLeftCorner.x, (row * rowVertOffset) + bottomLeftCorner.y, bottomLeftCorner.z);

                if (index == 0)
                {
                    BottomLeft = v.Clone();
                }
                if (index == FlatArray.GetBottomRightIndex(cols))
                {
                    BottomRight = v.Clone();
                }
                if (index == FlatArray.GetTopLeftIndex(rows, cols))
                {
                    TopLeft = v.Clone();
                }
                if (index == FlatArray.GetTopRightIndex(rows, cols))
                {
                    TopRight = v.Clone();
                }

                Vector3 scaledUV = Face.ToLocalVert(ToWorldVert(v)) * uvScalar;
                var     uvOffset = Face.GetUVOffset();
                Vector2 uv       = new Vector2(uvOffset.x + scaledUV.x, uvOffset.y + scaledUV.y);

                // set elevation
                v = GetInverseOffsetFromRoot(Face.ApplyElevation(GetOffsetFromRoot(v), uv));

                Vertices[index] = v;
                UVs[index]      = uv;
            }
        }

        DistanceTestCentre = GetInverseOffsetFromRoot(Face.ApplyElevation(GetOffsetFromRoot(CentrePoint), Face.GetUVOffset()));
    }
Esempio n. 2
0
 /// <summary>
 /// draw a fan of triangles like the following, leaving edges empty
 ///   | | |
 /// ---------
 ///   |\|/|
 /// ---------
 ///   |/|\|
 /// ---------
 ///   | | |
 /// </summary>
 private void GenerateCentreTriangles()
 {
     if (_centreTriangleCache == null)
     {
         List <int> tris = new List <int>();
         int        rows = _pointsOnSide - 4; // stop before last 2 rows of verts
         int        cols = rows;              // stop before last 2 columns of verts
         // skip the edge triangles as they are special case and just generate the centre triangle fans
         for (int row = 2; row < rows; row += 2)
         {
             for (int col = 2; col < cols; col += 2)
             {
                 tris.AddRange(GetTriangleFan(
                                   FlatArray.GetIndexFromRowCol(row, col, _pointsOnSide),
                                   true, true, true, true));
             }
         }
         _centreTriangleCache = tris.ToArray();
     }
 }
Esempio n. 3
0
    private void GenerateCase(int caseId, bool topActive, bool bottomActive, bool leftActive, bool rightActive)
    {
        int        rows = _pointsOnSide - 4;
        int        cols = _pointsOnSide - 4;
        List <int> tris = new List <int>(_centreTriangleCache);

        if (rows > 0)
        {
            // bottom left
            tris.AddRange(GetTriangleFan(0, bottomActive, true, true, leftActive));

            // bottom
            for (int col = 2; col < cols; col += 2)
            {
                tris.AddRange(GetTriangleFan(col, bottomActive, true, true, true));
            }

            // bottom right
            tris.AddRange(GetTriangleFan(_pointsOnSide - 3, bottomActive, rightActive, true, true));

            // right
            for (int row = 2; row < rows; row += 2)
            {
                tris.AddRange(GetTriangleFan(FlatArray.GetIndexFromRowCol(row, _pointsOnSide - 3, _pointsOnSide), true, rightActive, true, true));
            }

            // top right
            tris.AddRange(GetTriangleFan(FlatArray.GetIndexFromRowCol(_pointsOnSide - 3, _pointsOnSide - 3, _pointsOnSide), true, rightActive, topActive, true));

            // top
            for (int col = 2; col < cols; col += 2)
            {
                tris.AddRange(GetTriangleFan(FlatArray.GetIndexFromRowCol(_pointsOnSide - 3, col, _pointsOnSide), true, true, topActive, true));
            }

            // top left
            tris.AddRange(GetTriangleFan(FlatArray.GetIndexFromRowCol(_pointsOnSide - 3, 0, _pointsOnSide), true, true, topActive, leftActive));

            // left
            for (int row = 2; row < rows; row += 2)
            {
                tris.AddRange(GetTriangleFan(FlatArray.GetIndexFromRowCol(row, 0, _pointsOnSide), true, true, true, leftActive));
            }
        }
        else if (rows == 0)
        {
            // bottom left
            tris.AddRange(GetTriangleFan(0, bottomActive, true, true, leftActive));

            // bottom right
            tris.AddRange(GetTriangleFan(_pointsOnSide - 3, bottomActive, rightActive, true, true));

            // top right
            tris.AddRange(GetTriangleFan(FlatArray.GetIndexFromRowCol(_pointsOnSide - 3, _pointsOnSide - 3, _pointsOnSide), true, rightActive, topActive, true));

            // top left
            tris.AddRange(GetTriangleFan(FlatArray.GetIndexFromRowCol(_pointsOnSide - 3, 0, _pointsOnSide), true, true, topActive, leftActive));
        }
        else
        {
            tris.AddRange(GetTriangleFan(0, bottomActive, rightActive, topActive, leftActive));
        }

        _triangleCache[caseId] = tris.ToArray();
    }