UnityEngine.Bounds解析

Bounds是struct结构体,是一种轴对齐的对边界的表示方式。

定义

仅需要center和extends两个就能自定义一个Bounds;

        /// 
        ///   Creates a new Bounds.
        /// 
        /// The location of the origin of the Bounds.
        /// The dimensions of the Bounds.
        public Bounds(Vector3 center, Vector3 size)
        {
            this.m_Center = center;
            this.m_Extents = size * 0.5f;
        }

轴对齐

轴对齐的意思是无法将bounds进行轴旋转,例:

UnityEngine.Bounds解析_第1张图片
bounds-axi.png

cube1坐标为(0,0,0) scale为(1,1,1),cube2坐标为(0,4,0) scale为(1,1,1),且沿Y旋转45°。
通过两个物体上的Boxcollider组件访问到bounds后,其bounds分别为:
cube1 Center: (0.0, 4.0, 0.0), Extents: (0.7, 0.5, 0.7)
cube2 Center: (0.0, 0.0, 0.0), Extents: (0.5, 0.5, 0.5)
可见,由于bounds的轴对齐特性,不能和简单物体本身体积或其他属性化等号。

Encapsulate

封装由于多个Bounds中的计算,可以对点也可以对其他bounds进行封装。

        /// 
        ///   Sets the bounds to the min and max value of the box.
        /// 
        /// 
        /// 
        public void SetMinMax(Vector3 min, Vector3 max)
        {
            this.extents = (max - min) * 0.5f;
            this.center = min + this.extents;
        }

        /// 
        ///   Grows the Bounds to include the point.
        /// 
        /// 
        public void Encapsulate(Vector3 point)
        {
            this.SetMinMax(Vector3.Min(this.min, point), Vector3.Max(this.max, point));
        }

        /// 
        ///   Grow the bounds to encapsulate the bounds.
        /// 
        /// 
        public void Encapsulate(Bounds bounds)
        {
            this.Encapsulate(bounds.center - bounds.extents);
            this.Encapsulate(bounds.center + bounds.extents);
        }

你可能感兴趣的:(UnityEngine.Bounds解析)