Esempio n. 1
0
 public void Encapsulate(RotatableBounds rotatableBounds)
 {
     Encapsulate(rotatableBounds.RightTopFront);
     Encapsulate(rotatableBounds.RightTopBack);
     Encapsulate(rotatableBounds.RightBottomFront);
     Encapsulate(rotatableBounds.RightBottomBack);
     Encapsulate(rotatableBounds.LeftTopFront);
     Encapsulate(rotatableBounds.LeftTopBack);
     Encapsulate(rotatableBounds.LeftBottomFront);
     Encapsulate(rotatableBounds.LeftBottomBack);
 }
Esempio n. 2
0
        public static RotatableBounds GetLocalBound(GameObject gameObject, BoundsCreateOption option)
        {
            RotatableBounds rotatableBounds = new RotatableBounds();

            bool isCenterSettedByTransform = false;

            if (option == BoundsCreateOption.Transform || option == BoundsCreateOption.TransformAndMesh)
            {
                Transform[] transforms = gameObject.GetComponentsInChildren <Transform>();

                rotatableBounds           = new RotatableBounds(transforms[0].position, transforms[0].rotation);
                isCenterSettedByTransform = true;

                for (int i = 1; i < transforms.Length; i++)
                {
                    rotatableBounds.Encapsulate(transforms[i].position);
                }
            }

            if (option == BoundsCreateOption.Mesh || option == BoundsCreateOption.TransformAndMesh)
            {
                MeshFilter[] meshFilters = gameObject.GetComponentsInChildren <MeshFilter>();

                if (!isCenterSettedByTransform)
                {
                    if (meshFilters.Length == 0)
                    {
                        throw new BoundsCreationException("Object and it's childs not have MeshFilter component");
                    }
                    else
                    {
                        Vector3 boundsCenter = meshFilters[0].transform.TransformPoint(meshFilters[0].sharedMesh.bounds.center);
                        rotatableBounds = new RotatableBounds(boundsCenter, gameObject.transform.rotation);
                    }
                }

                for (int i = 0; i < meshFilters.Length; i++)
                {
                    Bounds meshBounds = meshFilters[i].sharedMesh.bounds;
                    meshBounds.center = meshFilters[i].transform.TransformPoint(meshBounds.center);
                    meshBounds.size   = Vector3.Scale(meshBounds.size, meshFilters[i].transform.lossyScale);

                    rotatableBounds.Encapsulate(new RotatableBounds(meshBounds, meshFilters[i].transform.rotation));
                }
            }

            return(rotatableBounds);
        }