Unity Rect和Bounds源码

Rect

public struct Rect : IEquatable<Rect>
{
	[NativeName("x")]
	private float m_XMin;

	[NativeName("y")]
	private float m_YMin;

	[NativeName("width")]
	private float m_Width;

	[NativeName("height")]
	private float m_Height;
	
	public static Rect zero		{get {return new Rect(0f, 0f, 0f, 0f);}}

	
	public float x	{get	{return this.m_XMin;}	set	{this.m_XMin = value;}}

	/// <summary>
	///   <para>The Y coordinate of the rectangle.</para>
	/// </summary>
	public float y	{get	{return this.m_YMin;}	set	{this.m_YMin = value;	}}

	/// <summary>
	///   <para>The X and Y position of the rectangle.</para>
	/// </summary>
	public Vector2 position{get	{return new Vector2(this.m_XMin, this.m_YMin);}
		set {this.m_XMin = value.x;	this.m_YMin = value.y;}}

	/// <summary>
	///   <para>The position of the center of the rectangle.</para>
	/// </summary>
	public Vector2 center
	{
		get
		{
			return new Vector2(this.x + this.m_Width / 2f, this.y + this.m_Height / 2f);
		}
		set
		{
			this.m_XMin = value.x - this.m_Width / 2f;
			this.m_YMin = value.y - this.m_Height / 2f;
		}
	}

	/// <summary>
	///   <para>The position of the minimum corner of the rectangle.</para>
	/// </summary>
	public Vector2 min
	{
		get
		{
			return new Vector2(this.xMin, this.yMin);
		}
		set
		{
			this.xMin = value.x;
			this.yMin = value.y;
		}
	}

	/// <summary>
	///   <para>The position of the maximum corner of the rectangle.</para>
	/// </summary>
	public Vector2 max
	{
		get
		{
			return new Vector2(this.xMax, this.yMax);
		}
		set
		{
			this.xMax = value.x;
			this.yMax = value.y;
		}
	}

	/// <summary>
	///   <para>The width of the rectangle, measured from the X position.</para>
	/// </summary>
	public float width
	{
		get
		{
			return this.m_Width;
		}
		set
		{
			this.m_Width = value;
		}
	}

	/// <summary>
	///   <para>The height of the rectangle, measured from the Y position.</para>
	/// </summary>
	public float height
	{
		get
		{
			return this.m_Height;
		}
		set
		{
			this.m_Height = value;
		}
	}

	/// <summary>
	///   <para>The width and height of the rectangle.</para>
	/// </summary>
	public Vector2 size
	{
		get
		{
			return new Vector2(this.m_Width, this.m_Height);
		}
		set
		{
			this.m_Width = value.x;
			this.m_Height = value.y;
		}
	}

	/// <summary>
	///   <para>The minimum X coordinate of the rectangle.</para>
	/// </summary>
	public float xMin
	{
		get
		{
			return this.m_XMin;
		}
		set
		{
			float xMax = this.xMax;
			this.m_XMin = value;
			this.m_Width = xMax - this.m_XMin;
		}
	}

	/// <summary>
	///   <para>The minimum Y coordinate of the rectangle.</para>
	/// </summary>
	public float yMin
	{
		get
		{
			return this.m_YMin;
		}
		set
		{
			float yMax = this.yMax;
			this.m_YMin = value;
			this.m_Height = yMax - this.m_YMin;
		}
	}

	/// <summary>
	///   <para>The maximum X coordinate of the rectangle.</para>
	/// </summary>
	public float xMax
	{
		get
		{
			return this.m_Width + this.m_XMin;
		}
		set
		{
			this.m_Width = value - this.m_XMin;
		}
	}

	/// <summary>
	///   <para>The maximum Y coordinate of the rectangle.</para>
	/// </summary>
	public float yMax
	{
		get
		{
			return this.m_Height + this.m_YMin;
		}
		set
		{
			this.m_Height = value - this.m_YMin;
		}
	}

	[Obsolete("use xMin")]
	public float left
	{
		get
		{
			return this.m_XMin;
		}
	}

	[Obsolete("use xMax")]
	public float right
	{
		get
		{
			return this.m_XMin + this.m_Width;
		}
	}

	[Obsolete("use yMin")]
	public float top
	{
		get
		{
			return this.m_YMin;
		}
	}

	[Obsolete("use yMax")]
	public float bottom
	{
		get
		{
			return this.m_YMin + this.m_Height;
		}
	}
	public Rect(float x, float y, float width, float height)
	{
		this.m_XMin = x;
		this.m_YMin = y;
		this.m_Width = width;
		this.m_Height = height;
	}
	public Rect(Vector2 position, Vector2 size)
	{
		this.m_XMin = position.x;
		this.m_YMin = position.y;
		this.m_Width = size.x;
		this.m_Height = size.y;
	}

	/// <summary>
	///   <para></para>
	/// </summary>
	/// <param name="source"></param>
	public Rect(Rect source)
	{
		this.m_XMin = source.m_XMin;
		this.m_YMin = source.m_YMin;
		this.m_Width = source.m_Width;
		this.m_Height = source.m_Height;
	}
	public static Rect MinMaxRect(float xmin, float ymin, float xmax, float ymax)
	{
		return new Rect(xmin, ymin, xmax - xmin, ymax - ymin);
	}
	public void Set(float x, float y, float width, float height)
	{
		this.m_XMin = x;
		this.m_YMin = y;
		this.m_Width = width;
		this.m_Height = height;
	}
	public bool Contains(Vector2 point)
	{
		return point.x >= this.xMin && point.x < this.xMax && point.y >= this.yMin && point.y < this.yMax;
	}
	public bool Contains(Vector3 point)
	{
		return point.x >= this.xMin && point.x < this.xMax && point.y >= this.yMin && point.y < this.yMax;
	}
	public bool Contains(Vector3 point, bool allowInverse)
	{
		bool result;
		if (!allowInverse)
		{
			result = this.Contains(point);
		}
		else
		{
			bool flag = false;
			if ((this.width < 0f && point.x <= this.xMin && point.x > this.xMax) || (this.width >= 0f && point.x >= this.xMin && point.x < this.xMax))
			{
				flag = true;
			}
			result = (flag && ((this.height < 0f && point.y <= this.yMin && point.y > this.yMax) || (this.height >= 0f && point.y >= this.yMin && point.y < this.yMax)));
		}
		return result;
	}

	private static Rect OrderMinMax(Rect rect)
	{
		if (rect.xMin > rect.xMax)
		{
			float xMin = rect.xMin;
			rect.xMin = rect.xMax;
			rect.xMax = xMin;
		}
		if (rect.yMin > rect.yMax)
		{
			float yMin = rect.yMin;
			rect.yMin = rect.yMax;
			rect.yMax = yMin;
		}
		return rect;
	}
	public bool Overlaps(Rect other)
	{
		return other.xMax > this.xMin && other.xMin < this.xMax && other.yMax > this.yMin && other.yMin < this.yMax;
	}
	public bool Overlaps(Rect other, bool allowInverse)
	{
		Rect rect = this;
		if (allowInverse)
		{
			rect = Rect.OrderMinMax(rect);
			other = Rect.OrderMinMax(other);
		}
		return rect.Overlaps(other);
	}
	public static Vector2 NormalizedToPoint(Rect rectangle, Vector2 normalizedRectCoordinates)
	{
		return new Vector2(Mathf.Lerp(rectangle.x, rectangle.xMax, normalizedRectCoordinates.x), Mathf.Lerp(rectangle.y, rectangle.yMax, normalizedRectCoordinates.y));
	}
	public static Vector2 PointToNormalized(Rect rectangle, Vector2 point)
	{
		return new Vector2(Mathf.InverseLerp(rectangle.x, rectangle.xMax, point.x), Mathf.InverseLerp(rectangle.y, rectangle.yMax, point.y));
	}

	public static bool operator !=(Rect lhs, Rect rhs)
	{
		return !(lhs == rhs);
	}

	public static bool operator ==(Rect lhs, Rect rhs)
	{
		return lhs.x == rhs.x && lhs.y == rhs.y && lhs.width == rhs.width && lhs.height == rhs.height;
	}

	public override int GetHashCode()
	{
		return this.x.GetHashCode() ^ this.width.GetHashCode() << 2 ^ this.y.GetHashCode() >> 2 ^ this.height.GetHashCode() >> 1;
	}

	public override bool Equals(object other)
	{
		return other is Rect && this.Equals((Rect)other);
	}

	public bool Equals(Rect other)
	{
		return this.x.Equals(other.x) && this.y.Equals(other.y) && this.width.Equals(other.width) && this.height.Equals(other.height);
	}
	public override string ToString()
	{
		return UnityString.Format("(x:{0:F2}, y:{1:F2}, width:{2:F2}, height:{3:F2})", new object[]
		{
			this.x,
			this.y,
			this.width,
			this.height
		});
	}
	public string ToString(string format)
	{
		return UnityString.Format("(x:{0}, y:{1}, width:{2}, height:{3})", new object[]
		{
			this.x.ToString(format),
			this.y.ToString(format),
			this.width.ToString(format),
			this.height.ToString(format)
		});
	}
}

Bounds

public struct Bounds : IEquatable<Bounds>
{
	private Vector3 m_Center;

	[NativeName("m_Extent")]
	private Vector3 m_Extents;

	/// <summary>
	///   <para>The center of the bounding box.</para>
	/// </summary>
	public Vector3 center
	{
		get
		{
			return this.m_Center;
		}
		set
		{
			this.m_Center = value;
		}
	}

	/// <summary>
	///   <para>The total size of the box. This is always twice as large as the extents.</para>
	/// </summary>
	public Vector3 size
	{
		get
		{
			return this.m_Extents * 2f;
		}
		set
		{
			this.m_Extents = value * 0.5f;
		}
	}

	/// <summary>
	///   <para>The extents of the Bounding Box. This is always half of the size of the Bounds.</para>
	/// </summary>
	public Vector3 extents
	{
		get
		{
			return this.m_Extents;
		}
		set
		{
			this.m_Extents = value;
		}
	}

	/// <summary>
	///   <para>The minimal point of the box. This is always equal to center-extents.</para>
	/// </summary>
	public Vector3 min
	{
		get
		{
			return this.center - this.extents;
		}
		set
		{
			this.SetMinMax(value, this.max);
		}
	}

	/// <summary>
	///   <para>The maximal point of the box. This is always equal to center+extents.</para>
	/// </summary>
	public Vector3 max
	{
		get
		{
			return this.center + this.extents;
		}
		set
		{
			this.SetMinMax(this.min, value);
		}
	}

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

	public override int GetHashCode()
	{
		return this.center.GetHashCode() ^ this.extents.GetHashCode() << 2;
	}

	public override bool Equals(object other)
	{
		return other is Bounds && this.Equals((Bounds)other);
	}

	public bool Equals(Bounds other)
	{
		return this.center.Equals(other.center) && this.extents.Equals(other.extents);
	}

	public static bool operator ==(Bounds lhs, Bounds rhs)
	{
		return lhs.center == rhs.center && lhs.extents == rhs.extents;
	}

	public static bool operator !=(Bounds lhs, Bounds rhs)
	{
		return !(lhs == rhs);
	}

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

	/// <summary>
	///   <para>Expand the bounds by increasing its size by amount along each side.</para>
	/// </summary>
	/// <param name="amount"></param>
	public void Expand(float amount)
	{
		amount *= 0.5f;
		this.extents += new Vector3(amount, amount, amount);
	}

	/// <summary>
	///   <para>Expand the bounds by increasing its size by amount along each side.</para>
	/// </summary>
	/// <param name="amount"></param>
	public void Expand(Vector3 amount)
	{
		this.extents += amount * 0.5f;
	}

	/// <summary>
	///   <para>Does another bounding box intersect with this bounding box?</para>
	/// </summary>
	/// <param name="bounds"></param>
	public bool Intersects(Bounds bounds)
	{
		return this.min.x <= bounds.max.x && this.max.x >= bounds.min.x && this.min.y <= bounds.max.y && this.max.y >= bounds.min.y && this.min.z <= bounds.max.z && this.max.z >= bounds.min.z;
	}

	/// <summary>
	///   <para>Does ray intersect this bounding box?</para>
	/// </summary>
	/// <param name="ray"></param>
	public bool IntersectRay(Ray ray)
	{
		float num;
		return Bounds.IntersectRayAABB(ray, this, out num);
	}

	public bool IntersectRay(Ray ray, out float distance)
	{
		return Bounds.IntersectRayAABB(ray, this, out distance);
	}
}

猜你喜欢

转载自blog.csdn.net/u010778229/article/details/118308436