Exemplo n.º 1
0
        public override void DoUpdate()
        {
            base.DoUpdate();

            //_LogTrData("at start");
            //for (int i = 0; i < m_initInfos.Count; ++i) //comment out to fix the IK-jitter
            //    m_initInfos[i].UpdateInitInfo();
            //for (int i = 0; i < m_initInfos.Count; ++i)
            //    m_initInfos[i].RevertToInitInfo();

            CCDSolver solver = GetSolver();

            if (m_target && m_influence != 0)
            {
                //Vector3 initPos = m_tr.position;
                Vector3 targetPos = m_target.position;

                //_LogTrData("before execute");
                solver.Target = targetPos;
                solver.Execute(m_influence);
                //_LogTrData("After execute");

                if (m_useTargetRotation)
                {
                    m_tr.rotation = m_target.rotation;
                }
            }

            //for (int i = 0; i < m_initInfos.Count; ++i)
            //    m_initInfos[i].RecordLastLocInfo();

            //~TODO?: find ConstraintStack on parent bones, and call RecordLastLocInfo on each one
        }
        void OnSceneGUI()
        {
            CCDSolverMB cp     = (CCDSolverMB)target;
            CCDSolver   solver = cp.GetSolver();

            if (solver == null || solver.Count < 1)
            {
                return;
            }

            if (cp.ShowGizmos)
            {
                var joints = solver.GetJoints();

                Camera    sceneCam = EUtil.GetSceneViewCamera();
                Transform camTr    = sceneCam.transform;

                EUtil.PushHandleColor(Pref.IKBoneLinkColor);
                //1. draw bone line
                for (int i = 0; i < joints.Length - 1; ++i)
                {
                    var p0 = joints[i].position;
                    var p1 = joints[i + 1].position;
                    Handles.DrawAAPolyLine(3f, p0, p1);
                    Handles.DotCap(0, p0, camTr.rotation, m_markerSize.val);
                }
                if (joints.Length > 0)
                {
                    Handles.DotCap(0, joints.Last().position, camTr.rotation, m_markerSize.val);
                }
                EUtil.PopHandleColor();

                //1.5 draw line from end-joint to target pos
                {
                    var p0 = joints.Last().position;
                    var p1 = solver.Target;
                    Handles.DrawDottedLine(p0, p1, 5f);
                }

                //2. call each constraint's OnSceneGUI
                for (int i = 0; i < joints.Length - 1; ++i)
                {
                    var cons = solver.GetConstraint(i);

                    foreach (var con in cons)
                    {
                        if (!con || !con.enabled)
                        {
                            continue;
                        }

                        Editor      e    = EUtil.GetEditor(con);
                        IOnSceneGUI igui = e as IOnSceneGUI;
                        if (igui != null)
                        {
                            igui.OnSceneGUI();
                        }
                    }
                }
            }

            //3. debug draw
            if (!cp.Target)
            {
                if (m_panel != EPanel.Normal)
                {
                    Tools.current = Tool.None;

                    // move handle
                    if (m_panel == EPanel.Continuous)
                    {
                        EditorGUI.BeginChangeCheck();
                    }

                    solver.Target = Handles.PositionHandle(solver.Target, Quaternion.identity);

                    if (m_panel == EPanel.Continuous && EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObjects(solver.GetJoints(), "IK execute");
                        solver.Execute();
                    }

                    // hotkeys
                    Event e = Event.current;
                    if (e.type == EventType.KeyUp && Tools.viewTool != ViewTool.FPS)
                    {
                        if (e.keyCode == KeyCode.E)
                        {
                            Tools.current = Tool.Rotate; m_panel = EPanel.Normal;
                        }
                        else if (e.keyCode == KeyCode.R)
                        {
                            Tools.current = Tool.Scale; m_panel = EPanel.Normal;
                        }
                        else if (e.keyCode == KeyCode.Q)
                        {
                            Tools.current = Tool.View; m_panel = EPanel.Normal;
                        }
                    }
                }
            }
        }