Esempio n. 1
0
        public void _AutoSetParameters()
        {
            ConeConstraintMB mb      = this;
            Transform        j       = mb.transform;
            Transform        jparent = j.parent;
            Transform        jchild  = mb.nextJoint;

            // decide the refAxis
            {
                Vector3 axis = Vector3.zero;
                if (jparent != null)
                {
                    axis = (j.position - jparent.position).normalized;
                }
                else
                {
                    axis = (jchild.position - j.position).normalized;
                }

                mb.refAxis = Misc.InverseTransformDirection(jparent, axis); //convert to parent space
            }

            //decide the angle limit
            mb.angleLimit = DEF_ANGLELIMIT;

            // decide the min/max limit
            mb.minTwistLimit = DEF_MinTwist;
            mb.maxTwistLimit = DEF_MaxTwist;

            mb.CalcInitData(); //calc startlocalRot
        }
        public void OnSceneGUI()
        {
            ConeConstraintMB mb = (ConeConstraintMB)target;

            if (mb == null)
            {
                return;
            }

            Transform j       = mb.transform;
            Transform jparent = j.parent;
            Transform jchild  = mb.nextJoint;

            if (jchild == null)
            {
                return;
            }

            var saveColor = Handles.color;

            float szMul = EUtil.GetHandleSize(j.position, 3f) * ms_markerSize;

            Vector3 worldRefAxis = Misc.TransformDirection(jparent, mb.refAxis).normalized;

            //1. draw the refAxis
            Handles.color = Color.red;
            Handles.DrawAAPolyLine(4f, j.position, j.position + worldRefAxis * szMul * 1.5f); //ref axis

            //2. draw the cone
            float angleLimit = mb.angleLimit;

            Handles.color = ms_markerColor;

            float   zdist        = Mathf.Cos(Mathf.Deg2Rad * angleLimit) * szMul;
            float   radius       = Mathf.Sin(Mathf.Deg2Rad * angleLimit) * szMul;
            Vector3 circleCenter = j.position + worldRefAxis * zdist;

            Handles.DrawWireDisc(circleCenter, worldRefAxis, radius);

            Vector3 pseudoXAxis = Vector3.Cross(Vector3.up, worldRefAxis).normalized;

            if (pseudoXAxis == Vector3.zero)
            {
                pseudoXAxis = Vector3.right;
            }
            Vector3 pseudoYAxis = Vector3.Cross(worldRefAxis, pseudoXAxis).normalized;

            Handles.DrawAAPolyLine(3f, j.position, circleCenter + radius * pseudoXAxis);
            Handles.DrawAAPolyLine(3f, j.position, circleCenter - radius * pseudoXAxis);
            Handles.DrawAAPolyLine(3f, j.position, circleCenter + radius * pseudoYAxis);
            Handles.DrawAAPolyLine(3f, j.position, circleCenter - radius * pseudoYAxis);

            Handles.color = saveColor;
        }
Esempio n. 3
0
        private void _OnNextJointChanged()
        {
            ConeConstraintMB mb = (ConeConstraintMB)this;
            var nextJoint       = mb.m_nextJoint;

            if (nextJoint != null)
            {
                if (nextJoint.parent == mb.transform)
                {
                    _AutoSetParameters();
                }
                else
                {
                    Dbg.CLogWarn(mb, nextJoint.name + " is not children of " + mb.name);
                    mb.m_nextJoint = null;
                }
            }
        }
        public override void OnInspectorGUI()
        {
            ConeConstraintMB mb = (ConeConstraintMB)target;

            mb.enabled = EditorGUILayout.Toggle("Enabled", mb.enabled);

            EditorGUI.BeginChangeCheck();
            mb.nextJoint = EditorGUILayout.ObjectField("nextJoint", mb.nextJoint, typeof(Transform), true) as Transform;
            if (EditorGUI.EndChangeCheck())
            {
                EUtil.RepaintSceneView();
            }

            if (mb.nextJoint == null)
            {
                EditorGUILayout.LabelField("Set the nextJoint first...");
                return;
            }
            else
            {
                EUtil.PushGUIEnable(mb.enabled);

                EditorGUILayout.BeginHorizontal();
                {
                    GUILayout.Space(30f);
                    if (EUtil.Button("ReInit", "decide all the parameters with default method", Color.green))
                    {
                        Undo.RecordObject(mb, "AutoInit");
                        mb._AutoSetParameters();
                        EUtil.RepaintSceneView();
                    }
                    GUILayout.Space(30f);
                }
                EditorGUILayout.EndHorizontal();

                // angle limit
                EditorGUI.BeginChangeCheck();
                float newAngleLimit = EditorGUILayout.Slider(CONT_AngleLimit, mb.angleLimit, 0, 180f);
                if (EditorGUI.EndChangeCheck())
                {
                    Undo.RecordObject(mb, "Modify Angle Limit");
                    mb.angleLimit = newAngleLimit;
                    EUtil.RepaintSceneView();
                }

                // ref-Axis
                EditorGUI.BeginChangeCheck();
                Vector3 newRefAxis = EUtil.DrawV3P(CONT_RefAxis, mb.refAxis);
                if (EditorGUI.EndChangeCheck())
                {
                    Undo.RecordObject(mb, "Modify ref axis");
                    mb.refAxis = newRefAxis;
                    mb.CalcInitData(); //!! recalc the startTwistRot
                    EUtil.RepaintSceneView();
                }

                mb.limitTwist = EditorGUILayout.Toggle(CONT_LimitTwist, mb.limitTwist);
                if (mb.limitTwist)
                {
                    float min = mb.minTwistLimit;
                    float max = mb.maxTwistLimit;
                    EditorGUI.BeginChangeCheck();
                    EUtil.DrawMinMaxSlider("Twist Limits", ref min, ref max, -180f, 180f);
                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(mb, "Modify twist limits");
                        mb.minTwistLimit = min;
                        mb.maxTwistLimit = max;
                        EUtil.RepaintSceneView();
                    }

                    EditorGUILayout.LabelField("Current twist: " + mb.CalcTwist());
                }

                ms_showDisplaySetting = EditorGUILayout.Foldout(ms_showDisplaySetting, "Display Settings:");
                if (ms_showDisplaySetting)
                {
                    EditorGUI.BeginChangeCheck();
                    ms_markerSize  = EditorGUILayout.FloatField("Marker size", ms_markerSize);
                    ms_markerColor = EditorGUILayout.ColorField("Marker color", ms_markerColor);
                    if (EditorGUI.EndChangeCheck())
                    {
                        EUtil.RepaintSceneView();
                    }
                }

                EUtil.PopGUIEnable();
            }
        }