Example #1
0
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            EditorGUILayout.Space();

            Inspector.AddContent(fixTransforms);

            // Editing References
            if (BipedReferencesInspector.AddModifiedInspector(references))
            {
                if (!Application.isPlaying)
                {
                    Warning.logged = false;
                    BipedReferences.CheckSetup(script.references);
                    script.InitiateBipedIK();
                }
            }

            // Editing Solvers
            BipedIKSolversInspector.AddInspector(solvers, solversProps, solverContents);

            EditorGUILayout.Space();

            serializedObject.ApplyModifiedProperties();
        }
Example #2
0
        public void OnEnable()
        {
            // Store the MonoScript for changing script execution order
            if (!Application.isPlaying)
            {
                MonoScript monoScript = MonoScript.FromMonoBehaviour(script);

                // Changing the script execution order to make sure BipedIK always executes after any other script except FullBodyBipedIK
                int executionOrder = MonoImporter.GetExecutionOrder(monoScript);
                if (executionOrder != 9998)
                {
                    MonoImporter.SetExecutionOrder(monoScript, 9998);
                }
            }

            references   = serializedObject.FindProperty("references");
            solvers      = serializedObject.FindProperty("solvers");
            solversProps = BipedIKSolversInspector.FindProperties(solvers);

            // Caching Solver Contents
            leftFootContent  = IKSolverLimbInspector.FindContent(solversProps[0]);
            rightFootContent = IKSolverLimbInspector.FindContent(solversProps[1]);
            leftHandContent  = IKSolverLimbInspector.FindContent(solversProps[2]);
            rightHandContent = IKSolverLimbInspector.FindContent(solversProps[3]);
            spineContent     = IKSolverHeuristicInspector.FindContent(solversProps[4]);
            aimContent       = IKSolverAimInspector.FindContent(solversProps[5]);
            lookAtContent    = IKSolverLookAtInspector.FindContent(solversProps[6]);
            pelvisContent    = ConstraintsInspector.FindContent(solversProps[7]);

            solverContents = new SerializedContent[8][] {
                leftFootContent, rightFootContent, leftHandContent, rightHandContent, spineContent, aimContent, lookAtContent, pelvisContent
            };

            // Automatically detecting references
            if (!Application.isPlaying)
            {
                if (script.references.isEmpty)
                {
                    BipedReferences.AutoDetectReferences(ref script.references, script.transform, new BipedReferences.AutoDetectParams(false, true));

                    references.isExpanded = true;
                    solvers.isExpanded    = false;
                    for (int i = 0; i < solversProps.Length; i++)
                    {
                        solversProps[i].isExpanded = false;
                    }

                    // Setting default values and initiating
                    script.InitiateBipedIK();
                    script.SetToDefaults();
                    EditorUtility.SetDirty(script);
                }
                else
                {
                    script.InitiateBipedIK();
                }

                Warning.logged = false;
                BipedReferences.CheckSetup(script.references);
            }
        }
        private bool BipedIsValid(BipedReferences references, IKSolverFullBodyBiped solver, Transform context, bool log)
        {
            BipedReferences.CheckSetup(script.references);

            if (!references.isValid)
            {
                //if (log) Warning.Log("BipedReferences contains one or more missing Transforms.", context, true);
                return(false);
            }
            if (references.spine.Length == 0)
            {
                //if (log) Warning.Log("Biped has no spine bones.", context, true);
                return(false);
            }

            if (solver.rootNode == null)
            {
                //if (log) Warning.Log("Root Node bone is null.", context, true);
                return(false);
            }

            Vector3 toRightShoulder    = references.rightUpperArm.position - references.leftUpperArm.position;
            Vector3 shoulderToRootNode = solver.rootNode.position - references.leftUpperArm.position;
            float   dot = Vector3.Dot(toRightShoulder.normalized, shoulderToRootNode.normalized);

            if (dot > 0.95f)
            {
                if (log)
                {
                    Warning.Log("The root node, the left upper arm and the right upper arm bones should ideally form a triangle that is as close to equilateral as possible. " +
                                "Currently the root node bone seems to be very close to the line between the left upper arm and the right upper arm bones. This might cause unwanted behaviour like the spine turning upside down when pulled by a hand effector." +
                                "Please set the root node bone to be one of the lower bones in the spine.", context, true);
                }
            }

            Vector3 toRightThigh    = references.rightThigh.position - references.leftThigh.position;
            Vector3 thighToRootNode = solver.rootNode.position - references.leftThigh.position;

            dot = Vector3.Dot(toRightThigh.normalized, thighToRootNode.normalized);

            if (dot > 0.95f && log)
            {
                Warning.Log("The root node, the left thigh and the right thigh bones should ideally form a triangle that is as close to equilateral as possible. " +
                            "Currently the root node bone seems to be very close to the line between the left thigh and the right thigh bones. This might cause unwanted behaviour like the hip turning upside down when pulled by an effector." +
                            "Please set the root node bone to be one of the higher bones in the spine.", context, true);
            }

            /*
             * Vector3 shoulderCross = Vector3.Cross(toRightShoulder, shoulderToRootNode);
             * Vector3 charCross = Vector3.Cross(references.rightHand.position - references.leftHand.position, references.rightFoot.position - references.leftHand.position);
             *
             * float shoulderInvertDot = Vector3.Dot(shoulderCross.normalized, charCross.normalized);
             *
             * if (shoulderInvertDot < 0 && log) {
             *      Warning.Log("The triangle formed by the root node, left upper arm and right upper arm bones seems to be flipped. The root node bone should be below the upper arm bones.", context, true);
             * }
             *
             * Vector3 thighCross = Vector3.Cross(toRightThigh, thighToRootNode);
             *
             * float thighInvertDot = Vector3.Dot(thighCross.normalized, charCross.normalized);
             *
             * if (thighInvertDot > 0 && log) {
             *      Warning.Log("The triangle formed by the root node, left thigh and right thigh bones seems to be flipped. The root node bone should be above the thigh bones.", context, true);
             * }
             */

            return(true);
        }