您的位置:首页 > 移动开发 > Unity3D

Unity3d 曲线Curvy插件的学习使用

2016-12-21 19:39 281 查看
第一次写博客  咳咳 有点紧张,马上要下班了就言简意赅的描述下今天的工作内容吧。

管线流动的效果的实现,Curvy的使用:

各种水的管道,希望能有一种方法表明水流的方向,于是打算通过Curvy来来实现这一效果,但是Curvy这个东西又没有办法拉直线,我很苦恼,哪位大神知道Curvy能拉直线么?或者其他比较不错的插件呢?主要介绍一下Curvy重合的CP,其他的不介绍了T.T


这是demo工程



这是重合的节点,重合节点处,可以通过操作参数,来使曲线物体向不同的方向走,控制这个不同的方向,就是Tags这个参数,在外部通过修改Tags这个参数来更换方向,SplineWalkerCon.cs文件中     、

void Update()

    {

        if (UpdateIn == CurvyUpdateMethod.Update)

            doUpdate();

    }

    void doUpdate()

    {

        

        if (!Spline || !Spline.IsInitialized) return;

        // Runtime processing

        if (Application.isPlaying) {

            int dir = Dir;    

            // Move at a constant speed?

            if (MoveByWorldUnits) {

                // either used cached values(slightly faster) or interpolate position now (more exact)

                // Note that we pass mTF and mDir by reference. These values will be changed by the Move methods

                mTransform.position = (FastInterpolation) ?

                    Spline.MoveByConnectionFast(ref Spline, ref mTF, ref dir, Speed * Time.deltaTime, Clamping, MinTagMatches,buildTags()) : // linear interpolate cached values

                    Spline.MoveByConnection(ref Spline, ref mTF, ref dir, Speed * Time.deltaTime, Clamping, MinTagMatches, buildTags()); // interpolate now

            }

            else { // Move at constant F

                // either used cached values(slightly faster) or interpolate position now (more exact)

                // Note that we pass Spline, mTF and mDir by reference. These values will be changed by the MoveConnection methods

                mTransform.position = (FastInterpolation) ?

                    Spline.MoveConnectionFast(ref Spline, ref mTF, ref dir, Speed * Time.deltaTime, Clamping, MinTagMatches, buildTags()): // interpolate now

                    Spline.MoveConnection(ref Spline, ref mTF, ref dir, Speed * Time.deltaTime, Clamping, MinTagMatches, buildTags()); // interpolate now

                

            }

            // Rotate the transform to match the spline's orientation

            if (SetOrientation) {

                transform.rotation = Spline.GetOrientationFast(mTF);

            }

            

            Dir = dir;

        }

        else // Editor processing: continuously place the transform to reflect property changes in the editor

            InitPosAndRot();

    }

                    Spline.MoveByConnectionFast(ref Spline, ref mTF, ref dir, Speed * Time.deltaTime, Clamping, MinTagMatches,buildTags()) : // linear interpolate cached values

                    Spline.MoveByConnection(ref Spline, ref mTF, ref dir, Speed * Time.deltaTime, Clamping, MinTagMatches, buildTags()); // interpolate now

这两行代码是控制曲线上的物体移动的,调入到CurvySpline.cs之中

    public Vector3 MoveConnection(ref CurvySpline spline, ref float tf, ref int direction, float fDistance, CurvyClamping clamping, int minMatchesNeeded, params string[] tags)

    {

        List<CurvyConnection> cons = GetConnectionsWithin(tf, direction, fDistance, minMatchesNeeded,true, tags);

        if (cons.Count > 0) {

            CurvyConnection con;

            if (cons.Count == 1)

                con = cons[0];

            else

                con = CurvyConnection.GetBestMatchingConnection(cons, tags);

            CurvySplineSegment cp=con.GetFromSpline(this);

            float cptf = SegmentToTF(cp);

            fDistance-= cptf-tf;

            CurvySplineSegment counterp=con.GetCounterpart(cp);

            tf = counterp.LocalFToTF(0);

            spline = counterp.Spline;

            return spline.MoveConnection(ref spline, ref tf, ref direction, fDistance, clamping, minMatchesNeeded, tags);

        }

        else

            return spline.Move(ref tf, ref direction, fDistance, clamping);

     }

接着:

    public List<CurvyConnection> GetConnectionsWithin(float tf, int direction, float fDistance, int minMatchesNeeded, bool skipCurrent, params string[] tags)

    {

        List<CurvyConnection> res = new List<CurvyConnection>();

        // get Segments in the range TF => TF+f

        int fromIdx = 0;

        int toIdx = -1;

        float fdelta = fDistance * direction;

        float fromLocalF;

        float toLocalF;

        if (fdelta >= 0) {

            fromIdx = TFToSegment(tf, out fromLocalF).ControlPointIndex;

            toIdx = TFToSegment(tf + fdelta, out toLocalF).ControlPointIndex;

            if (fromLocalF > 0) // don't check a CP we already passed

                fromIdx++;

            if (toLocalF == 1)

                toIdx = Mathf.Min(ControlPointCount - 1, toIdx + 1);

            if (fromIdx == toIdx && (fromLocalF == 0 && skipCurrent)) // from on CP, skip it?

                    return res;

        }

        else {

            fromIdx = TFToSegment(tf + fdelta, out fromLocalF).ControlPointIndex;

            toIdx = TFToSegment(tf, out toLocalF).ControlPointIndex;

            if (fromIdx == toIdx) {

                if (fromLocalF > 0) // not reached cp yet

                    return res;

            }

            else {

                if (fromLocalF > 0)

                    fromIdx++;

                if (toLocalF == 0 && skipCurrent)

                    return res;

            }

        }

        for (int idx = fromIdx; idx <= toIdx; idx++) {

            res.AddRange(this.ControlPoints[idx].GetAllConnections(minMatchesNeeded,tags));

        }

        return res;

    }

获取下一步选择节点,根据tags在外部修改的参数进行选择

    public List<CurvyConnection> GetAllConnections(int minMatchesNeeded, params string[] tags)

    {

        List<CurvyConnection> res = new List<CurvyConnection>();

        CurvyConnection con = Connection;

   
9957
    if (con != null && con.MatchingTags(tags).Count >= minMatchesNeeded)

            res.Add(con);

        for (int i = ConnectedBy.Count - 1; i > -1; i--)

        {

            if (ConnectedBy[i].Connection != null)

            {

                if (ConnectedBy[i].Connection.Other != this)

                    ConnectedBy.RemoveAt(i);

                else

                    if (ConnectedBy[i].Connection.MatchingTags(tags).Count >= minMatchesNeeded)

                        res.Add(ConnectedBy[i].Connection);

            }

            else

                ConnectedBy.RemoveAt(i);

        }

        return res;

    }

这样就得到了根据参数的下一步曲线的方向。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: