149 lines
4.8 KiB
C#
149 lines
4.8 KiB
C#
//----------------------------------------------
|
|
// NGUI: Next-Gen UI kit
|
|
// Copyright © 2011-2015 Tasharen Entertainment
|
|
//----------------------------------------------
|
|
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
|
|
public class UIGeometry
|
|
{
|
|
/// <summary>
|
|
/// Widget's vertices (before they get transformed).
|
|
/// </summary>
|
|
|
|
public BetterList<Vector3> verts = new BetterList<Vector3>();
|
|
|
|
/// <summary>
|
|
/// Widget's texture coordinates for the geometry's vertices.
|
|
/// </summary>
|
|
|
|
public BetterList<Vector2> uvs = new BetterList<Vector2>();
|
|
|
|
/// <summary>
|
|
/// Array of colors for the geometry's vertices.
|
|
/// </summary>
|
|
|
|
public BetterList<Color32> cols = new BetterList<Color32>();
|
|
|
|
// Relative-to-panel vertices, normal, and tangent
|
|
public BetterList<Vector3> mRtpVerts = new BetterList<Vector3>();
|
|
|
|
Vector3 mRtpNormal;
|
|
Vector4 mRtpTan;
|
|
|
|
/// <summary>
|
|
/// Whether the geometry contains usable vertices.
|
|
/// </summary>
|
|
|
|
public bool hasVertices { get { return (verts != null && verts.size > 0); } }
|
|
|
|
/// <summary>
|
|
/// Whether the geometry has usable transformed vertex data.
|
|
/// </summary>
|
|
|
|
public bool hasTransformed { get { return (mRtpVerts != null) && (mRtpVerts.size > 0) && (mRtpVerts.size == verts.size); } }
|
|
|
|
/// <summary>
|
|
/// Step 1: Prepare to fill the buffers -- make them clean and valid.
|
|
/// </summary>
|
|
|
|
public void Clear()
|
|
{
|
|
if (verts != null) verts.Clear();
|
|
if (uvs != null) uvs.Clear();
|
|
if (cols != null) cols.Clear();
|
|
if (mRtpVerts != null) mRtpVerts.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Step 2: Transform the vertices by the provided matrix.
|
|
/// </summary>
|
|
|
|
public void ApplyTransform(Matrix4x4 widgetToPanel, bool generateNormals, float height, float width)
|
|
{
|
|
mRtpVerts.Clear();
|
|
if (verts.size > 0)
|
|
{
|
|
//如果不是整体处理
|
|
mRtpVerts.AddRange(verts);
|
|
var buffer = mRtpVerts.buffer;
|
|
for (int i = 0, imax = verts.size; i < imax; ++i)
|
|
{
|
|
buffer[i] = widgetToPanel.MultiplyPoint3x4(buffer[i]);
|
|
}
|
|
// Calculate the widget's normal and tangent
|
|
if (generateNormals)
|
|
{
|
|
mRtpNormal = widgetToPanel.MultiplyVector(Vector3.back).normalized;
|
|
Vector3 tangent = widgetToPanel.MultiplyVector(Vector3.right).normalized;
|
|
mRtpTan = new Vector4(tangent.x, tangent.y, tangent.z, -1f);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Step 3: Fill the specified buffer using the transformed values.
|
|
/// </summary>
|
|
|
|
public void WriteToBuffers(BetterList<Vector3> v, BetterList<Vector2> u, BetterList<Color32> c, BetterList<Vector3> n, BetterList<Vector4> t, float alpha)
|
|
{
|
|
if (mRtpVerts != null && mRtpVerts.size > 0)
|
|
{
|
|
v.AddRange(mRtpVerts);
|
|
u.AddRange(uvs);
|
|
var cOriSize = c.size;
|
|
c.AddRange(cols);
|
|
var colBuffer = c.buffer;
|
|
for(int i = cOriSize; i < c.size; ++i)
|
|
{
|
|
colBuffer[i].a = (byte)(colBuffer[i].a * alpha);
|
|
}
|
|
if(n != null)
|
|
{
|
|
for (int i = 0; i < mRtpVerts.size; ++i)
|
|
{
|
|
n.Add(mRtpNormal);
|
|
t.Add(mRtpTan);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//清理缓存
|
|
public void FreeCache()
|
|
{
|
|
CachedGeometries.Free(verts);
|
|
CachedGeometries.Free(uvs);
|
|
CachedGeometries.Free(cols);
|
|
CachedGeometries.Free(mRtpVerts);
|
|
verts = null;
|
|
uvs = null;
|
|
cols = null;
|
|
mRtpVerts = null;
|
|
}
|
|
//检查缓存
|
|
public void CheckCacke(int vecCount)
|
|
{
|
|
if (verts == null || verts.buffer == null || verts.buffer.Length < vecCount)
|
|
{
|
|
FreeCache();
|
|
CachedGeometries.Get(vecCount, ref verts);
|
|
CachedGeometries.Get(vecCount, ref uvs);
|
|
CachedGeometries.Get(vecCount, ref cols);
|
|
CachedGeometries.Get(vecCount, ref mRtpVerts);
|
|
}
|
|
}
|
|
}
|