美文网首页
UnityEngine.Bounds解析

UnityEngine.Bounds解析

作者: lyx2002py | 来源:发表于2017-09-17 00:11 被阅读0次

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);
        }

相关文章

网友评论

      本文标题:UnityEngine.Bounds解析

      本文链接:https://www.haomeiwen.com/subject/kjpwattx.html