public (float Near, float Far) GetClippingPlanes(Transform world, Vector3 cameraPosition, Quaternion cameraOrientation) { Vector3[] clipPoints = new Vector3[4] { new Vector3(-1, 0, HIGHWAY_ADD), new Vector3(1, 0, HIGHWAY_ADD), new Vector3(-1, 0, -HIGHWAY_LENGTH), new Vector3(1, 0, -HIGHWAY_LENGTH) }; float minClipDist = float.MaxValue; float maxClipDist = float.MinValue; Vector3 cameraForward = Vector3.Transform(new Vector3(0, 0, -1), cameraOrientation); for (int i = 0; i < 4; i++) { float clipDist = SignedDistance(Vector3.Transform(clipPoints[i], world.Matrix) - cameraPosition, cameraForward); minClipDist = Math.Min(minClipDist, clipDist); maxClipDist = Math.Max(maxClipDist, clipDist); } float clipNear = Math.Max(0.01f, minClipDist); float clipFar = maxClipDist; // TODO(local): see if the default epsilon is enough? There's no easy way to check clip planes manually right now if (clipNear.ApproxEq(clipFar)) { clipFar = clipNear + 0.001f; } return(clipNear, clipFar); Vector3 V3Project(Vector3 a, Vector3 b) => b * (Vector3.Dot(a, b) / Vector3.Dot(b, b)); float SignedDistance(Vector3 point, Vector3 ray) { Vector3 projected = V3Project(point, ray); return(MathL.Sign(Vector3.Dot(ray, projected)) * projected.Length()); } }
public static bool FactorIsTrivial(this float factor) { return(factor.ApproxEq(Constants.TRIVIAL_FACTOR)); }