From e3735655369d619da7f5898a57ee8d6c3dcbe086 Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Mon, 12 Jun 2017 09:02:46 +0100 Subject: [PATCH] Resolves #139 New properties to improve the resolution for a line, which when combined with the LineList creates a dotted line. Supports two modes: dashes based on segments or the entire line. --- Scripts/Primitives/UILineRenderer.cs | 13 +++++- Scripts/Primitives/UIPrimitiveBase.cs | 65 +++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/Scripts/Primitives/UILineRenderer.cs b/Scripts/Primitives/UILineRenderer.cs index 4be0362..d0860f0 100644 --- a/Scripts/Primitives/UILineRenderer.cs +++ b/Scripts/Primitives/UILineRenderer.cs @@ -131,6 +131,11 @@ namespace UnityEngine.UI.Extensions pointsToDraw = drawingPoints.ToArray(); } + if (ImproveResolution != ResolutionMode.None) + { + pointsToDraw = IncreaseResolution(pointsToDraw); + } + // scale based on the size of the rect or use absolute, this is switchable var sizeX = !relativeSize ? 1 : rectTransform.rect.width; var sizeY = !relativeSize ? 1 : rectTransform.rect.height; @@ -243,7 +248,13 @@ namespace UnityEngine.UI.Extensions vh.AddUIVertexQuad(segments[i]); } - } + if (vh.currentVertCount > 64000) + { + Debug.LogError("Max Verticies size is 64000, current mesh vertcies count is [" + vh.currentVertCount + "] - Cannot Draw"); + vh.Clear(); + return; + } + } private UIVertex[] CreateLineCap(Vector2 start, Vector2 end, SegmentType type) { diff --git a/Scripts/Primitives/UIPrimitiveBase.cs b/Scripts/Primitives/UIPrimitiveBase.cs index 79b9834..a8cd6e6 100644 --- a/Scripts/Primitives/UIPrimitiveBase.cs +++ b/Scripts/Primitives/UIPrimitiveBase.cs @@ -1,7 +1,15 @@ using System; +using System.Collections.Generic; namespace UnityEngine.UI.Extensions { + public enum ResolutionMode + { + None, + PerSegment, + PerLine + } + public class UIPrimitiveBase : MaskableGraphic, ILayoutElement, ICanvasRaycastFilter { @@ -17,6 +25,14 @@ namespace UnityEngine.UI.Extensions internal float m_EventAlphaThreshold = 1; public float eventAlphaThreshold { get { return m_EventAlphaThreshold; } set { m_EventAlphaThreshold = value; } } + [SerializeField] + private ResolutionMode m_improveResolution; + public ResolutionMode ImproveResolution { get { return m_improveResolution; } set { m_improveResolution = value; SetAllDirty(); } } + + [SerializeField] + private float m_Resolution; + public float Resoloution { get { return m_Resolution; } set { m_Resolution = value; SetAllDirty(); } } + /// @@ -70,6 +86,52 @@ namespace UnityEngine.UI.Extensions return vbo; } + protected Vector2[] IncreaseResolution(Vector2[] input) + { + var outputList = new List(); + + switch (ImproveResolution) + { + case ResolutionMode.PerLine: + float totalDistance = 0, increments = 0; + for (int i = 0; i < input.Length - 1; i++) + { + totalDistance += Vector2.Distance(input[i], input[i + 1]); + } + increments = totalDistance / m_Resolution; + var incrementCount = 0; + for (int i = 0; i < input.Length - 1; i++) + { + var p1 = input[i]; + outputList.Add(p1); + var p2 = input[i + 1]; + var segmentDistance = Vector2.Distance(p1, p2) / increments; + var incrementTime = 1f / segmentDistance; + for (int j=0; j < segmentDistance; j++) + { + outputList.Add(Vector2.Lerp(p1, (Vector2)p2, j * incrementTime)); + incrementCount++; + } + outputList.Add(p2); + } + break; + case ResolutionMode.PerSegment: + for (int i = 0; i < input.Length - 1; i++) + { + var p1 = input[i]; + outputList.Add(p1); + var p2 = input[i + 1]; + increments = 1f / m_Resolution; + for (Single j = 1; j < m_Resolution; j++) + { + outputList.Add(Vector2.Lerp(p1, (Vector2)p2, increments * j)); + } + outputList.Add(p2); + } + break; + } + return outputList.ToArray(); + } #region ILayoutElement Interface @@ -111,6 +173,7 @@ namespace UnityEngine.UI.Extensions #region ICanvasRaycastFilter Interface public virtual bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera) { + // add test for line check if (m_EventAlphaThreshold >= 1) return true; @@ -129,6 +192,8 @@ namespace UnityEngine.UI.Extensions local = MapCoordinate(local, rect); + //test local coord with Mesh + // Normalize local coordinates. Rect spriteRect = sprite.textureRect; Vector2 normalized = new Vector2(local.x / spriteRect.width, local.y / spriteRect.height);