Example #1
0
    /*
     * Draw custom viewport render
     * */
    void OnSceneGUI()
    {
        // only draw handles if array parts are not null, otherwise console spams crap when component is added while object is selected
        if (biped.spine != null && biped.spine.Length > 0)
        {
            // get the current scaleFactor
            Matrix4x4 matrix      = biped.transform.localToWorldMatrix;
            float     scaleFactor = Mathf.Sqrt(VectorHelpers.MaxValue(new Vector3(
                                                                          new Vector3(matrix.m00, matrix.m01, matrix.m02).sqrMagnitude,
                                                                          new Vector3(matrix.m10, matrix.m11, matrix.m12).sqrMagnitude,
                                                                          new Vector3(matrix.m20, matrix.m21, matrix.m22).sqrMagnitude)));

            // draw balls and lines for all defined parts
            Handles.color = ballsColor;
            foreach (BodyPart part in biped.allParts)
            {
                DrawBall(part, scaleFactor);
            }

            // draw a shape gizmo for each body part
            Handles.color = shapeColor;
            foreach (BodyPart part in biped.allParts)
            {
                BodyPartEditor.DrawShapeHandle(part, false);
            }

            // draw joint gizmos for all the body parts
            foreach (BodyPart part in biped.allParts)
            {
                BodyPartEditor.DrawJointHandle(part, false);
            }
        }

        // begin GUI or transform handles will be disabled
        Handles.BeginGUI();

        // create the viewport controls
        ViewportControls.BeginArea(BodyPartEditor.viewportControlsWidth, GUIAnchor.TopLeft);
        {
            // common controls for BodyParts (shape, center, joints)
            BodyPartEditor.ViewportCommonControls();
            // only update values if they change to prevent constant updating of player pref keys
            float fVal;
            GUILayout.BeginHorizontal();
            {
                GUILayout.Label(string.Format("Transform Display Size: {0:0.00}", ballSize), GUILayout.Width(BodyPartEditor.viewportControlsWidth * 0.65f));
                fVal = GUILayout.HorizontalSlider(ballSize, 0f, 1f);
                if (fVal != ballSize)
                {
                    ballSize = fVal;
                }
            }
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            {
                GUILayout.Label(string.Format("Body Mass: {0:0.00}", biped.mass), GUILayout.Width(BodyPartEditor.viewportControlsWidth * 0.65f));
                fVal = GUILayout.HorizontalSlider(biped.mass, 1f, 500f);
                if (fVal != biped.mass)
                {
                    biped.mass = fVal;
                    biped.DistributeMass();
                }
            }
            GUILayout.EndHorizontal();

            // if the editor is playing, then create testing controls
            if (Application.isPlaying)
            {
                // padding after previous controls
                GUILayout.Space(ViewportControls.viewportPadding * 2f);

                // only display ragdoll controls if minimum requirements have been met
                if (biped.ValidateMinimumRequirements())
                {
                    // if the biped is currently ragdoll, then create a button to remove the ragdoll
                    if (biped.isRagdoll)
                    {
                        if (GUILayout.Button("Remove Ragdoll"))
                        {
                            biped.RemoveRagdoll();
                        }
                    }
                    // otherwise create a button to turn the biped into a ragdoll
                    else
                    {
                        if (GUILayout.Button("Create Ragdoll"))
                        {
                            biped.CreateRagdoll(BodyPartEditor.jointResistance, 1f);
                        }
                    }

                    // only update an active ragdoll's spring value if the resistance slider changes
                    float oldResist = BodyPartEditor.jointResistance;
                    float oldForce  = BodyPartEditor.maxForce;
                    BodyPartEditor.ViewportResistanceSlider();
                    BodyPartEditor.ViewportMaxForceSlider();
                    if ((oldResist != BodyPartEditor.jointResistance || oldForce != BodyPartEditor.maxForce) && biped.isRagdoll)
                    {
                        JointDrive drive;
                        foreach (BodyPart part in biped.allParts)
                        {
                            if (part == null || part.joint == null)
                            {
                                continue;
                            }
                            drive = part.joint.slerpDrive;
                            drive.maximumForce    = BodyPartEditor.maxForce;
                            drive.positionSpring  = BodyPartEditor.jointResistance;
                            part.joint.slerpDrive = drive;
                        }
                    }

                    // if the biped is out of ragdoll, include a button to return to the snapshot
                    if (!biped.isRagdoll)
                    {
                        if (GUILayout.Button("Restore Pose to Snapshot"))
                        {
                            foreach (BodyPart part in biped.allParts)
                            {
                                if (part == null)
                                {
                                    continue;
                                }
                                part.ResetToInitialRotation();
                                part.ResetToPositionSnapshot();
                            }
                        }
                    }
                }
                else
                {
                    GUILayout.Label("Minimum biped definition not specified. Please stop the game and ensure that biped minimum requirements have been met.");
                }
            }
            // otherwise, create additional setup controls
            else
            {
                GUILayout.Label("Bone Naming Convention:");
                GUILayout.BeginVertical();
                {
                    namingConvention = (DCCApplication)GUILayout.SelectionGrid((int)namingConvention, System.Enum.GetNames(typeof(DCCApplication)), 2);
                    if (GUILayout.Button(string.Format("Set Up Biped Using {0} Names", namingConvention)))
                    {
                        biped.AutomateSetup(namingConvention);
                    }
                }
                GUILayout.EndVertical();
            }
        }
        ViewportControls.EndArea();

        // display symmetry status at the top of the viewport
        BodyPartEditor.ViewportStatus();

        // finish GUI
        Handles.EndGUI();
    }