基类:
using System.Collections; using System.Collections.Generic; using UnityEngine; public abstract class MeshDrawBase : MonoBehaviour { protected MeshFilter targetFilter; protected Mesh mh; protected int[] tris; protected Vector2[] uvs; protected Vector3[] nornals; private void Awake() { targetFilter = GetComponent<MeshFilter>(); } protected virtual void Update() { } protected abstract void DrawMesh(); }
画出顶点:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MeshInfoImationPrint : MeshDrawBase { protected override void DrawMesh() { } private void OnDrawGizmos()//编辑器模式调用 { targetFilter = GetComponent<MeshFilter>(); mh = targetFilter.sharedMesh; Gizmos.color = Color.red; for (int i = 0; i < mh.vertices.Length; i++) { Vector3 worldPoint = transform.TransformPoint(mh.vertices[i]);//局部坐标转成世界坐标 Gizmos.DrawSphere(worldPoint, .1f); } } }
画三角形:
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 画三角形 /// </summary> [RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshRenderer))] public class TriangleDraw : MeshDrawBase { public List<Vector3> vts = new List<Vector3>(); private void Start() { mh = new Mesh(); //获取顶点 //世界坐标转换成局部坐标 for (int i = 0; i < vts.Count; i++) { vts[i] = transform.InverseTransformPoint(vts[i]); } mh.vertices = vts.ToArray(); //三角形 tris = new int[3]; tris[0] = 0; tris[1] = 1; tris[2] = 2; mh.triangles = tris; //uv uvs = new Vector2[vts.Count]; uvs[0] = new Vector2(0, 0); uvs[1] = new Vector2(0, 1); uvs[2] = new Vector2(1, 0); mh.uv = uvs; mh.RecalculateBounds(); mh.RecalculateNormals();//法线 mh.RecalculateTangents();//切线 targetFilter.mesh = mh; } protected override void DrawMesh() { } }
画多边形:
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 画多边形 /// </summary> [RequireComponent(typeof(MeshFilter))]//挂脚本时,自动添加组件 [RequireComponent(typeof(MeshRenderer))] public class CreatManyTriabgle : MeshDrawBase { private List<Vector3> vts = new List<Vector3>(); protected override void DrawMesh() { } private void DrawPolygon() { mh = new Mesh(); //顶点 mh.vertices = vts.ToArray(); //三角形 int trislength = (vts.Count - 2) * 3; tris = new int[trislength]; for (int i = 0,n=1; i < trislength; i+=3,n++) { tris[i] = 0; tris[i + 1] = n; tris[i + 2] = n + 1; } mh.triangles = tris; //法线 nornals = new Vector3[vts.Count]; for (int i = 0; i < vts.Count; i++) { nornals[i] = new Vector3(0, 0, 1); } mh.normals = nornals; mh.RecalculateBounds(); mh.RecalculateTangents(); targetFilter.mesh = mh; } protected override void Update() { base.Update(); if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray,out hit, Mathf.Infinity)) { Vector3 worldHitPoint = hit.point; //世界转成局部坐标 Vector3 localHitPoint = transform.InverseTransformPoint(worldHitPoint); vts.Add(localHitPoint); } } if (Input.GetKeyDown(KeyCode.R)) { Reset(); } if (Input.GetKeyDown(KeyCode.S)) { // Debug.LogError(111); DrawPolygon(); } } private void Reset()//重置 { vts.Clear(); targetFilter.mesh = null; Destroy(mh); } private void OnGUI()//显示数字序号 { if (vts.Count == 0) return; GUI.color = Color.red; for (int i = 0; i < vts.Count; i++) { Vector3 worldPoint = transform.TransformPoint(vts[i]); Vector3 screenPoint = Camera.main.WorldToScreenPoint(worldPoint); Vector3 uiPoint = new Vector3(screenPoint.x, Camera.main.pixelHeight - screenPoint.y, screenPoint.z); GUI.Label(new Rect(uiPoint, new Vector3(100, 80)), i.ToString()); } } private void OnDrawGizmos()//画线 { if (vts.Count == 0) return; Gizmos.color = Color.cyan;//金色 for (int i = 0; i < vts.Count; i++) { //转成世界坐标 Vector3 worldHitPoint = transform.TransformPoint(vts[i]); Gizmos.DrawSphere(worldHitPoint, .2f); } } }
画正方体:
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 画正方体 /// </summary> [RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshRenderer))] public class DrawMeshCube : MeshDrawBase { public List<Vector3> vts = new List<Vector3>(); private void Start() { mh = new Mesh(); //顶点 mh.vertices = vts.ToArray(); //三角形 tris = new int[] { 0,3,1, 3,2,1,//下 0,4,7, 0,7,3,//前 3,7,2,//右 2,7,6, 2,6,1,//后 6,5,1, 1,5,4, 1,4,0,//左 7,4,5, 7,5,6//上 }; mh.triangles = tris; mh.RecalculateBounds(); mh.RecalculateNormals(); mh.RecalculateTangents(); targetFilter.mesh = mh; } protected override void DrawMesh() { } private void OnDrawGizmos() { if (vts.Count == 0) return; Gizmos.color = Color.red;//点的颜色 for (int i = 0; i < vts.Count; i++) { Vector3 worldPoint = transform.TransformPoint(vts[i]);//转成世界坐标 Gizmos.DrawSphere(worldPoint, .1f);//画点 } } }