From 79d8969f836e68f587c92a4d668bc81e845fb906 Mon Sep 17 00:00:00 2001 From: "Simon (darkside) Jackson" Date: Wed, 9 Sep 2015 22:31:17 +0100 Subject: [PATCH] Initial Check-int 5.2 Resolved forced script errors due to change from UIVertex to Mesh **ToDo Updated FILLVBO updates to PopulateMesh --HG-- branch : develop_5.2 --- Scripts/BestFitOutline.cs | 26 ++++++++---- Scripts/CurvedText.cs | 11 ++--- Scripts/Gradient.cs | 39 ++++++++++-------- Scripts/LetterSpacing.cs | 31 +++++++------- Scripts/NicerOutline.cs | 85 ++++++++++++++++++++++----------------- Scripts/UIFlippable.cs | 19 +++++---- 6 files changed, 125 insertions(+), 86 deletions(-) diff --git a/Scripts/BestFitOutline.cs b/Scripts/BestFitOutline.cs index ae202ed..f65d65a 100644 --- a/Scripts/BestFitOutline.cs +++ b/Scripts/BestFitOutline.cs @@ -17,14 +17,20 @@ namespace UnityEngine.UI.Extensions // // Methods // - public override void ModifyVertices (List verts) + public override void ModifyMesh (Mesh mesh) { if (!this.IsActive ()) { return; } - Text foundtext = GetComponent(); + List verts = new List(); + using (var helper = new VertexHelper(mesh)) + { + helper.GetUIVertexStream(verts); + } + + Text foundtext = GetComponent(); float best_fit_adjustment = 1f; @@ -35,16 +41,22 @@ namespace UnityEngine.UI.Extensions int start = 0; int count = verts.Count; - base.ApplyShadow (verts, base.effectColor, start, verts.Count, base.effectDistance.x*best_fit_adjustment, base.effectDistance.y*best_fit_adjustment); + base.ApplyShadowZeroAlloc(verts, base.effectColor, start, verts.Count, base.effectDistance.x*best_fit_adjustment, base.effectDistance.y*best_fit_adjustment); start = count; count = verts.Count; - base.ApplyShadow (verts, base.effectColor, start, verts.Count, base.effectDistance.x*best_fit_adjustment, -base.effectDistance.y*best_fit_adjustment); + base.ApplyShadowZeroAlloc(verts, base.effectColor, start, verts.Count, base.effectDistance.x*best_fit_adjustment, -base.effectDistance.y*best_fit_adjustment); start = count; count = verts.Count; - base.ApplyShadow (verts, base.effectColor, start, verts.Count, -base.effectDistance.x*best_fit_adjustment, base.effectDistance.y*best_fit_adjustment); + base.ApplyShadowZeroAlloc(verts, base.effectColor, start, verts.Count, -base.effectDistance.x*best_fit_adjustment, base.effectDistance.y*best_fit_adjustment); start = count; count = verts.Count; - base.ApplyShadow (verts, base.effectColor, start, verts.Count, -base.effectDistance.x*best_fit_adjustment, -base.effectDistance.y*best_fit_adjustment); - } + base.ApplyShadowZeroAlloc(verts, base.effectColor, start, verts.Count, -base.effectDistance.x*best_fit_adjustment, -base.effectDistance.y*best_fit_adjustment); + + using (var helper = new VertexHelper()) + { + helper.AddUIVertexTriangleStream(verts); + helper.FillMesh(mesh); + } + } } } diff --git a/Scripts/CurvedText.cs b/Scripts/CurvedText.cs index 1343fc3..6508187 100644 --- a/Scripts/CurvedText.cs +++ b/Scripts/CurvedText.cs @@ -5,7 +5,7 @@ namespace UnityEngine.UI.Extensions { [RequireComponent(typeof(Text), typeof(RectTransform))] [AddComponentMenu("UI/Effects/Extensions/Curved Text")] - public class CurvedText : BaseVertexEffect + public class CurvedText : BaseMeshEffect { public AnimationCurve curveForText = AnimationCurve.Linear(0, 0, 1, 10); public float curveMultiplier = 1; @@ -40,18 +40,19 @@ namespace UnityEngine.UI.Extensions rectTrans = GetComponent(); OnRectTransformDimensionsChange(); } - public override void ModifyVertices(System.Collections.Generic.List verts) + public override void ModifyMesh(Mesh mesh) { if (!IsActive()) return; - - for (int index = 0; index < verts.Count; index++) + Vector3[] verts = mesh.vertices; + for (int index = 0; index < verts.Length; index++) { var uiVertex = verts[index]; //Debug.Log (); - uiVertex.position.y += curveForText.Evaluate(rectTrans.rect.width * rectTrans.pivot.x + uiVertex.position.x) * curveMultiplier; + uiVertex.y += curveForText.Evaluate(rectTrans.rect.width * rectTrans.pivot.x + uiVertex.x) * curveMultiplier; verts[index] = uiVertex; } + mesh.vertices = verts; } protected override void OnRectTransformDimensionsChange() { diff --git a/Scripts/Gradient.cs b/Scripts/Gradient.cs index 11c593c..6393092 100644 --- a/Scripts/Gradient.cs +++ b/Scripts/Gradient.cs @@ -6,7 +6,7 @@ using System.Collections.Generic; namespace UnityEngine.UI.Extensions { [AddComponentMenu("UI/Effects/Extensions/Gradient")] - public class Gradient : BaseVertexEffect + public class Gradient : BaseMeshEffect { public GradientMode gradientMode = GradientMode.Global; public GradientDir gradientDir = GradientDir.Vertical; @@ -20,14 +20,18 @@ namespace UnityEngine.UI.Extensions targetGraphic = GetComponent(); } - public override void ModifyVertices(List vertexList) + public override void ModifyMesh(Mesh mesh) { - if (!IsActive() || vertexList.Count == 0) + if (!IsActive() || mesh.vertexCount == 0) { return; } - int count = vertexList.Count; - UIVertex uiVertex = vertexList[0]; + + Vector3[] vertexList = mesh.vertices; + Color[] vertexListColors = mesh.colors; + int count = mesh.vertexCount; + Vector3 uiVertex = vertexList[0]; + Color uiVertexColor = vertexListColors[0]; if (gradientMode == GradientMode.Global) { if (gradientDir == GradientDir.DiagonalLeftToRight || gradientDir == GradientDir.DiagonalRightToLeft) @@ -37,18 +41,19 @@ namespace UnityEngine.UI.Extensions #endif gradientDir = GradientDir.Vertical; } - float bottomY = gradientDir == GradientDir.Vertical ? vertexList[vertexList.Count - 1].position.y : vertexList[vertexList.Count - 1].position.x; - float topY = gradientDir == GradientDir.Vertical ? vertexList[0].position.y : vertexList[0].position.x; + float bottomY = gradientDir == GradientDir.Vertical ? vertexList[vertexList.Length - 1].y : vertexList[vertexList.Length - 1].x; + float topY = gradientDir == GradientDir.Vertical ? vertexList[0].y : vertexList[0].x; float uiElementHeight = topY - bottomY; for (int i = 0; i < count; i++) { uiVertex = vertexList[i]; - if (!overwriteAllColor && uiVertex.color != targetGraphic.color) + uiVertexColor = vertexListColors[i]; + if (!overwriteAllColor && uiVertexColor != targetGraphic.color) continue; - uiVertex.color *= Color.Lerp(vertex2, vertex1, ((gradientDir == GradientDir.Vertical ? uiVertex.position.y : uiVertex.position.x) - bottomY) / uiElementHeight); - vertexList[i] = uiVertex; + uiVertexColor *= Color.Lerp(vertex2, vertex1, ((gradientDir == GradientDir.Vertical ? uiVertex.y : uiVertex.x) - bottomY) / uiElementHeight); + vertexListColors[i] = uiVertexColor; } } else @@ -56,27 +61,29 @@ namespace UnityEngine.UI.Extensions for (int i = 0; i < count; i++) { uiVertex = vertexList[i]; - if (!overwriteAllColor && !CompareCarefully(uiVertex.color, targetGraphic.color)) + uiVertexColor = vertexListColors[i]; + if (!overwriteAllColor && !CompareCarefully(uiVertexColor, targetGraphic.color)) continue; switch (gradientDir) { case GradientDir.Vertical: - uiVertex.color *= (i % 4 == 0 || (i - 1) % 4 == 0) ? vertex1 : vertex2; + uiVertexColor *= (i % 4 == 0 || (i - 1) % 4 == 0) ? vertex1 : vertex2; break; case GradientDir.Horizontal: - uiVertex.color *= (i % 4 == 0 || (i - 3) % 4 == 0) ? vertex1 : vertex2; + uiVertexColor *= (i % 4 == 0 || (i - 3) % 4 == 0) ? vertex1 : vertex2; break; case GradientDir.DiagonalLeftToRight: - uiVertex.color *= (i % 4 == 0) ? vertex1 : ((i - 2) % 4 == 0 ? vertex2 : Color.Lerp(vertex2, vertex1, 0.5f)); + uiVertexColor *= (i % 4 == 0) ? vertex1 : ((i - 2) % 4 == 0 ? vertex2 : Color.Lerp(vertex2, vertex1, 0.5f)); break; case GradientDir.DiagonalRightToLeft: - uiVertex.color *= ((i - 1) % 4 == 0) ? vertex1 : ((i - 3) % 4 == 0 ? vertex2 : Color.Lerp(vertex2, vertex1, 0.5f)); + uiVertexColor *= ((i - 1) % 4 == 0) ? vertex1 : ((i - 3) % 4 == 0 ? vertex2 : Color.Lerp(vertex2, vertex1, 0.5f)); break; } - vertexList[i] = uiVertex; + vertexListColors[i] = uiVertexColor; } } + mesh.colors = vertexListColors; } private bool CompareCarefully(Color col1, Color col2) { diff --git a/Scripts/LetterSpacing.cs b/Scripts/LetterSpacing.cs index cf6335f..4f1d38d 100644 --- a/Scripts/LetterSpacing.cs +++ b/Scripts/LetterSpacing.cs @@ -46,7 +46,7 @@ using System.Collections.Generic; namespace UnityEngine.UI.Extensions { [AddComponentMenu("UI/Effects/Extensions/Letter Spacing")] - public class LetterSpacing : BaseVertexEffect + public class LetterSpacing : BaseMeshEffect { [SerializeField] private float m_spacing = 0f; @@ -72,11 +72,13 @@ namespace UnityEngine.UI.Extensions } } - public override void ModifyVertices(List verts) + public override void ModifyMesh(Mesh mesh) { if (! IsActive()) return; - - Text text = GetComponent(); + + Vector3[] verts = mesh.vertices; + + Text text = GetComponent(); if (text == null) { Debug.LogWarning("LetterSpacing: Missing Text component"); @@ -123,19 +125,19 @@ namespace UnityEngine.UI.Extensions int idx4 = glyphIdx * 4 + 3; // Check for truncated text (doesn't generate verts for all characters) - if (idx4 > verts.Count - 1) return; + if (idx4 > verts.Length - 1) return; - UIVertex vert1 = verts[idx1]; - UIVertex vert2 = verts[idx2]; - UIVertex vert3 = verts[idx3]; - UIVertex vert4 = verts[idx4]; + Vector3 vert1 = verts[idx1]; + Vector3 vert2 = verts[idx2]; + Vector3 vert3 = verts[idx3]; + Vector3 vert4 = verts[idx4]; pos = Vector3.right * (letterOffset * charIdx - lineOffset); - vert1.position += pos; - vert2.position += pos; - vert3.position += pos; - vert4.position += pos; + vert1 += pos; + vert2 += pos; + vert3 += pos; + vert4 += pos; verts[idx1] = vert1; verts[idx2] = vert2; @@ -148,6 +150,7 @@ namespace UnityEngine.UI.Extensions // Offset for carriage return character that still generates verts glyphIdx++; } - } + mesh.vertices = verts; + } } } diff --git a/Scripts/NicerOutline.cs b/Scripts/NicerOutline.cs index d951602..797d69f 100644 --- a/Scripts/NicerOutline.cs +++ b/Scripts/NicerOutline.cs @@ -6,7 +6,7 @@ namespace UnityEngine.UI.Extensions { //An outline that looks a bit nicer than the default one. It has less "holes" in the outline by drawing more copies of the effect [AddComponentMenu("UI/Effects/Extensions/Nicer Outline")] - public class NicerOutline : BaseVertexEffect + public class NicerOutline : BaseMeshEffect { [SerializeField] private Color m_EffectColor = new Color (0f, 0f, 0f, 0.5f); @@ -86,48 +86,55 @@ namespace UnityEngine.UI.Extensions } } } - - // - // Methods - // - protected void ApplyShadow (List verts, Color32 color, int start, int end, float x, float y) - { - //Debug.Log("verts count: "+verts.Count); - int num = verts.Count * 2; - if (verts.Capacity < num) - { - verts.Capacity = num; - } - for (int i = start; i < end; i++) - { - UIVertex uIVertex = verts [i]; - verts.Add (uIVertex); + protected void ApplyShadowZeroAlloc(List verts, Color32 color, int start, int end, float x, float y) + { + UIVertex vt; - Vector3 position = uIVertex.position; - //Debug.Log("vertex pos: "+position); - position.x += x; - position.y += y; - uIVertex.position = position; - Color32 color2 = color; - if (this.m_UseGraphicAlpha) - { - color2.a = (byte)(color2.a * verts [i].color.a / 255); - } - uIVertex.color = color2; - //uIVertex.color = (Color32)Color.blue; - verts [i] = uIVertex; - } - } - - public override void ModifyVertices (List verts) + var neededCpacity = verts.Count * 2; + if (verts.Capacity < neededCpacity) + verts.Capacity = neededCpacity; + + for (int i = start; i < end; ++i) + { + vt = verts[i]; + verts.Add(vt); + + Vector3 v = vt.position; + v.x += x; + v.y += y; + vt.position = v; + var newColor = color; + if (m_UseGraphicAlpha) + newColor.a = (byte)((newColor.a * verts[i].color.a) / 255); + vt.color = newColor; + verts[i] = vt; + } + } + + protected void ApplyShadow(List verts, Color32 color, int start, int end, float x, float y) + { + var neededCpacity = verts.Count * 2; + if (verts.Capacity < neededCpacity) + verts.Capacity = neededCpacity; + + ApplyShadowZeroAlloc(verts, color, start, end, x, y); + } + + + public override void ModifyMesh (Mesh mesh) { if (!this.IsActive ()) { return; } + List < UIVertex > verts = new List(); + using (var helper = new VertexHelper(mesh)) + { + helper.GetUIVertexStream(verts); + } - Text foundtext = GetComponent(); + Text foundtext = GetComponent(); float best_fit_adjustment = 1f; @@ -166,7 +173,13 @@ namespace UnityEngine.UI.Extensions start = count; count = verts.Count; this.ApplyShadow (verts, this.effectColor, start, verts.Count, 0, -distanceY); - } + + using (var helper = new VertexHelper()) + { + helper.AddUIVertexTriangleStream(verts); + helper.FillMesh(mesh); + } + } #if UNITY_EDITOR protected override void OnValidate () diff --git a/Scripts/UIFlippable.cs b/Scripts/UIFlippable.cs index 1fc87e2..cb9ec4d 100644 --- a/Scripts/UIFlippable.cs +++ b/Scripts/UIFlippable.cs @@ -7,7 +7,7 @@ namespace UnityEngine.UI.Extensions { [RequireComponent(typeof(RectTransform), typeof(Graphic)), DisallowMultipleComponent] [AddComponentMenu("UI/Effects/Extensions/Flippable")] - public class UIFlippable : MonoBehaviour, IVertexModifier + public class UIFlippable : MonoBehaviour, IMeshModifier { [SerializeField] private bool m_Horizontal = false; [SerializeField] private bool m_Veritical = false; @@ -37,24 +37,27 @@ namespace UnityEngine.UI.Extensions this.GetComponent().SetVerticesDirty(); } - public void ModifyVertices(List verts) + public void ModifyMesh(Mesh mesh) { + Vector3[] verts = mesh.vertices; RectTransform rt = this.transform as RectTransform; - for (int i = 0; i < verts.Count; ++i) + for (int i = 0; i < verts.Length; ++i) { - UIVertex v = verts[i]; + Vector3 v = verts[i]; // Modify positions - v.position = new Vector3( - (this.m_Horizontal ? (v.position.x + (rt.rect.center.x - v.position.x) * 2) : v.position.x), - (this.m_Veritical ? (v.position.y + (rt.rect.center.y - v.position.y) * 2) : v.position.y), - v.position.z + v = new Vector3( + (this.m_Horizontal ? (v.x + (rt.rect.center.x - v.x) * 2) : v.x), + (this.m_Veritical ? (v.y + (rt.rect.center.y - v.y) * 2) : v.y), + v.z ); // Apply verts[i] = v; } + + mesh.vertices = verts; } } } \ No newline at end of file