public override MeshData GetData() { if (roundings.Length != resolution) { int[] arr = new int[resolution]; for (int i = 0; i < resolution && i < roundings.Length; i++) { arr[i] = roundings[i]; } roundings = arr; } if (roundingsDistances.Length != resolution) { float[] arr = new float[resolution]; for (int i = 0; i < resolution && i < roundingsDistances.Length; i++) { arr[i] = roundingsDistances[i]; } roundingsDistances = arr; } bool rounded = false; uniform = true; // This is why using rounding for at least one corner adds redundant vertices. int segments = resolution * 2; for (int i = 0; i < roundings.Length; i++) { roundings[i] = Mathf.RoundToInt(1 + (roundingsDistances[i] * 20)); if (roundings[i] > 1) { uniform = false; rounded = true; segments += 2; } } Vector3[] vertices = new Vector3[segments + 1]; int[] indices = new int[segments * 3]; int[] borderVertices = new int[segments]; int[] roundableVertices = new int[resolution]; vertices[0] = Vector3.zero; int index = 0; int a = 0; int furtherCorner = 1; int closerCorner = furtherCorner + 1; if (roundings[0] > 1) { if (roundType) { furtherCorner = 1; } else { furtherCorner = 2; } closerCorner = furtherCorner + 2; } for (int i = 1; i < vertices.Length; i++) { if (i == closerCorner) { vertices[i] = MakeVertex(2 * a - 1); } else if (i == furtherCorner && a < roundings.Length) { vertices[i] = MakeVertex(2 * a); closerCorner = i + (roundings[a] > 1 ? 2 : 1); if (roundType) { roundableVertices[a] = closerCorner; } else { roundableVertices[a] = i; } a++; int nex = a < roundings.Length ? a : 0; if (roundings[a - 1] > 1 && roundings[nex] > 1) { furtherCorner += 4; } else if (roundings[a - 1] == 1 && roundings[nex] == 1) { furtherCorner += 2; } else { if (roundType) { furtherCorner += roundings[a - 1] > 1 ? 4 : 2; } else { furtherCorner += 3; } } } else { vertices[i] = Vector3.zero; // will be overwiten below, based on previous and next vertex position } borderVertices[i - 1] = i; indices[index++] = 0; indices[index++] = i; indices[index++] = i == vertices.Length - 1 ? 1 : i + 1; } if (rounded) { for (int i = 0; i < roundableVertices.Length; i++) { if (roundings[i] > 1) { int tipID = roundableVertices[i]; int prevI = tipID == 1 ? vertices.Length - 1 : tipID - 1; int nextI = tipID + 1; int prevR = tipID == 1 ? vertices.Length - 2 : tipID == 2 ? vertices.Length - 1 : tipID - 2; int nextR = tipID == vertices.Length - 2 ? 1 : tipID + 2; vertices[prevI] = Vector3.Lerp(vertices[prevR], vertices[tipID], 1 - roundingsDistances[i]); vertices[nextI] = Vector3.Lerp(vertices[nextR], vertices[tipID], 1 - roundingsDistances[i]); } } } var data = new MeshData(vertices, indices, borderVertices, roundableVertices, 0); if (rounded) { for (int i = 0; i < resolution; i++) { if (roundings[i] > 1) { data.Bevel(i, roundings[i], 1); } } } return(data); }
public void RecalculateMesh() { if (Shape == null) { return; } Shape.facingDirection = 0; LastData = Shape.GetData(); ProportionsModifier.ModifyData(LastData, GetComponent <RectTransform>()); if (Fill != null) { Fill.ModifyData(LastData, Material); } else { EmptyFill.ModifyData(LastData, Material); } if (Border != null) { ((StandardBorder)Border).KeepGeometry = ShapeFillIndex != FillType.None; Border.ModifyData(LastData, Material); } // Don't show the fill mesh if the fill is empty. if (ShapeFillIndex == FillType.None && Border == null) { LastData.Triangles = new int[0]; } if (!IsUI) { PivotModifier.ModifyData(LastData, transform); } UVModifier.ModifyData(LastData); FacingModifier.ModifyData(LastData); SortingLayerModifier.ModifyData(LastData, GetComponent <MeshRenderer>()); Mesh mesh = GetComponent <MeshFilter>().sharedMesh; mesh.Clear(); mesh.vertices = LastData.Vertices; mesh.triangles = LastData.Triangles; mesh.colors = LastData.Colors; mesh.uv = LastData.UV; mesh.name = Shape.Name; mesh.RecalculateNormals(); if (ShapeColliderIndex != ColliderType.None && (Fill == null || Fill is EmptyFill) && Border == null) { Debug.LogWarning(Warnings.COLLIDER_POLYGON_FILL_AND_BORDER_NOT_EXIST, this); ShapeColliderIndex = ColliderType.None; } ColliderManager.ReloadCollider(this, ShapeColliderIndex, LastData, ((StandardBorder)Border)); Lighting = lighting; if (GetComponent <RectTransform>() != null) { RectTransform tfm = GetComponent <RectTransform>(); Vector3 size = new Vector3(ProportionsModifier.width, ProportionsModifier.height); tfm.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size.x * 90.6f); tfm.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size.y * 90.6f); } }
public override MeshData GetData() { if (roundings.Length != 5) { roundings = new int[5]; } if (roundingsDistances.Length != 5) { roundingsDistances = new float[5]; } uniform = true; bool rounded = false; float directionMlt = facingDirection == 0 ? 1f : -1f; float row = 0.5f * (2 * middleHeight - 1f); float hatX = topWidth * 0.5f * directionMlt; float shoulderX = 0.5f * directionMlt; Vector3[] vertices = new[] { new Vector3(0, 0, 0), new Vector3(hatX, 0.5f, 0), new Vector3(-hatX, 0.5f, 0), new Vector3(-shoulderX, row, 0), new Vector3(0, -0.5f, 0), new Vector3(shoulderX, row, 0), }; int[] indices = new[] { 5, 4, 0, 4, 3, 0, 3, 2, 0, 2, 1, 0, 1, 5, 0, }; int[] borderVertices = new[] { 5, 4, 3, 2, 1 }; int[] roundableVertices = new int[] { 1, 2, 3, 4, 5 }; for (int i = 0; i < roundableVertices.Length; i++) { float m1, m2; if (i == 0) { m1 = Vector3.Distance(vertices[roundableVertices[i]], vertices[roundableVertices[roundableVertices.Length - 1]]); m2 = Vector3.Distance(vertices[roundableVertices[i]], vertices[roundableVertices[i + 1]]); } else if (i == roundableVertices.Length - 1) { m1 = Vector3.Distance(vertices[roundableVertices[i]], vertices[roundableVertices[0]]); m2 = Vector3.Distance(vertices[roundableVertices[i]], vertices[roundableVertices[i - 1]]); } else { m1 = Vector3.Distance(vertices[roundableVertices[i]], vertices[roundableVertices[i - 1]]); m2 = Vector3.Distance(vertices[roundableVertices[i]], vertices[roundableVertices[i + 1]]); } roundingsDistances[i] = Mathf.Min(roundingsDistances[i], Mathf.Min(m1, m2) / 2.2f); roundings[i] = Mathf.RoundToInt(1 + (roundingsDistances[i] * 40)); } MeshData data = new MeshData(vertices, indices, borderVertices, roundableVertices, 0); for (int i = 0; i < roundings.Length; i++) { if (roundings[i] > 1) { rounded = true; uniform = false; } } if (rounded) { for (int i = 0; i < roundableVertices.Length; i++) { if (roundings[i] > 1) { data.PrepareBevel(i, roundingsDistances[i]); } } for (int i = 0; i < roundableVertices.Length; i++) { if (roundings[i] > 1) { data.Bevel(i, roundings[i], 1); } } } return(data); }