MeshUtils.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace KairoEngine.Utility
  5. {
  6. public static class MeshUtils {
  7. private static readonly Vector3 Vector3zero = Vector3.zero;
  8. private static readonly Vector3 Vector3one = Vector3.one;
  9. private static readonly Vector3 Vector3yDown = new Vector3(0,-1);
  10. private static Quaternion[] cachedQuaternionEulerArr;
  11. private static void CacheQuaternionEuler() {
  12. if (cachedQuaternionEulerArr != null) return;
  13. cachedQuaternionEulerArr = new Quaternion[360];
  14. for (int i=0; i<360; i++) {
  15. cachedQuaternionEulerArr[i] = Quaternion.Euler(0,0,i);
  16. }
  17. }
  18. private static Quaternion GetQuaternionEuler(float rotFloat) {
  19. int rot = Mathf.RoundToInt(rotFloat);
  20. rot = rot % 360;
  21. if (rot < 0) rot += 360;
  22. //if (rot >= 360) rot -= 360;
  23. if (cachedQuaternionEulerArr == null) CacheQuaternionEuler();
  24. return cachedQuaternionEulerArr[rot];
  25. }
  26. public static Mesh CreateEmptyMesh() {
  27. Mesh mesh = new Mesh();
  28. mesh.vertices = new Vector3[0];
  29. mesh.uv = new Vector2[0];
  30. mesh.triangles = new int[0];
  31. return mesh;
  32. }
  33. public static void CreateEmptyMeshArrays(int quadCount, out Vector3[] vertices, out Vector2[] uvs, out int[] triangles) {
  34. vertices = new Vector3[4 * quadCount];
  35. uvs = new Vector2[4 * quadCount];
  36. triangles = new int[6 * quadCount];
  37. }
  38. public static Mesh CreateMesh(Vector3 pos, float rot, Vector3 baseSize, Vector2 uv00, Vector2 uv11) {
  39. return AddToMesh(null, pos, rot, baseSize, uv00, uv11);
  40. }
  41. public static Mesh AddToMesh(Mesh mesh, Vector3 pos, float rot, Vector3 baseSize, Vector2 uv00, Vector2 uv11) {
  42. if (mesh == null) {
  43. mesh = CreateEmptyMesh();
  44. }
  45. Vector3[] vertices = new Vector3[4 + mesh.vertices.Length];
  46. Vector2[] uvs = new Vector2[4 + mesh.uv.Length];
  47. int[] triangles = new int[6 + mesh.triangles.Length];
  48. mesh.vertices.CopyTo(vertices, 0);
  49. mesh.uv.CopyTo(uvs, 0);
  50. mesh.triangles.CopyTo(triangles, 0);
  51. int index = vertices.Length / 4 - 1;
  52. //Relocate vertices
  53. int vIndex = index*4;
  54. int vIndex0 = vIndex;
  55. int vIndex1 = vIndex+1;
  56. int vIndex2 = vIndex+2;
  57. int vIndex3 = vIndex+3;
  58. baseSize *= .5f;
  59. bool skewed = baseSize.x != baseSize.y;
  60. if (skewed) {
  61. vertices[vIndex0] = pos+GetQuaternionEuler(rot)*new Vector3(-baseSize.x, baseSize.y);
  62. vertices[vIndex1] = pos+GetQuaternionEuler(rot)*new Vector3(-baseSize.x, -baseSize.y);
  63. vertices[vIndex2] = pos+GetQuaternionEuler(rot)*new Vector3( baseSize.x, -baseSize.y);
  64. vertices[vIndex3] = pos+GetQuaternionEuler(rot)*baseSize;
  65. } else {
  66. vertices[vIndex0] = pos+GetQuaternionEuler(rot-270)*baseSize;
  67. vertices[vIndex1] = pos+GetQuaternionEuler(rot-180)*baseSize;
  68. vertices[vIndex2] = pos+GetQuaternionEuler(rot- 90)*baseSize;
  69. vertices[vIndex3] = pos+GetQuaternionEuler(rot- 0)*baseSize;
  70. }
  71. //Relocate UVs
  72. uvs[vIndex0] = new Vector2(uv00.x, uv11.y);
  73. uvs[vIndex1] = new Vector2(uv00.x, uv00.y);
  74. uvs[vIndex2] = new Vector2(uv11.x, uv00.y);
  75. uvs[vIndex3] = new Vector2(uv11.x, uv11.y);
  76. //Create triangles
  77. int tIndex = index*6;
  78. triangles[tIndex+0] = vIndex0;
  79. triangles[tIndex+1] = vIndex3;
  80. triangles[tIndex+2] = vIndex1;
  81. triangles[tIndex+3] = vIndex1;
  82. triangles[tIndex+4] = vIndex3;
  83. triangles[tIndex+5] = vIndex2;
  84. mesh.vertices = vertices;
  85. mesh.triangles = triangles;
  86. mesh.uv = uvs;
  87. //mesh.bounds = bounds;
  88. return mesh;
  89. }
  90. public static void AddToMeshArrays(Vector3[] vertices, Vector2[] uvs, int[] triangles, int index, Vector3 pos, float rot, Vector3 baseSize, Vector2 uv00, Vector2 uv11) {
  91. //Relocate vertices
  92. int vIndex = index*4;
  93. int vIndex0 = vIndex;
  94. int vIndex1 = vIndex+1;
  95. int vIndex2 = vIndex+2;
  96. int vIndex3 = vIndex+3;
  97. baseSize *= .5f;
  98. bool skewed = baseSize.x != baseSize.y;
  99. if (skewed) {
  100. vertices[vIndex0] = pos+GetQuaternionEuler(rot)*new Vector3(-baseSize.x, 0, baseSize.z);
  101. vertices[vIndex1] = pos+GetQuaternionEuler(rot)*new Vector3(-baseSize.x, 0, -baseSize.z);
  102. vertices[vIndex2] = pos+GetQuaternionEuler(rot)*new Vector3( baseSize.x, 0, -baseSize.z);
  103. vertices[vIndex3] = pos+GetQuaternionEuler(rot)*baseSize;
  104. } else {
  105. vertices[vIndex0] = pos+GetQuaternionEuler(rot-270)*baseSize;
  106. vertices[vIndex1] = pos+GetQuaternionEuler(rot-180)*baseSize;
  107. vertices[vIndex2] = pos+GetQuaternionEuler(rot- 90)*baseSize;
  108. vertices[vIndex3] = pos+GetQuaternionEuler(rot- 0)*baseSize;
  109. }
  110. //Relocate UVs
  111. uvs[vIndex0] = new Vector2(uv00.x, uv11.y);
  112. uvs[vIndex1] = new Vector2(uv00.x, uv00.y);
  113. uvs[vIndex2] = new Vector2(uv11.x, uv00.y);
  114. uvs[vIndex3] = new Vector2(uv11.x, uv11.y);
  115. //Create triangles
  116. int tIndex = index*6;
  117. triangles[tIndex+0] = vIndex0;
  118. triangles[tIndex+1] = vIndex3;
  119. triangles[tIndex+2] = vIndex1;
  120. triangles[tIndex+3] = vIndex1;
  121. triangles[tIndex+4] = vIndex3;
  122. triangles[tIndex+5] = vIndex2;
  123. }
  124. }
  125. }