UnityQuards2D.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using CommonUI.Display;
  5. using UnityEngine;
  6. using CommonLang;
  7. namespace CommonUI_Unity3D.Impl
  8. {
  9. public struct UnityVertexBuffer : VertexBuffer
  10. {
  11. private int mTopology;
  12. private List<int> mIndices;
  13. private List<Vector3> mPos;
  14. private List<Vector2> mUVs;
  15. public UnityVertexBuffer(int capacity)
  16. {
  17. this.mTopology = GL.QUADS;
  18. this.mIndices = ListObjectPool<int>.AllocAutoRelease(capacity);
  19. this.mPos = ListObjectPool<Vector3>.AllocAutoRelease(capacity);
  20. this.mUVs = ListObjectPool<Vector2>.AllocAutoRelease(capacity);
  21. }
  22. public void Dispose()
  23. {
  24. ListObjectPool<int>.Release(mIndices);
  25. ListObjectPool<Vector3>.Release(mPos);
  26. ListObjectPool<Vector2>.Release(mUVs);
  27. mIndices = null;
  28. mPos = null;
  29. mUVs = null;
  30. }
  31. /// <summary>
  32. /// 顶点数量
  33. /// </summary>
  34. public int Count
  35. {
  36. get { return mPos.Count; }
  37. set
  38. {
  39. CUtils.SetListSize<Vector3>(mPos, value);
  40. CUtils.SetListSize<Vector2>(mUVs, value);
  41. }
  42. }
  43. public int IndicesCount
  44. {
  45. get
  46. {
  47. return mIndices.Count;
  48. }
  49. }
  50. /// <summary>
  51. /// Appends the vertices from another VertexData object.
  52. /// </summary>
  53. /// <param name="data"></param>
  54. public void Append(VertexBuffer data)
  55. {
  56. mPos.AddRange(((UnityVertexBuffer)data).mPos);
  57. mUVs.AddRange(((UnityVertexBuffer)data).mUVs);
  58. }
  59. public void Append(float x, float y, uint color, float u, float v)
  60. {
  61. mPos.Add(new Vector3(x, y, 0));
  62. mUVs.Add(new Vector2(u, 1f - v));
  63. }
  64. public void Append(VertexPoint vertex)
  65. {
  66. mPos.Add(new Vector3(vertex.X, vertex.Y, 0));
  67. mUVs.Add(new Vector2(vertex.U, 1f - vertex.V));
  68. }
  69. /// <summary>
  70. /// Updates the vertex point of a vertex.
  71. /// </summary>
  72. /// <param name="index"></param>
  73. /// <param name="vertex"></param>
  74. public void SetVertex(int index, VertexPoint vertex)
  75. {
  76. this.mPos[index] = new Vector3(vertex.X, vertex.Y, 0);
  77. this.mUVs[index] = new Vector2(vertex.U, 1f - vertex.V);
  78. }
  79. /// <summary>
  80. /// Returns the vertex point of a vertex.
  81. /// </summary>
  82. /// <param name="index"></param>
  83. /// <param name="vertex"></param>
  84. public void GetVertex(int index, out VertexPoint vertex)
  85. {
  86. VertexPoint v = new VertexPoint();
  87. v.X = mPos[index].x;
  88. v.Y = mPos[index].y;
  89. v.U = mUVs[index].x;
  90. v.V = mUVs[index].y;
  91. vertex = v;
  92. }
  93. /// <summary>
  94. /// Updates the position values of a vertex.
  95. /// </summary>
  96. /// <param name="index"></param>
  97. /// <param name="x"></param>
  98. /// <param name="y"></param>
  99. public void SetPosition(int index, float x, float y)
  100. {
  101. this.mPos[index] = new Vector3(x, y, 0);
  102. }
  103. /// <summary>
  104. /// Updates the RGB color values of a vertex (alpha is not changed).
  105. /// </summary>
  106. /// <param name="index"></param>
  107. /// <param name="color"></param>
  108. public void SetColor(int index, uint color)
  109. {
  110. }
  111. /// <summary>
  112. /// Updates the texture coordinates of a vertex (range 0-1).
  113. /// </summary>
  114. /// <param name="index"></param>
  115. /// <param name="u"></param>
  116. /// <param name="v"></param>
  117. public void SetTexCoords(int index, float u, float v)
  118. {
  119. this.mUVs[index] = new Vector2(u, 1f - v);
  120. }
  121. public void SetIndices(int[] indices, VertexTopology topology)
  122. {
  123. if (indices == null)
  124. {
  125. throw new Exception("Indices Can not be set to NULL !");
  126. }
  127. for (int i = 0; i < indices.Length; i++)
  128. {
  129. if (indices[i] < 0 && indices[i] >= mPos.Count)
  130. {
  131. throw new Exception("Indices Value Must In Vertex Length range !");
  132. }
  133. }
  134. mIndices.Clear();
  135. mIndices.AddRange(indices);
  136. switch (topology)
  137. {
  138. case VertexTopology.QUADS:
  139. mTopology = GL.QUADS;
  140. break;
  141. case VertexTopology.LINES:
  142. mTopology = GL.LINES;
  143. break;
  144. case VertexTopology.TRIANGLE_STRIP:
  145. mTopology = GL.TRIANGLE_STRIP;
  146. break;
  147. case VertexTopology.TRIANGLES:
  148. mTopology = GL.TRIANGLES;
  149. break;
  150. }
  151. }
  152. public void SetIndices(int index, int[] indices)
  153. {
  154. for (int i = 0; i < indices.Length; i++)
  155. {
  156. mIndices[i + index] = indices[i];
  157. }
  158. }
  159. public void AddIndicesQuard(int a, int b, int c, int d)
  160. {
  161. mIndices.Add(a);
  162. mIndices.Add(b);
  163. mIndices.Add(c);
  164. mIndices.Add(d);
  165. }
  166. public void Optimize()
  167. {
  168. }
  169. public object Clone()
  170. {
  171. UnityVertexBuffer ret = new UnityVertexBuffer(this.Count);
  172. ret.Append(this);
  173. return ret;
  174. }
  175. public void Clear()
  176. {
  177. this.mUVs.Clear();
  178. this.mPos.Clear();
  179. }
  180. internal void Draw()
  181. {
  182. GL.Begin(mTopology);
  183. for (int i = 0; i < mIndices.Count; i++)
  184. {
  185. Vector2 uv = mUVs[mIndices[i]];
  186. GL.TexCoord2(uv.x, uv.y);
  187. GL.Vertex(mPos[mIndices[i]]);
  188. }
  189. GL.End();
  190. }
  191. internal void DrawVertex(int[] indices, int mode)
  192. {
  193. GL.Begin(mode);
  194. for (int i = 0; i < indices.Length; i++)
  195. {
  196. Vector2 uv = mUVs[indices[i]];
  197. GL.TexCoord2(uv.x, uv.y);
  198. GL.Vertex(mPos[indices[i]]);
  199. }
  200. GL.End();
  201. }
  202. internal void DrawSequence(int mode)
  203. {
  204. GL.Begin(mode);
  205. for (int i = 0; i < mPos.Count; i++)
  206. {
  207. Vector2 uv = mUVs[i];
  208. GL.TexCoord2(uv.x, uv.y);
  209. GL.Vertex(mPos[i]);
  210. }
  211. GL.End();
  212. }
  213. public void translate(float x, float y)
  214. {
  215. throw new NotImplementedException();
  216. }
  217. public void rotate(float angle)
  218. {
  219. throw new NotImplementedException();
  220. }
  221. public void scale(float sx, float sy)
  222. {
  223. throw new NotImplementedException();
  224. }
  225. public void pushTransform()
  226. {
  227. throw new NotImplementedException();
  228. }
  229. public void popTransform()
  230. {
  231. throw new NotImplementedException();
  232. }
  233. }
  234. public struct UnityQuards2D
  235. {
  236. public IUnityImageInterface src;
  237. public float u0, v0, u1, v1;
  238. public float x0, y0, x1, y1, x2, y2, x3, y3;
  239. public static UnityQuards2D DrawImage(IUnityImageInterface src, float x, float y)
  240. {
  241. UnityQuards2D q = new UnityQuards2D();
  242. q.src = src;
  243. q.u0 = 0;
  244. q.v0 = 1f;
  245. q.u1 = src.MaxU;
  246. q.v1 = 1f - src.MaxV;
  247. q.x0 = x;
  248. q.y0 = y;
  249. q.x1 = x + src.Width;
  250. q.y1 = y;
  251. q.x2 = x + src.Width;
  252. q.y2 = y + src.Height;
  253. q.x3 = x;
  254. q.y3 = y + src.Height;
  255. return q;
  256. }
  257. public static UnityQuards2D DrawImageZoom(IUnityImageInterface src, float x, float y, float w, float h)
  258. {
  259. UnityQuards2D q = new UnityQuards2D();
  260. q.src = src;
  261. q.u0 = 0;
  262. q.v0 = 1f;
  263. q.u1 = src.MaxU;
  264. q.v1 = 1f - src.MaxV;
  265. q.x0 = x;
  266. q.y0 = y;
  267. q.x1 = x + w;
  268. q.y1 = y;
  269. q.x2 = x + w;
  270. q.y2 = y + h;
  271. q.x3 = x;
  272. q.y3 = y + h;
  273. return q;
  274. }
  275. public static UnityQuards2D DrawImageTrans(IUnityImageInterface src, float x, float y, CommonUI.Display.Trans trans)
  276. {
  277. return DrawImageRegion(src, 0, 0, src.Width, src.Height, x, y, src.Width, src.Height, trans);
  278. }
  279. public static UnityQuards2D DrawImageRegion(
  280. IUnityImageInterface src,
  281. float sx, float sy, float sw, float sh,
  282. float dx, float dy, float dw, float dh,
  283. Trans trans)
  284. {
  285. float sx2 = sx + sw;
  286. float sy2 = sy + sh;
  287. float dx2 = dx + dw;
  288. float dy2 = dy + dh;
  289. VertexUtils.TransformTrans(ref sx, ref sy, ref sx2, ref sy2, trans, ref dx, ref dy, ref dx2, ref dy2);
  290. UnityQuards2D q = new UnityQuards2D();
  291. q.src = src;
  292. q.u0 = (sx) * src.MaxU / src.Width;
  293. q.v0 = 1f - (sy) * src.MaxV / src.Height;
  294. q.u1 = (sx2) * src.MaxU / src.Width;
  295. q.v1 = 1f - (sy2) * src.MaxV / src.Height;
  296. q.x0 = dx;
  297. q.y0 = dy;
  298. q.x1 = dx2;
  299. q.y1 = dy;
  300. q.x2 = dx2;
  301. q.y2 = dy2;
  302. q.x3 = dx;
  303. q.y3 = dy2;
  304. return q;
  305. }
  306. internal void Draw()
  307. {
  308. GL.TexCoord2(u0, v0);
  309. GL.Vertex3(x0, y0, 0);
  310. GL.TexCoord2(u1, v0);
  311. GL.Vertex3(x1, y1, 0);
  312. GL.TexCoord2(u1, v1);
  313. GL.Vertex3(x2, y2, 0);
  314. GL.TexCoord2(u0, v1);
  315. GL.Vertex3(x3, y3, 0);
  316. }
  317. }
  318. }