private void BatchPath(SFPolygon poly, int pathIndex, Matrix4x4 t, ref int j, bool flipped, Vector3 properties, Vector3[] verts, Vector4[] tangents, Vector2[] uvs, int[] tris) { int startIndex; Vector2 a; var path = poly.GetPath(pathIndex); if (poly.looped) { startIndex = 0; a = Transform(t, path[path.Length - 1]); } else { startIndex = 1; a = Transform(t, path[0]); } for (int i = startIndex; i < path.Length; i++) { var b = Transform(t, path[i]); var ab = (flipped ? new Vector4(b.x, b.y, a.x, a.y) : new Vector4(a.x, a.y, b.x, b.y)); verts[j * 4 + 0] = properties; tangents[j * 4 + 0] = ab; uvs[j * 4 + 0] = new Vector2(0.0f, 0.0f); verts[j * 4 + 1] = properties; tangents[j * 4 + 1] = ab; uvs[j * 4 + 1] = new Vector2(1.0f, 0.0f); verts[j * 4 + 2] = properties; tangents[j * 4 + 2] = ab; uvs[j * 4 + 2] = new Vector2(0.0f, 1.0f); verts[j * 4 + 3] = properties; tangents[j * 4 + 3] = ab; uvs[j * 4 + 3] = new Vector2(1.0f, 1.0f); tris[j * 6 + 0] = j * 4 + 0; tris[j * 6 + 1] = j * 4 + 1; tris[j * 6 + 2] = j * 4 + 2; tris[j * 6 + 3] = j * 4 + 1; tris[j * 6 + 4] = j * 4 + 3; tris[j * 6 + 5] = j * 4 + 2; j++; a = b; } }
private void PathGUI(SFPolygon poly, int pathIndex) { bool dirty = false; var path = poly.GetPath(pathIndex); bool invalidVerts = true; for (int i = 0; i < path.Length && invalidVerts; i++) { invalidVerts &= (path[i] == Vector2.zero); } if (invalidVerts) { Debug.Log("Invalid verts, attempting to set up from collider."); // copy in collider verts if possible. poly._CopyFromCollider(); path = poly.GetPath(pathIndex); dirty = true; } var verts = new List <Vector2>(path); // Copy of verts that is looped if the polygon is looped. var looped = new List <Vector2>(verts); if (poly.looped) { looped.Add(looped[0]); } Handles.matrix = poly.transform.localToWorldMatrix; if (!inEditMode) { Handles.color = new Color(1f, 1f, 0f, 0.4f); } else { SetupUndo("edited SFPolygon"); var removePressed = ((Event.current.modifiers & (EventModifiers.Command | EventModifiers.Control)) != 0); var addPressed = ((Event.current.modifiers & (EventModifiers.Shift)) != 0); if (removePressed) { if (verts.Count > 2) { Handles.color = Color.red; for (int i = 0; i < verts.Count; i++) { var handleSize = 0.05f * HandleUtility.GetHandleSize(verts [i]); if (Handles.Button(verts [i], Quaternion.identity, handleSize, handleSize, Handles.DotHandleCap)) { verts.RemoveAt(i); dirty = true; break; } } } } else if (addPressed) { int insertIndex = 0; Vector3 insertPosition = ClosestPoint(poly.transform, looped, out insertIndex); var s = HandleUtility.GetHandleSize(insertPosition) * 0.05f; // Draw the existing vertexes Handles.color = Color.white; for (int i = 0; i < verts.Count; i++) { Handles.DotHandleCap(0, verts [i], Quaternion.identity, s, EventType.Layout); } // Draw the insert handle Handles.color = Color.green; if (Handles.Button(insertPosition, Quaternion.identity, s, s, Handles.DotHandleCap)) { verts.Insert(insertIndex, (Vector2)insertPosition); dirty = true; } HandleUtility.Repaint(); } else { // Move an existing vertex Handles.color = Color.white; for (int i = 0; i < verts.Count; i++) { Vector3 v = verts [i]; Vector2 delta = DotHandle(v) - (Vector2)v; if (delta != Vector2.zero) { verts [i] = (Vector2)v + delta; dirty = true; } } } Handles.color = Color.white; } Handles.DrawPolyLine(looped.Select(v => (Vector3)v).ToArray()); if (dirty) { // Unity 5.3 and later only Undo.RecordObject(poly, "Move Shadow Geometry"); poly.SetPath(pathIndex, verts.ToArray()); } }