UnityEngine.Bounds解析

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

定义

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

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

轴对齐

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

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进行封装。

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

        /// <summary>
        ///   <para>Grows the Bounds to include the point.</para>
        /// </summary>
        /// <param name="point"></param>
        public void Encapsulate(Vector3 point)
        {
            this.SetMinMax(Vector3.Min(this.min, point), Vector3.Max(this.max, point));
        }

        /// <summary>
        ///   <para>Grow the bounds to encapsulate the bounds.</para>
        /// </summary>
        /// <param name="bounds"></param>
        public void Encapsulate(Bounds bounds)
        {
            this.Encapsulate(bounds.center - bounds.extents);
            this.Encapsulate(bounds.center + bounds.extents);
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容