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); } }
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]); } } }
private void ModifyTriangle(VertexHelper vh, List <UIVertex> vertices, int[] triangleIndices, GradationMaterial.Grid grid, Vector3[] localCorners) { var triangleVertices = triangleIndices.Select(x => vertices[x]).ToArray(); var triangleNormalizePositions = new Vector2[triangleVertices.Length]; for (int i = 0, iMax = triangleVertices.Length; i < iMax; ++i) { triangleNormalizePositions[i] = new Vector2(Mathf.InverseLerp(localCorners[(int)RectangleIndex.UpperLeft].x, localCorners[(int)RectangleIndex.LowerRight].x, triangleVertices[i].position.x) , Mathf.InverseLerp(localCorners[(int)RectangleIndex.LowerRight].y, localCorners[(int)RectangleIndex.UpperLeft].y, triangleVertices[i].position.y) ); } var xMin = Array.BinarySearch <float>(grid.xThresholds, triangleNormalizePositions.Min(x => x.x)); if (xMin < 0) { xMin = ~xMin - 1; } var xMax = Array.BinarySearch <float>(grid.xThresholds, triangleNormalizePositions.Max(x => x.x)); if (xMax < 0) { xMax = ~xMax; } var yMin = Array.BinarySearch <float>(grid.yThresholds, triangleNormalizePositions.Min(x => x.y)); if (yMin < 0) { yMin = ~yMin - 1; } var yMax = Array.BinarySearch <float>(grid.yThresholds, triangleNormalizePositions.Max(x => x.y)); if (yMax < 0) { yMax = ~yMax; } for (int y = yMin; y < yMax; ++y) { for (int x = xMin; x < xMax; ++x) { ModifyTriangleGrid(vh, triangleVertices, grid, x, y, triangleNormalizePositions); } } }