public void LimitBend(float solverWeight, float positionWeight)
        {
            if (!this.initiated)
            {
                return;
            }
            Vector3    vector        = this.bone1.rotation * -this.defaultLocalDirection;
            Vector3    fromDirection = this.bone3.position - this.bone2.position;
            bool       flag          = false;
            Vector3    toDirection   = V3Tools.ClampDirection(fromDirection, vector, this.clampF * solverWeight, 0, out flag);
            Quaternion rotation      = this.bone3.rotation;

            if (flag)
            {
                Quaternion lhs = Quaternion.FromToRotation(fromDirection, toDirection);
                this.bone2.rotation = lhs * this.bone2.rotation;
            }
            if (positionWeight > 0f)
            {
                Vector3 vector2        = this.bone2.position - this.bone1.position;
                Vector3 fromDirection2 = this.bone3.position - this.bone2.position;
                Vector3.OrthoNormalize(ref vector2, ref fromDirection2);
                Quaternion lhs2 = Quaternion.FromToRotation(fromDirection2, vector);
                this.bone2.rotation = Quaternion.Lerp(this.bone2.rotation, lhs2 * this.bone2.rotation, positionWeight * solverWeight);
            }
            if (flag || positionWeight > 0f)
            {
                this.bone3.rotation = rotation;
            }
        }
Exemple #2
0
        /*
         * Limits the bending joint of the limb to 90 degrees from the default 90 degrees of bend direction
         * */
        public void LimitBend(float solverWeight, float positionWeight)
        {
            if (!initiated)
            {
                return;
            }

            Vector3 normalDirection = bone1.rotation * -defaultLocalDirection;

            Vector3 axis2 = bone3.position - bone2.position;

            // Clamp the direction from knee/elbow to foot/hand to valid range (90 degrees from right-angledly bent limb)
            bool    changed      = false;
            Vector3 clampedAxis2 = V3Tools.ClampDirection(axis2, normalDirection, clampF * solverWeight, 0, out changed);

            Quaternion bone3Rotation = bone3.rotation;

            if (changed)
            {
                Quaternion f = Quaternion.FromToRotation(axis2, clampedAxis2);
                bone2.rotation = f * bone2.rotation;
            }

            // Rotating bend direction to normal when the limb is stretched out
            if (positionWeight > 0f)
            {
                Vector3 normal  = bone2.position - bone1.position;
                Vector3 tangent = bone3.position - bone2.position;

                Vector3.OrthoNormalize(ref normal, ref tangent);
                Quaternion q = Quaternion.FromToRotation(tangent, normalDirection);

                bone2.rotation = Quaternion.Lerp(bone2.rotation, q * bone2.rotation, positionWeight * solverWeight);
            }

            if (changed || positionWeight > 0f)
            {
                bone3.rotation = bone3Rotation;
            }
        }
        /*
         * Limits the bending joint of the limb to 90 degrees from the default 90 degrees of bend direction
         * */
        public void LimitBend(float solverWeight)
        {
            Vector3 normalDirection = bone1.rotation * -defaultLocalDirection;

            Vector3 axis2 = bone3.position - bone2.position;

            bool    changed      = false;
            Vector3 clampedAxis2 = V3Tools.ClampDirection(axis2, normalDirection, 0.505f * solverWeight, 0, out changed);

            if (!changed)
            {
                return;
            }

            Quaternion bone3Rotation = bone3.rotation;

            Quaternion f = Quaternion.FromToRotation(axis2, clampedAxis2);

            bone2.rotation = f * bone2.rotation;

            bone3.rotation = bone3Rotation;
        }
        private void ChestDirection()
        {
            float num = this.chestDirectionWeight * this.ik.solver.IKPositionWeight;

            if (num <= 0f)
            {
                return;
            }
            bool flag = false;

            this.chestDirection = V3Tools.ClampDirection(this.chestDirection, this.ik.references.root.forward, 0.45f, 2, out flag);
            if (this.chestDirection == Vector3.zero)
            {
                return;
            }
            Quaternion quaternion = Quaternion.FromToRotation(this.ik.references.root.forward, this.chestDirection);

            quaternion = Quaternion.Lerp(Quaternion.identity, quaternion, num * (1f / (float)this.chestBones.Length));
            foreach (Transform transform in this.chestBones)
            {
                transform.rotation = quaternion * transform.rotation;
            }
        }