ProjectorPlane.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. [CustomEditor(typeof(ProjectorPlane))]
  7. public class ProjectorPlaneEditor : Editor
  8. {
  9. public override void OnInspectorGUI()
  10. {
  11. serializedObject.Update();
  12. EditorGUI.BeginChangeCheck();
  13. {
  14. EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Radius"));
  15. EditorGUILayout.PropertyField(serializedObject.FindProperty("m_DotPitch"));
  16. EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Suspend"));
  17. EditorGUILayout.PropertyField(serializedObject.FindProperty("m_CreateOnStart"));
  18. if (EditorGUI.EndChangeCheck())
  19. {
  20. serializedObject.ApplyModifiedProperties();
  21. }
  22. }
  23. if (Application.isPlaying && GUILayout.Button("Projector"))
  24. (target as ProjectorPlane).Projector();
  25. }
  26. }
  27. #endif
  28. [RequireComponent(typeof(MeshFilter))]
  29. public class ProjectorPlane : MonoBehaviour
  30. {
  31. [SerializeField]
  32. private float m_Radius = 10;//半径
  33. [SerializeField]
  34. private float m_DotPitch = 1;//点间距
  35. [SerializeField]
  36. private float m_Suspend = 0.01f;
  37. [SerializeField]
  38. private bool m_CreateOnStart;
  39. private Mesh m_Mesh;
  40. public float Radius
  41. {
  42. get
  43. {
  44. return m_Radius;
  45. }
  46. set
  47. {
  48. m_Radius = Mathf.Max(0.1f, value);
  49. }
  50. }
  51. public float DotPitch
  52. {
  53. get
  54. {
  55. return m_DotPitch;
  56. }
  57. set
  58. {
  59. m_DotPitch = Mathf.Max(0.1f, value);
  60. }
  61. }
  62. public float Suspend
  63. {
  64. get
  65. {
  66. return m_Suspend;
  67. }
  68. set
  69. {
  70. m_Suspend = value;
  71. }
  72. }
  73. public bool CreateOnStart
  74. {
  75. get
  76. {
  77. return m_CreateOnStart;
  78. }
  79. set
  80. {
  81. m_CreateOnStart = value;
  82. }
  83. }
  84. public Mesh Mesh
  85. {
  86. get
  87. {
  88. return m_Mesh;
  89. }
  90. set
  91. {
  92. m_Mesh = value;
  93. }
  94. }
  95. private List<int> tris;
  96. private List<Vector3> verties;
  97. private List<Vector2> uvs;
  98. private int vertCount;
  99. MeshCollider mc;
  100. private Ray ray = new Ray();
  101. private RaycastHit hit;
  102. private Vector3 vertWorldPos;
  103. private bool needUpdate = false;
  104. private void Start()
  105. {
  106. Radius = Radius;
  107. DotPitch = DotPitch;
  108. if (CreateOnStart)
  109. CreateMesh();
  110. //mc = GameSceneMgr.Instance.NavMeshCollider;
  111. }
  112. private void OnDestroy()
  113. {
  114. if (Mesh)
  115. Destroy(Mesh);
  116. }
  117. /// <summary>
  118. /// 生成一个投影面。
  119. /// </summary>
  120. public void Projector()
  121. {
  122. if (Mesh)
  123. UpdateMesh();
  124. else
  125. CreateMesh();
  126. }
  127. /// <summary>
  128. /// 新建一个网格。
  129. /// </summary>
  130. private void CreateMesh()
  131. {
  132. if (Mesh)
  133. Destroy(Mesh);
  134. Mesh = new Mesh();
  135. Mesh.MarkDynamic();
  136. Mesh.name = "ProjectorPlane";
  137. GetComponent<MeshFilter>().mesh = Mesh;
  138. int[] vertCountInColumn;
  139. SetVerties(out vertCountInColumn);
  140. SetUVs();
  141. SetTriangles(vertCountInColumn);
  142. UpdateMesh();
  143. }
  144. /// <summary>
  145. /// 更新当前网格。
  146. /// </summary>
  147. private void UpdateMesh()
  148. {
  149. if (UpdateMeshVerties())
  150. {
  151. Mesh.SetVertices(verties);
  152. Mesh.SetUVs(0, uvs);
  153. Mesh.SetTriangles(tris, 0);
  154. }
  155. }
  156. /// <summary>
  157. /// 计算顶点位置。
  158. /// </summary>
  159. /// <param name="vertCountInColumn">每列顶点数。</param>
  160. private void SetVerties(out int[] vertCountInColumn)
  161. {
  162. verties = new List<Vector3>();
  163. float hUnit = Mathf.Cos(Mathf.PI / 6) * DotPitch;
  164. int vertVerticalMax = Mathf.FloorToInt(2 * Radius / DotPitch) + 1;
  165. int vertColOneSide = Mathf.FloorToInt(Radius / hUnit);
  166. vertCountInColumn = new int[vertColOneSide * 2 + 1];
  167. float hOffset;
  168. for (int col = 0, cp = -vertColOneSide; cp <= vertColOneSide; col++, cp++)//从左至右
  169. {
  170. vertCountInColumn[col] = vertVerticalMax - Mathf.Abs(cp);//每一竖列的点数目
  171. hOffset = cp * hUnit;
  172. if (vertCountInColumn[col] % 2 == 0)//双数
  173. {
  174. for (int i = 1, n = vertCountInColumn[col] / 2; i <= n; i++)
  175. {
  176. verties.Add(new Vector3(hOffset, 0, DotPitch * (i - 0.5f)));
  177. verties.Add(new Vector3(hOffset, 0, -DotPitch * (i - 0.5f)));
  178. }
  179. }
  180. else//单数
  181. {
  182. verties.Add(new Vector3(hOffset, 0, 0));
  183. for (int i = 1, n = (vertCountInColumn[col] - 1) / 2; i <= n; i++)
  184. {
  185. verties.Add(new Vector3(hOffset, 0, DotPitch * i));
  186. verties.Add(new Vector3(hOffset, 0, -DotPitch * i));
  187. }
  188. }
  189. }
  190. verties.Sort(CompareVert);
  191. vertCount = verties.Count;
  192. }
  193. /// <summary>
  194. /// 网格UV对齐。
  195. /// </summary>
  196. private void SetUVs()
  197. {
  198. uvs = new List<Vector2>();
  199. verties.ForEach(Vert2UV);
  200. }
  201. /// <summary>
  202. /// 多边形绘制顺序。
  203. /// </summary>
  204. /// <param name="vertCountInColumn">每列顶点数。</param>
  205. private void SetTriangles(int[] vertCountInColumn)
  206. {
  207. tris = new List<int>();
  208. for (int col = 0, vid = 0, n = vertCountInColumn.Length; col < n - 1; col++)
  209. {
  210. if (col < n / 2)//在左半区时
  211. {
  212. for (int i = 0; i < vertCountInColumn[col]; i++, vid++)
  213. {
  214. //有右侧上方点
  215. tris.Add(vid);//当前点
  216. tris.Add(vid + vertCountInColumn[col] + 1);//右列上方点
  217. tris.Add(vid + vertCountInColumn[col]);//右列同位点
  218. if (i < vertCountInColumn[col] - 1)//非每列最高点时
  219. {
  220. tris.Add(vid);//当前点
  221. tris.Add(vid + 1);//上方点
  222. tris.Add(vid + vertCountInColumn[col] + 1);//右列上方点
  223. }
  224. }
  225. }
  226. else
  227. {
  228. for (int i = 0; i < vertCountInColumn[col]; i++, vid++)
  229. {
  230. if (i < vertCountInColumn[col] - 1)
  231. {
  232. if (i > 0)//右半区的最低点和最高点都不做右侧绘制
  233. {
  234. tris.Add(vid);//当前点
  235. tris.Add(vid + vertCountInColumn[col]);//右列同位点
  236. tris.Add(vid + vertCountInColumn[col] - 1);//右列下方点
  237. }
  238. tris.Add(vid);//当前点
  239. tris.Add(vid + 1);//上方点
  240. tris.Add(vid + vertCountInColumn[col]);//右列同位点
  241. }
  242. }
  243. }
  244. }
  245. }
  246. /// <summary>
  247. /// 更新网格点的高度。
  248. /// </summary>
  249. /// <returns>网格需要重绘。</returns>
  250. private bool UpdateMeshVerties()
  251. {
  252. needUpdate = false;
  253. if (!mc)
  254. return false;
  255. for (int i = 0; i < vertCount; i++)
  256. {
  257. vertWorldPos = transform.TransformPoint(verties[i]);
  258. vertWorldPos.y = mc.bounds.max.y;
  259. ray.origin = vertWorldPos;
  260. ray.direction = Vector3.down;
  261. if (mc.Raycast(ray, out hit, mc.bounds.size.y))
  262. {
  263. vertWorldPos.y = hit.point.y + Suspend;
  264. verties[i] = transform.InverseTransformPoint(vertWorldPos);
  265. needUpdate = true;
  266. }
  267. }
  268. return needUpdate;
  269. }
  270. private static int CompareVert(Vector3 a, Vector3 b)
  271. {
  272. int comp = a.x.CompareTo(b.x);
  273. if (comp == 0)
  274. comp = a.z.CompareTo(b.z);
  275. return comp;
  276. }
  277. private void Vert2UV(Vector3 v)
  278. {
  279. uvs.Add((new Vector2((v.x + Radius), v.z + Radius)) / (2 * Radius));
  280. }
  281. }