예제 #1
0
    private void ModifyRectangleGrid(VertexHelper vh, UIVertex[] rectangleVertices, GradationMaterial.Grid grid, int x, int y, Vector2[] rectangleNormalizePositions)
    {
        var gridPositions = new Vector2[System.Enum.GetValues(typeof(RectangleIndex)).Length];
        var gridColors    = new Color[System.Enum.GetValues(typeof(RectangleIndex)).Length];

        for (int i = 0, iMax = System.Enum.GetValues(typeof(RectangleIndex)).Length; i < iMax; ++i)
        {
            var xOffset = (i ^ (i >> 1)) & 0x01;
            var yOffest = i >> 1;
            var xIndex  = x + xOffset;
            var yIndex  = y + yOffest;
            gridPositions[i] = new Vector2(grid.xThresholds[xIndex], grid.yThresholds[yIndex]);
            gridColors[i]    = grid.GetColor(xIndex, yIndex);
        }

        System.Func <Vector2, UIVertex> PickupUIVertex = (position) => {
            var v = position;
            for (int i = 0, iMax = 2; i < iMax; ++i)
            {
                v[i] = Mathf.InverseLerp(rectangleNormalizePositions[(int)RectangleIndex.UpperLeft][i], rectangleNormalizePositions[(int)RectangleIndex.LowerRight][i], v[i]);
            }
            var result = Lerp2D <UIVertex>(rectangleVertices, v, LerpUIVertex);

            var c = position;
            for (int i = 0, iMax = 2; i < iMax; ++i)
            {
                c[i] = Mathf.InverseLerp(gridPositions[(int)RectangleIndex.UpperLeft][i], gridPositions[(int)RectangleIndex.LowerRight][i], c[i]);
            }
            var gridColor = Lerp2D <Color>(gridColors, c, Color.Lerp);
            result.color = BlendColor(result.color, gridColor);

            return(result);
        };

        var maskedPositions = new Vector2[System.Enum.GetValues(typeof(RectangleIndex)).Length];

        for (int i = 0, iMax = maskedPositions.Length; i < iMax; ++i)
        {
            for (int k = 0, kMax = 2; k < kMax; ++k)
            {
                maskedPositions[i][k] = Mathf.Clamp(rectangleNormalizePositions[i][k], gridPositions[(int)RectangleIndex.UpperLeft][k], gridPositions[(int)RectangleIndex.LowerRight][k]);
            }
        }
        maskedPositions = maskedPositions.Distinct(new Vector2Approximately()).ToArray();

        if (maskedPositions.Length == 4)
        {
            var maskedVertices = maskedPositions.Select(z => PickupUIVertex(z)).ToArray();
            vh.AddUIVertexQuad(maskedVertices);
        }
    }
예제 #2
0
    private void ModifyTriangleGrid(VertexHelper vh, UIVertex[] triangleVertices, GradationMaterial.Grid grid, int x, int y, Vector2[] triangleNormalizePositions)
    {
        var gridPositions = new Vector2[System.Enum.GetValues(typeof(RectangleIndex)).Length];
        var gridColors    = new Color[System.Enum.GetValues(typeof(RectangleIndex)).Length];

        for (int i = 0, iMax = System.Enum.GetValues(typeof(RectangleIndex)).Length; i < iMax; ++i)
        {
            var xOffset = (i ^ (i >> 1)) & 0x01;
            var yOffest = i >> 1;
            var xIndex  = x + xOffset;
            var yIndex  = y + yOffest;
            gridPositions[i] = new Vector2(grid.xThresholds[xIndex], grid.yThresholds[yIndex]);
            gridColors[i]    = grid.GetColor(xIndex, yIndex);
        }

        System.Func <Vector2, UIVertex> PickupUIVertex = (position) => {
            var inverseArea = 1.0f / GetAreaOfTriangle(triangleNormalizePositions[0], triangleNormalizePositions[1], triangleNormalizePositions[2]);
            var result      = UIVertexTriWeightedAverage(triangleVertices[0], GetAreaOfTriangle(position, triangleNormalizePositions[1], triangleNormalizePositions[2]) * inverseArea
                                                         , triangleVertices[1], GetAreaOfTriangle(triangleNormalizePositions[0], position, triangleNormalizePositions[2]) * inverseArea
                                                         , triangleVertices[2], GetAreaOfTriangle(triangleNormalizePositions[0], triangleNormalizePositions[1], position) * inverseArea
                                                         );

            var c = position;
            for (int i = 0, iMax = 2; i < iMax; ++i)
            {
                c[i] = Mathf.InverseLerp(gridPositions[(int)RectangleIndex.UpperLeft][i], gridPositions[(int)RectangleIndex.LowerRight][i], c[i]);
            }
            var gridColor = Lerp2D <Color>(gridColors, c, Color.Lerp);
            result.color = BlendColor(result.color, gridColor);

            return(result);
        };

        var maskedPositions = new List <Vector2>(6);

        {
            var overlap = OverlapPosition(triangleNormalizePositions, gridPositions[0]);
            for (int i = 0, iMax = gridPositions.Length; i < iMax; ++i)
            {
                if (overlap)
                {
                    maskedPositions.Add(gridPositions[i]);
                }
                var nextIndex   = (i + 1) % iMax;
                var nextOverlap = OverlapPosition(triangleNormalizePositions, gridPositions[nextIndex]);
                if (!overlap || !nextOverlap)
                {
                    var crossPositions = GetCrossPositions(gridPositions[i], gridPositions[nextIndex], triangleNormalizePositions);
                    maskedPositions.AddRange(crossPositions);
                }
                overlap = nextOverlap;
            }
        }
        for (int i = 0, iMax = triangleNormalizePositions.Length; i < iMax; ++i)
        {
            var overlap = OverlapPosition(gridPositions, triangleNormalizePositions[i]);
            if (overlap)
            {
                maskedPositions.Add(triangleNormalizePositions[i]);
            }
        }
        maskedPositions = maskedPositions.Distinct(new Vector2Approximately()).ToList();

        if (3 <= maskedPositions.Count)
        {
            var currentVertCount = vh.currentVertCount;
            var maskedVertices   = maskedPositions.Select(z => PickupUIVertex(z)).ToArray();
            foreach (var maskedvertex in maskedVertices)
            {
                vh.AddVert(maskedvertex);
            }
            var triangleIndices = GetTriangleIndices(maskedPositions);
            for (int i = 0, iMax = triangleIndices.Count; i < iMax; i += 3)
            {
                vh.AddTriangle(currentVertCount + triangleIndices[i]
                               , currentVertCount + triangleIndices[i + 1]
                               , currentVertCount + triangleIndices[i + 2]);
            }
        }
    }