示例#1
0
        /// <summary>
        /// Gets a normal for a given segment. Note that there are an infinite number of possible normals, this method just
        /// produces a locally consistent normal.
        /// </summary>
        /// <returns>A normal at a given point.</returns>
        /// <param name="segment">The segment to calculate the tangent of.</param>
        /// <param name="segment">The precalculated coefficients of the curve.</param>
        /// <param name="t">
        /// The time along the curve, where t=0 is the start point, and t=1 is the end point. Note that t can be extrapolated
        /// beyond these bounds.
        /// </param>
        /// <param name="up">
        /// The direction the normal should try to match.
        /// </param>
        public static Vector3 GetNormal(this CubicBezierSegment segment, CubicBezierTangentCoefficients coeffs, double t, Vector3 up)
        {
            Vector3 baseNormal = segment.GetNormal(coeffs, t);
            Vector3 tangent    = coeffs.GetTangent(t);

            Vector3 upNormal = Vector3.Cross(Vector3.Cross(tangent, up), tangent).normalized;

            if (Vector3.Angle(tangent, up) == 0f)
            {
                return(baseNormal);
            }

            float angle = Mathf.Min(Mathf.Abs(Vector3.Angle(tangent, up)), Mathf.Abs(Vector3.Angle(tangent, -up)));
            float lerp  = Mathf.Clamp01((angle + 5) / 45f);

            return(AngleLerp(baseNormal, upNormal, lerp));
        }
示例#2
0
        /// <summary>
        /// Gets a normal for a given segment. Note that there are an infinite number of possible normals, this method just
        /// produces a locally consistent normal.
        /// </summary>
        /// <returns>A normal at a given point.</returns>
        /// <param name="segment">The segment to calculate the tangent of.</param>
        /// <param name="segment">The precalculated coefficients of the curve.</param>
        /// <param name="t">
        /// The time along the curve, where t=0 is the start point, and t=1 is the end point. Note that t can be extrapolated
        /// beyond these bounds.
        /// </param>
        public static Vector3 GetNormal(this CubicBezierSegment segment, CubicBezierTangentCoefficients coeffs, double t)
        {
            Vector3 currentTangent = coeffs.GetTangent(t);

            Vector3 normal;

            if (currentTangent == Vector3.zero)
            {
                Vector3 acceleration = coeffs.GetAcceleration(t);
                currentTangent = acceleration;
            }

            // The tangent doesn't change, (such in the case of a straight line).
            // We pick a vector which shouldn't be parallel to the tangent, and
            // use the cross product with the tangent to find a normal.
            normal   = currentTangent;
            normal.x = -currentTangent.y;
            normal.y = -currentTangent.z;
            normal.z = currentTangent.x;

            normal = Vector3.Cross(currentTangent, normal).normalized;
            return(normal);
        }