GetAllBounds.cs 14 KB

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