/// <summary>
        /// Anchoring reference position for look start point basing on current frame's pose of LeadBone
        /// </summary>
        public void RefreshLookStartPositionAnchor()
        {
            if (!usingAxisCorrection)
            {
                leadBoneInitLocalOffset = BaseTransform.InverseTransformPoint(LeadBone.position);
            }
            else
            {
                leadBoneInitLocalOffset = axisCorrectionMatrix.inverse.MultiplyPoint(LeadBone.position);
            }

            RefreshStartLookPoint = false;
        }
Example #2
0
        public void InitializeBaseVariables()
        {
            _LOG_NoRefs();

            LookState = EFHeadLookState.Null;

            SetAutoWeightsDefault();
            ComputeBonesRotationsFixVariables();

            InitBirdMode();
            ResetBones();

            smoothLookPosition    = GetForwardPosition();
            lookFreezeFocusPoint  = BaseTransform.InverseTransformPoint(smoothLookPosition);
            refreshReferencePose  = true;
            RefreshStartLookPoint = true;
            rootStaticRotation    = BaseTransform.rotation;
            _preBackBonesCount    = BackBonesCount;
            lastBaseRotation      = BaseTransform.rotation;

            // Reset corrections, sometimes quaternion is NaN there we fix it
            for (int i = 0; i < LookBones.Count; i++)
            {
                if (LookBones[i].correctionOffset == Vector3.zero)
                {
                    LookBones[i].correctionOffset = Vector3.zero;
                }
                LookBones[i].lastKeyframeRotation = LookBones[i].Transform.localRotation;
                LookBones[i].RefreshBoneDirections(BaseTransform);
            }


            if (UseEyes)
            {
                InitEyesModule();
            }

            initialized = true;
        }
Example #3
0
        /// <summary>
        /// Main head look rotation calculations logics execution
        /// </summary>
        private void CalculateLookAnimation()
        {
            // Begin check for stop lokoing
            _stopLooking = false;

            // Stopping looking when object to follow is null
            if (FollowMode != EFFollowMode.FollowJustPosition)
            {
                if (ObjectToFollow == null)
                {
                    if (MomentLookTransform == null)
                    {
                        _stopLooking = true;
                    }
                }
            }


            Vector3 lookRotation;

            LookPositionUpdate();
            LookWhenAboveGoBackCalculations();

            // When stopping look, we don't want head to goo too fast onto forward look pose
            if (_stopLooking)
            {
                finalLookPosition = transform.TransformPoint(lookFreezeFocusPoint);
            }
            else
            {
                if (!BirdMode)
                {
                    finalLookPosition = smoothLookPosition;
                }
                else
                {
                    finalLookPosition = BirdTargetPosition;
                }
            }


            if (FixingPreset != EFAxisFixOrder.Parental)
            {
                #region Calculating target rotation and angles

                // If our target is out of max distance let's look at default direction
                if (LookState == EFHeadLookState.OutOfMaxDistance)
                {
                    targetLookAngles = Vector3.MoveTowards(targetLookAngles, Vector3.zero, 1f + RotationSpeed);
                }
                else
                {
                    // Direction towards target in different spaces
                    Vector3 worldDirectionAndTargetAngles = (finalLookPosition - GetLookStartMeasurePosition()).normalized;

                    // Supporting different axis orientation using matrix
                    if (usingAxisCorrection)
                    {
                        worldDirectionAndTargetAngles = axisCorrectionMatrix.inverse.MultiplyVector(worldDirectionAndTargetAngles).normalized;
                        worldDirectionAndTargetAngles = WrapVector(Quaternion.LookRotation(worldDirectionAndTargetAngles, axisCorrectionMatrix.MultiplyVector(ModelUpAxis).normalized).eulerAngles);
                    }
                    else // Easier calculations when using standard z - forward - y up
                    {
                        worldDirectionAndTargetAngles = BaseTransform.InverseTransformDirection(worldDirectionAndTargetAngles);
                        worldDirectionAndTargetAngles = WrapVector(Quaternion.LookRotation(worldDirectionAndTargetAngles, BaseTransform.TransformDirection(ModelUpAxis)).eulerAngles);
                    }

                    targetLookAngles = worldDirectionAndTargetAngles;
                }

                Vector2 angles = targetLookAngles;
                angles = LimitAnglesCalculations(angles);
                AnimateAnglesTowards(angles);

                #endregion

                // Character rotation offset
                if (usingAxisCorrection)
                {
                    Quaternion fromto = Quaternion.FromToRotation(Vector3.right, Vector3.Cross(Vector3.up, ModelForwardAxis));
                    fromto       = Quaternion.Euler(finalLookAngles) * fromto * BaseTransform.rotation;
                    lookRotation = fromto.eulerAngles;
                }
                else
                {
                    lookRotation = finalLookAngles + BaseTransform.eulerAngles;
                }

                // Additional operations
                lookRotation += RotationOffset;
                lookRotation  = ConvertFlippedAxes(lookRotation);
            }
            else // Universal correction method
            {
                lookRotation  = LookRotationParental((finalLookPosition - GetLookStartMeasurePosition()).normalized).eulerAngles;
                lookRotation += RotationOffset;
            }

            if (!_stopLooking)
            {
                lookFreezeFocusPoint = BaseTransform.InverseTransformPoint(finalLookPosition);
            }
            targetLookRotation = Quaternion.Euler(lookRotation);

            SetTargetBonesRotations();
        }
Example #4
0
        private void Gizmos_DrawClamping(float radius, Vector3 lookPos)
        {
            if (!_gizmosDrawingLimiting && DebugRays == false)
            {
                return;
            }

            Handles.matrix = BaseTransform.localToWorldMatrix;

            Vector3 startLook      = GetLookStartMeasurePosition();
            Vector3 startLookLocal = BaseTransform.InverseTransformPoint(startLook);
            Vector3 dir            = (lookPos - startLook).normalized;

            if (LookState == EFHeadLookState.ClampedAngle)
            {
                Handles.color = new Color(1f, 1f, 0.1f, 0.7f);
            }
            else
            {
                Handles.color = new Color(0.3f, 1f, 0.1f, 0.7f);
            }

            //Gizmos.DrawLine(startLook, startLook + dir);

            Vector3 axisDir = BaseTransform.InverseTransformDirection(dir);

            axisDir.y = 0; axisDir.Normalize();
            Handles.DrawLine(startLookLocal, startLookLocal + axisDir * radius);

            if (YRotationLimits != _gizmosLastVertClamp)
            {
                _gizmosVertAlpha = 1.5f;
            }

            // Vertical
            Handles.color = new Color(0.3f, 1f, 0.7f, 0.08f * Mathf.Min(_gizmosVertAlpha, 1f));
            Handles.DrawSolidArc(startLookLocal, Vector3.right, ModelForwardAxis, YRotationLimits.y, radius / 1.2f);
            Handles.DrawSolidArc(startLookLocal, Vector3.right, ModelForwardAxis, YRotationLimits.x, radius / 1.2f);

            Handles.color = new Color(0.22f, .8f, 0.5f, 0.75f);
            Handles.DrawWireArc(startLookLocal, Vector3.right, ModelForwardAxis, YRotationLimits.y, radius / 1.2f);
            Handles.DrawWireArc(startLookLocal, Vector3.right, ModelForwardAxis, YRotationLimits.x, radius / 1.2f);

            if (StartLookElasticRangeY > 0f)
            {
                Handles.color = new Color(0.3f, 0.3f, 0.3f, 0.04f);
                Handles.DrawSolidArc(startLookLocal, Vector3.right, ModelForwardAxis, StartLookElasticRangeY, radius / 3.2f);
                Handles.DrawSolidArc(startLookLocal, Vector3.right, ModelForwardAxis, -StartLookElasticRangeY, radius / 3.2f);
            }

            if (XRotationLimits != _gizmosLastHorizClamp)
            {
                _gizmosHorizAlpha = 1.5f;
            }

            // Horizontal
            Handles.color = new Color(0.3f, 1f, 0.1f, 0.08f * Mathf.Min(_gizmosHorizAlpha, 1f));
            Handles.DrawSolidArc(startLookLocal, ModelUpAxis, ModelForwardAxis, XRotationLimits.x, radius);
            Handles.DrawSolidArc(startLookLocal, ModelUpAxis, ModelForwardAxis, XRotationLimits.y, radius);

            Handles.color = new Color(0.22f, .8f, 0.08f, 0.75f);
            Handles.DrawWireArc(startLookLocal, ModelUpAxis, ModelForwardAxis, XRotationLimits.x, radius);
            Handles.DrawWireArc(startLookLocal, ModelUpAxis, ModelForwardAxis, XRotationLimits.y, radius);

            if (StartLookElasticRangeX > 0f)
            {
                Handles.color = new Color(0.3f, 0.3f, 0.3f, 0.04f);
                Handles.DrawSolidArc(startLookLocal, ModelUpAxis, ModelForwardAxis, StartLookElasticRangeX, radius / 3.2f);
                Handles.DrawSolidArc(startLookLocal, ModelUpAxis, ModelForwardAxis, -StartLookElasticRangeX, radius / 3.2f);
            }


            if (LookState == EFHeadLookState.ClampedAngle)
            {
                Handles.color = new Color(1f, 1f, 0.1f, 0.7f);
            }
            else
            {
                Handles.color = new Color(0.3f, 1f, 0.7f, 0.7f);
            }

            axisDir   = BaseTransform.InverseTransformDirection(dir);
            axisDir.x = 0; axisDir.z = Mathf.Abs(axisDir.z); axisDir.Normalize();
            Handles.DrawLine(startLookLocal, startLookLocal + axisDir * radius / 1.2f);


            _gizmosLastHorizClamp = XRotationLimits;
            _gizmosLastVertClamp  = YRotationLimits;

            _gizmosVertAlpha  -= 0.03f;
            _gizmosHorizAlpha -= 0.03f;

            Handles.matrix = Matrix4x4.identity;
        }