publicpartialstruct Plane { // sizeof(Plane) is not const in C# and so cannot be used in fixed arrays, so we define it here internalconstint size = 16;
Vector3 m_Normal; float m_Distance;
// Normal vector of the plane. public Vector3 normal { get { return m_Normal; } set { m_Normal = value; } } // Distance from the origin to the plane. publicfloat distance { get { return m_Distance; } set { m_Distance = value; } }
// Creates a plane. publicPlane(Vector3 a, Vector3 b, Vector3 c) { m_Normal = Vector3.Normalize(Vector3.Cross(b - a, c - a)); m_Distance = -Vector3.Dot(m_Normal, a); }
// Sets a plane using a point that lies within it plus a normal to orient it (note that the normal must be a normalized vector). publicvoidSetNormalAndPosition(Vector3 inNormal, Vector3 inPoint) { m_Normal = Vector3.Normalize(inNormal); m_Distance = -Vector3.Dot(inNormal, inPoint); }
// Sets a plane using three points that lie within it. The points go around clockwise as you look down on the top surface of the plane. publicvoidSet3Points(Vector3 a, Vector3 b, Vector3 c) { m_Normal = Vector3.Normalize(Vector3.Cross(b - a, c - a)); m_Distance = -Vector3.Dot(m_Normal, a); }
// Translates the plane into a given direction publicvoidTranslate(Vector3 translation) { m_Distance += Vector3.Dot(m_Normal, translation); }
// Creates a plane that's translated into a given direction publicstatic Plane Translate(Plane plane, Vector3 translation) { returnnew Plane(plane.m_Normal, plane.m_Distance += Vector3.Dot(plane.m_Normal, translation)); }
// Calculates the closest point on the plane. public Vector3 ClosestPointOnPlane(Vector3 point) { var pointToPlaneDistance = Vector3.Dot(m_Normal, point) + m_Distance; return point - (m_Normal * pointToPlaneDistance); }
// Returns a signed distance from plane to point. publicfloatGetDistanceToPoint(Vector3 point) { return Vector3.Dot(m_Normal, point) + m_Distance; }
// Is a point on the positive side of the plane? publicboolGetSide(Vector3 point) { return Vector3.Dot(m_Normal, point) + m_Distance > 0.0F; }
// Are two points on the same side of the plane? publicboolSameSide(Vector3 inPt0, Vector3 inPt1) { float d0 = GetDistanceToPoint(inPt0); float d1 = GetDistanceToPoint(inPt1); return (d0 > 0.0f && d1 > 0.0f) || (d0 <= 0.0f && d1 <= 0.0f); }
// Intersects a ray with the plane. publicboolRaycast(Ray ray, outfloat enter) { float vdot = Vector3.Dot(ray.direction, m_Normal); float ndot = -Vector3.Dot(ray.origin, m_Normal) - m_Distance;
if (Mathf.Approximately(vdot, 0.0f)) { enter = 0.0F; returnfalse; }