Example #1
0
        private static float GetT(this SplineBase spline, float tStart, float tEnd, Vector3 testPoint, ref int iter, float eps = 0.01f)
        {
            iter++;
            float   sqrEps = eps * eps;
            Vector3 start  = spline.GetPoint(tStart);
            Vector3 end    = spline.GetPoint(tEnd);

            Vector3 toStart = start - testPoint;
            Vector3 toEnd   = end - testPoint;

            if (toStart.sqrMagnitude < toEnd.sqrMagnitude)
            {
                if ((end - start).sqrMagnitude <= sqrEps)
                {
                    return(tStart);
                }
                return(spline.GetT(tStart, (tStart + tEnd) / 2.0f, testPoint, ref iter, eps));
            }

            if ((end - start).sqrMagnitude <= sqrEps)
            {
                return(tEnd);
            }
            return(spline.GetT((tStart + tEnd) / 2.0f, tEnd, testPoint, ref iter, eps));
        }
        private void Start()
        {
            SplineFollow[] splineFollow = SplineFollow.Where(sf => sf != null).ToArray();
            if (splineFollow.Length == 0)
            {
                return;
            }
            if (Distances.Length == 0)
            {
                Debug.LogError("At least one distance required");
                return;
            }
            int initialDistancesCount = Distances.Length;

            System.Array.Resize(ref Distances, SplineFollow.Length - 1);
            for (int i = initialDistancesCount; i < Distances.Length; ++i)
            {
                Distances[i] = Distances[i % initialDistancesCount];
            }

            float      offset = InitialOffset;
            SplineBase spline = splineFollow[0].Spline;

            for (int i = 0;; ++i)
            {
                SplineFollow sf = splineFollow[i];
                if (sf.Spline != spline)
                {
                    Debug.LogError("SplineFollow.Spline != " + spline);
                    return;
                }
                sf.Offset = offset;
                if (i == splineFollow.Length - 1)
                {
                    break;
                }
                float   distance = Distances[i];
                Vector3 pt0      = spline.GetPoint(offset);
                for (int j = 1; j <= Precision; ++j)
                {
                    float t = offset - ((float)j) / Precision;
                    if (t < 0)
                    {
                        t = (1.0f + t % 1.0f);
                    }
                    Vector3 pt = spline.GetPoint(t);
                    if ((pt - pt0).magnitude >= distance)
                    {
                        offset = t;
                        break;
                    }
                }
            }
        }
Example #3
0
        private void ShowSplineLength(SplineBase spline, GUIStyle style)
        {
            float distance     = spline.EvalDistance();
            float splineLength = spline.EvalSplineLength(GetStepsPerCurve());

            Handles.Label(spline.GetPoint(0.5f), string.Format("D: {0:0.00} m, S: {1:0.00} m", distance, splineLength), style);
        }
Example #4
0
        private void ShowLength(SplineBase spline, int curveIndex, GUIStyle style)
        {
            float distance     = spline.EvalDistance(curveIndex);
            float curveLength  = spline.EvalCurveLength(curveIndex, GetStepsPerCurve());
            float splineLength = spline.EvalSplineLength(GetStepsPerCurve());

            Handles.Label(spline.GetPoint(0.5f, curveIndex), string.Format("D: {0:0.00} m, C: {1:0.00} m, S: {2:0.00}", distance, curveLength, splineLength), style);
        }
        private void UpdatePosition(float t)
        {
            Vector3 position = m_spline.GetPoint(t);
            Vector3 dir      = m_spline.GetDirection(t);
            float   twist    = m_spline.GetTwist(t);

            transform.position = position;
            transform.LookAt(position + dir);
            transform.RotateAround(position, dir, twist);
        }
        public static float GetLengthBF(this SplineBase spline, int curve, float deltaT = 0.01f, float from = 0.0f, float to = 1.0f)
        {
            from = Mathf.Min(Mathf.Max(0, from), 1.0f);
            to   = Mathf.Max(Mathf.Min(1.0f, to), from);

            int     steps  = Mathf.CeilToInt(Mathf.Max(Mathf.Min((to - from) / deltaT, 1000000), 1));
            float   length = 0;
            Vector3 prev   = spline.GetPoint(from, curve);

            for (int i = 0; i < steps; ++i)
            {
                float   t    = from + (((float)(i + 1)) / steps) * (to - from);
                Vector3 next = spline.GetPoint(t, curve);
                length += (next - prev).magnitude;
                prev    = next;
            }

            return(length);
        }
        protected virtual void ShowTwistAngles()
        {
            Handles.color = Color.green;

            int steps = GetStepsPerCurve() * m_splineBase.CurveCount;

            for (int i = 0; i <= steps; i++)
            {
                Vector3 dir   = m_splineBase.GetDirection(i / (float)steps);
                Vector3 point = m_splineBase.GetPoint(i / (float)steps);

                float   t          = i / (float)steps;
                float   twistAngle = m_splineBase.GetTwist(t);
                Vector3 v3;
                Vector3 up = GetUpVector();
                if (Math.Abs(Vector3.Dot(dir, up)) < 1.0f)
                {
                    v3 = Vector3.Cross(dir, up).normalized;
                }
                else
                {
                    v3 = Vector3.Cross(dir, GetSideVector()).normalized;
                }
                if (dir == Vector3.zero)
                {
                    continue;
                }
                Handles.DrawLine(point, point + Quaternion.AngleAxis(twistAngle, dir) * Quaternion.LookRotation(v3, up) * Vector3.forward * TwistAngleScale);
            }
        }
Example #8
0
        private void UpdatePosition(float t)
        {
            Vector3 position = m_spline.GetPoint(t);
            Vector3 dir      = m_spline.GetDirection(t);
            float   twist    = m_spline.GetTwist(t);

            transform.position = position;
            Vector3    direction    = ((position + dir) - transform.position).normalized;
            Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, direction.y, direction.z));

            transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);
            //transform.LookAt(position + dir);
            transform.RotateAround(position, dir, twist);
        }
        private void Spawn()
        {
            int          index        = 0;
            int          nextIndex    = index + 1;
            Twist        twist        = m_spline.GetTwist(index);
            Vector3      ptPrev       = m_spline.GetControlPoint(index);
            Vector3      pt           = m_spline.GetControlPoint(nextIndex);
            GameObject   paperplaneGo = (GameObject)Instantiate(PaperplanePrefab, m_spline.GetPoint(0.0f), Quaternion.AngleAxis(twist.Data, pt - ptPrev) * Quaternion.LookRotation(pt - ptPrev));
            SplineFollow splineFollow = paperplaneGo.GetComponent <SplineFollow>();

            splineFollow.Spline = m_spline;

            if (!SmoothFollow.enabled)
            {
                SmoothFollow.SetTarget(paperplaneGo.transform);
                SmoothFollow.enabled = true;
            }
        }