Example #1
0
        /// <summary>
        /// Displays the axis handles.
        /// </summary>
        /// <param name="joint">Joint.</param>
        /// <param name="bindpose">Bindpose.</param>
        /// <returns><see langword="true"/> if the handles changed; otherwise, <see langword="false"/>.</returns>
        public static bool DisplayAxisHandles(Joint joint, Matrix4x4 bindpose)
        {
            bool isChar  = joint is CharacterJoint;
            bool isConf  = joint is ConfigurableJoint;
            bool isHinge = joint is HingeJoint;

            if (!isChar && !isConf && !isHinge)
            {
                return(false);
            }
            bool      result    = false;
            Matrix4x4 oldMatrix = Handles.matrix;

            Handles.matrix = (
                joint.connectedBody == null ? Matrix4x4.identity : joint.connectedBody.transform.localToWorldMatrix
                ) * bindpose;
            Vector3 ax1 = Vector3.forward;
            Vector3 ax2 = Vector3.up;

            if (isChar)
            {
                ax1 = (joint as CharacterJoint).axis;
                ax2 = (joint as CharacterJoint).swingAxis;
            }
            else if (isConf)
            {
                ax1 = (joint as ConfigurableJoint).axis;
                ax2 = (joint as ConfigurableJoint).secondaryAxis;
            }
            else if (isHinge)
            {
                ax1 = (joint as HingeJoint).axis;
            }
            Quaternion axisOrientation = Quaternion.LookRotation(ax1, ax2) * s_BindposeAxisHandleOffset;

            if (SceneGUI.BeginHandles(joint, "Change Axes"))
            {
                axisOrientation = TransformHandles.Rotation(axisOrientation, Vector3.zero);
            }
            if (SceneGUI.EndHandles())
            {
                if (isChar)
                {
                    (joint as CharacterJoint).axis      = axisOrientation * s_PrimaryAxisHandleDirection;
                    (joint as CharacterJoint).swingAxis = axisOrientation * s_SecondaryAxisHandleDirection;
                }
                else if (isConf)
                {
                    (joint as ConfigurableJoint).axis          = axisOrientation * s_PrimaryAxisHandleDirection;
                    (joint as ConfigurableJoint).secondaryAxis = axisOrientation * s_SecondaryAxisHandleDirection;
                }
                else if (isHinge)
                {
                    (joint as HingeJoint).axis = axisOrientation * s_PrimaryAxisHandleDirection;
                }
                result = true;
            }
            Handles.matrix = oldMatrix;
            return(result);
        }