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;
}
//舊版的繪制圓形方法,留下來做一個(gè)參考
// 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,所需繪制的坐標(biāo)。
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的矩形進(jìn)行模擬
public void DrawLine(Vector3 srcPosi, float pLineLenth)
{
DrawRect (srcPosi, 0.1f, pLineLenth);
}
//繪制梯形,和繪制矩形基本上一個(gè)思路。
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;
}
//繪制圓形,,扇形,圓環(huán),,部分圓環(huán),。 //radiusLong 外徑的長(zhǎng)度, //radiusShort 內(nèi)徑的長(zhǎng)度,,圓形,, 扇形的時(shí)候,此值為0 //angle 繪制角度,,圓形,,圓環(huán)為360, 其他情況根據(jù)需要設(shè)置 //useMiniAngle 預(yù)留參數(shù),,未實(shí)現(xiàn)功能,。
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;
}
//延遲清除頂點(diǎn)信息的協(xié)程
IEnumerator ClearMeshInfo()
{
yield return new WaitForSeconds (lastTime);
mesh.Clear ();
}
}
|