GetAllBounds.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using UnityEngine;
  2. /// <summary>
  3. /// 获取某一物体的实际Bounds 参数
  4. ///
  5. ///
  6. /// </summary>
  7. public class GetAllBounds
  8. {
  9. public static Bounds GetRendererBounds( GameObject obj)
  10. {
  11. Renderer[] renderers = obj.GetComponentsInChildren<Renderer>();
  12. Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);
  13. if (renderers.Length < 1)
  14. return bounds;
  15. bounds = new Bounds(renderers[0].transform.position, Vector3.zero);
  16. float maxX = renderers[0].transform.position.x + renderers[0].bounds.size.x / 2.0f;
  17. float minX = renderers[0].transform.position.x - renderers[0].bounds.size.x / 2.0f;
  18. float maxY = renderers[0].transform.position.y + renderers[0].bounds.size.y / 2.0f;
  19. float minY = renderers[0].transform.position.y - renderers[0].bounds.size.y / 2.0f;
  20. float maxZ = renderers[0].transform.position.z + renderers[0].bounds.size.z / 2.0f;
  21. float minZ = renderers[0].transform.position.z - renderers[0].bounds.size.z / 2.0f;
  22. foreach (Renderer renderer in renderers)
  23. {
  24. bounds.Encapsulate(renderer.bounds);
  25. if (maxX < renderer.transform.position.x + renderer.bounds.size.x / 2.0f)
  26. maxX = renderer.transform.position.x + renderer.bounds.size.x / 2.0f;
  27. if (minX > renderer.transform.position.x - renderer.bounds.size.x / 2.0f)
  28. minX = renderer.transform.position.x - renderer.bounds.size.x / 2.0f;
  29. if (maxY < renderer.transform.position.y + renderer.bounds.size.y / 2.0f)
  30. maxY = renderer.transform.position.y + renderer.bounds.size.y / 2.0f;
  31. if (minY > renderer.transform.position.y - renderer.bounds.size.y / 2.0f)
  32. minY = renderer.transform.position.y - renderer.bounds.size.y / 2.0f;
  33. if (maxZ < renderer.transform.position.z + renderer.bounds.size.z / 2.0f)
  34. maxZ = renderer.transform.position.z + renderer.bounds.size.z / 2.0f;
  35. if (minZ > renderer.transform.position.z - renderer.bounds.size.z / 2.0f)
  36. minZ = renderer.transform.position.z - renderer.bounds.size.z / 2.0f;
  37. }
  38. bounds.center = new Vector3(maxX - (maxX - minX) / 2.0f, maxY - (maxY - minY) / 2.0f, maxZ - (maxZ - minZ) / 2.0f);
  39. return bounds;
  40. }
  41. public static Bounds GetMeshFiletBounds(GameObject obj)
  42. {
  43. MeshFilter[] meshFilters = obj.GetComponentsInChildren<MeshFilter>();
  44. Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);
  45. if (meshFilters.Length < 1)
  46. return bounds;
  47. bounds = new Bounds(meshFilters[0].transform.position, Vector3.zero);
  48. Bounds meshBounds = GetMeshBounds(meshFilters[0].gameObject, meshFilters[0].mesh);
  49. float maxX = meshFilters[0].transform.position.x + meshBounds.size.x / 2.0f;
  50. float minX = meshFilters[0].transform.position.x - meshBounds.size.x / 2.0f;
  51. float maxY = meshFilters[0].transform.position.y + meshBounds.size.y / 2.0f;
  52. float minY = meshFilters[0].transform.position.y - meshBounds.size.y / 2.0f;
  53. float maxZ = meshFilters[0].transform.position.z + meshBounds.size.z / 2.0f;
  54. float minZ = meshFilters[0].transform.position.z - meshBounds.size.z / 2.0f;
  55. foreach (MeshFilter meshFilter in meshFilters)
  56. {
  57. meshBounds = GetMeshBounds(meshFilter.gameObject, meshFilter.mesh);
  58. bounds.Encapsulate(meshBounds);
  59. if (maxX < meshFilter.transform.position.x + meshBounds.size.x / 2.0f)
  60. maxX = meshFilter.transform.position.x + meshBounds.size.x / 2.0f;
  61. if (minX > meshFilter.transform.position.x - meshBounds.size.x / 2.0f)
  62. minX = meshFilter.transform.position.x - meshBounds.size.x / 2.0f;
  63. if (maxY < meshFilter.transform.position.y + meshBounds.size.y / 2.0f)
  64. maxY = meshFilter.transform.position.y + meshBounds.size.y / 2.0f;
  65. if (minY > meshFilter.transform.position.y - meshBounds.size.y / 2.0f)
  66. minY = meshFilter.transform.position.y - meshBounds.size.y / 2.0f;
  67. if (maxZ < meshFilter.transform.position.z + meshBounds.size.z / 2.0f)
  68. maxZ = meshFilter.transform.position.z + meshBounds.size.z / 2.0f;
  69. if (minZ > meshFilter.transform.position.z - meshBounds.size.z / 2.0f)
  70. minZ = meshFilter.transform.position.z - meshBounds.size.z / 2.0f;
  71. }
  72. bounds.center = new Vector3(maxX - (maxX - minX) / 2.0f, maxY - (maxY - minY) / 2.0f, maxZ - (maxZ - minZ) / 2.0f);
  73. return bounds;
  74. }
  75. public static Bounds GetSkinnedMeshBounds(GameObject obj)
  76. {
  77. SkinnedMeshRenderer[] skinnedMeshRenderers = obj.GetComponentsInChildren<SkinnedMeshRenderer>();
  78. Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);
  79. if (skinnedMeshRenderers.Length < 1)
  80. return bounds;
  81. Mesh mesh = new Mesh();
  82. skinnedMeshRenderers[0].BakeMesh(mesh);
  83. bounds = new Bounds(skinnedMeshRenderers[0].transform.position, Vector3.zero);
  84. Bounds meshBounds = GetMeshBounds(skinnedMeshRenderers[0].gameObject, mesh);
  85. float maxX = skinnedMeshRenderers[0].transform.position.x + meshBounds.size.x / 2.0f;
  86. float minX = skinnedMeshRenderers[0].transform.position.x - meshBounds.size.x / 2.0f;
  87. float maxY = skinnedMeshRenderers[0].transform.position.y + meshBounds.size.y / 2.0f;
  88. float minY = skinnedMeshRenderers[0].transform.position.y - meshBounds.size.y / 2.0f;
  89. float maxZ = skinnedMeshRenderers[0].transform.position.z + meshBounds.size.z / 2.0f;
  90. float minZ = skinnedMeshRenderers[0].transform.position.z - meshBounds.size.z / 2.0f;
  91. foreach (SkinnedMeshRenderer skinnedRenderer in skinnedMeshRenderers)
  92. {
  93. skinnedRenderer.BakeMesh(mesh);
  94. meshBounds = GetMeshBounds(skinnedRenderer.gameObject, mesh);
  95. bounds.Encapsulate(meshBounds);
  96. if (maxX < skinnedRenderer.transform.position.x + meshBounds.size.x / 2.0f)
  97. maxX = skinnedRenderer.transform.position.x + meshBounds.size.x / 2.0f;
  98. if (minX > skinnedRenderer.transform.position.x - meshBounds.size.x / 2.0f)
  99. minX = skinnedRenderer.transform.position.x - meshBounds.size.x / 2.0f;
  100. if (maxY < skinnedRenderer.transform.position.y + meshBounds.size.y / 2.0f)
  101. maxY = skinnedRenderer.transform.position.y + meshBounds.size.y / 2.0f;
  102. if (minY > skinnedRenderer.transform.position.y - meshBounds.size.y / 2.0f)
  103. minY = skinnedRenderer.transform.position.y - meshBounds.size.y / 2.0f;
  104. if (maxZ < skinnedRenderer.transform.position.z + meshBounds.size.z / 2.0f)
  105. maxZ = skinnedRenderer.transform.position.z + meshBounds.size.z / 2.0f;
  106. if (minZ > skinnedRenderer.transform.position.z - meshBounds.size.z / 2.0f)
  107. minZ = skinnedRenderer.transform.position.z - meshBounds.size.z / 2.0f;
  108. }
  109. bounds.center = new Vector3(maxX - (maxX - minX) / 2.0f, maxY - (maxY - minY) / 2.0f, maxZ - (maxZ - minZ) / 2.0f);
  110. return bounds;
  111. }
  112. public static Bounds GetAllObjectBounds(GameObject obj)
  113. {
  114. Renderer[] renderers = obj.GetComponentsInChildren<Renderer>();
  115. MeshFilter[] meshFilters = obj.GetComponentsInChildren<MeshFilter>();
  116. Bounds meshBounds = new Bounds(Vector3.zero,Vector3.zero);
  117. SkinnedMeshRenderer[] skinnedMeshRenderers = obj.GetComponentsInChildren<SkinnedMeshRenderer>();
  118. Mesh mesh = new Mesh();
  119. Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);
  120. float maxX;
  121. float minX;
  122. float maxY;
  123. float minY;
  124. float maxZ;
  125. float minZ;
  126. if (renderers.Length>0)
  127. {
  128. bounds = new Bounds(renderers[0].transform.position, Vector3.zero);
  129. maxX = renderers[0].transform.position.x + renderers[0].bounds.size.x / 2.0f;
  130. minX = renderers[0].transform.position.x - renderers[0].bounds.size.x / 2.0f;
  131. maxY = renderers[0].transform.position.y + renderers[0].bounds.size.y / 2.0f;
  132. minY = renderers[0].transform.position.y - renderers[0].bounds.size.y / 2.0f;
  133. maxZ = renderers[0].transform.position.z + renderers[0].bounds.size.z / 2.0f;
  134. minZ = renderers[0].transform.position.z - renderers[0].bounds.size.z / 2.0f;
  135. }
  136. else if( meshFilters.Length>0)
  137. {
  138. bounds = new Bounds(meshFilters[0].transform.position, Vector3.zero);
  139. meshBounds = GetMeshBounds(meshFilters[0].gameObject, meshFilters[0].mesh);
  140. maxX = meshFilters[0].transform.position.x + meshBounds.size.x / 2.0f;
  141. minX = meshFilters[0].transform.position.x - meshBounds.size.x / 2.0f;
  142. maxY = meshFilters[0].transform.position.y + meshBounds.size.y / 2.0f;
  143. minY = meshFilters[0].transform.position.y - meshBounds.size.y / 2.0f;
  144. maxZ = meshFilters[0].transform.position.z + meshBounds.size.z / 2.0f;
  145. minZ = meshFilters[0].transform.position.z - meshBounds.size.z / 2.0f;
  146. }
  147. else if( skinnedMeshRenderers.Length>0)
  148. {
  149. skinnedMeshRenderers[0].BakeMesh(mesh);
  150. meshBounds = GetMeshBounds(skinnedMeshRenderers[0].gameObject, mesh);
  151. maxX = skinnedMeshRenderers[0].transform.position.x + meshBounds.size.x / 2.0f;
  152. minX = skinnedMeshRenderers[0].transform.position.x - meshBounds.size.x / 2.0f;
  153. maxY = skinnedMeshRenderers[0].transform.position.y + meshBounds.size.y / 2.0f;
  154. minY = skinnedMeshRenderers[0].transform.position.y - meshBounds.size.y / 2.0f;
  155. maxZ = skinnedMeshRenderers[0].transform.position.z + meshBounds.size.z / 2.0f;
  156. minZ = skinnedMeshRenderers[0].transform.position.z - meshBounds.size.z / 2.0f;
  157. }
  158. else
  159. {
  160. return bounds;
  161. }
  162. foreach (Renderer renderer in renderers)
  163. {
  164. bounds.Encapsulate(renderer.bounds);
  165. if (maxX < renderer.transform.position.x + renderer.bounds.size.x / 2.0f)
  166. maxX = renderer.transform.position.x + renderer.bounds.size.x / 2.0f;
  167. if (minX > renderer.transform.position.x - renderer.bounds.size.x / 2.0f)
  168. minX = renderer.transform.position.x - renderer.bounds.size.x / 2.0f;
  169. if (maxY < renderer.transform.position.y + renderer.bounds.size.y / 2.0f)
  170. maxY = renderer.transform.position.y + renderer.bounds.size.y / 2.0f;
  171. if (minY > renderer.transform.position.y - renderer.bounds.size.y / 2.0f)
  172. minY = renderer.transform.position.y - renderer.bounds.size.y / 2.0f;
  173. if (maxZ < renderer.transform.position.z + renderer.bounds.size.z / 2.0f)
  174. maxZ = renderer.transform.position.z + renderer.bounds.size.z / 2.0f;
  175. if (minZ > renderer.transform.position.z - renderer.bounds.size.z / 2.0f)
  176. minZ = renderer.transform.position.z - renderer.bounds.size.z / 2.0f;
  177. }
  178. foreach (MeshFilter meshFilter in meshFilters)
  179. {
  180. meshBounds = GetMeshBounds(meshFilter.gameObject, meshFilter.mesh);
  181. bounds.Encapsulate(meshBounds);
  182. if (maxX < meshFilter.transform.position.x + meshBounds.size.x / 2.0f)
  183. maxX = meshFilter.transform.position.x + meshBounds.size.x / 2.0f;
  184. if (minX > meshFilter.transform.position.x - meshBounds.size.x / 2.0f)
  185. minX = meshFilter.transform.position.x - meshBounds.size.x / 2.0f;
  186. if (maxY < meshFilter.transform.position.y + meshBounds.size.y / 2.0f)
  187. maxY = meshFilter.transform.position.y + meshBounds.size.y / 2.0f;
  188. if (minY > meshFilter.transform.position.y - meshBounds.size.y / 2.0f)
  189. minY = meshFilter.transform.position.y - meshBounds.size.y / 2.0f;
  190. if (maxZ < meshFilter.transform.position.z + meshBounds.size.z / 2.0f)
  191. maxZ = meshFilter.transform.position.z + meshBounds.size.z / 2.0f;
  192. if (minZ > meshFilter.transform.position.z - meshBounds.size.z / 2.0f)
  193. minZ = meshFilter.transform.position.z - meshBounds.size.z / 2.0f;
  194. }
  195. foreach (SkinnedMeshRenderer skinnedRenderer in skinnedMeshRenderers)
  196. {
  197. skinnedRenderer.BakeMesh(mesh);
  198. meshBounds = GetMeshBounds(skinnedRenderer.gameObject, mesh);
  199. bounds.Encapsulate(meshBounds);
  200. if (maxX < skinnedRenderer.transform.position.x + meshBounds.size.x / 2.0f)
  201. maxX = skinnedRenderer.transform.position.x + meshBounds.size.x / 2.0f;
  202. if (minX > skinnedRenderer.transform.position.x - meshBounds.size.x / 2.0f)
  203. minX = skinnedRenderer.transform.position.x - meshBounds.size.x / 2.0f;
  204. if (maxY < skinnedRenderer.transform.position.y + meshBounds.size.y / 2.0f)
  205. maxY = skinnedRenderer.transform.position.y + meshBounds.size.y / 2.0f;
  206. if (minY > skinnedRenderer.transform.position.y - meshBounds.size.y / 2.0f)
  207. minY = skinnedRenderer.transform.position.y - meshBounds.size.y / 2.0f;
  208. if (maxZ < skinnedRenderer.transform.position.z + meshBounds.size.z / 2.0f)
  209. maxZ = skinnedRenderer.transform.position.z + meshBounds.size.z / 2.0f;
  210. if (minZ > skinnedRenderer.transform.position.z - meshBounds.size.z / 2.0f)
  211. minZ = skinnedRenderer.transform.position.z - meshBounds.size.z / 2.0f;
  212. }
  213. bounds.center = new Vector3(maxX - (maxX - minX) / 2.0f, maxY - (maxY - minY) / 2.0f, maxZ - (maxZ - minZ) / 2.0f);
  214. return bounds;
  215. }
  216. private static Bounds GetMeshBounds(GameObject gameObject, Mesh mesh)
  217. {
  218. Bounds bounds = new Bounds();
  219. Vector3[] vertices = mesh.vertices;
  220. if (vertices.Length != 0)
  221. {
  222. bounds = new Bounds(gameObject.transform.TransformPoint(vertices[0]), Vector3.zero);
  223. for (int index = 1; index < vertices.Length; ++index)
  224. bounds.Encapsulate(gameObject.transform.TransformPoint(vertices[index]));
  225. }
  226. return bounds;
  227. }
  228. }