Archived
175 lines
5.1 KiB
C#
175 lines
5.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.Pool;
|
|
using UnityEngine.UI;
|
|
|
|
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<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(Vector3 offset, float angle)
|
|
{
|
|
var index = _arrayTransformList.Count;
|
|
_arrayTransformList.Add(new Vector4(offset.x, offset.y, offset.z, angle));
|
|
Refresh();
|
|
return index;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清空阵列节点
|
|
/// </summary>
|
|
public void ClearArrayModifier()
|
|
{
|
|
_arrayTransformList.Clear();
|
|
Refresh();
|
|
}
|
|
|
|
public bool ModifyArrayModifier(int index, Vector3 offset, float angle)
|
|
{
|
|
if (index < 0 || index >= _arrayTransformList.Count)
|
|
return false;
|
|
_arrayTransformList[index] = new Vector4(offset.x, offset.y, offset.z, angle);
|
|
Refresh();
|
|
return true;
|
|
}
|
|
|
|
#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 =>
|
|
{
|
|
var newPos = a.position;
|
|
if (transform.w != 0)
|
|
{
|
|
var rot = Quaternion.Euler(0, 0, transform.w);
|
|
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,
|
|
};
|
|
}));
|
|
_temp_indexList.AddRange(_indexList.Select(a => index * _vertexList.Count + a));
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |