/// <summary>
        /// initialize the path
        /// </summary>
        /// <returns></returns>
        private bool Init()
        {
            if (controlPoints == null)
            {
                return(false); // can't initialize
            }
            int points = controlPoints.Length;

            if (curves == null || curves.Length != points)
            {
                curves = new CurveSegment[points];
            }

            // insteantiate a curve segment for each valid sequence of points
            // since we only support curves that connect the second and third points
            // (b-spline and Catmull-Rom) we can advance our index by only one
            for (int i = 0; i < points; i++)
            {
                // notice how the modulo operator ensures 'i' is in the range [0, points-1]
                // and is used to wrap around the list of points to make a closed path
                Vector3 cp1 = controlPoints[i].position;
                Vector3 cp2 = controlPoints[(i + 1) % points].position;
                Vector3 cp3 = controlPoints[(i + 2) % points].position;
                Vector3 cp4 = controlPoints[(i + 3) % points].position;

                curves[i] = new CurveSegment(cp1, cp2, cp3, cp4, (CurveType)curveType);
            }
            // compute the cumulative arclength, which we use to sample the curve
            // with a parameter that is linear with relation to the arclength of the curve
            UpdateArclength();

            return(true);
        }
示例#2
0
 public static void DrawTangents(CurveSegment curve,
                                 Color color, int segments = 50, float scale = 0.1f)
 {
     // TODO exercise 2.2
     // evaluate the curve and tangent from start to end (range [0, 1])
     // and draw the tangent as a line from the current curve point
     // to the current point + the tangent vector
 }
示例#3
0
 public static void DrawCurveSegments(CurveSegment curve,
                                      Color color, int segments = 50)
 {
     // TODO exercise 2.2
     // evaluate the curve from start to end (range [0, 1])
     // and you draw a number of line segments between
     // consecutive points
 }
示例#4
0
 bool Init()
 {
     if (cp1 == null || cp2 == null || cp3 == null || cp4 == null)
     {
         return(false);
     }
     curve = new CurveSegment(cp1.position, cp2.position, cp3.position, cp4.position, curveType);
     return(true);
 }
示例#5
0
 bool Init()
 {
     // initialize curve if all control points are valid
     if (cp1 == null || cp2 == null || cp3 == null || cp4 == null)
     {
         return(false);
     }
     curve = new CurveSegment(cp1.position, cp2.position, cp3.position, cp4.position, curveType);
     return(true);
 }
        public static void DrawCurveCurvatures(CurveSegment curve,
                                               Color color, int segments = 50, float scale = 0.1f)
        {
            float interval = 1.0f / segments;

            for (int i = 0; i <= segments; i++)
            {
                float   u         = interval * (float)i;
                Vector3 pos       = curve.Evaluate(u);
                Vector3 curvature = curve.EvaluateDv2(u);

                UnityEngine.Debug.DrawLine(pos, pos + curvature * scale, color);
            }
        }
        public static void DrawCurveSegments(CurveSegment curve,
                                             Color color, int segments = 50)
        {
            float   interval = 1.0f / segments;
            Vector3 lastPos  = curve.Evaluate(0);

            for (int i = 1; i <= segments; i++)
            {
                float   u   = interval * (float)i;
                Vector3 pos = curve.Evaluate(u);

                UnityEngine.Debug.DrawLine(lastPos, pos, color);
                lastPos = pos;
            }
        }
示例#8
0
        /// <summary>
        /// initialize the path
        /// </summary>
        /// <returns></returns>
        private bool Init()
        {
            if (controlPoints == null)
            {
                return(false); // can't initialize
            }
            int points = numOfPoints;

            if (curves == null || curves.Length != points)
            {
                curves = new CurveSegment[points - 1];
            }

            // insteantiate a curve segment for each valid sequence of points
            // since we only support curves that connect the second and third points
            // (b-spline and Catmull-Rom) we can advance our index by only one
            for (int i = 0; i < points; i++)
            {
                // if 'i' is 0, missing start tangent?, rearrange so start is i, an start tangent is i-1
                // and so the first and last segments is included as well, with custom tangents
                // TODO
                // or this solution, creating the first segment and adding the rest to curves[i+1] instead of curves[i]
                if (i == 0)
                {
                    Vector3 cp1x = controlPoints[i].position - ((controlPoints[i + 1].position - controlPoints[i].position) * 0.5f);
                    Vector3 cp2x = controlPoints[i].position;
                    Vector3 cp3x = controlPoints[(i + 1) % points].position;
                    Vector3 cp4x = controlPoints[(i + 2) % points].position;

                    curves[i] = new CurveSegment(cp1x, cp2x, cp3x, cp4x, (CurveType)curveType, normalizeTangents, curveTightness, alignTangents);
                }

                // if 'i' = controlPoints.Length-2, missing end point + end tangent
                // if 'i' = controlPoints.Length-1, reached goal
                if (i == points - 2 || i == points - 1)
                {
                    continue;
                }

                // if 'i' = controlPoints.Length-3, missing end tangent?, smooth tangent
                if (i == points - 3)
                {
                    Vector3 cp11 = controlPoints[i].position;
                    Vector3 cp22 = controlPoints[(i + 1) % points].position;
                    Vector3 cp33 = controlPoints[(i + 2) % points].position;
                    Vector3 cp44 = controlPoints[(i + 2) % points].position - ((controlPoints[(i + 1) % points].position - controlPoints[(i + 2) % points].position) * 0.5f);

                    curves[i + 1] = new CurveSegment(cp11, cp22, cp33, cp44, (CurveType)curveType, normalizeTangents, curveTightness, alignTangents);

                    continue;
                }

                // notice how the modulo operator ensures 'i' is in the range [0, points-1]
                // and is used to wrap around the list of points to make a closed path
                Vector3 cp1 = controlPoints[i].position;
                Vector3 cp2 = controlPoints[(i + 1) % points].position;
                Vector3 cp3 = controlPoints[(i + 2) % points].position;
                Vector3 cp4 = controlPoints[(i + 3) % points].position;

                curves[i + 1] = new CurveSegment(cp1, cp2, cp3, cp4, (CurveType)curveType, normalizeTangents, curveTightness, alignTangents);

                Debug.Log("cp2: " + cp2 + " cp3" + cp3 + " cp4" + cp4);
            }
            // compute the cumulative arclength, which we use to sample the curve
            // with a parameter that is linear with relation to the arclength of the curve
            UpdateArclength();

            return(true);
        }
示例#9
0
        /// <summary>
        /// initialize the path
        /// </summary>
        /// <returns></returns>
        private bool Init()
        {
            List <Vector3> path = this.pathFinder.GetPath();

            controlPoints = new Vector3[path.Count];

            for (int i = 0; i < path.Count; i++)
            {
                controlPoints[i] = path[i];
            }

            if (controlPoints == null)
            {
                return(false); // can't initialize
            }
            int points = controlPoints.Length;

            if (curveType == CurveTypeSimple.CATMULLROM || curveType == CurveTypeSimple.BSPLINE)
            {
                curves = new CurveSegment[points - 1];

                // insteantiate a curve segment for each valid sequence of points
                // since we only support curves that connect the second and third points
                // (b-spline and Catmull-Rom) we can advance our index by only one
                for (int i = 0; i + 1 < points; i++)
                {
                    // notice how the modulo operator ensures 'i' is in the range [0, points-1]
                    // and is used to wrap around the list of points to make a closed path
                    Vector3 cp1 = controlPoints[i];
                    Vector3 cp2 = controlPoints[i + 1];

                    Vector3 cp0 = i <= 0 ? cp1 : controlPoints[i - 1];
                    Vector3 cp3 = i + 2 >= points ? cp2 : controlPoints[i + 2];

                    curves[i] = new CurveSegment(cp0, cp1, cp2, cp3, (CurveType)curveType);
                }
                // compute the cumulative arclength, which we use to sample the curve
                // with a parameter that is linear with relation to the arclength of the curve
                UpdateArclength();

                return(true);
            }
            else if (curveType == CurveTypeSimple.HERMITE)
            {
                curves = new CurveSegment[points - 1];
                for (int i = 0; i + 1 < points; i++)
                {
                    Vector3 cp1 = controlPoints[i];
                    Vector3 cp2 = controlPoints[i + 1];

                    Vector3 cp0 = i <= 0 ? cp1 : controlPoints[i - 1];
                    Vector3 cp3 = i + 2 >= points ? cp2 : controlPoints[i + 2];
                    Vector3 t1  = ((cp2 - cp1) + (cp1 - cp0)) / 2;
                    Vector3 t2  = ((cp3 - cp2) + (cp2 - cp1)) / 2;

                    curves[i] = new CurveSegment(cp1, t1, t2, cp2, (CurveType)curveType);
                }
                UpdateArclength();

                return(true);
            }
            else if (curveType == CurveTypeSimple.BEZIER)
            {
                return(false);
            }
            else
            {
                return(false);
            }
        }