分享

Unity中使用填充Mesh顶点的方法绘制伤害区域范围

 勤奋不止 2017-05-05

using
UnityEngine; using System.Collections; using System.Collections.Generic; [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] //public static class meshDraw : MonoBehaviour { public class MeshDraw : MonoBehaviour { private Vector3 point = Vector3.right; private int numberOfPoint = 10; private Mesh mesh; private Vector3[] vertices; private int[] triangles; private Material meshMaterial; private float lastTime = 1f; private Material _material; //public Transform heroTransform; public static Color COLOR_GREEN = new Color (1,0,0,0.2f); public static Color COLOR_RED = new Color (1,0,0,0.2f); void Start () { GetComponent<MeshFilter> ().mesh = mesh = new Mesh (); mesh.name = "areaMesh"; _material = GetComponent<MeshRenderer>().material; _material.color = COLOR_GREEN; }
   //旧版的绘制圆形方法,留下来做一个参考 // public void DrawCicle()// Old one.Abandened! // { // mesh.Clear (); // vertices = new Vector3[numberOfPoint + 1]; // triangles = new int[numberOfPoint * 3]; // float angle = - 360f / numberOfPoint; // for (int v = 1, t = 1; v <vertices.Length; v++, t = t + 3) { // vertices [v] = Quaternion.Euler (0f, 0f, angle * (v - 1)) * point; // triangles[t] = v; // triangles [t + 1] = v + 1; // } // triangles [triangles.Length - 1] = 1; // mesh.vertices = vertices; // mesh.triangles = triangles; // mesh.RecalculateBounds (); // } //   //srcPosi,所需绘制的坐标。 private void AdjustPosition(Vector3 srcPosi) { srcPosi.y += 0.1f; transform.position = srcPosi; }
  //绘制矩形范围 public void DrawRect(Vector3 srcPosi, float pRectWidth, float pRectHeight) { mesh.Clear (); AdjustPosition(srcPosi); Vector3[] vertics = new Vector3[4]; vertics [0] = new Vector3 (pRectWidth / 2, 0, 0); vertics [1] = new Vector3 (-pRectWidth / 2, 0, 0); vertics [2] = new Vector3 (-pRectWidth / 2, 0, pRectHeight); vertics [3] = new Vector3 (pRectWidth / 2, 0, pRectHeight); int[] indics = new int[6]; indics [0] = 0; indics [1] = 1; indics [2] = 2; indics [3] = 0; indics [4] = 2; indics [5] = 3; mesh.vertices = vertics; mesh.triangles = indics; mesh.RecalculateBounds (); StartCoroutine (ClearMeshInfo ()); }
  //绘制直线,采用宽度为0.1的矩形进行模拟 public void DrawLine(Vector3 srcPosi, float pLineLenth) { DrawRect (srcPosi, 0.1f, pLineLenth); }   //绘制梯形,和绘制矩形基本上一个思路。 public void DrawTrapezoid(Vector3 srcPosi, float pRectWidth, float pRectHeight, float cutLength) { mesh.Clear (); AdjustPosition(srcPosi); Vector3[] vertics = new Vector3[4]; vertics [0] = new Vector3 (pRectWidth / 2 - cutLength, 0, 0); vertics [1] = new Vector3 (-pRectWidth / 2 + cutLength, 0, 0); vertics [2] = new Vector3 (-pRectWidth / 2, 0, pRectHeight); vertics [3] = new Vector3 (pRectWidth / 2, 0, pRectHeight); int[] indics = new int[6]; indics [0] = 0; indics [1] = 1; indics [2] = 2; indics [3] = 0; indics [4] = 2; indics [5] = 3; mesh.vertices = vertics; mesh.triangles = indics; mesh.RecalculateBounds (); StartCoroutine (ClearMeshInfo ()); } public void SetColor(Color color) { _material.color = color; }   
  //绘制圆形,扇形,圆环,部分圆环。
  //radiusLong 外径的长度,
  //radiusShort 内径的长度,圆形, 扇形的时候,此值为0
  //angle 绘制角度,圆形,圆环为360, 其他情况根据需要设置
  //useMiniAngle 预留参数,未实现功能。 public void DrawSector(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]; } mesh.vertices = vertics.ToArray(); mesh.triangles = triangles.ToArray(); mesh.RecalculateBounds(); StartCoroutine (ClearMeshInfo ()); }   // 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; }

  //延迟清除顶点信息的协程 IEnumerator ClearMeshInfo() { yield return new WaitForSeconds (lastTime); mesh.Clear (); } }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多