Example #1
0
        public void AddCurveData(CurveData data)
        {
            for (int i = 0; i < m_listCurveData.Count; ++i)
            {
                if (m_listCurveData[i].strAnimationName.Equals(data.strAnimationName))
                {
                    //已经存在了
                    return;
                }
            }

            m_listCurveData.Add(data);
        }
Example #2
0
        public Vector3 GetCurveMoveEndPosition(string clipName)
        {
            if (m_animationCurveData == null)
            {
                return(Vector3.zero);
            }

            //给定动画是否有位移曲线
            CurveData curveData = m_animationCurveData.GetCurveData(clipName);

            if (curveData == null)
            {
                LogMgr.UnityLog("can't find " + clipName + " curve data");
                return(Vector3.zero);
            }

            Vector3 endPos = Vector3.zero;

            //这里必须帧数一样
            for (int i = 0; i < curveData.curve.Length; ++i)
            {
                if (curveData.curve[i].propertyName.Contains("m_LocalPosition.x"))
                {
                    AnimationCurve tempX = curveData.curve[i].curve;

                    endPos.x = tempX.keys[tempX.length - 1].value;
                }
                else if (curveData.curve[i].propertyName.Contains("m_LocalPosition.y"))
                {
                    AnimationCurve tempY = curveData.curve[i].curve;
                    endPos.y = tempY.keys[tempY.length - 1].value;
                }
                else if (curveData.curve[i].propertyName.Contains("m_LocalPosition.z"))
                {
                    AnimationCurve tempZ = curveData.curve[i].curve;
                    endPos.z = tempZ.keys[tempZ.length - 1].value;
                }
            }

            endPos = m_transform.rotation * endPos + m_transform.position;

            return(endPos);
        }
Example #3
0
        //给定终点移动
        public bool MoveToPos(string strClipName, Vector3 dstPos, bool bHitBack = false)
        {
            m_needMove = true;

            //重新获取下radius,防止init函数获取失败
            m_curRadius = m_actor.GetColliderRadius();

            //给定动画是否有位移曲线
            if (null == m_animationCurveData)
            {
                return(false);
            }
            CurveData curveData = m_animationCurveData.GetCurveData(strClipName);

            if (curveData == null)
            {
                LogMgr.DebugLog("can't find {0} curve data", strClipName);
                return(false);
            }

            //是否是击退
            m_bHitBack = bHitBack;

            m_DeltaPos = Vector3.zero;
            m_LastPos  = m_transform.position;

            List <float> listValueX = new List <float>();
            List <float> listValueY = new List <float>();
            List <float> listValueZ = new List <float>();
            List <float> listTime   = new List <float>();

            List <Keyframe> listKeyFrameX = new List <Keyframe>();
            List <Keyframe> listKeyFrameY = new List <Keyframe>();
            List <Keyframe> listKeyFrameZ = new List <Keyframe>();

            //这里必须帧数一样
            for (int i = 0; i < curveData.curve.Length; ++i)
            {
                if (curveData.curve[i].propertyName.Contains("m_LocalPosition.x"))
                {
                    AnimationCurve tempX = curveData.curve[i].curve;

                    //LogMgr.UnityLog("curveX length=" + tempX.length);
                    for (int j = 0; j < tempX.length; ++j)
                    {
                        listTime.Add(tempX.keys[j].time);
                        listValueX.Add(tempX.keys[j].value);
                        listKeyFrameX.Add(tempX.keys[j]);
                    }
                }
                else if (curveData.curve[i].propertyName.Contains("m_LocalPosition.y"))
                {
                    AnimationCurve tempY = curveData.curve[i].curve;

                    //LogMgr.UnityLog("curveY length=" + tempY.length);
                    for (int j = 0; j < tempY.length; ++j)
                    {
                        listValueY.Add(tempY.keys[j].value);
                        listKeyFrameY.Add(tempY.keys[j]);
                    }
                }
                else if (curveData.curve[i].propertyName.Contains("m_LocalPosition.z"))
                {
                    AnimationCurve tempZ = curveData.curve[i].curve;

                    //LogMgr.UnityLog("curveZ length=" + tempZ.length);
                    for (int j = 0; j < tempZ.length; ++j)
                    {
                        listValueZ.Add(tempZ.keys[j].value);
                        listKeyFrameZ.Add(tempZ.keys[j]);
                    }
                }
            }

            //帧数不一致,不处理
            if (listValueX.Count != listValueY.Count || listValueX.Count != listValueZ.Count ||
                listValueY.Count != listValueZ.Count)
            {
                LogMgr.LogError(strClipName + " have diffent key count");
                return(false);
            }

            bool needCutKey = false;

            //缩放系数
            float distanceA = Vector3.Distance(dstPos, m_transform.position);

            Vector3 pos1 = new Vector3(listValueX[0], listValueY[0], listValueZ[0]);
            Vector3 pos2 = new Vector3(listValueX[listValueX.Count - 1],
                                       listValueY[listValueX.Count - 1], listValueZ[listValueX.Count - 1]);
            float distanceB = Vector3.Distance(pos1, pos2);

            //是否有卡帧系数
            if (curveData.keyIndex > 0 & curveData.keyIndex < listValueX.Count - 1)
            {
                //卡帧前面的位移
                Vector3 pos3      = new Vector3(listValueX[curveData.keyIndex], listValueY[0], listValueZ[curveData.keyIndex]);
                float   distanceC = Vector3.Distance(pos1, pos3);

                //LogMgr.UnityLog("distanceC=" + distanceC);

                if (distanceA > distanceC)
                {
                    distanceA -= distanceC;
                    distanceB -= distanceC;
                    needCutKey = true;
                }
            }

            float fscale = distanceA / distanceB;

            if (Mathf.Approximately(distanceB, 0) || fscale <= 0)
            {
                fscale = 1f;
            }

            //LogMgr.UnityLog("distanceA=" + distanceA + ", distanceB=" + distanceB + ", fscale=" + fscale);

            //使用点的旋转
            List <Vector3> listCurvePos = new List <Vector3>();

            for (int i = 0; i < listValueX.Count; ++i)
            {
                Vector3 pos = new Vector3(listValueX[i], listValueY[i], listValueZ[i]);

                //缩放
                if (needCutKey && curveData.keyIndex > 0 && curveData.keyIndex < listValueX.Count - 1)
                {
                    //后面的缩放
                    if (i > curveData.keyIndex)
                    {
                        pos.Scale(new Vector3(fscale, 1, fscale));
                        pos += new Vector3(listValueX[curveData.keyIndex], pos.y, listValueZ[curveData.keyIndex]);
                    }
                }
                else
                {
                    pos.Scale(new Vector3(fscale, 1f, fscale));
                }

                //自身旋转
                pos = m_transform.rotation * pos;

                listCurvePos.Add(pos);
            }

            //终点旋转
            Vector3 curveDstPos = listCurvePos[listCurvePos.Count - 1] + m_transform.position;

            Vector3 fromDir = curveDstPos - m_transform.position;
            Vector3 toDir   = dstPos - m_transform.position;

            Quaternion rot = Quaternion.FromToRotation(fromDir, toDir);

            //旋转到指定点方向
            for (int i = 0; i < listCurvePos.Count; ++i)
            {
                listCurvePos[i] = rot * listCurvePos[i];
            }

            //找到对应的值
            AnimationCurve curveX = new AnimationCurve();
            AnimationCurve curveY = new AnimationCurve();
            AnimationCurve curveZ = new AnimationCurve();

            //动画曲线
            for (int i = 0; i < listCurvePos.Count; ++i)
            {
                Vector3 pos = listCurvePos[i] + m_transform.position;

                //LogMgr.UnityLog("pos=" + pos.ToString("f4") + ", time=" + listTime[i]);

                Keyframe keyX = new Keyframe(listTime[i], pos.x, listKeyFrameX[i].inTangent, listKeyFrameX[i].outTangent);
                keyX.tangentMode = listKeyFrameX[i].tangentMode;

                Keyframe keyY = new Keyframe(listTime[i], pos.y, listKeyFrameY[i].inTangent, listKeyFrameY[i].outTangent);
                keyY.tangentMode = listKeyFrameY[i].tangentMode;

                Keyframe keyZ = new Keyframe(listTime[i], pos.z, listKeyFrameZ[i].inTangent, listKeyFrameZ[i].outTangent);
                keyZ.tangentMode = listKeyFrameZ[i].tangentMode;

                curveX.AddKey(keyX);
                curveY.AddKey(keyY);
                curveZ.AddKey(keyZ);
            }

            // 平滑处理
            if (curveX.length > 3)
            {
                for (int i = 1; i < curveX.length - 1; i++)
                {
                    // 小的颤动平滑掉
                    if (Mathf.Abs((curveX.keys[i].value - curveX.keys[i - 1].value) + (curveX.keys[i].value - curveX.keys[i + 1].value)) > 0.3f)
                    {
                        continue;
                    }
                    if (Mathf.Abs((curveY.keys[i].value - curveY.keys[i - 1].value) + (curveY.keys[i].value - curveY.keys[i + 1].value)) > 0.3f)
                    {
                        continue;
                    }
                    if (Mathf.Abs((curveZ.keys[i].value - curveZ.keys[i - 1].value) + (curveZ.keys[i].value - curveZ.keys[i + 1].value)) > 0.3f)
                    {
                        continue;
                    }

                    curveX.SmoothTangents(i, 0.5f);
                    curveY.SmoothTangents(i, 0.5f);
                    curveZ.SmoothTangents(i, 0.5f);
                }
            }

            m_curveX = curveX;
            m_curveY = curveY;
            m_curveZ = curveZ;

            m_beginTime = Time.time;

            return(true);
        }