/Assets/NGUI/Scripts/Internal/UIGeometry.cs

https://bitbucket.org/concuror/gamaton_demo · C# · 127 lines · 62 code · 22 blank · 43 comment · 15 complexity · 9e7a818497f21fc31287e8e644fe92eb MD5 · raw file

  1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2013 Tasharen Entertainment
  4. //----------------------------------------------
  5. using UnityEngine;
  6. using System.Collections.Generic;
  7. /// <summary>
  8. /// Generated geometry class. All widgets have one.
  9. /// This class separates the geometry creation into several steps, making it possible to perform
  10. /// actions selectively depending on what has changed. For example, the widget doesn't need to be
  11. /// rebuilt unless something actually changes, so its geometry can be cached. Likewise, the widget's
  12. /// transformed coordinates only change if the widget's transform moves relative to the panel,
  13. /// so that can be cached as well. In the end, using this class means using more memory, but at
  14. /// the same time it allows for significant performance gains, especially when using widgets that
  15. /// spit out a lot of vertices, such as UILabels.
  16. /// </summary>
  17. public class UIGeometry
  18. {
  19. /// <summary>
  20. /// Widget's vertices (before they get transformed).
  21. /// </summary>
  22. public BetterList<Vector3> verts = new BetterList<Vector3>();
  23. /// <summary>
  24. /// Widget's texture coordinates for the geometry's vertices.
  25. /// </summary>
  26. public BetterList<Vector2> uvs = new BetterList<Vector2>();
  27. /// <summary>
  28. /// Array of colors for the geometry's vertices.
  29. /// </summary>
  30. public BetterList<Color32> cols = new BetterList<Color32>();
  31. // Relative-to-panel vertices, normal, and tangent
  32. BetterList<Vector3> mRtpVerts = new BetterList<Vector3>();
  33. Vector3 mRtpNormal;
  34. Vector4 mRtpTan;
  35. /// <summary>
  36. /// Whether the geometry contains usable vertices.
  37. /// </summary>
  38. public bool hasVertices { get { return (verts.size > 0); } }
  39. /// <summary>
  40. /// Whether the geometry has usable transformed vertex data.
  41. /// </summary>
  42. public bool hasTransformed { get { return (mRtpVerts != null) && (mRtpVerts.size > 0) && (mRtpVerts.size == verts.size); } }
  43. /// <summary>
  44. /// Step 1: Prepare to fill the buffers -- make them clean and valid.
  45. /// </summary>
  46. public void Clear ()
  47. {
  48. verts.Clear();
  49. uvs.Clear();
  50. cols.Clear();
  51. mRtpVerts.Clear();
  52. }
  53. /// <summary>
  54. /// Step 2: After the buffers have been filled, apply the specified pivot offset to the generated geometry.
  55. /// </summary>
  56. public void ApplyOffset (Vector3 pivotOffset)
  57. {
  58. for (int i = 0; i < verts.size; ++i) verts.buffer[i] += pivotOffset;
  59. }
  60. /// <summary>
  61. /// Step 3: Transform the vertices by the provided matrix.
  62. /// </summary>
  63. public void ApplyTransform (Matrix4x4 widgetToPanel, bool normals)
  64. {
  65. if (verts.size > 0)
  66. {
  67. mRtpVerts.Clear();
  68. for (int i = 0, imax = verts.size; i < imax; ++i) mRtpVerts.Add(widgetToPanel.MultiplyPoint3x4(verts[i]));
  69. // Calculate the widget's normal and tangent
  70. mRtpNormal = widgetToPanel.MultiplyVector(Vector3.back).normalized;
  71. Vector3 tangent = widgetToPanel.MultiplyVector(Vector3.right).normalized;
  72. mRtpTan = new Vector4(tangent.x, tangent.y, tangent.z, -1f);
  73. }
  74. else mRtpVerts.Clear();
  75. }
  76. /// <summary>
  77. /// Step 4: Fill the specified buffer using the transformed values.
  78. /// </summary>
  79. public void WriteToBuffers (BetterList<Vector3> v, BetterList<Vector2> u, BetterList<Color32> c, BetterList<Vector3> n, BetterList<Vector4> t)
  80. {
  81. if (mRtpVerts != null && mRtpVerts.size > 0)
  82. {
  83. if (n == null)
  84. {
  85. for (int i = 0; i < mRtpVerts.size; ++i)
  86. {
  87. v.Add(mRtpVerts.buffer[i]);
  88. u.Add(uvs.buffer[i]);
  89. c.Add(cols.buffer[i]);
  90. }
  91. }
  92. else
  93. {
  94. for (int i = 0; i < mRtpVerts.size; ++i)
  95. {
  96. v.Add(mRtpVerts.buffer[i]);
  97. u.Add(uvs.buffer[i]);
  98. c.Add(cols.buffer[i]);
  99. n.Add(mRtpNormal);
  100. t.Add(mRtpTan);
  101. }
  102. }
  103. }
  104. }
  105. }