SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
7.0 KiB

4 years ago
  1. /*
  2. ------------------- Code Monkey -------------------
  3. Thank you for downloading the Code Monkey Utilities
  4. I hope you find them useful in your projects
  5. If you have any questions use the contact form
  6. Cheers!
  7. unitycodemonkey.com
  8. --------------------------------------------------
  9. */
  10. using UnityEngine;
  11. namespace CodeMonkey.Utils {
  12. /*
  13. * Mesh in the World
  14. * */
  15. public class World_Mesh {
  16. private const int sortingOrderDefault = 5000;
  17. public GameObject gameObject;
  18. public Transform transform;
  19. private Material material;
  20. private Vector3[] vertices;
  21. private Vector2[] uv;
  22. private int[] triangles;
  23. private Mesh mesh;
  24. public static World_Mesh Create(Vector3 position, float eulerZ, float meshWidth, float meshHeight, Material material, UVCoords uvCoords, int sortingOrderOffset = 0) {
  25. return new World_Mesh(null, position, Vector3.one, eulerZ, meshWidth, meshHeight, material, uvCoords, sortingOrderOffset);
  26. }
  27. public static World_Mesh Create(Vector3 lowerLeftCorner, float width, float height, Material material, UVCoords uvCoords, int sortingOrderOffset = 0) {
  28. return Create(lowerLeftCorner, lowerLeftCorner + new Vector3(width, height), material, uvCoords, sortingOrderOffset);
  29. }
  30. public static World_Mesh Create(Vector3 lowerLeftCorner, Vector3 upperRightCorner, Material material, UVCoords uvCoords, int sortingOrderOffset = 0) {
  31. float width = upperRightCorner.x - lowerLeftCorner.x;
  32. float height = upperRightCorner.y - lowerLeftCorner.y;
  33. Vector3 localScale = upperRightCorner - lowerLeftCorner;
  34. Vector3 position = lowerLeftCorner + localScale * .5f;
  35. return new World_Mesh(null, position, Vector3.one, 0f, width, height, material, uvCoords, sortingOrderOffset);
  36. }
  37. private static int GetSortingOrder(Vector3 position, int offset, int baseSortingOrder = sortingOrderDefault) {
  38. return (int)(baseSortingOrder - position.y) + offset;
  39. }
  40. public class UVCoords {
  41. public int x, y, width, height;
  42. public UVCoords(int x, int y, int width, int height) {
  43. this.x = x;
  44. this.y = y;
  45. this.width = width;
  46. this.height = height;
  47. }
  48. }
  49. public World_Mesh(Transform parent, Vector3 localPosition, Vector3 localScale, float eulerZ, float meshWidth, float meshHeight, Material material, UVCoords uvCoords, int sortingOrderOffset) {
  50. this.material = material;
  51. vertices = new Vector3[4];
  52. uv = new Vector2[4];
  53. triangles = new int[6];
  54. /* 0,1
  55. * 1,1
  56. * 0,0
  57. * 1,0
  58. */
  59. float meshWidthHalf = meshWidth / 2f;
  60. float meshHeightHalf = meshHeight / 2f;
  61. vertices[0] = new Vector3(-meshWidthHalf, meshHeightHalf);
  62. vertices[1] = new Vector3( meshWidthHalf, meshHeightHalf);
  63. vertices[2] = new Vector3(-meshWidthHalf, -meshHeightHalf);
  64. vertices[3] = new Vector3( meshWidthHalf, -meshHeightHalf);
  65. if (uvCoords == null) {
  66. uvCoords = new UVCoords(0, 0, material.mainTexture.width, material.mainTexture.height);
  67. }
  68. Vector2[] uvArray = GetUVRectangleFromPixels(uvCoords.x, uvCoords.y, uvCoords.width, uvCoords.height, material.mainTexture.width, material.mainTexture.height);
  69. ApplyUVToUVArray(uvArray, ref uv);
  70. triangles[0] = 0;
  71. triangles[1] = 1;
  72. triangles[2] = 2;
  73. triangles[3] = 2;
  74. triangles[4] = 1;
  75. triangles[5] = 3;
  76. mesh = new Mesh();
  77. mesh.vertices = vertices;
  78. mesh.uv = uv;
  79. mesh.triangles = triangles;
  80. gameObject = new GameObject("Mesh", typeof(MeshFilter), typeof(MeshRenderer));
  81. gameObject.transform.parent = parent;
  82. gameObject.transform.localPosition = localPosition;
  83. gameObject.transform.localScale = localScale;
  84. gameObject.transform.localEulerAngles = new Vector3(0, 0, eulerZ);
  85. gameObject.GetComponent<MeshFilter>().mesh = mesh;
  86. gameObject.GetComponent<MeshRenderer>().material = material;
  87. transform = gameObject.transform;
  88. SetSortingOrderOffset(sortingOrderOffset);
  89. }
  90. private Vector2 ConvertPixelsToUVCoordinates(int x, int y, int textureWidth, int textureHeight) {
  91. return new Vector2((float)x / textureWidth, (float)y / textureHeight);
  92. }
  93. private Vector2[] GetUVRectangleFromPixels(int x, int y, int width, int height, int textureWidth, int textureHeight) {
  94. /* 0, 1
  95. * 1, 1
  96. * 0, 0
  97. * 1, 0
  98. * */
  99. return new Vector2[] {
  100. ConvertPixelsToUVCoordinates(x, y + height, textureWidth, textureHeight),
  101. ConvertPixelsToUVCoordinates(x + width, y + height, textureWidth, textureHeight),
  102. ConvertPixelsToUVCoordinates(x, y, textureWidth, textureHeight),
  103. ConvertPixelsToUVCoordinates(x + width, y, textureWidth, textureHeight)
  104. };
  105. }
  106. private void ApplyUVToUVArray(Vector2[] uv, ref Vector2[] mainUV) {
  107. if (uv == null || uv.Length < 4 || mainUV == null || mainUV.Length < 4) throw new System.Exception();
  108. mainUV[0] = uv[0];
  109. mainUV[1] = uv[1];
  110. mainUV[2] = uv[2];
  111. mainUV[3] = uv[3];
  112. }
  113. public void SetUVCoords(UVCoords uvCoords) {
  114. Vector2[] uvArray = GetUVRectangleFromPixels(uvCoords.x, uvCoords.y, uvCoords.width, uvCoords.height, material.mainTexture.width, material.mainTexture.height);
  115. ApplyUVToUVArray(uvArray, ref uv);
  116. mesh.uv = uv;
  117. }
  118. public void SetSortingOrderOffset(int sortingOrderOffset) {
  119. SetSortingOrder(GetSortingOrder(gameObject.transform.position, sortingOrderOffset));
  120. }
  121. public void SetSortingOrder(int sortingOrder) {
  122. gameObject.GetComponent<Renderer>().sortingOrder = sortingOrder;
  123. }
  124. public void SetLocalScale(Vector3 localScale) {
  125. transform.localScale = localScale;
  126. }
  127. public void SetPosition(Vector3 localPosition) {
  128. transform.localPosition = localPosition;
  129. }
  130. public void AddPosition(Vector3 addPosition) {
  131. transform.localPosition += addPosition;
  132. }
  133. public Vector3 GetPosition() {
  134. return transform.localPosition;
  135. }
  136. public int GetSortingOrder() {
  137. return gameObject.GetComponent<Renderer>().sortingOrder;
  138. }
  139. public void Show() {
  140. gameObject.SetActive(true);
  141. }
  142. public void Hide() {
  143. gameObject.SetActive(false);
  144. }
  145. public void DestroySelf() {
  146. Object.Destroy(gameObject);
  147. }
  148. }
  149. }