1、
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class DamageAreaDraw : BehaviourBase
{
private Mesh mesh;
void Start()
{
GetComponent<MeshFilter>().mesh = mesh = new Mesh();
mesh.name = "areaMesh";
}
//srcPosi,所需绘制的坐标。
private void AdjustPosition(Vector3 srcPosi)
{
srcPosi.y += 0.1f;
transform.position = srcPosi;
}
//绘制直线
public void DrawLine(Vector3 srcPosi, float pLineWidth, float pLineLenth, float div_s, float div_e)
{
mesh.Clear();
AdjustPosition(srcPosi);
Vector3[] vertics = new Vector3[12];
vertics[0] = new Vector3(pLineWidth / 2, 0, 0);
vertics[1] = new Vector3(-pLineWidth / 2, 0, 0);
vertics[2] = new Vector3(-pLineWidth / 2, 0, pLineLenth * div_s);
vertics[3] = new Vector3(pLineWidth / 2, 0, pLineLenth * div_s);
vertics[4] = new Vector3(pLineWidth / 2, 0, pLineLenth * div_s);
vertics[5] = new Vector3(-pLineWidth / 2, 0, pLineLenth * div_s);
vertics[6] = new Vector3(-pLineWidth / 2, 0, pLineLenth * (1 - div_e));
vertics[7] = new Vector3(pLineWidth / 2, 0, pLineLenth * (1 - div_e));
vertics[8] = new Vector3(pLineWidth / 2, 0, pLineLenth * (1 - div_e));
vertics[9] = new Vector3(-pLineWidth / 2, 0, pLineLenth * (1 - div_e));
vertics[10] = new Vector3(-pLineWidth / 2, 0, pLineLenth);
vertics[11] = new Vector3(pLineWidth / 2, 0, pLineLenth);
mesh.vertices = vertics;
mesh.subMeshCount = 6;
int[] triangle = new int[] { 0, 1, 2 };
mesh.SetTriangles(triangle, 0);
triangle = new int[] { 0, 2, 3 };
mesh.SetTriangles(triangle, 1);
triangle = new int[] { 4, 5, 6 };
mesh.SetTriangles(triangle, 2);
triangle = new int[] { 4, 6, 7 };
mesh.SetTriangles(triangle, 3);
triangle = new int[] { 8, 9, 10 };
mesh.SetTriangles(triangle, 4);
triangle = new int[] { 8, 10, 11 };
mesh.SetTriangles(triangle, 5);
Vector2[] uvs = new Vector2[12];
uvs[0] = new Vector2(0, 0);
uvs[1] = new Vector2(1, 0);
uvs[2] = new Vector2(1, 1);
uvs[3] = new Vector2(0, 1);
uvs[4] = new Vector2(0, 0);
uvs[5] = new Vector2(1, 0);
uvs[6] = new Vector2(1, 1);
uvs[7] = new Vector2(0, 1);
uvs[8] = new Vector2(0, 0);
uvs[9] = new Vector2(1, 0);
uvs[10] = new Vector2(1, 1);
uvs[11] = new Vector2(0, 1);
mesh.SetUVs(0, uvs.ToDynList());
mesh.RecalculateBounds();
}
//绘制矩形范围
public void DrawRect(Vector3 srcPosi, float pRectWidth, float pRectHeight, float Divide, float uvdive)
{
mesh.Clear();
AdjustPosition(srcPosi);
float start_w = pRectWidth / 2;
Vector3[] vertics = new Vector3[16];
vertics[0] = new Vector3(start_w, 0, 0);
vertics[1] = new Vector3(start_w - pRectWidth * Divide, 0, 0);
vertics[2] = new Vector3(start_w - pRectWidth * Divide, 0, pRectHeight * Divide);
vertics[3] = new Vector3(start_w, 0, pRectHeight * Divide);
vertics[4] = new Vector3(-start_w + pRectWidth * Divide, 0, 0);
vertics[5] = new Vector3(-start_w, 0, 0);
vertics[6] = new Vector3(-start_w, 0, pRectHeight * Divide);
vertics[7] = new Vector3(-start_w + pRectWidth * Divide, 0, pRectHeight * Divide);
vertics[8] = new Vector3(start_w, 0, pRectHeight - pRectHeight * Divide);
vertics[9] = new Vector3(start_w - pRectWidth * Divide, 0, pRectHeight - pRectHeight * Divide);
vertics[10] = new Vector3(start_w - pRectWidth * Divide, 0, pRectHeight);
vertics[11] = new Vector3(start_w, 0, pRectHeight);
vertics[12] = new Vector3(-start_w + pRectWidth * Divide, 0, pRectHeight - pRectHeight * Divide);
vertics[13] = new Vector3(-start_w, 0, pRectHeight - pRectHeight * Divide);
vertics[14] = new Vector3(-start_w, 0, pRectHeight);
vertics[15] = new Vector3(-start_w + pRectWidth * Divide, 0, pRectHeight);
Vector2[] uvs = new Vector2[16];
uvs[0] = new Vector2(0, 1);
uvs[1] = new Vector2(uvdive, 1);
uvs[2] = new Vector2(uvdive, 1 - uvdive);
uvs[3] = new Vector2(0, 1 - uvdive);
uvs[4] = new Vector2(1 - uvdive, 1);
uvs[5] = new Vector2(1, 1);
uvs[6] = new Vector2(1, 1 - uvdive);
uvs[7] = new Vector2(1 - uvdive, 1 - uvdive);
uvs[8] = new Vector2(0, uvdive);
uvs[9] = new Vector2(uvdive, uvdive);
uvs[10] = new Vector2(uvdive, 0);
uvs[11] = new Vector2(0, 0);
uvs[12] = new Vector2(1 - uvdive, uvdive);
uvs[13] = new Vector2(1, uvdive);
uvs[14] = new Vector2(1, 0);
uvs[15] = new Vector2(1 - uvdive, 0);
int[] indics = new int[54];
indics[0] = 0; indics[1] = 1; indics[2] = 2;
indics[3] = 0; indics[4] = 2; indics[5] = 3;
indics[6] = 1; indics[7] = 4; indics[8] = 7;
indics[9] = 1; indics[10] = 7; indics[11] = 2;
indics[12] = 4; indics[13] = 5; indics[14] = 6;
indics[15] = 4; indics[16] = 6; indics[17] = 7;
indics[18] = 3; indics[19] = 2; indics[20] = 9;
indics[21] = 3; indics[22] = 9; indics[23] = 8;
indics[24] = 2; indics[25] = 7; indics[26] = 12;
indics[27] = 2; indics[28] = 12; indics[29] = 9;
indics[30] = 7; indics[31] = 6; indics[32] = 13;
indics[33] = 7; indics[34] = 13; indics[35] = 12;
indics[36] = 8; indics[37] = 9; indics[38] = 10;
indics[39] = 8; indics[40] = 10; indics[41] = 11;
indics[42] = 9; indics[43] = 12; indics[44] = 15;
indics[45] = 9; indics[46] = 15; indics[47] = 10;
indics[48] = 12; indics[49] = 13; indics[50] = 14;
indics[51] = 12; indics[52] = 14; indics[53] = 15;
mesh.vertices = vertics;
mesh.triangles = indics;
mesh.uv = uvs;
mesh.RecalculateBounds();
}
public void DrawCircle(Vector3 srcPosi, float radiusLong, float radiusShort, float angle, bool useMiniAngle)
{
mesh.Clear();
// 设置位置
AdjustPosition(srcPosi);
// 旋转矩阵
Matrix4x4 matrix = GetYAxisMatrix(1);
//内圈
Vector3 posShort = new Vector3(radiusShort, 0, 0);
// 外圈
Vector3 posLong = new Vector3(radiusLong, 0, 0);
//定点数组
List<Vector3> verticsTemp = new List<Vector3>();
Vector3 posShort0;
Vector3 posLonge0;
verticsTemp.Add(posShort);
verticsTemp.Add(posLong);
for (int i = 0; i < angle / 1; i++)
{
posShort0 = matrix.MultiplyVector(posShort);
posLonge0 = matrix.MultiplyVector(posLong);
verticsTemp.Add(posShort0);
verticsTemp.Add(posLonge0);
posShort = posShort0;
posLong = posLonge0;
}
List<Vector3> vertics = new List<Vector3>();
List<int> triangles = new List<int>();
for (int i = 2; i < verticsTemp.Count; i++)
{
vertics.Add(verticsTemp[i - 2]);
vertics.Add(verticsTemp[i - 1]);
vertics.Add(verticsTemp[i]);
}
// 三角形设置
int triangleCount = vertics.Count / 3;
for (int i = 0; i < triangleCount; i++)
{
if (i % 2 == 0)
{
triangles.Add(i * 3);
triangles.Add(i * 3 + 1);
triangles.Add(i * 3 + 2);
}
else
{
triangles.Add(i * 3 + 2);
triangles.Add(i * 3 + 1);
triangles.Add(i * 3);
}
}
// 旋转模型
for (int i = 0; i < vertics.Count; i++)
{
vertics[i] = Quaternion.Euler(0, 270 - angle / 2, 0) * vertics[i];
}
// 设置uv
float uv_start = 0.25f;
float uv_end = 0.75f;
float m = (vertics.Count - 4) / 2;
float d = uv_end - uv_start;
float deta = d / m;
Vector2[] uvs = new Vector2[vertics.Count];
for (int i = 0; i < vertics.Count; ++i)
{
if (i == 0)
{
uvs[i] = new Vector2(0, 0);
}
else if (i == 1 || i == 3) // 1、3定点重合
{
uvs[i] = new Vector2(0, 1);
}
else if (i == 2 || i == 4 || i == 6) // 2,4,6重合
{
uvs[i] = new Vector2(uv_start, 0);
}
else if (i == 5 || i == 7 || i == 9) // 5,7,9重合
{
uvs[i] = new Vector2(uv_start, 1);
}
else if (i == vertics.Count - 1) // 尾部边界
{
uvs[i] = new Vector2(1, 1);
}
else if (i == vertics.Count - 2)
{
uvs[i] = new Vector2(1, 0);
}
else if (i == vertics.Count - 3)
{
uvs[i] = new Vector2(uv_end, 1);
}
else if (i == vertics.Count - 4)
{
uvs[i] = new Vector2(uv_end, 0);
}
else // 其他定点纹理寻址
{
int n = i / 2;
float u = n * deta;
if (i % 2 == 0)
{
uvs[i] = new Vector2(uv_start + u, 0);
}
else
{
uvs[i] = new Vector2(uv_start + u, 1);
}
}
}
mesh.vertices = vertics.ToArray();
mesh.triangles = triangles.ToArray();
mesh.uv = uvs;
mesh.RecalculateBounds();
}
//
public Matrix4x4 GetYAxisMatrix(float angle)
{
Matrix4x4 matrix = new Matrix4x4();
matrix.m00 = Mathf.Cos(angle / 180 * Mathf.PI);
matrix.m02 = Mathf.Sin(angle / 180 * Mathf.PI);
matrix.m20 = -Mathf.Sin(angle / 180 * Mathf.PI);
matrix.m22 = Mathf.Cos(angle / 180 * Mathf.PI);
matrix.m11 = 1;
return matrix;
}
public void ClearMeshInfo()
{
mesh.Clear();
}
}
2备忘