Archived
244 lines
7.5 KiB
C#
244 lines
7.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.Pool;
|
|
using UnityEngine.UI;
|
|
using XericLibrary.Runtime.MacroLibrary;
|
|
|
|
namespace XericLibrary.Runtime.UIGraph
|
|
{
|
|
/// <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 List<Vector4> _arrayTransformList = new List<Vector4>();
|
|
private List<Color32> _arrayColorList = new List<Color32>();
|
|
// 中间数据
|
|
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)
|
|
{
|
|
if (vh == null) return;
|
|
vh.Clear();
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加一个阵列节点
|
|
/// </summary>
|
|
/// <param name="offset">偏移</param>
|
|
/// <param name="angle">角度</param>
|
|
public int AddArrayModifier(Vector2 offset, float angle, float size)
|
|
{
|
|
var index = _arrayTransformList.Count;
|
|
_arrayTransformList.Add(new Vector4(offset.x, offset.y, angle, size));
|
|
Refresh();
|
|
return index;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置阵列节点
|
|
/// </summary>
|
|
/// <param name="trans"></param>
|
|
public void SetArrayModifier(IList<Vector4> trans)
|
|
{
|
|
if (trans == null)
|
|
return;
|
|
_arrayTransformList.Clear();
|
|
_arrayTransformList.AddRange(trans);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清空阵列节点
|
|
/// </summary>
|
|
public void ClearArrayModifier()
|
|
{
|
|
_arrayTransformList.Clear();
|
|
Refresh();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改阵列节点
|
|
/// </summary>
|
|
/// <param name="index"></param>
|
|
/// <param name="offset"></param>
|
|
/// <param name="angle"></param>
|
|
/// <param name="size"></param>
|
|
/// <returns></returns>
|
|
public bool ModifyArrayModifier(int index, Vector2 offset, float angle, float size)
|
|
{
|
|
if (index < 0 || index >= _arrayTransformList.Count)
|
|
return false;
|
|
_arrayTransformList[index] = new Vector4(offset.x, offset.y, angle, size);
|
|
Refresh();
|
|
return true;
|
|
}
|
|
|
|
public IList<Vector4> GetArrayModifier() => _arrayTransformList;
|
|
|
|
/// <summary>
|
|
/// 添加颜色
|
|
/// </summary>
|
|
/// <param name="color"></param>
|
|
public void AddColor(Color color) => _arrayColorList.Add(color);
|
|
|
|
/// <summary>
|
|
/// 添加颜色
|
|
/// </summary>
|
|
/// <param name="color"></param>
|
|
public void AddColor(Color32 color) => _arrayColorList.Add(color);
|
|
|
|
/// <summary>
|
|
/// 设置颜色节点
|
|
/// </summary>
|
|
/// <param name="index"></param>
|
|
/// <param name="color"></param>
|
|
public void SetColor(int index, Color color) => _arrayColorList.SetAtIndex<Color32>(color, index);
|
|
|
|
/// <summary>
|
|
/// 设置颜色节点
|
|
/// </summary>
|
|
/// <param name="index"></param>
|
|
/// <param name="color"></param>
|
|
public void SetColor(int index, Color32 color) => _arrayColorList.SetAtIndex<Color32>(color, index);
|
|
|
|
/// <summary>
|
|
/// 清空颜色
|
|
/// </summary>
|
|
public void ClearArrayColor() => _arrayColorList.Clear();
|
|
|
|
/// <summary>
|
|
/// 获取颜色列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public IList<Color32> GetArrayColor() => _arrayColorList;
|
|
|
|
#endregion
|
|
|
|
#region 阵列
|
|
|
|
/// <summary>
|
|
/// 根据原点阵列
|
|
/// </summary>
|
|
/// <param name="vh"></param>
|
|
/// <param name="trans"></param>
|
|
protected void ArrayVertexList(List<Vector4> trans)
|
|
{
|
|
_temp_vertexList.Clear();
|
|
_temp_indexList.Clear();
|
|
|
|
if (trans == null || trans.Count == 0)
|
|
{
|
|
Copy();
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < trans.Count; i++)
|
|
Copy2(i, trans[i]);
|
|
|
|
void Copy()
|
|
{
|
|
_temp_vertexList.AddRange(_vertexList.GetRange(0, _vertexList.Count));
|
|
_temp_indexList.AddRange(_indexList.GetRange(0, _indexList.Count));
|
|
}
|
|
void Copy2(int index, Vector4 transform)
|
|
{
|
|
_temp_vertexList.AddRange(_vertexList.Select(a =>
|
|
{
|
|
Vector2 newPos = a.position;
|
|
if (transform.z != 0)
|
|
{
|
|
var rot = Quaternion.Euler(0, 0, transform.z);
|
|
newPos = rot * newPos;
|
|
}
|
|
if (transform.z > 0)
|
|
newPos *= transform.w;
|
|
newPos += (Vector2)transform;
|
|
var newColor = a.color;
|
|
if (index < _arrayColorList.Count && !default(Color32).Equals(_arrayColorList[index]))
|
|
{
|
|
newColor = _arrayColorList[index];
|
|
}
|
|
|
|
return new UIVertex()
|
|
{
|
|
position = newPos,
|
|
normal = a.normal,
|
|
tangent = a.tangent,
|
|
color = newColor,
|
|
uv0 = a.uv0,
|
|
uv1 = a.uv1,
|
|
uv2 = a.uv2,
|
|
uv3 = a.uv3,
|
|
};
|
|
}));
|
|
_temp_indexList.AddRange(_indexList.Select(a => index * _vertexList.Count + a));
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |