GridEditorUtility.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityEngine.Tilemaps;
  6. using Event = UnityEngine.Event;
  7. namespace UnityEditor.Tilemaps
  8. {
  9. internal static class GridEditorUtility
  10. {
  11. private const int k_GridGizmoVertexCount = 32000;
  12. private const float k_GridGizmoDistanceFalloff = 50f;
  13. public static Vector3Int ClampToGrid(Vector3Int p, Vector2Int origin, Vector2Int gridSize)
  14. {
  15. return new Vector3Int(
  16. Math.Max(Math.Min(p.x, origin.x + gridSize.x - 1), origin.x),
  17. Math.Max(Math.Min(p.y, origin.y + gridSize.y - 1), origin.y),
  18. p.z
  19. );
  20. }
  21. public static Vector3 ScreenToLocal(Transform transform, Vector2 screenPosition)
  22. {
  23. return ScreenToLocal(transform, screenPosition, new Plane(transform.forward * -1f, transform.position));
  24. }
  25. public static Vector3 ScreenToLocal(Transform transform, Vector2 screenPosition, Plane plane)
  26. {
  27. Ray ray;
  28. if (Camera.current.orthographic)
  29. {
  30. Vector2 screen = EditorGUIUtility.PointsToPixels(GUIClip.Unclip(screenPosition));
  31. screen.y = Screen.height - screen.y;
  32. Vector3 cameraWorldPoint = Camera.current.ScreenToWorldPoint(screen);
  33. ray = new Ray(cameraWorldPoint, Camera.current.transform.forward);
  34. }
  35. else
  36. {
  37. ray = HandleUtility.GUIPointToWorldRay(screenPosition);
  38. }
  39. float result;
  40. plane.Raycast(ray, out result);
  41. Vector3 world = ray.GetPoint(result);
  42. return transform.InverseTransformPoint(world);
  43. }
  44. public static RectInt GetMarqueeRect(Vector2Int p1, Vector2Int p2)
  45. {
  46. return new RectInt(
  47. Math.Min(p1.x, p2.x),
  48. Math.Min(p1.y, p2.y),
  49. Math.Abs(p2.x - p1.x) + 1,
  50. Math.Abs(p2.y - p1.y) + 1
  51. );
  52. }
  53. public static BoundsInt GetMarqueeBounds(Vector3Int p1, Vector3Int p2)
  54. {
  55. return new BoundsInt(
  56. Math.Min(p1.x, p2.x),
  57. Math.Min(p1.y, p2.y),
  58. Math.Min(p1.z, p2.z),
  59. Math.Abs(p2.x - p1.x) + 1,
  60. Math.Abs(p2.y - p1.y) + 1,
  61. Math.Abs(p2.z - p1.z) + 1
  62. );
  63. }
  64. // http://ericw.ca/notes/bresenhams-line-algorithm-in-csharp.html
  65. public static IEnumerable<Vector2Int> GetPointsOnLine(Vector2Int p1, Vector2Int p2)
  66. {
  67. int x0 = p1.x;
  68. int y0 = p1.y;
  69. int x1 = p2.x;
  70. int y1 = p2.y;
  71. bool steep = Math.Abs(y1 - y0) > Math.Abs(x1 - x0);
  72. if (steep)
  73. {
  74. int t;
  75. t = x0; // swap x0 and y0
  76. x0 = y0;
  77. y0 = t;
  78. t = x1; // swap x1 and y1
  79. x1 = y1;
  80. y1 = t;
  81. }
  82. if (x0 > x1)
  83. {
  84. int t;
  85. t = x0; // swap x0 and x1
  86. x0 = x1;
  87. x1 = t;
  88. t = y0; // swap y0 and y1
  89. y0 = y1;
  90. y1 = t;
  91. }
  92. int dx = x1 - x0;
  93. int dy = Math.Abs(y1 - y0);
  94. int error = dx / 2;
  95. int ystep = (y0 < y1) ? 1 : -1;
  96. int y = y0;
  97. for (int x = x0; x <= x1; x++)
  98. {
  99. yield return new Vector2Int((steep ? y : x), (steep ? x : y));
  100. error = error - dy;
  101. if (error < 0)
  102. {
  103. y += ystep;
  104. error += dx;
  105. }
  106. }
  107. }
  108. public static void DrawBatchedHorizontalLine(float x1, float x2, float y)
  109. {
  110. GL.Vertex3(x1, y, 0f);
  111. GL.Vertex3(x2, y, 0f);
  112. GL.Vertex3(x2, y + 1, 0f);
  113. GL.Vertex3(x1, y + 1, 0f);
  114. }
  115. public static void DrawBatchedVerticalLine(float y1, float y2, float x)
  116. {
  117. GL.Vertex3(x, y1, 0f);
  118. GL.Vertex3(x, y2, 0f);
  119. GL.Vertex3(x + 1, y2, 0f);
  120. GL.Vertex3(x + 1, y1, 0f);
  121. }
  122. public static void DrawBatchedLine(Vector3 p1, Vector3 p2)
  123. {
  124. GL.Vertex3(p1.x, p1.y, p1.z);
  125. GL.Vertex3(p2.x, p2.y, p2.z);
  126. }
  127. public static void DrawLine(Vector2 p1, Vector2 p2, Color color)
  128. {
  129. if (Event.current.type != EventType.Repaint)
  130. return;
  131. HandleUtility.ApplyWireMaterial();
  132. GL.PushMatrix();
  133. GL.MultMatrix(GUI.matrix);
  134. GL.Begin(GL.LINES);
  135. GL.Color(color);
  136. DrawBatchedLine(p1, p2);
  137. GL.End();
  138. GL.PopMatrix();
  139. }
  140. public static void DrawBox(Rect r, Color color)
  141. {
  142. if (Event.current.type != EventType.Repaint)
  143. return;
  144. HandleUtility.ApplyWireMaterial();
  145. GL.PushMatrix();
  146. GL.MultMatrix(GUI.matrix);
  147. GL.Begin(GL.LINES);
  148. GL.Color(color);
  149. DrawBatchedLine(new Vector3(r.xMin, r.yMin, 0f), new Vector3(r.xMax, r.yMin, 0f));
  150. DrawBatchedLine(new Vector3(r.xMax, r.yMin, 0f), new Vector3(r.xMax, r.yMax, 0f));
  151. DrawBatchedLine(new Vector3(r.xMax, r.yMax, 0f), new Vector3(r.xMin, r.yMax, 0f));
  152. DrawBatchedLine(new Vector3(r.xMin, r.yMax, 0f), new Vector3(r.xMin, r.yMin, 0f));
  153. GL.End();
  154. GL.PopMatrix();
  155. }
  156. public static void DrawFilledBox(Rect r, Color color)
  157. {
  158. if (Event.current.type != EventType.Repaint)
  159. return;
  160. HandleUtility.ApplyWireMaterial();
  161. GL.PushMatrix();
  162. GL.MultMatrix(GUI.matrix);
  163. GL.Begin(GL.QUADS);
  164. GL.Color(color);
  165. GL.Vertex3(r.xMin, r.yMin, 0f);
  166. GL.Vertex3(r.xMax, r.yMin, 0f);
  167. GL.Vertex3(r.xMax, r.yMax, 0f);
  168. GL.Vertex3(r.xMin, r.yMax, 0f);
  169. GL.End();
  170. GL.PopMatrix();
  171. }
  172. public static void DrawGridMarquee(GridLayout gridLayout, BoundsInt area, Color color)
  173. {
  174. switch (gridLayout.cellLayout)
  175. {
  176. case GridLayout.CellLayout.Hexagon:
  177. DrawSelectedHexGridArea(gridLayout, area, color);
  178. break;
  179. case GridLayout.CellLayout.Isometric:
  180. case GridLayout.CellLayout.IsometricZAsY:
  181. case GridLayout.CellLayout.Rectangle:
  182. var cellStride = gridLayout.cellSize + gridLayout.cellGap;
  183. var cellGap = Vector3.one;
  184. if (!Mathf.Approximately(cellStride.x, 0f))
  185. {
  186. cellGap.x = gridLayout.cellSize.x / cellStride.x;
  187. }
  188. if (!Mathf.Approximately(cellStride.y, 0f))
  189. {
  190. cellGap.y = gridLayout.cellSize.y / cellStride.y;
  191. }
  192. Vector3[] cellLocals =
  193. {
  194. gridLayout.CellToLocal(new Vector3Int(area.xMin, area.yMin, area.zMin)),
  195. gridLayout.CellToLocalInterpolated(new Vector3(area.xMax - 1 + cellGap.x, area.yMin, area.zMin)),
  196. gridLayout.CellToLocalInterpolated(new Vector3(area.xMax - 1 + cellGap.x, area.yMax - 1 + cellGap.y, area.zMin)),
  197. gridLayout.CellToLocalInterpolated(new Vector3(area.xMin, area.yMax - 1 + cellGap.y, area.zMin))
  198. };
  199. HandleUtility.ApplyWireMaterial();
  200. GL.PushMatrix();
  201. GL.MultMatrix(gridLayout.transform.localToWorldMatrix);
  202. GL.Begin(GL.LINES);
  203. GL.Color(color);
  204. int i = 0;
  205. for (int j = cellLocals.Length - 1; i < cellLocals.Length; j = i++)
  206. DrawBatchedLine(cellLocals[j], cellLocals[i]);
  207. GL.End();
  208. GL.PopMatrix();
  209. break;
  210. }
  211. }
  212. public static void DrawSelectedHexGridArea(GridLayout gridLayout, BoundsInt area, Color color)
  213. {
  214. int requiredVertices = 4 * (area.size.x + area.size.y) - 2;
  215. if (requiredVertices < 0)
  216. return;
  217. Vector3[] cellLocals = new Vector3[requiredVertices];
  218. int horizontalCount = area.size.x * 2;
  219. int verticalCount = area.size.y * 2 - 1;
  220. int bottom = 0;
  221. int top = horizontalCount + verticalCount + horizontalCount - 1;
  222. int left = requiredVertices - 1;
  223. int right = horizontalCount;
  224. Vector3[] cellOffset =
  225. {
  226. Grid.Swizzle(gridLayout.cellSwizzle, new Vector3(0, gridLayout.cellSize.y / 2, area.zMin)),
  227. Grid.Swizzle(gridLayout.cellSwizzle, new Vector3(gridLayout.cellSize.x / 2, gridLayout.cellSize.y / 4, area.zMin)),
  228. Grid.Swizzle(gridLayout.cellSwizzle, new Vector3(gridLayout.cellSize.x / 2, -gridLayout.cellSize.y / 4, area.zMin)),
  229. Grid.Swizzle(gridLayout.cellSwizzle, new Vector3(0, -gridLayout.cellSize.y / 2, area.zMin)),
  230. Grid.Swizzle(gridLayout.cellSwizzle, new Vector3(-gridLayout.cellSize.x / 2, -gridLayout.cellSize.y / 4, area.zMin)),
  231. Grid.Swizzle(gridLayout.cellSwizzle, new Vector3(-gridLayout.cellSize.x / 2, gridLayout.cellSize.y / 4, area.zMin))
  232. };
  233. // Fill Top and Bottom Vertices
  234. for (int x = area.min.x; x < area.max.x; x++)
  235. {
  236. cellLocals[bottom++] = gridLayout.CellToLocal(new Vector3Int(x, area.min.y, area.zMin)) + cellOffset[4];
  237. cellLocals[bottom++] = gridLayout.CellToLocal(new Vector3Int(x, area.min.y, area.zMin)) + cellOffset[3];
  238. cellLocals[top--] = gridLayout.CellToLocal(new Vector3Int(x, area.max.y - 1, area.zMin)) + cellOffset[0];
  239. cellLocals[top--] = gridLayout.CellToLocal(new Vector3Int(x, area.max.y - 1, area.zMin)) + cellOffset[1];
  240. }
  241. // Fill first Left and Right Vertices
  242. cellLocals[left--] = gridLayout.CellToLocal(new Vector3Int(area.min.x, area.min.y, area.zMin)) + cellOffset[5];
  243. cellLocals[top--] = gridLayout.CellToLocal(new Vector3Int(area.max.x - 1, area.max.y - 1, area.zMin)) + cellOffset[2];
  244. // Fill Left and Right Vertices
  245. for (int y = area.min.y + 1; y < area.max.y; y++)
  246. {
  247. cellLocals[left--] = gridLayout.CellToLocal(new Vector3Int(area.min.x, y, area.zMin)) + cellOffset[4];
  248. cellLocals[left--] = gridLayout.CellToLocal(new Vector3Int(area.min.x, y, area.zMin)) + cellOffset[5];
  249. }
  250. for (int y = area.min.y; y < (area.max.y - 1); y++)
  251. {
  252. cellLocals[right++] = gridLayout.CellToLocal(new Vector3Int(area.max.x - 1, y, area.zMin)) + cellOffset[2];
  253. cellLocals[right++] = gridLayout.CellToLocal(new Vector3Int(area.max.x - 1, y, area.zMin)) + cellOffset[1];
  254. }
  255. HandleUtility.ApplyWireMaterial();
  256. GL.PushMatrix();
  257. GL.MultMatrix(gridLayout.transform.localToWorldMatrix);
  258. GL.Begin(GL.LINES);
  259. GL.Color(color);
  260. int i = 0;
  261. for (int j = cellLocals.Length - 1; i < cellLocals.Length; j = i++)
  262. {
  263. DrawBatchedLine(cellLocals[j], cellLocals[i]);
  264. }
  265. GL.End();
  266. GL.PopMatrix();
  267. }
  268. public static void DrawGridGizmo(GridLayout gridLayout, Transform transform, Color color, ref Mesh gridMesh, ref Material gridMaterial)
  269. {
  270. // TODO: Hook this up with DrawGrid
  271. if (Event.current.type != EventType.Repaint)
  272. return;
  273. if (gridMesh == null)
  274. gridMesh = GenerateCachedGridMesh(gridLayout, color);
  275. if (gridMaterial == null)
  276. {
  277. gridMaterial = (Material)EditorGUIUtility.LoadRequired("SceneView/GridGap.mat");
  278. }
  279. if (gridLayout.cellLayout == GridLayout.CellLayout.Hexagon)
  280. {
  281. gridMaterial.SetVector("_Gap", new Vector4(1f, 1f / 3f, 1f, 1f));
  282. gridMaterial.SetVector("_Stride", new Vector4(1f, 1f, 1f, 1f));
  283. }
  284. else
  285. {
  286. gridMaterial.SetVector("_Gap", gridLayout.cellSize);
  287. gridMaterial.SetVector("_Stride", gridLayout.cellGap + gridLayout.cellSize);
  288. }
  289. gridMaterial.SetPass(0);
  290. GL.PushMatrix();
  291. if (gridMesh.GetTopology(0) == MeshTopology.Lines)
  292. GL.Begin(GL.LINES);
  293. else
  294. GL.Begin(GL.QUADS);
  295. Graphics.DrawMeshNow(gridMesh, transform.localToWorldMatrix);
  296. GL.End();
  297. GL.PopMatrix();
  298. }
  299. public static Vector3 GetSpriteWorldSize(Sprite sprite)
  300. {
  301. if (sprite != null && sprite.rect.size.magnitude > 0f)
  302. {
  303. return new Vector3(
  304. sprite.rect.size.x / sprite.pixelsPerUnit,
  305. sprite.rect.size.y / sprite.pixelsPerUnit,
  306. 1f
  307. );
  308. }
  309. return Vector3.one;
  310. }
  311. private static Mesh GenerateCachedGridMesh(GridLayout gridLayout, Color color)
  312. {
  313. switch (gridLayout.cellLayout)
  314. {
  315. case GridLayout.CellLayout.Hexagon:
  316. return GenerateCachedHexagonalGridMesh(gridLayout, color);
  317. case GridLayout.CellLayout.Isometric:
  318. case GridLayout.CellLayout.IsometricZAsY:
  319. case GridLayout.CellLayout.Rectangle:
  320. int min = k_GridGizmoVertexCount / -32;
  321. int max = min * -1;
  322. int numCells = max - min;
  323. RectInt bounds = new RectInt(min, min, numCells, numCells);
  324. return GenerateCachedGridMesh(gridLayout, color, 0f, bounds, MeshTopology.Lines);
  325. }
  326. return null;
  327. }
  328. public static Mesh GenerateCachedGridMesh(GridLayout gridLayout, Color color, float screenPixelSize, RectInt bounds, MeshTopology topology)
  329. {
  330. Mesh mesh = new Mesh();
  331. mesh.hideFlags = HideFlags.HideAndDontSave;
  332. int vertex = 0;
  333. int totalVertices = topology == MeshTopology.Quads ?
  334. 8 * (bounds.size.x + bounds.size.y) :
  335. 4 * (bounds.size.x + bounds.size.y);
  336. Vector3 horizontalPixelOffset = new Vector3(screenPixelSize, 0f, 0f);
  337. Vector3 verticalPixelOffset = new Vector3(0f, screenPixelSize, 0f);
  338. Vector3[] vertices = new Vector3[totalVertices];
  339. Vector2[] uvs2 = new Vector2[totalVertices];
  340. Vector3 cellStride = gridLayout.cellSize + gridLayout.cellGap;
  341. Vector3Int minPosition = new Vector3Int(0, bounds.min.y, 0);
  342. Vector3Int maxPosition = new Vector3Int(0, bounds.max.y, 0);
  343. Vector3 cellGap = Vector3.zero;
  344. if (!Mathf.Approximately(cellStride.x, 0f))
  345. {
  346. cellGap.x = gridLayout.cellSize.x / cellStride.x;
  347. }
  348. for (int x = bounds.min.x; x < bounds.max.x; x++)
  349. {
  350. minPosition.x = x;
  351. maxPosition.x = x;
  352. vertices[vertex + 0] = gridLayout.CellToLocal(minPosition);
  353. vertices[vertex + 1] = gridLayout.CellToLocal(maxPosition);
  354. uvs2[vertex + 0] = Vector2.zero;
  355. uvs2[vertex + 1] = new Vector2(0f, cellStride.y * bounds.size.y);
  356. if (topology == MeshTopology.Quads)
  357. {
  358. vertices[vertex + 2] = gridLayout.CellToLocal(maxPosition) + horizontalPixelOffset;
  359. vertices[vertex + 3] = gridLayout.CellToLocal(minPosition) + horizontalPixelOffset;
  360. uvs2[vertex + 2] = new Vector2(0f, cellStride.y * bounds.size.y);
  361. uvs2[vertex + 3] = Vector2.zero;
  362. }
  363. vertex += topology == MeshTopology.Quads ? 4 : 2;
  364. vertices[vertex + 0] = gridLayout.CellToLocalInterpolated(minPosition + cellGap);
  365. vertices[vertex + 1] = gridLayout.CellToLocalInterpolated(maxPosition + cellGap);
  366. uvs2[vertex + 0] = Vector2.zero;
  367. uvs2[vertex + 1] = new Vector2(0f, cellStride.y * bounds.size.y);
  368. if (topology == MeshTopology.Quads)
  369. {
  370. vertices[vertex + 2] = gridLayout.CellToLocalInterpolated(maxPosition + cellGap) + horizontalPixelOffset;
  371. vertices[vertex + 3] = gridLayout.CellToLocalInterpolated(minPosition + cellGap) + horizontalPixelOffset;
  372. uvs2[vertex + 2] = new Vector2(0f, cellStride.y * bounds.size.y);
  373. uvs2[vertex + 3] = Vector2.zero;
  374. }
  375. vertex += topology == MeshTopology.Quads ? 4 : 2;
  376. }
  377. minPosition = new Vector3Int(bounds.min.x, 0, 0);
  378. maxPosition = new Vector3Int(bounds.max.x, 0, 0);
  379. cellGap = Vector3.zero;
  380. if (!Mathf.Approximately(cellStride.y, 0f))
  381. {
  382. cellGap.y = gridLayout.cellSize.y / cellStride.y;
  383. }
  384. for (int y = bounds.min.y; y < bounds.max.y; y++)
  385. {
  386. minPosition.y = y;
  387. maxPosition.y = y;
  388. vertices[vertex + 0] = gridLayout.CellToLocal(minPosition);
  389. vertices[vertex + 1] = gridLayout.CellToLocal(maxPosition);
  390. uvs2[vertex + 0] = Vector2.zero;
  391. uvs2[vertex + 1] = new Vector2(cellStride.x * bounds.size.x, 0f);
  392. if (topology == MeshTopology.Quads)
  393. {
  394. vertices[vertex + 2] = gridLayout.CellToLocal(maxPosition) + verticalPixelOffset;
  395. vertices[vertex + 3] = gridLayout.CellToLocal(minPosition) + verticalPixelOffset;
  396. uvs2[vertex + 2] = new Vector2(cellStride.x * bounds.size.x, 0f);
  397. uvs2[vertex + 3] = Vector2.zero;
  398. }
  399. vertex += topology == MeshTopology.Quads ? 4 : 2;
  400. vertices[vertex + 0] = gridLayout.CellToLocalInterpolated(minPosition + cellGap);
  401. vertices[vertex + 1] = gridLayout.CellToLocalInterpolated(maxPosition + cellGap);
  402. uvs2[vertex + 0] = Vector2.zero;
  403. uvs2[vertex + 1] = new Vector2(cellStride.x * bounds.size.x, 0f);
  404. if (topology == MeshTopology.Quads)
  405. {
  406. vertices[vertex + 2] = gridLayout.CellToLocalInterpolated(maxPosition + cellGap) + verticalPixelOffset;
  407. vertices[vertex + 3] = gridLayout.CellToLocalInterpolated(minPosition + cellGap) + verticalPixelOffset;
  408. uvs2[vertex + 2] = new Vector2(cellStride.x * bounds.size.x, 0f);
  409. uvs2[vertex + 3] = Vector2.zero;
  410. }
  411. vertex += topology == MeshTopology.Quads ? 4 : 2;
  412. }
  413. var uv0 = new Vector2(k_GridGizmoDistanceFalloff, 0f);
  414. var uvs = new Vector2[vertex];
  415. var indices = new int[vertex];
  416. var colors = new Color[vertex];
  417. var normals = new Vector3[totalVertices]; // Normal channel stores the position of the other end point of the line.
  418. var uvs3 = new Vector2[totalVertices]; // UV3 channel stores the UV2 value of the other end point of the line.
  419. for (int i = 0; i < vertex; i++)
  420. {
  421. uvs[i] = uv0;
  422. indices[i] = i;
  423. colors[i] = color;
  424. var alternate = i + ((i % 2) == 0 ? 1 : -1);
  425. normals[i] = vertices[alternate];
  426. uvs3[i] = uvs2[alternate];
  427. }
  428. mesh.vertices = vertices;
  429. mesh.uv = uvs;
  430. mesh.uv2 = uvs2;
  431. mesh.uv3 = uvs3;
  432. mesh.colors = colors;
  433. mesh.normals = normals;
  434. mesh.SetIndices(indices, topology, 0);
  435. return mesh;
  436. }
  437. private static Mesh GenerateCachedHexagonalGridMesh(GridLayout gridLayout, Color color)
  438. {
  439. Mesh mesh = new Mesh();
  440. mesh.hideFlags = HideFlags.HideAndDontSave;
  441. int vertex = 0;
  442. int max = k_GridGizmoVertexCount / (2 * (6 * 2));
  443. max = (max / 4) * 4;
  444. int min = -max;
  445. float numVerticalCells = 6 * (max / 4);
  446. int totalVertices = max * 2 * 6 * 2;
  447. var cellStrideY = gridLayout.cellGap.y + gridLayout.cellSize.y;
  448. var cellOffsetY = gridLayout.cellSize.y / 2;
  449. var hexOffset = (1.0f / 3.0f);
  450. var drawTotal = numVerticalCells * 2.0f * hexOffset;
  451. var drawDiagTotal = 2 * drawTotal;
  452. Vector3[] vertices = new Vector3[totalVertices];
  453. Vector2[] uvs2 = new Vector2[totalVertices];
  454. // Draw Vertical Lines
  455. for (int x = min; x < max; x++)
  456. {
  457. vertices[vertex] = gridLayout.CellToLocal(new Vector3Int(x, min, 0));
  458. vertices[vertex + 1] = gridLayout.CellToLocal(new Vector3Int(x, max, 0));
  459. uvs2[vertex] = new Vector2(0f, 2 * hexOffset);
  460. uvs2[vertex + 1] = new Vector2(0f, 2 * hexOffset + drawTotal);
  461. vertex += 2;
  462. // Alternate Row Offset
  463. vertices[vertex] = gridLayout.CellToLocal(new Vector3Int(x, min - 1, 0));
  464. vertices[vertex + 1] = gridLayout.CellToLocal(new Vector3Int(x, max - 1, 0));
  465. uvs2[vertex] = new Vector2(0f, 2 * hexOffset);
  466. uvs2[vertex + 1] = new Vector2(0f, 2 * hexOffset + drawTotal);
  467. vertex += 2;
  468. }
  469. // Draw Diagonals
  470. for (int y = min; y < max; y++)
  471. {
  472. float drawDiagOffset = ((y + 1) % 3) * hexOffset;
  473. var cellOffSet = Grid.Swizzle(gridLayout.cellSwizzle, new Vector3(0f, y * cellStrideY + cellOffsetY, 0.0f));
  474. // Slope Up
  475. vertices[vertex] = gridLayout.CellToLocal(new Vector3Int(Mathf.RoundToInt(1.5f * min), min, 0)) + cellOffSet;
  476. vertices[vertex + 1] = gridLayout.CellToLocal(new Vector3Int(Mathf.RoundToInt(1.5f * max), max, 0)) + cellOffSet;
  477. uvs2[vertex] = new Vector2(0f, drawDiagOffset);
  478. uvs2[vertex + 1] = new Vector2(0f, drawDiagOffset + drawDiagTotal);
  479. vertex += 2;
  480. // Slope Down
  481. vertices[vertex] = gridLayout.CellToLocal(new Vector3Int(Mathf.RoundToInt(1.5f * max), min, 0)) + cellOffSet;
  482. vertices[vertex + 1] = gridLayout.CellToLocal(new Vector3Int(Mathf.RoundToInt(1.5f * min), max, 0)) + cellOffSet;
  483. uvs2[vertex] = new Vector2(0f, drawDiagOffset);
  484. uvs2[vertex + 1] = new Vector2(0f, drawDiagOffset + drawDiagTotal);
  485. vertex += 2;
  486. // Alternate Row Offset
  487. vertices[vertex] = gridLayout.CellToLocal(new Vector3Int(Mathf.RoundToInt(1.5f * min) + 1, min, 0)) + cellOffSet;
  488. vertices[vertex + 1] = gridLayout.CellToLocal(new Vector3Int(Mathf.RoundToInt(1.5f * max) + 1, max, 0)) + cellOffSet;
  489. uvs2[vertex] = new Vector2(0f, drawDiagOffset);
  490. uvs2[vertex + 1] = new Vector2(0f, drawDiagOffset + drawDiagTotal);
  491. vertex += 2;
  492. vertices[vertex] = gridLayout.CellToLocal(new Vector3Int(Mathf.RoundToInt(1.5f * max) + 1, min, 0)) + cellOffSet;
  493. vertices[vertex + 1] = gridLayout.CellToLocal(new Vector3Int(Mathf.RoundToInt(1.5f * min) + 1, max, 0)) + cellOffSet;
  494. uvs2[vertex] = new Vector2(0f, drawDiagOffset);
  495. uvs2[vertex + 1] = new Vector2(0f, drawDiagOffset + drawDiagTotal);
  496. vertex += 2;
  497. }
  498. var uv0 = new Vector2(k_GridGizmoDistanceFalloff, 0f);
  499. var indices = new int[totalVertices];
  500. var uvs = new Vector2[totalVertices];
  501. var colors = new Color[totalVertices];
  502. var normals = new Vector3[totalVertices]; // Normal channel stores the position of the other end point of the line.
  503. var uvs3 = new Vector2[totalVertices]; // UV3 channel stores the UV2 value of the other end point of the line.
  504. for (int i = 0; i < totalVertices; i++)
  505. {
  506. uvs[i] = uv0;
  507. indices[i] = i;
  508. colors[i] = color;
  509. var alternate = i + ((i % 2) == 0 ? 1 : -1);
  510. normals[i] = vertices[alternate];
  511. uvs3[i] = uvs2[alternate];
  512. }
  513. mesh.vertices = vertices;
  514. mesh.uv = uvs;
  515. mesh.uv2 = uvs2;
  516. mesh.uv3 = uvs3;
  517. mesh.colors = colors;
  518. mesh.normals = normals;
  519. mesh.SetIndices(indices, MeshTopology.Lines, 0);
  520. return mesh;
  521. }
  522. }
  523. }