public Shape BuildLinkedSpline <T>(List <Vector2> points, float width, Color color, Material mat, bool loop = false) where T : BaseLineStyle { GameObject go = new GameObject(); Mesh mesh = new Mesh(); go.AddComponent <MeshFilter>().mesh = mesh; MeshRenderer mr = go.AddComponent <MeshRenderer>(); _basicMaterial = mat; if (_basicMaterial == null) { _basicMaterial = Resources.Load("Basic2DMaterial") as Material; } mr.material = _basicMaterial; BaseLineStyle lineStyle = ScriptableObject.CreateInstance(typeof(T)) as BaseLineStyle; List <Vector2> splinePoints = MeshUtils.Vector3ToVector2(MeshUtils.CatmullRom(MeshUtils.Vector2ToVector3(points))); BuildLinkedMesh(mesh, splinePoints, color, width, loop, lineStyle); Shape s = go.AddComponent <Shape>(); s.Col = color; s.Points = Vector3ToVector2(mesh.vertices); s.BuiltGameObject = go; s.LineStyle = lineStyle; s.LinePoints = splinePoints.ToArray(); return(s); }
public void RebuildLinked(Shape shape, List <Vector2> points, float width, bool loop = false) { Mesh m = shape.BuiltGameObject.GetComponent <MeshFilter>().sharedMesh; if (shape.LineStyle == null) { BaseLineStyle lineStyle = ScriptableObject.CreateInstance <BaseLineStyle>(); shape.LineStyle = lineStyle; } BuildLinkedMesh(m, points, shape.Col, width, loop, shape.LineStyle); shape.Points = Vector3ToVector2(m.vertices); shape.LinePoints = points.ToArray(); }
void OnDestroy() { LineStyle = null; }
private void BuildLinkedMesh(Mesh mesh, List <Vector2> points, Color color, float width, bool loop, BaseLineStyle linestyle) { mesh.Clear(); List <Vector3> vertices = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); List <int> tris = new List <int>(); List <Color> colours = new List <Color>(); Vector3 previousLeft = Vector3.zero; Vector3 previousRight = Vector3.zero; float prevX = 0; int i = 1, l = points.Count; linestyle.Setup(); for (; i < l; ++i) { Vector3 point = points[i]; Vector3 prevPoint = points[i - 1]; Vector3 normal = new Vector2(-(point.y - prevPoint.y), (point.x - prevPoint.x)); Vector3 d = normal; point.z = 0; prevPoint.z = 0; d.Normalize(); int t = vertices.Count; tris.Add(t); tris.Add(t + 1); tris.Add(t + 2); tris.Add(t + 1); tris.Add(t + 3); tris.Add(t + 2); Vector2 p1 = Vector2.zero; Vector2 p2 = Vector2.zero; if (i == l - 1 && loop) { vertices.Add(vertices[1]); } else { p1 = linestyle.Style(point, -d, width, i, l, points); vertices.Add(p1); } if (i == 1) { vertices.Add(linestyle.Style(prevPoint, -d, width, i - 1, l, points)); } else { vertices.Add(previousLeft); } if (i == l - 1 && loop) { vertices.Add(vertices[3]); } else { p2 = linestyle.Style(point, d, width, i, l, points); vertices.Add(p2); } if (i == 1) { vertices.Add(linestyle.Style(prevPoint, d, width, i - 1, l, points)); } else { vertices.Add(previousRight); } previousLeft = p1; previousRight = p2; float dist = Vector3.Distance(prevPoint, point) * 2; if (i == l - 1) { dist = Mathf.RoundToInt(prevX) - prevX; } uvs.Add(new Vector2(prevX + dist, 1)); uvs.Add(new Vector2(prevX, 1)); uvs.Add(new Vector2(prevX + dist, 0)); uvs.Add(new Vector2(prevX, 0)); prevX += dist; colours.Add(color); colours.Add(color); colours.Add(color); colours.Add(color); } mesh.vertices = vertices.ToArray(); mesh.uv = uvs.ToArray(); mesh.triangles = tris.ToArray(); mesh.colors = colours.ToArray(); mesh.RecalculateBounds(); mesh.RecalculateNormals(); }
void OnDestroy() { LineStyle = null; }