Quads.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using UnityEngine;
  3. using Object = UnityEngine.Object;
  4. // same as Triangles but creates quads instead which generally
  5. // saves fillrate at the expense for more triangles to issue
  6. namespace UnityStandardAssets.ImageEffects
  7. {
  8. class Quads
  9. {
  10. static Mesh[] meshes;
  11. static int currentQuads = 0;
  12. static bool HasMeshes ()
  13. {
  14. if (meshes == null)
  15. return false;
  16. foreach (Mesh m in meshes)
  17. if (null == m)
  18. return false;
  19. return true;
  20. }
  21. public static void Cleanup ()
  22. {
  23. if (meshes == null)
  24. return;
  25. for (int i = 0; i < meshes.Length; i++)
  26. {
  27. if (null != meshes[i])
  28. {
  29. Object.DestroyImmediate (meshes[i]);
  30. meshes[i] = null;
  31. }
  32. }
  33. meshes = null;
  34. }
  35. public static Mesh[] GetMeshes ( int totalWidth, int totalHeight)
  36. {
  37. if (HasMeshes () && (currentQuads == (totalWidth * totalHeight))) {
  38. return meshes;
  39. }
  40. int maxQuads = 65000 / 6;
  41. int totalQuads = totalWidth * totalHeight;
  42. currentQuads = totalQuads;
  43. int meshCount = Mathf.CeilToInt ((1.0f * totalQuads) / (1.0f * maxQuads));
  44. meshes = new Mesh [meshCount];
  45. int i = 0;
  46. int index = 0;
  47. for (i = 0; i < totalQuads; i += maxQuads)
  48. {
  49. int quads = Mathf.FloorToInt (Mathf.Clamp ((totalQuads-i), 0, maxQuads));
  50. meshes[index] = GetMesh (quads, i, totalWidth, totalHeight);
  51. index++;
  52. }
  53. return meshes;
  54. }
  55. static Mesh GetMesh (int triCount, int triOffset, int totalWidth, int totalHeight)
  56. {
  57. var mesh = new Mesh ();
  58. mesh.hideFlags = HideFlags.DontSave;
  59. var verts = new Vector3[triCount * 4];
  60. var uvs = new Vector2[triCount * 4];
  61. var uvs2 = new Vector2[triCount * 4];
  62. var tris = new int[triCount * 6];
  63. for (int i = 0; i < triCount; i++)
  64. {
  65. int i4 = i * 4;
  66. int i6 = i * 6;
  67. int vertexWithOffset = triOffset + i;
  68. float x = Mathf.Floor (vertexWithOffset % totalWidth) / totalWidth;
  69. float y = Mathf.Floor (vertexWithOffset / totalWidth) / totalHeight;
  70. Vector3 position = new Vector3 (x * 2 - 1, y * 2 - 1, 1.0f);
  71. verts[i4 + 0] = position;
  72. verts[i4 + 1] = position;
  73. verts[i4 + 2] = position;
  74. verts[i4 + 3] = position;
  75. uvs[i4 + 0] = new Vector2 (0.0f, 0.0f);
  76. uvs[i4 + 1] = new Vector2 (1.0f, 0.0f);
  77. uvs[i4 + 2] = new Vector2 (0.0f, 1.0f);
  78. uvs[i4 + 3] = new Vector2 (1.0f, 1.0f);
  79. uvs2[i4 + 0] = new Vector2 (x, y);
  80. uvs2[i4 + 1] = new Vector2 (x, y);
  81. uvs2[i4 + 2] = new Vector2 (x, y);
  82. uvs2[i4 + 3] = new Vector2 (x, y);
  83. tris[i6 + 0] = i4 + 0;
  84. tris[i6 + 1] = i4 + 1;
  85. tris[i6 + 2] = i4 + 2;
  86. tris[i6 + 3] = i4 + 1;
  87. tris[i6 + 4] = i4 + 2;
  88. tris[i6 + 5] = i4 + 3;
  89. }
  90. mesh.vertices = verts;
  91. mesh.triangles = tris;
  92. mesh.uv = uvs;
  93. mesh.uv2 = uvs2;
  94. return mesh;
  95. }
  96. }
  97. }