//---------------------------------------------- // NGUI: Next-Gen UI kit // Copyright © 2011-2015 Tasharen Entertainment //---------------------------------------------- using UnityEngine; using System.Collections.Generic; /// /// Generated geometry class. All widgets have one. /// This class separates the geometry creation into several steps, making it possible to perform /// actions selectively depending on what has changed. For example, the widget doesn't need to be /// rebuilt unless something actually changes, so its geometry can be cached. Likewise, the widget's /// transformed coordinates only change if the widget's transform moves relative to the panel, /// so that can be cached as well. In the end, using this class means using more memory, but at /// the same time it allows for significant performance gains, especially when using widgets that /// spit out a lot of vertices, such as UILabels. /// public class UIGeometry { /// /// Widget's vertices (before they get transformed). /// public BetterList verts = new BetterList(); /// /// Widget's texture coordinates for the geometry's vertices. /// public BetterList uvs = new BetterList(); /// /// Array of colors for the geometry's vertices. /// public BetterList cols = new BetterList(); // Relative-to-panel vertices, normal, and tangent BetterList mRtpVerts = new BetterList(); Vector3 mRtpNormal; Vector4 mRtpTan; /// /// Whether the geometry contains usable vertices. /// public bool hasVertices { get { return (verts.size > 0); } } /// /// Whether the geometry has usable transformed vertex data. /// public bool hasTransformed { get { return (mRtpVerts != null) && (mRtpVerts.size > 0) && (mRtpVerts.size == verts.size); } } /// /// Step 1: Prepare to fill the buffers -- make them clean and valid. /// public void Clear () { verts.Clear(); uvs.Clear(); cols.Clear(); mRtpVerts.Clear(); } /// /// Step 2: Transform the vertices by the provided matrix. /// public void ApplyTransform (Matrix4x4 widgetToPanel) { if (verts.size > 0) { mRtpVerts.Clear(); for (int i = 0, imax = verts.size; i < imax; ++i) mRtpVerts.Add(widgetToPanel.MultiplyPoint3x4(verts[i])); // Calculate the widget's normal and tangent mRtpNormal = widgetToPanel.MultiplyVector(Vector3.back).normalized; Vector3 tangent = widgetToPanel.MultiplyVector(Vector3.right).normalized; mRtpTan = new Vector4(tangent.x, tangent.y, tangent.z, -1f); } else mRtpVerts.Clear(); } /// /// Step 3: Fill the specified buffer using the transformed values. /// public void WriteToBuffers (BetterList v, BetterList u, BetterList c, BetterList n, BetterList t) { if (mRtpVerts != null && mRtpVerts.size > 0) { if (n == null) { for (int i = 0; i < mRtpVerts.size; ++i) { v.Add(mRtpVerts.buffer[i]); u.Add(uvs.buffer[i]); c.Add(cols.buffer[i]); } } else { for (int i = 0; i < mRtpVerts.size; ++i) { v.Add(mRtpVerts.buffer[i]); u.Add(uvs.buffer[i]); c.Add(cols.buffer[i]); n.Add(mRtpNormal); t.Add(mRtpTan); } } } } }