This repository has been archived on 2026-07-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
UIGraphRenderer/Runtime/Renderer/XericRendererComponent.cs
T
lrss3 903a7938b3 改进ui多边形渲染组件功能。
改进渲染器基类流程。
改进多边形shader逻辑。
2026-03-30 09:27:19 +08:00

149 lines
4.4 KiB
C#

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Pool;
using UnityEngine.UI;
namespace XericLibrary
{
/// <summary>
/// 自定义渲染组件
/// </summary>
[RequireComponent(typeof(CanvasRenderer))]
public class XericRendererComponent : MaskableGraphic
{
public virtual string ShaderName => "Hidden/Texture2D";
#region 字段属性
// 原始数据
private readonly List<UIVertex> _vertexList = new List<UIVertex>();
private readonly List<int> _indexList = new List<int>();
// 阵列原点
private readonly List<Vector4> _arrayTransformList = new List<Vector4>();
// 中间数据
private List<UIVertex> _temp_vertexList = new List<UIVertex>();
private List<int> _temp_indexList = new List<int>();
private bool _dirty;
#endregion
#if UNITY_EDITOR
protected override void OnValidate()
{
base.OnValidate();
if (m_Material == null)
material = new Material(Shader.Find(ShaderName));
}
#endif
#region 绘制
/// <summary>
/// 刷新
/// </summary>
public virtual void Refresh()
{
SetVerticesDirty();
}
/// <summary>
/// ui模型填充流程
/// </summary>
/// <param name="vh"></param>
protected override void OnPopulateMesh(VertexHelper vh)
{
ClearCache();
// 填充图元
OnPopulatePrefabricateVertex(_vertexList, _indexList);
// 阵列
ArrayVertexList(_arrayTransformList);
// 将顶点填充到mesh
vh.AddUIVertexStream(_temp_vertexList, _temp_indexList);
}
/// <summary>
/// 填充预制顶点
/// </summary>
/// <param name="vertexList">顶点列表</param>
/// <param name="indexList">三角面索引</param>
protected virtual void OnPopulatePrefabricateVertex(List<UIVertex> vertexList, List<int> indexList)
{
}
/// <summary>
/// 清除所有缓存
/// </summary>
protected void ClearCache()
{
_vertexList.Clear();
_indexList.Clear();
_arrayTransformList.Clear();
}
/// <summary>
/// 添加一个阵列节点
/// </summary>
/// <param name="offset">偏移</param>
/// <param name="angle">角度</param>
protected void AddArrayModifier(Vector3 offset, float angle)
{
_arrayTransformList.Add(new Vector4(offset.x, offset.y, offset.z, angle));
}
#endregion
#region 阵列
/// <summary>
/// 根据原点阵列
/// </summary>
/// <param name="vh"></param>
/// <param name="trans"></param>
protected void ArrayVertexList(List<Vector4> trans)
{
if (trans == null || trans.Count == 0)
{
Copy();
return;
}
foreach (var t in trans)
Copy2(t);
void Copy()
{
_temp_vertexList = _vertexList.GetRange(0, _vertexList.Count);
_temp_indexList = _indexList.GetRange(0, _indexList.Count);
}
void Copy2(Vector4 transform)
{
_temp_vertexList = _vertexList.Select(a =>
{
var newPos = new Vector3(a.position.x, a.position.y, a.position.z);
if (a.position.z != 0)
{
var rot = Quaternion.Euler(0, 0, a.position.z);
newPos = rot * newPos;
}
newPos += (Vector3)transform;
return new UIVertex()
{
position = newPos,
normal = a.normal,
tangent = a.tangent,
color = a.color,
uv0 = a.uv0,
uv1 = a.uv1,
uv2 = a.uv2,
uv3 = a.uv3,
};
}).ToList();
_temp_indexList = _indexList.GetRange(0, _indexList.Count);
}
}
#endregion
}
}