67 lines
2.7 KiB
C#
67 lines
2.7 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using System.Collections.Generic;
|
||
/// <summary>
|
||
/// 特殊3段变色,上段和下段为纯色,中段有渐变效果
|
||
/// </summary>
|
||
public class UIGradient3Slice : BaseMeshEffect
|
||
{
|
||
public Color topColor = Color.white;
|
||
public Color bottomColor = Color.black;
|
||
public float topHeight = 0.225f;
|
||
public float bottomHeight = 0.225f;
|
||
|
||
public override void ModifyMesh(VertexHelper vh)
|
||
{
|
||
if (!IsActive())
|
||
return;
|
||
List<UIVertex> vertList = new List<UIVertex>(vh.currentVertCount);
|
||
vh.GetUIVertexStream(vertList);
|
||
vh.Clear();
|
||
// 按每四个顶点为一个四边形
|
||
for (int i = 0; i < vertList.Count; i += 6)
|
||
{
|
||
// 分解为8个顶点
|
||
UIVertex top_0 = vertList[i];
|
||
UIVertex top_1 = vertList[i + 1];
|
||
UIVertex bottom_0 = vertList[i + 4];
|
||
UIVertex bottom_1 = vertList[i + 2];
|
||
UIVertex midUp_0 = LerpVertex(bottom_0, top_0, 1f - topHeight);
|
||
UIVertex midUp_1 = LerpVertex(bottom_1, top_1, 1f - topHeight);
|
||
UIVertex midDown_0 = LerpVertex(bottom_0, top_0, bottomHeight);
|
||
UIVertex midDown_1 = LerpVertex(bottom_1, top_1, bottomHeight);
|
||
top_0.color = topColor;
|
||
top_1.color = topColor;
|
||
midUp_0.color = topColor;
|
||
midUp_1.color = topColor;
|
||
midDown_0.color = bottomColor;
|
||
midDown_1.color = bottomColor;
|
||
bottom_0.color = bottomColor;
|
||
bottom_1.color = bottomColor;
|
||
// 重新组装为3个四边形
|
||
WriteQuad(vh, top_0, top_1, midUp_0, midUp_1);
|
||
WriteQuad(vh, midUp_0, midUp_1, midDown_0, midDown_1);
|
||
WriteQuad(vh, midDown_0, midDown_1, bottom_0, bottom_1);
|
||
}
|
||
}
|
||
public UIVertex LerpVertex(UIVertex a, UIVertex b, float ratio)
|
||
{
|
||
UIVertex result = new UIVertex();
|
||
result.position = Vector3.Lerp(a.position, b.position, ratio);
|
||
result.uv0 = Vector3.Lerp(a.uv0, b.uv0, ratio);
|
||
result.uv1 = Vector3.Lerp(a.uv1, b.uv1, ratio);
|
||
result.normal = Vector3.Lerp(a.normal, b.normal, ratio);
|
||
result.tangent = Vector3.Lerp(a.tangent, b.tangent, ratio);
|
||
return result;
|
||
}
|
||
public void WriteQuad(VertexHelper vh, UIVertex top_0, UIVertex top_1, UIVertex bottom_0, UIVertex bottom_1)
|
||
{
|
||
int startIndex = vh.currentVertCount;
|
||
vh.AddVert(top_0);
|
||
vh.AddVert(top_1);
|
||
vh.AddVert(bottom_1);
|
||
vh.AddVert(bottom_0);
|
||
vh.AddTriangle(startIndex, startIndex + 1, startIndex + 2);
|
||
vh.AddTriangle(startIndex, startIndex + 3, startIndex + 2);
|
||
}
|
||
} |