Bounds是struct結(jié)構(gòu)體,是一種軸對(duì)齊的對(duì)邊界的表示方式年柠。
定義
僅需要center和extends兩個(gè)就能自定義一個(gè)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;
}
軸對(duì)齊
軸對(duì)齊的意思是無法將bounds進(jìn)行軸旋轉(zhuǎn)畸陡,例:
bounds-axi.png
cube1坐標(biāo)為(0,0,0) scale為(1,1,1),cube2坐標(biāo)為(0,4,0) scale為(1,1,1)序芦,且沿Y旋轉(zhuǎn)45°。
通過兩個(gè)物體上的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的軸對(duì)齊特性,不能和簡單物體本身體積或其他屬性化等號(hào)寥枝。
Encapsulate
封裝由于多個(gè)Bounds中的計(jì)算宪塔,可以對(duì)點(diǎn)也可以對(duì)其他bounds進(jìn)行封裝。
/// <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);
}