using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.Pool; using UnityEngine.UI; using XericLibrary.Runtime.MacroLibrary; namespace XericLibrary.Runtime.UIGraph { /// /// 自定义渲染组件 /// [RequireComponent(typeof(CanvasRenderer))] public class XericRendererComponent : MaskableGraphic { public virtual string ShaderName => "Hidden/Texture2D"; #region 字段属性 // 原始数据 private readonly List _vertexList = new List(); private readonly List _indexList = new List(); // 阵列原点 private List _arrayTransformList = new List(); private List _arrayColorList = new List(); // 中间数据 private List _temp_vertexList = new List(); private List _temp_indexList = new List(); 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 绘制 /// /// 刷新 /// public virtual void Refresh() { SetVerticesDirty(); } /// /// ui模型填充流程 /// /// 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); } /// /// 填充预制顶点 /// /// 顶点列表 /// 三角面索引 protected virtual void OnPopulatePrefabricateVertex(List vertexList, List indexList) { } /// /// 清除所有缓存 /// protected void ClearCache() { _vertexList.Clear(); _indexList.Clear(); } /// /// 添加一个阵列节点 /// /// 偏移 /// 角度 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; } /// /// 设置阵列节点 /// /// public void SetArrayModifier(IList trans) { if (trans == null) return; _arrayTransformList.Clear(); _arrayTransformList.AddRange(trans); } /// /// 清空阵列节点 /// public void ClearArrayModifier() { _arrayTransformList.Clear(); Refresh(); } /// /// 修改阵列节点 /// /// /// /// /// /// 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 GetArrayModifier() => _arrayTransformList; /// /// 添加颜色 /// /// public void AddColor(Color color) => _arrayColorList.Add(color); /// /// 添加颜色 /// /// public void AddColor(Color32 color) => _arrayColorList.Add(color); /// /// 设置颜色节点 /// /// /// public void SetColor(int index, Color color) => _arrayColorList.SetAtIndex(color, index); /// /// 设置颜色节点 /// /// /// public void SetColor(int index, Color32 color) => _arrayColorList.SetAtIndex(color, index); /// /// 清空颜色 /// public void ClearArrayColor() => _arrayColorList.Clear(); /// /// 获取颜色列表 /// /// public IList GetArrayColor() => _arrayColorList; #endregion #region 阵列 /// /// 根据原点阵列 /// /// /// protected void ArrayVertexList(List 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 } }