VertexBuffer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace FairyGUI
  4. {
  5. /// <summary>
  6. ///
  7. /// </summary>
  8. public sealed class VertexBuffer
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. public Rect contentRect;
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. public Rect uvRect;
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. public Color32 vertexColor;
  22. /// <summary>
  23. ///
  24. /// </summary>
  25. public Vector2 textureSize;
  26. /// <summary>
  27. ///
  28. /// </summary>
  29. public readonly List<Vector3> vertices;
  30. /// <summary>
  31. ///
  32. /// </summary>
  33. public readonly List<Color32> colors;
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. public readonly List<Vector2> uvs;
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. public readonly List<Vector2> uvs2;
  42. /// <summary>
  43. ///
  44. /// </summary>
  45. public readonly List<int> triangles;
  46. static public Vector2[] NormalizedUV = new Vector2[] {
  47. new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) };
  48. static public Vector2[] NormalizedPosition = new Vector2[] {
  49. new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1) };
  50. internal bool _alphaInVertexColor;
  51. internal bool _isArbitraryQuad;
  52. static Stack<VertexBuffer> _pool = new Stack<VertexBuffer>();
  53. /// <summary>
  54. ///
  55. /// </summary>
  56. /// <returns></returns>
  57. public static VertexBuffer Begin()
  58. {
  59. if (_pool.Count > 0)
  60. {
  61. VertexBuffer inst = _pool.Pop();
  62. inst.Clear();
  63. return inst;
  64. }
  65. else
  66. return new VertexBuffer();
  67. }
  68. /// <summary>
  69. ///
  70. /// </summary>
  71. /// <param name="source"></param>
  72. public static VertexBuffer Begin(VertexBuffer source)
  73. {
  74. VertexBuffer vb = Begin();
  75. vb.contentRect = source.contentRect;
  76. vb.uvRect = source.uvRect;
  77. vb.vertexColor = source.vertexColor;
  78. vb.textureSize = source.textureSize;
  79. return vb;
  80. }
  81. private VertexBuffer()
  82. {
  83. vertices = new List<Vector3>();
  84. colors = new List<Color32>();
  85. uvs = new List<Vector2>();
  86. uvs2 = new List<Vector2>();
  87. triangles = new List<int>();
  88. }
  89. /// <summary>
  90. ///
  91. /// </summary>
  92. public void End()
  93. {
  94. _pool.Push(this);
  95. }
  96. /// <summary>
  97. ///
  98. /// </summary>
  99. public void Clear()
  100. {
  101. vertices.Clear();
  102. colors.Clear();
  103. uvs.Clear();
  104. uvs2.Clear();
  105. triangles.Clear();
  106. _isArbitraryQuad = false;
  107. _alphaInVertexColor = false;
  108. }
  109. /// <summary>
  110. ///
  111. /// </summary>
  112. public int currentVertCount
  113. {
  114. get
  115. {
  116. return vertices.Count;
  117. }
  118. }
  119. /// <summary>
  120. ///
  121. /// </summary>
  122. /// <param name="position"></param>
  123. public void AddVert(Vector3 position)
  124. {
  125. position.y = -position.y;
  126. vertices.Add(position);
  127. colors.Add(vertexColor);
  128. if (vertexColor.a != 255)
  129. _alphaInVertexColor = true;
  130. uvs.Add(new Vector2(
  131. Mathf.Lerp(uvRect.xMin, uvRect.xMax, (position.x - contentRect.xMin) / contentRect.width),
  132. Mathf.Lerp(uvRect.yMax, uvRect.yMin, (-position.y - contentRect.yMin) / contentRect.height)));
  133. }
  134. /// <summary>
  135. ///
  136. /// </summary>
  137. /// <param name="position"></param>
  138. /// <param name="color"></param>
  139. public void AddVert(Vector3 position, Color32 color)
  140. {
  141. position.y = -position.y;
  142. vertices.Add(position);
  143. colors.Add(color);
  144. if (color.a != 255)
  145. _alphaInVertexColor = true;
  146. uvs.Add(new Vector2(
  147. Mathf.Lerp(uvRect.xMin, uvRect.xMax, (position.x - contentRect.xMin) / contentRect.width),
  148. Mathf.Lerp(uvRect.yMax, uvRect.yMin, (-position.y - contentRect.yMin) / contentRect.height)));
  149. }
  150. /// <summary>
  151. ///
  152. /// </summary>
  153. /// <param name="position"></param>
  154. /// <param name="color"></param>
  155. /// <param name="uv"></param>
  156. public void AddVert(Vector3 position, Color32 color, Vector2 uv)
  157. {
  158. position.y = -position.y;
  159. vertices.Add(position);
  160. uvs.Add(new Vector2(uv.x, uv.y));
  161. colors.Add(color);
  162. if (color.a != 255)
  163. _alphaInVertexColor = true;
  164. }
  165. /// <summary>
  166. ///
  167. /// 1---2
  168. /// | / |
  169. /// 0---3
  170. /// </summary>
  171. /// <param name="vertRect"></param>
  172. public void AddQuad(Rect vertRect)
  173. {
  174. AddVert(new Vector3(vertRect.xMin, vertRect.yMax, 0f));
  175. AddVert(new Vector3(vertRect.xMin, vertRect.yMin, 0f));
  176. AddVert(new Vector3(vertRect.xMax, vertRect.yMin, 0f));
  177. AddVert(new Vector3(vertRect.xMax, vertRect.yMax, 0f));
  178. }
  179. /// <summary>
  180. ///
  181. /// </summary>
  182. /// <param name="vertRect"></param>
  183. /// <param name="color"></param>
  184. public void AddQuad(Rect vertRect, Color32 color)
  185. {
  186. AddVert(new Vector3(vertRect.xMin, vertRect.yMax, 0f), color);
  187. AddVert(new Vector3(vertRect.xMin, vertRect.yMin, 0f), color);
  188. AddVert(new Vector3(vertRect.xMax, vertRect.yMin, 0f), color);
  189. AddVert(new Vector3(vertRect.xMax, vertRect.yMax, 0f), color);
  190. }
  191. /// <summary>
  192. ///
  193. /// </summary>
  194. /// <param name="vertRect"></param>
  195. /// <param name="color"></param>
  196. /// <param name="uvRect"></param>
  197. public void AddQuad(Rect vertRect, Color32 color, Rect uvRect)
  198. {
  199. vertices.Add(new Vector3(vertRect.xMin, -vertRect.yMax, 0f));
  200. vertices.Add(new Vector3(vertRect.xMin, -vertRect.yMin, 0f));
  201. vertices.Add(new Vector3(vertRect.xMax, -vertRect.yMin, 0f));
  202. vertices.Add(new Vector3(vertRect.xMax, -vertRect.yMax, 0f));
  203. uvs.Add(new Vector2(uvRect.xMin, uvRect.yMin));
  204. uvs.Add(new Vector2(uvRect.xMin, uvRect.yMax));
  205. uvs.Add(new Vector2(uvRect.xMax, uvRect.yMax));
  206. uvs.Add(new Vector2(uvRect.xMax, uvRect.yMin));
  207. colors.Add(color);
  208. colors.Add(color);
  209. colors.Add(color);
  210. colors.Add(color);
  211. if (color.a != 255)
  212. _alphaInVertexColor = true;
  213. }
  214. static List<Vector4> helperV4List = new List<Vector4>(4) { Vector4.zero, Vector4.zero, Vector4.zero, Vector4.zero };
  215. internal List<Vector4> FixUVForArbitraryQuad()
  216. {
  217. //ref1 http://www.reedbeta.com/blog/quadrilateral-interpolation-part-1/
  218. //ref2 https://bitlush.com/blog/arbitrary-quadrilaterals-in-opengl-es-2-0
  219. Vector4 qq = Vector4.one;
  220. Vector2 a = vertices[2] - vertices[0];
  221. Vector2 b = vertices[1] - vertices[3];
  222. Vector2 c = vertices[0] - vertices[3];
  223. float cross = a.x * b.y - a.y * b.x;
  224. if (cross != 0)
  225. {
  226. float s = (a.x * c.y - a.y * c.x) / cross;
  227. if (s > 0 && s < 1)
  228. {
  229. float t = (b.x * c.y - b.y * c.x) / cross;
  230. if (t > 0 && t < 1)
  231. {
  232. qq.x = 1 / (1 - t);
  233. qq.y = 1 / s;
  234. qq.z = 1 / t;
  235. qq.w = 1 / (1 - s);
  236. }
  237. }
  238. }
  239. for (int i = 0; i < 4; i++)
  240. {
  241. Vector4 v = uvs[i];
  242. float q = qq[i];
  243. v.x *= q;
  244. v.y *= q;
  245. v.w = q;
  246. helperV4List[i] = v;
  247. }
  248. return helperV4List;
  249. }
  250. /// <summary>
  251. ///
  252. /// </summary>
  253. /// <param name="value"></param>
  254. /// <param name="startIndex"></param>
  255. /// <param name="count"></param>
  256. public void RepeatColors(Color32[] value, int startIndex, int count)
  257. {
  258. int len = Mathf.Min(startIndex + count, vertices.Count);
  259. int colorCount = value.Length;
  260. int k = 0;
  261. for (int i = startIndex; i < len; i++)
  262. {
  263. Color32 c = value[(k++) % colorCount];
  264. colors[i] = c;
  265. if (c.a != 255)
  266. _alphaInVertexColor = true;
  267. }
  268. }
  269. /// <summary>
  270. ///
  271. /// </summary>
  272. /// <param name="idx0"></param>
  273. /// <param name="idx1"></param>
  274. /// <param name="idx2"></param>
  275. public void AddTriangle(int idx0, int idx1, int idx2)
  276. {
  277. triangles.Add(idx0);
  278. triangles.Add(idx1);
  279. triangles.Add(idx2);
  280. }
  281. /// <summary>
  282. ///
  283. /// </summary>
  284. /// <param name="idxList"></param>
  285. /// <param name="startVertexIndex"></param>
  286. public void AddTriangles(int[] idxList, int startVertexIndex = 0)
  287. {
  288. if (startVertexIndex != 0)
  289. {
  290. if (startVertexIndex < 0)
  291. startVertexIndex = vertices.Count + startVertexIndex;
  292. int cnt = idxList.Length;
  293. for (int i = 0; i < cnt; i++)
  294. triangles.Add(idxList[i] + startVertexIndex);
  295. }
  296. else
  297. triangles.AddRange(idxList);
  298. }
  299. /// <summary>
  300. ///
  301. /// </summary>
  302. /// <param name="startVertexIndex"></param>
  303. public void AddTriangles(int startVertexIndex = 0)
  304. {
  305. int cnt = vertices.Count;
  306. if (startVertexIndex < 0)
  307. startVertexIndex = cnt + startVertexIndex;
  308. for (int i = startVertexIndex; i < cnt; i += 4)
  309. {
  310. triangles.Add(i);
  311. triangles.Add(i + 1);
  312. triangles.Add(i + 2);
  313. triangles.Add(i + 2);
  314. triangles.Add(i + 3);
  315. triangles.Add(i);
  316. }
  317. }
  318. /// <summary>
  319. ///
  320. /// </summary>
  321. /// <param name="index"></param>
  322. /// <returns></returns>
  323. public Vector3 GetPosition(int index)
  324. {
  325. if (index < 0)
  326. index = vertices.Count + index;
  327. Vector3 vec = vertices[index];
  328. vec.y = -vec.y;
  329. return vec;
  330. }
  331. /// <summary>
  332. ///
  333. /// </summary>
  334. /// <param name="position"></param>
  335. /// <param name="usePercent"></param>
  336. /// <returns></returns>
  337. public Vector2 GetUVAtPosition(Vector2 position, bool usePercent)
  338. {
  339. if (usePercent)
  340. {
  341. return new Vector2(Mathf.Lerp(uvRect.xMin, uvRect.xMax, position.x),
  342. Mathf.Lerp(uvRect.yMax, uvRect.yMin, position.y));
  343. }
  344. else
  345. return new Vector2(Mathf.Lerp(uvRect.xMin, uvRect.xMax, (position.x - contentRect.xMin) / contentRect.width),
  346. Mathf.Lerp(uvRect.yMax, uvRect.yMin, (position.y - contentRect.yMin) / contentRect.height));
  347. }
  348. /// <summary>
  349. ///
  350. /// </summary>
  351. /// <param name="vb"></param>
  352. public void Append(VertexBuffer vb)
  353. {
  354. int len = vertices.Count;
  355. vertices.AddRange(vb.vertices);
  356. uvs.AddRange(vb.uvs);
  357. uvs2.AddRange(vb.uvs2);
  358. colors.AddRange(vb.colors);
  359. if (len != 0)
  360. {
  361. int len1 = vb.triangles.Count;
  362. for (int i = 0; i < len1; i++)
  363. triangles.Add(vb.triangles[i] + len);
  364. }
  365. else
  366. triangles.AddRange(vb.triangles);
  367. if (vb._alphaInVertexColor)
  368. _alphaInVertexColor = true;
  369. }
  370. /// <summary>
  371. ///
  372. /// </summary>
  373. /// <param name="vb"></param>
  374. public void Insert(VertexBuffer vb)
  375. {
  376. vertices.InsertRange(0, vb.vertices);
  377. uvs.InsertRange(0, vb.uvs);
  378. uvs2.InsertRange(0, vb.uvs2);
  379. colors.InsertRange(0, vb.colors);
  380. int len = triangles.Count;
  381. if (len != 0)
  382. {
  383. int len1 = vb.vertices.Count;
  384. for (int i = 0; i < len; i++)
  385. triangles[i] += len1;
  386. }
  387. triangles.InsertRange(0, vb.triangles);
  388. if (vb._alphaInVertexColor)
  389. _alphaInVertexColor = true;
  390. }
  391. }
  392. }