123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- using UnityEngine;
- /// <summary>
- /// 获取某一物体的实际Bounds 参数
- ///
- ///
- /// </summary>
- public class GetAllBounds
- {
- public static Bounds GetRendererBounds( GameObject obj)
- {
- Renderer[] renderers = obj.GetComponentsInChildren<Renderer>();
-
- Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);
- if (renderers.Length < 1)
- return bounds;
- bounds = new Bounds(renderers[0].transform.position, Vector3.zero);
- float maxX = renderers[0].transform.position.x + renderers[0].bounds.size.x / 2.0f;
- float minX = renderers[0].transform.position.x - renderers[0].bounds.size.x / 2.0f;
- float maxY = renderers[0].transform.position.y + renderers[0].bounds.size.y / 2.0f;
- float minY = renderers[0].transform.position.y - renderers[0].bounds.size.y / 2.0f;
- float maxZ = renderers[0].transform.position.z + renderers[0].bounds.size.z / 2.0f;
- float minZ = renderers[0].transform.position.z - renderers[0].bounds.size.z / 2.0f;
- foreach (Renderer renderer in renderers)
- {
- bounds.Encapsulate(renderer.bounds);
- if (maxX < renderer.transform.position.x + renderer.bounds.size.x / 2.0f)
- maxX = renderer.transform.position.x + renderer.bounds.size.x / 2.0f;
- if (minX > renderer.transform.position.x - renderer.bounds.size.x / 2.0f)
- minX = renderer.transform.position.x - renderer.bounds.size.x / 2.0f;
- if (maxY < renderer.transform.position.y + renderer.bounds.size.y / 2.0f)
- maxY = renderer.transform.position.y + renderer.bounds.size.y / 2.0f;
- if (minY > renderer.transform.position.y - renderer.bounds.size.y / 2.0f)
- minY = renderer.transform.position.y - renderer.bounds.size.y / 2.0f;
- if (maxZ < renderer.transform.position.z + renderer.bounds.size.z / 2.0f)
- maxZ = renderer.transform.position.z + renderer.bounds.size.z / 2.0f;
- if (minZ > renderer.transform.position.z - renderer.bounds.size.z / 2.0f)
- minZ = renderer.transform.position.z - renderer.bounds.size.z / 2.0f;
- }
- bounds.center = new Vector3(maxX - (maxX - minX) / 2.0f, maxY - (maxY - minY) / 2.0f, maxZ - (maxZ - minZ) / 2.0f);
- return bounds;
- }
- public static Bounds GetMeshFiletBounds(GameObject obj)
- {
- MeshFilter[] meshFilters = obj.GetComponentsInChildren<MeshFilter>();
- Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);
- if (meshFilters.Length < 1)
- return bounds;
- bounds = new Bounds(meshFilters[0].transform.position, Vector3.zero);
- Bounds meshBounds = GetMeshBounds(meshFilters[0].gameObject, meshFilters[0].mesh);
- float maxX = meshFilters[0].transform.position.x + meshBounds.size.x / 2.0f;
- float minX = meshFilters[0].transform.position.x - meshBounds.size.x / 2.0f;
- float maxY = meshFilters[0].transform.position.y + meshBounds.size.y / 2.0f;
- float minY = meshFilters[0].transform.position.y - meshBounds.size.y / 2.0f;
- float maxZ = meshFilters[0].transform.position.z + meshBounds.size.z / 2.0f;
- float minZ = meshFilters[0].transform.position.z - meshBounds.size.z / 2.0f;
- foreach (MeshFilter meshFilter in meshFilters)
- {
- meshBounds = GetMeshBounds(meshFilter.gameObject, meshFilter.mesh);
- bounds.Encapsulate(meshBounds);
- if (maxX < meshFilter.transform.position.x + meshBounds.size.x / 2.0f)
- maxX = meshFilter.transform.position.x + meshBounds.size.x / 2.0f;
- if (minX > meshFilter.transform.position.x - meshBounds.size.x / 2.0f)
- minX = meshFilter.transform.position.x - meshBounds.size.x / 2.0f;
- if (maxY < meshFilter.transform.position.y + meshBounds.size.y / 2.0f)
- maxY = meshFilter.transform.position.y + meshBounds.size.y / 2.0f;
- if (minY > meshFilter.transform.position.y - meshBounds.size.y / 2.0f)
- minY = meshFilter.transform.position.y - meshBounds.size.y / 2.0f;
- if (maxZ < meshFilter.transform.position.z + meshBounds.size.z / 2.0f)
- maxZ = meshFilter.transform.position.z + meshBounds.size.z / 2.0f;
- if (minZ > meshFilter.transform.position.z - meshBounds.size.z / 2.0f)
- minZ = meshFilter.transform.position.z - meshBounds.size.z / 2.0f;
- }
- bounds.center = new Vector3(maxX - (maxX - minX) / 2.0f, maxY - (maxY - minY) / 2.0f, maxZ - (maxZ - minZ) / 2.0f);
- return bounds;
- }
- public static Bounds GetSkinnedMeshBounds(GameObject obj)
- {
- SkinnedMeshRenderer[] skinnedMeshRenderers = obj.GetComponentsInChildren<SkinnedMeshRenderer>();
- Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);
- if (skinnedMeshRenderers.Length < 1)
- return bounds;
- Mesh mesh = new Mesh();
- skinnedMeshRenderers[0].BakeMesh(mesh);
- bounds = new Bounds(skinnedMeshRenderers[0].transform.position, Vector3.zero);
- Bounds meshBounds = GetMeshBounds(skinnedMeshRenderers[0].gameObject, mesh);
- float maxX = skinnedMeshRenderers[0].transform.position.x + meshBounds.size.x / 2.0f;
- float minX = skinnedMeshRenderers[0].transform.position.x - meshBounds.size.x / 2.0f;
- float maxY = skinnedMeshRenderers[0].transform.position.y + meshBounds.size.y / 2.0f;
- float minY = skinnedMeshRenderers[0].transform.position.y - meshBounds.size.y / 2.0f;
- float maxZ = skinnedMeshRenderers[0].transform.position.z + meshBounds.size.z / 2.0f;
- float minZ = skinnedMeshRenderers[0].transform.position.z - meshBounds.size.z / 2.0f;
- foreach (SkinnedMeshRenderer skinnedRenderer in skinnedMeshRenderers)
- {
- skinnedRenderer.BakeMesh(mesh);
- meshBounds = GetMeshBounds(skinnedRenderer.gameObject, mesh);
- bounds.Encapsulate(meshBounds);
- if (maxX < skinnedRenderer.transform.position.x + meshBounds.size.x / 2.0f)
- maxX = skinnedRenderer.transform.position.x + meshBounds.size.x / 2.0f;
- if (minX > skinnedRenderer.transform.position.x - meshBounds.size.x / 2.0f)
- minX = skinnedRenderer.transform.position.x - meshBounds.size.x / 2.0f;
- if (maxY < skinnedRenderer.transform.position.y + meshBounds.size.y / 2.0f)
- maxY = skinnedRenderer.transform.position.y + meshBounds.size.y / 2.0f;
- if (minY > skinnedRenderer.transform.position.y - meshBounds.size.y / 2.0f)
- minY = skinnedRenderer.transform.position.y - meshBounds.size.y / 2.0f;
- if (maxZ < skinnedRenderer.transform.position.z + meshBounds.size.z / 2.0f)
- maxZ = skinnedRenderer.transform.position.z + meshBounds.size.z / 2.0f;
- if (minZ > skinnedRenderer.transform.position.z - meshBounds.size.z / 2.0f)
- minZ = skinnedRenderer.transform.position.z - meshBounds.size.z / 2.0f;
- }
- bounds.center = new Vector3(maxX - (maxX - minX) / 2.0f, maxY - (maxY - minY) / 2.0f, maxZ - (maxZ - minZ) / 2.0f);
- return bounds;
- }
- public static Bounds GetAllObjectBounds(GameObject obj)
- {
- Renderer[] renderers = obj.GetComponentsInChildren<Renderer>();
- MeshFilter[] meshFilters = obj.GetComponentsInChildren<MeshFilter>();
- Bounds meshBounds = new Bounds(Vector3.zero,Vector3.zero);
- SkinnedMeshRenderer[] skinnedMeshRenderers = obj.GetComponentsInChildren<SkinnedMeshRenderer>();
- Mesh mesh = new Mesh();
- Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);
- float maxX;
- float minX;
- float maxY;
- float minY;
- float maxZ;
- float minZ;
- if (renderers.Length>0)
- {
- bounds = new Bounds(renderers[0].transform.position, Vector3.zero);
- maxX = renderers[0].transform.position.x + renderers[0].bounds.size.x / 2.0f;
- minX = renderers[0].transform.position.x - renderers[0].bounds.size.x / 2.0f;
- maxY = renderers[0].transform.position.y + renderers[0].bounds.size.y / 2.0f;
- minY = renderers[0].transform.position.y - renderers[0].bounds.size.y / 2.0f;
- maxZ = renderers[0].transform.position.z + renderers[0].bounds.size.z / 2.0f;
- minZ = renderers[0].transform.position.z - renderers[0].bounds.size.z / 2.0f;
- }
- else if( meshFilters.Length>0)
- {
- bounds = new Bounds(meshFilters[0].transform.position, Vector3.zero);
- meshBounds = GetMeshBounds(meshFilters[0].gameObject, meshFilters[0].mesh);
- maxX = meshFilters[0].transform.position.x + meshBounds.size.x / 2.0f;
- minX = meshFilters[0].transform.position.x - meshBounds.size.x / 2.0f;
- maxY = meshFilters[0].transform.position.y + meshBounds.size.y / 2.0f;
- minY = meshFilters[0].transform.position.y - meshBounds.size.y / 2.0f;
- maxZ = meshFilters[0].transform.position.z + meshBounds.size.z / 2.0f;
- minZ = meshFilters[0].transform.position.z - meshBounds.size.z / 2.0f;
- }
- else if( skinnedMeshRenderers.Length>0)
- {
- skinnedMeshRenderers[0].BakeMesh(mesh);
- meshBounds = GetMeshBounds(skinnedMeshRenderers[0].gameObject, mesh);
- maxX = skinnedMeshRenderers[0].transform.position.x + meshBounds.size.x / 2.0f;
- minX = skinnedMeshRenderers[0].transform.position.x - meshBounds.size.x / 2.0f;
- maxY = skinnedMeshRenderers[0].transform.position.y + meshBounds.size.y / 2.0f;
- minY = skinnedMeshRenderers[0].transform.position.y - meshBounds.size.y / 2.0f;
- maxZ = skinnedMeshRenderers[0].transform.position.z + meshBounds.size.z / 2.0f;
- minZ = skinnedMeshRenderers[0].transform.position.z - meshBounds.size.z / 2.0f;
- }
- else
- {
- return bounds;
- }
-
- foreach (Renderer renderer in renderers)
- {
- bounds.Encapsulate(renderer.bounds);
- if (maxX < renderer.transform.position.x + renderer.bounds.size.x / 2.0f)
- maxX = renderer.transform.position.x + renderer.bounds.size.x / 2.0f;
- if (minX > renderer.transform.position.x - renderer.bounds.size.x / 2.0f)
- minX = renderer.transform.position.x - renderer.bounds.size.x / 2.0f;
- if (maxY < renderer.transform.position.y + renderer.bounds.size.y / 2.0f)
- maxY = renderer.transform.position.y + renderer.bounds.size.y / 2.0f;
- if (minY > renderer.transform.position.y - renderer.bounds.size.y / 2.0f)
- minY = renderer.transform.position.y - renderer.bounds.size.y / 2.0f;
- if (maxZ < renderer.transform.position.z + renderer.bounds.size.z / 2.0f)
- maxZ = renderer.transform.position.z + renderer.bounds.size.z / 2.0f;
- if (minZ > renderer.transform.position.z - renderer.bounds.size.z / 2.0f)
- minZ = renderer.transform.position.z - renderer.bounds.size.z / 2.0f;
- }
- foreach (MeshFilter meshFilter in meshFilters)
- {
- meshBounds = GetMeshBounds(meshFilter.gameObject, meshFilter.mesh);
- bounds.Encapsulate(meshBounds);
- if (maxX < meshFilter.transform.position.x + meshBounds.size.x / 2.0f)
- maxX = meshFilter.transform.position.x + meshBounds.size.x / 2.0f;
- if (minX > meshFilter.transform.position.x - meshBounds.size.x / 2.0f)
- minX = meshFilter.transform.position.x - meshBounds.size.x / 2.0f;
- if (maxY < meshFilter.transform.position.y + meshBounds.size.y / 2.0f)
- maxY = meshFilter.transform.position.y + meshBounds.size.y / 2.0f;
- if (minY > meshFilter.transform.position.y - meshBounds.size.y / 2.0f)
- minY = meshFilter.transform.position.y - meshBounds.size.y / 2.0f;
- if (maxZ < meshFilter.transform.position.z + meshBounds.size.z / 2.0f)
- maxZ = meshFilter.transform.position.z + meshBounds.size.z / 2.0f;
- if (minZ > meshFilter.transform.position.z - meshBounds.size.z / 2.0f)
- minZ = meshFilter.transform.position.z - meshBounds.size.z / 2.0f;
- }
- foreach (SkinnedMeshRenderer skinnedRenderer in skinnedMeshRenderers)
- {
- skinnedRenderer.BakeMesh(mesh);
- meshBounds = GetMeshBounds(skinnedRenderer.gameObject, mesh);
- bounds.Encapsulate(meshBounds);
- if (maxX < skinnedRenderer.transform.position.x + meshBounds.size.x / 2.0f)
- maxX = skinnedRenderer.transform.position.x + meshBounds.size.x / 2.0f;
- if (minX > skinnedRenderer.transform.position.x - meshBounds.size.x / 2.0f)
- minX = skinnedRenderer.transform.position.x - meshBounds.size.x / 2.0f;
- if (maxY < skinnedRenderer.transform.position.y + meshBounds.size.y / 2.0f)
- maxY = skinnedRenderer.transform.position.y + meshBounds.size.y / 2.0f;
- if (minY > skinnedRenderer.transform.position.y - meshBounds.size.y / 2.0f)
- minY = skinnedRenderer.transform.position.y - meshBounds.size.y / 2.0f;
- if (maxZ < skinnedRenderer.transform.position.z + meshBounds.size.z / 2.0f)
- maxZ = skinnedRenderer.transform.position.z + meshBounds.size.z / 2.0f;
- if (minZ > skinnedRenderer.transform.position.z - meshBounds.size.z / 2.0f)
- minZ = skinnedRenderer.transform.position.z - meshBounds.size.z / 2.0f;
- }
- bounds.center = new Vector3(maxX - (maxX - minX) / 2.0f, maxY - (maxY - minY) / 2.0f, maxZ - (maxZ - minZ) / 2.0f);
- return bounds;
- }
- private static Bounds GetMeshBounds(GameObject gameObject, Mesh mesh)
- {
- Bounds bounds = new Bounds();
- Vector3[] vertices = mesh.vertices;
- if (vertices.Length != 0)
- {
- bounds = new Bounds(gameObject.transform.TransformPoint(vertices[0]), Vector3.zero);
- for (int index = 1; index < vertices.Length; ++index)
- bounds.Encapsulate(gameObject.transform.TransformPoint(vertices[index]));
- }
- return bounds;
- }
-
- }
|