/// <summary> /// use prev data to calculate current up dir /// </summary> private Vector3 _CalcUp(float t, Vector3 prevTan, Vector3 currTan, ref Vector3 prevBaseUp) { Vector3 currUp = Vector3.zero; switch (m_twistMtd) { case ETwistMethod.YUp: { Vector3 bino = Vector3.Cross(Vector3.up, currTan); currUp = Vector3.Cross(currTan, bino).normalized; } break; case ETwistMethod.Minimum: { Vector3 prevBino = Vector3.Cross(prevBaseUp, prevTan); currUp = Vector3.Cross(currTan, prevBino).normalized; } break; default: Dbg.LogErr("CatmullRomUniform._CalcUp: unexpected twistMtd: {0}", m_twistMtd); break; } if (currUp == Vector3.zero) { currUp = prevBaseUp; } prevBaseUp = currUp; currUp = ConUtil.ApplyTiltToUp(m_points, t, ref currTan, ref currUp); return(currUp); }
/// <summary> /// * calculate BigT for mapping t to segment /// * calculate the length of curve /// * extra: calculate smaller segments for editor to draw /// </summary> private void _Recalc() { m_dirty = false; m_length = 0; m_BigT = 0; m_pointTpos.Clear(); // calculate bigT & pointTpos list m_pointTpos.Add(0); for (int i = 1; i < m_points.Count; ++i) { Vector3 p0 = m_points[i - 1].pos; Vector3 p1 = m_points[i].pos; m_BigT += Mathf.Sqrt((p1 - p0).magnitude); m_pointTpos.Add(m_BigT); } // calculate length of curve Vector3 prevPt = m_points[0].pos; Vector3 prevTan = Tangent(0); Vector3 prevBaseUp = (prevTan == Vector3.up) ? Vector3.back : Vector3.Cross(prevTan, Vector3.Cross(Vector3.up, prevTan)).normalized; #if UNITY_EDITOR m_interPts.Clear(); m_interPts.Add(prevPt); m_interTangents.Clear(); m_interTangents.Add(prevTan); #endif m_interUps.Clear(); m_interUps.Add(ConUtil.ApplyTiltToUp(m_points, 0, ref prevTan, ref prevBaseUp)); int SmoothAmount = (PointCount - 1) * m_ptsPerSeg; for (int i = 1; i <= SmoothAmount; i++) { float pm = (float)i / SmoothAmount; Vector3 currPt = Interp(pm); m_length += Vector3.Distance(prevPt, currPt); Vector3 currTan = Tangent(pm); Vector3 up = _CalcUp(pm, prevTan, currTan, ref prevBaseUp); m_interUps.Add(up); #if UNITY_EDITOR m_interPts.Add(currPt); m_interTangents.Add(currTan); #endif prevPt = currPt; prevTan = currTan; } }
/// <summary> /// calculate the length of curve /// </summary> private void _RecalcLen() { m_length = 0; Vector3 prevPt = m_points[0].pos; Vector3 prevTan = Tangent(0); Vector3 prevBaseUp = (prevTan == Vector3.up) ? Vector3.back : Vector3.Cross(prevTan, Vector3.Cross(Vector3.up, prevTan)).normalized; #if UNITY_EDITOR m_interPts.Clear(); m_interPts.Add(prevPt); m_interTangents.Clear(); m_interTangents.Add(prevTan); #endif m_interUps.Clear(); m_interUps.Add(ConUtil.ApplyTiltToUp(m_points, 0, ref prevTan, ref prevBaseUp)); int SmoothAmount = (PointCount - 1) * m_ptsPerSeg; for (int i = 1; i <= SmoothAmount; i++) { float pm = (float)i / SmoothAmount; Vector3 currPt = Interp(pm); m_length += Vector3.Distance(prevPt, currPt); Vector3 currTan = Tangent(pm); Vector3 up = _CalcUp(pm, prevTan, currTan, ref prevBaseUp); m_interUps.Add(up); #if UNITY_EDITOR m_interPts.Add(currPt); m_interTangents.Add(currTan); #endif prevPt = currPt; //prevBaseUp = up; //prevBaseUp is updated in _CalcUp prevTan = currTan; } m_dirty = false; }