public Quaternion CalculateChestRotation(HipsTarget hipsTarget, Quaternion baseRotation, Quaternion hipsRotation, Quaternion neckRotation)
        {
            float      factor        = hipsTarget.bendingFactor * 0.3F;
            Quaternion chestRotation = Quaternion.Slerp(neckRotation, hipsRotation, factor) * baseRotation;

            return(chestRotation);
        }
        //float bendingFactor = 1;

        private void NewIK(HipsTarget hipsTarget, Vector3 neckBonePosition, Quaternion neckBoneTargetRotation)
        {
            Quaternion hipsTargetRotation = CalculateHipsRotation(hipsTarget, neckBonePosition);

            hipsTarget.hips.SetBoneRotation(hipsTargetRotation);

            hipsTargetRotation *= Quaternion.Inverse(hipsTarget.hips.bone.baseRotation);

            Quaternion spineTargetRotation = CalculateSpineRotation(hipsTarget, hipsTarget.spine.bone.baseRotation, hipsTargetRotation, neckBoneTargetRotation);

            hipsTarget.spine.SetBoneRotation(spineTargetRotation);

            Quaternion chestTargetRotation = CalculateChestRotation(hipsTarget, hipsTarget.chest.bone.baseRotation, hipsTargetRotation, neckBoneTargetRotation);

            hipsTarget.chest.SetBoneRotation(chestTargetRotation);

            if (hipsTarget.chest.bone.transform != null)
            {
                Vector3 chestTopPosition = hipsTarget.chest.bone.transform.position + hipsTarget.chest.bone.targetRotation * Vector3.up * hipsTarget.chest.bone.length;
                Vector3 spineVector      = chestTopPosition - hipsTarget.hips.bone.transform.position;
                hipsTarget.hips.SetBonePosition(neckBonePosition - spineVector);
            }
            else if (hipsTarget.spine.bone.transform != null)
            {
                Vector3 spineTopPosition = hipsTarget.spine.bone.transform.position + hipsTarget.spine.bone.targetRotation * Vector3.up * hipsTarget.spine.bone.length;
                Vector3 spineVector      = spineTopPosition - hipsTarget.hips.bone.transform.position;
                hipsTarget.hips.SetBonePosition(neckBonePosition - spineVector);
            }
        }
        public void CharacterNoIK(HipsTarget hipsTarget, Vector3 neckBonePosition)
        {
            hipsTarget.hips.SetBoneRotation(hipsTarget.hips.target.transform.rotation);
            hipsTarget.spine.SetBoneRotation(hipsTarget.spine.target.transform.rotation);
            hipsTarget.chest.SetBoneRotation(hipsTarget.chest.target.transform.rotation);

            if (hipsTarget.humanoid.headTarget.head.target.confidence.position > 0)
            {
                if (hipsTarget.chest.bone.transform != null)
                {
                    Vector3 chestTopPosition = hipsTarget.chest.bone.transform.position + hipsTarget.chest.bone.targetRotation * Vector3.up * hipsTarget.chest.bone.length;
                    Vector3 spineVector      = chestTopPosition - hipsTarget.hips.bone.transform.position;
                    hipsTarget.hips.SetBonePosition(neckBonePosition - spineVector);
                }
                else if (hipsTarget.spine.bone.transform != null)
                {
                    Vector3 spineTopPosition = hipsTarget.spine.bone.transform.position + hipsTarget.spine.bone.targetRotation * Vector3.up * hipsTarget.spine.bone.length;
                    Vector3 spineVector      = spineTopPosition - hipsTarget.hips.bone.transform.position;
                    hipsTarget.hips.SetBonePosition(neckBonePosition - spineVector);
                }
            }
            else
            {
                Vector3 hipsPosition = hipsTarget.hips.target.transform.position + hipsTarget.hipsBaseHeight * Vector3.up;
                hipsTarget.hips.SetBonePosition(hipsPosition);
            }
        }
Ejemplo n.º 4
0
        // Do not remove this, this is dynamically called from Target_Editor!
        // Changes the target transform used for this head target
        // Generates a new headtarget component, so parameters will be lost if transform is changed
        public static HipsTarget SetTarget(HumanoidControl humanoid, Transform targetTransform, bool isLeft)
        {
            HipsTarget currentHipsTarget = humanoid.hipsTarget;

            if (targetTransform == currentHipsTarget.transform)
            {
                return(currentHipsTarget);
            }

            GetDefaultBone(humanoid.targetsRig, ref targetTransform, HumanBodyBones.Hips);
            if (targetTransform == null)
            {
                return(currentHipsTarget);
            }

            HipsTarget hipsTarget = targetTransform.GetComponent <HipsTarget>();

            if (hipsTarget == null)
            {
                hipsTarget = targetTransform.gameObject.AddComponent <HipsTarget>();
            }

            hipsTarget.NewComponent(humanoid);
            hipsTarget.InitComponent();

            return(hipsTarget);
        }
Ejemplo n.º 5
0
        /// <summary>Animates the arm swing based on foot movements</summary>
        /// <returns>boolean indicating is the arms are swinging</returns>
        protected bool ArmSwingAnimation()
        {
            HumanoidControl humanoid   = handTarget.humanoid;
            HipsTarget      hipsTarget = humanoid.hipsTarget;

            Vector3 newPosition;
            float   localFootZ;

            if (handTarget.isLeft)
            {
                localFootZ = hipsTarget.transform.InverseTransformPoint(humanoid.rightFootTarget.transform.position).z;
                Vector3 localFootPosition = Quaternion.Inverse(hipsTarget.hips.bone.targetRotation) * (humanoid.rightFootTarget.transform.position - humanoid.transform.position);
                newPosition = localFootPosition + foot2hand;

                handTarget.hand.target.transform.rotation  = handTarget.forearm.bone.targetRotation;
                handTarget.hand.target.confidence.rotation = 0.2F;
            }
            else
            {
                localFootZ = hipsTarget.transform.InverseTransformPoint(humanoid.leftFootTarget.transform.position).z;
                Vector3 localFootPosition = Quaternion.Inverse(hipsTarget.hips.bone.targetRotation) * (humanoid.leftFootTarget.transform.position - humanoid.transform.position);
                newPosition = localFootPosition + foot2hand;

                handTarget.hand.target.transform.rotation  = handTarget.forearm.bone.targetRotation;
                handTarget.hand.target.confidence.rotation = 0.2F;
            }

            float newY = Mathf.Abs(localFootZ / 4 + 0.02f) - humanoid.transform.position.y;

            handTarget.hand.target.transform.position  = hipsTarget.transform.TransformPoint(new Vector3(newPosition.x, newY, newPosition.z));
            handTarget.hand.target.confidence.position = 0.2F;
            return(true);
        }
        private void SimpleIK(HipsTarget hipsTarget)
        {
            // This implementation ignores the spine and chest bones
            // Which results in a stiff spine
            Vector3    neckTargetPosition = hipsTarget.humanoid.headTarget.neck.target.transform.position;
            Quaternion neckRotation       = hipsTarget.humanoid.headTarget.neck.bone.transform.rotation;

            Vector3 hipsTargetPosition = hipsTarget.hips.target.transform.position;

            Quaternion hipsTargetRotation = hipsTarget.hips.target.transform.rotation;

            Vector3 hipsBack = hipsTargetRotation * Vector3.back;
            Vector3 hipsUp   = neckTargetPosition - hipsTargetPosition;

            if (hipsUp.sqrMagnitude <= 0.0001F)
            {
                hipsUp = hipsTarget.humanoid.up;
            }

            Quaternion spineRotation = Quaternion.LookRotation(hipsUp, hipsBack) * Quaternion.Euler(90, 0, 0);
            Quaternion hipsRotation  = spineRotation; /** hipsTarget.spine2HipsRotation;*/

            hipsTarget.hips.SetBoneRotation(hipsRotation);

            Vector3 spineVector = spineRotation * Vector3.up * hipsTarget.torsoLength;

            hipsTarget.hips.SetBonePosition(hipsTarget.humanoid.headTarget.neck.target.transform.position - spineVector);

            hipsTarget.humanoid.headTarget.neck.bone.transform.rotation = neckRotation;
        }
Ejemplo n.º 7
0
        public void InitController(SerializedProperty sensorProp, HipsTarget hipsTarget)
        {
            if (sensorProp == null)
            {
                return;
            }

            Init(hipsTarget);

            SerializedProperty sensorTransformProp = sensorProp.FindPropertyRelative("sensorTransform");

            sensorTransformProp.objectReferenceValue = sensorTransform;

            SerializedProperty targetProp = sensorProp.FindPropertyRelative("target");

            targetProp.objectReferenceValue = target;

            if (!tracker.enabled || !enabled)
            {
                return;
            }

            CheckSensorTransform();
            sensorTransformProp.objectReferenceValue = sensorTransform;

            ShowSensor(hipsTarget.humanoid.showRealObjects && hipsTarget.showRealObjects);

            SerializedProperty sensor2TargetPositionProp = sensorProp.FindPropertyRelative("sensor2TargetPosition");

            sensor2TargetPositionProp.vector3Value = sensor2TargetPosition;
            SerializedProperty sensor2TargetRotationProp = sensorProp.FindPropertyRelative("sensor2TargetRotation");

            sensor2TargetRotationProp.quaternionValue = sensor2TargetRotation;
        }
Ejemplo n.º 8
0
        public override void Init(HipsTarget _hipsTarget)
        {
            base.Init(_hipsTarget);
#if hSTEAMVR
            tracker = hipsTarget.humanoid.steam;
#elif hOPENVR
            tracker = hipsTarget.humanoid.openVR;
#endif
        }
        public void Start(HumanoidControl _humanoid, HipsTarget _target)
        {
            hipsTarget = _target;

            if (hipsTarget.hips.bone.transform == null)
            {
                return;
            }
        }
        public Quaternion CalculateSpineRotation(HipsTarget hipsTarget, Quaternion baseRotation, Quaternion hipsRotation, Quaternion neckRotation)
        {
            float factor = hipsTarget.chest.bone.transform != null ? 0.4F : 0.3F;

            factor *= hipsTarget.bendingFactor;
            Quaternion spineRotation = Quaternion.Slerp(neckRotation, hipsRotation, factor) * baseRotation;

            return(spineRotation);
        }
Ejemplo n.º 11
0
        private void InitConfiguration(HipsTarget target)
        {
            if (target.humanoid.avatarRig == null)
            {
                return;
            }

            InitChestConfiguration(target.chest);
            InitSpineConfiguration(target.spine);
            InitHipsConfiguration(target.hips);
        }
Ejemplo n.º 12
0
        private void FeetOffGround()
        {
            HipsTarget hipsTarget = footTarget.humanoid.hipsTarget;

            footTarget.foot.target.transform.rotation = Quaternion.AngleAxis(hipsTarget.hips.target.transform.eulerAngles.y, footTarget.humanoid.up) * Quaternion.AngleAxis(30, Vector3.right);

            float   legLength         = footTarget.upperLeg.bone.length + footTarget.lowerLeg.bone.length;
            Vector3 localFootPosition = new Vector3(footTarget.isLeft ? -footSeparation : footSeparation, -legLength, 0);

            footTarget.foot.target.transform.position = hipsTarget.hips.target.transform.position + hipsTarget.hips.target.transform.rotation * localFootPosition;
        }
Ejemplo n.º 13
0
        private void UpdateBones(HipsTarget target)
        {
            if (target.humanoid.avatarRig == null)
            {
                return;
            }

            UpdateHipsBones(target.hips);
            UpdateSpineBones(target.spine);
            UpdateChestBones(target.chest);
        }
Ejemplo n.º 14
0
            public TargetProps(SerializedObject serializedObject, UnityTorsoSensor _sensor, HipsTarget _hipsTarget, string unitySensorName)
            {
                enabledProp               = serializedObject.FindProperty(unitySensorName + ".enabled");
                sensorTransformProp       = serializedObject.FindProperty(unitySensorName + ".sensorTransform");
                sensor2TargetPositionProp = serializedObject.FindProperty(unitySensorName + ".sensor2TargetPosition");
                sensor2TargetRotationProp = serializedObject.FindProperty(unitySensorName + ".sensor2TargetRotation");

                hipsTarget = _hipsTarget;
                sensor     = _sensor;

                sensor.Init(hipsTarget);
            }
Ejemplo n.º 15
0
            private static void CheckSensorComponent(HipsTarget hipsTarget)
            {
                if (hipsTarget.viveTracker.sensorTransform == null)
                {
                    return;
                }

                ViveTrackerComponent sensorComponent = hipsTarget.viveTracker.sensorTransform.GetComponent <ViveTrackerComponent>();

                if (sensorComponent == null)
                {
                    sensorComponent = hipsTarget.viveTracker.sensorTransform.gameObject.AddComponent <ViveTrackerComponent>();
                }
            }
Ejemplo n.º 16
0
            protected static void CheckRigidbodyComponent(HipsTarget hipsTarget)
            {
                if (hipsTarget.optitrack.sensorTransform == null)
                {
                    return;
                }

                OptitrackRigidbodyComponent sensorComponent = hipsTarget.optitrack.sensorTransform.GetComponent <OptitrackRigidbodyComponent>();

                if (sensorComponent == null)
                {
                    hipsTarget.optitrack.sensorTransform.gameObject.AddComponent <OptitrackRigidbodyComponent>();
                }
            }
Ejemplo n.º 17
0
        private static Quaternion NaturalShoulderOrientation(HandTarget handTarget, ref Quaternion lastLocalShoulderRotation)
        {
            if (handTarget.shoulder.target.confidence.rotation > 0.5F)
            {
                return(handTarget.shoulder.target.transform.rotation);
            }

            HipsTarget hipsTarget = handTarget.humanoid.hipsTarget;

            Quaternion torsoRotation;

            if (hipsTarget.chest.bone.transform != null)
            {
                torsoRotation = hipsTarget.chest.bone.targetRotation;
            }
            else
            {
                torsoRotation = Quaternion.LookRotation(hipsTarget.hips.bone.targetRotation * Vector3.forward, handTarget.humanoid.up);
            }

            Vector3 upperArmBasePosition = handTarget.shoulder.bone.transform.position + torsoRotation * handTarget.shoulder.target.baseRotation * handTarget.outward * handTarget.shoulder.bone.length;

            float upperArm2HandDistance = Vector3.Distance(upperArmBasePosition, handTarget.hand.target.transform.position);
            float armLength             = handTarget.upperArm.bone.length + handTarget.forearm.bone.length;

            float distanceToTarget = upperArm2HandDistance - armLength - 0.03F;

            Quaternion shoulderRotation = handTarget.shoulder.bone.targetRotation;

            if (distanceToTarget > 0)
            {
                // we need to use the shoulder now to get closer to the target
                Vector3    targetDirection  = handTarget.hand.bone.transform.position - handTarget.shoulder.bone.transform.position;
                Quaternion toTargetRotation = Quaternion.LookRotation(targetDirection) * Quaternion.AngleAxis(handTarget.isLeft ? 90 : -90, Vector3.up);

                shoulderRotation = Quaternion.Slerp(torsoRotation * handTarget.shoulder.bone.baseRotation, toTargetRotation, distanceToTarget * 4);
            }
            else if (distanceToTarget < 0)
            {
                shoulderRotation = Quaternion.Slerp(shoulderRotation, torsoRotation * handTarget.shoulder.bone.baseRotation, -distanceToTarget * 10);
            }

            if (handTarget.shoulder.bone.jointLimitations)
            {
                shoulderRotation = LimitAngle(handTarget.shoulder, ref lastLocalShoulderRotation, shoulderRotation);
            }

            return(shoulderRotation);
        }
        private Quaternion CalculateHipsRotation(HipsTarget hipsTarget, Vector3 neckBonePosition)
        {
            // hips target has only effect on the Y rotation (via hipsForward)
            // the effect of the other movements and rotations of hips on the movements of the spine is very limited
            // for now, we will not take these movements into account

            Vector3    torsoUp            = neckBonePosition - hipsTarget.hips.target.transform.position;
            Quaternion spine2HipsRotation = Rotations.Rotate(hipsTarget.spine2HipsRotation, hipsTarget.hips.target.transform.rotation);
            Vector3    hipsUp             = spine2HipsRotation * torsoUp;

            Vector3 hipsForward = hipsTarget.hips.target.transform.forward;

            Quaternion hipsRotation = Quaternion.LookRotation(hipsUp, -hipsForward) * Quaternion.AngleAxis(90, Vector3.right);

            return(hipsRotation);
        }
Ejemplo n.º 19
0
        private void ConfigurationInspector(HipsTarget hipsTarget)
        {
            hipsTarget.RetrieveBones();

            showConfiguration = EditorGUILayout.Foldout(showConfiguration, "Configuration", true);
            if (showConfiguration)
            {
                EditorGUI.indentLevel++;

                ChestConfigurationInspector(ref hipsTarget.chest);
                SpineConfigurationInspector(ref hipsTarget.spine);
                HipsConfigurationInspector(ref hipsTarget.hips);

                EditorGUI.indentLevel--;
            }
        }
Ejemplo n.º 20
0
        public Vector3 CalculateElbowAxis(HandTarget handTarget, Vector3 handPosition, Quaternion handRotation)
        {
            HipsTarget hipsTarget      = handTarget.humanoid.hipsTarget;
            Quaternion hipsOrientation = Quaternion.LookRotation(hipsTarget.hips.target.transform.forward, handTarget.humanoid.up);

            Vector3 elbowAxis = Quaternion.Inverse(hipsOrientation) * handRotation * Vector3.up;

            if (handTarget.isLeft)
            {
                elbowAxis = (elbowAxis + (Vector3.left + Vector3.up).normalized) / 2;
            }
            else
            {
                elbowAxis = (elbowAxis + (Vector3.right + Vector3.up).normalized) / 2;
            }

            float   elbowAxisY = elbowAxis.y;
            float   elbowAxisZ = elbowAxis.z;
            Vector3 dHandUpper = Quaternion.Inverse(hipsOrientation) * (handPosition - handTarget.upperArm.bone.transform.position);

            if (!handTarget.isLeft)
            {
                if (dHandUpper.x < 0)
                {
                    elbowAxisZ -= dHandUpper.x * 10;
                }
            }
            else
            {
                if (dHandUpper.x > 0)
                {
                    elbowAxisZ += dHandUpper.x * 10;
                }
            }
            if (dHandUpper.y > 0)
            {
                elbowAxisY += dHandUpper.y * 10;
            }

            elbowAxisY = Mathf.Clamp(elbowAxisY, 0.01F, 1);
            elbowAxisZ = Mathf.Clamp(elbowAxisZ, -0.1F, 0.1F);
            elbowAxis  = hipsOrientation * new Vector3(elbowAxis.x, elbowAxisY, elbowAxisZ);

            return(elbowAxis);
        }
        public static void Update(HipsTarget hipsTarget)
        {
            if (hipsTarget.hips.bone.transform == null || !hipsTarget.humanoid.calculateBodyPose)
            {
                return;
            }

            //if (Mathf.Abs(hipsTarget.hipsBaseHeight) < 0.00001F)
            //    hipsTarget.hipsBaseHeight = hipsTarget.hips.bone.transform.position.y - hipsTarget.hips.target.transform.position.y;

            HeadTarget headTarget = hipsTarget.humanoid.headTarget;

            HumanoidTarget.BoneTransform neckBone = (headTarget.neck.bone.transform != null) ? headTarget.neck.bone : headTarget.head.bone;
            Vector3    neckPosition       = neckBone.transform.position;
            Quaternion neckRotation       = neckBone.transform.rotation;
            Quaternion neckTargetRotation = neckRotation * neckBone.toTargetRotation * Quaternion.Inverse(headTarget.neck.bone.baseRotation);

            if (hipsTarget.humanoid.targetsRig.runtimeAnimatorController != null && hipsTarget.hips.target.confidence.position == 0)
            {
                hipsTarget.torsoMovements.CharacterNoIK(hipsTarget, neckPosition);
            }
            else
            {
                if (hipsTarget.hips.target.confidence.rotation > 0.5F && hipsTarget.spine.target.confidence.rotation > 0.5F && hipsTarget.chest.target.confidence.rotation > 0.5F)
                {
                    hipsTarget.torsoMovements.CharacterNoIK(hipsTarget, neckPosition);
                }
                else if (!hipsTarget.newSpineIK)
                {
                    hipsTarget.torsoMovements.NewIK(hipsTarget, neckPosition, neckTargetRotation);
                }
                else
                {
                    hipsTarget.torsoMovements.SimpleIK(hipsTarget);
                }

                if (Application.isPlaying)
                {
                    hipsTarget.torsoMovements.breathing.Update();
                }

                neckBone.transform.position = neckPosition;
                neckBone.transform.rotation = neckRotation;
            }
        }
Ejemplo n.º 22
0
        private void SensorInspectors(HipsTarget hipsTarget)
        {
            showSensors = EditorGUILayout.Foldout(showSensors, "Controllers", true);
            if (showSensors)
            {
                EditorGUI.indentLevel++;

                foreach (TargetProps props in allProps)
                {
                    props.Inspector();
                }

                if (humanoid.animatorEnabled)
                {
                    hipsTarget.animator.enabled = EditorGUILayout.ToggleLeft("Procedural Animation", hipsTarget.animator.enabled, GUILayout.MinWidth(80));
                }
                EditorGUI.indentLevel--;
            }
        }
Ejemplo n.º 23
0
        public void OnEnable()
        {
            hipsTarget = (HipsTarget)target;

            if (hipsTarget.humanoid == null)
            {
                hipsTarget.humanoid = GetHumanoid(hipsTarget);
            }
            humanoid = hipsTarget.humanoid;
            if (humanoid == null)
            {
                return;
            }

            InitEditors();

            hipsTarget.InitSensors();
            InitConfiguration(hipsTarget);
            InitSettings();
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Checks whether the humanoid has an HipsTarget
        /// and adds one if none has been found
        /// </summary>
        /// <param name="humanoid">The humanoid to check</param>
        public static void DetermineTarget(HumanoidControl humanoid)
        {
            HipsTarget hipsTarget = humanoid.hipsTarget;

            if (hipsTarget == null)
            {
                Transform hipsTargetTransform = humanoid.targetsRig.GetBoneTransform(HumanBodyBones.Hips);
                if (hipsTargetTransform == null)
                {
                    Debug.LogError("Could not find hips bone in targets rig");
                    return;
                }

                hipsTarget = hipsTargetTransform.GetComponent <HipsTarget>();
                if (hipsTarget == null)
                {
                    hipsTarget          = hipsTargetTransform.gameObject.AddComponent <HipsTarget>();
                    hipsTarget.humanoid = humanoid;
                }
            }

            humanoid.hipsTarget = hipsTarget;
        }
Ejemplo n.º 25
0
        // Do not remove this, this is dynamically called from Target_Editor!
        public static HipsTarget CreateTarget(HumanoidTarget oldTarget)
        {
            GameObject      targetObject    = new GameObject("Hips Target");
            Transform       targetTransform = targetObject.transform;
            HumanoidControl humanoid        = oldTarget.humanoid;

            targetTransform.parent   = oldTarget.humanoid.transform;
            targetTransform.position = oldTarget.transform.position;
            targetTransform.rotation = oldTarget.transform.rotation;

            HipsTarget hipsTarget = targetTransform.gameObject.AddComponent <HipsTarget>();

            hipsTarget.humanoid = humanoid;
            humanoid.hipsTarget = hipsTarget;

            hipsTarget.RetrieveBones();
            hipsTarget.InitAvatar();
            hipsTarget.MatchTargetsToAvatar();
            //hipsTarget.NewComponent(oldTarget.humanoid);
            //hipsTarget.InitComponent();

            return(hipsTarget);
        }
Ejemplo n.º 26
0
 public TargetedHipsBone(HipsTarget hipsTarget)
 {
     this.hipsTarget = hipsTarget;
     boneId          = Bone.Hips;
 }
Ejemplo n.º 27
0
 public TargetedSpineBone(HipsTarget hipsTarget)
 {
     this.hipsTarget = hipsTarget;
     boneId          = Bone.Spine;
 }
Ejemplo n.º 28
0
 public TargetedChestBone(HipsTarget hipsTarget)
 {
     this.hipsTarget = hipsTarget;
     boneId          = Bone.Chest;
 }
Ejemplo n.º 29
0
 public override void Init(HipsTarget _hipsTarget)
 {
     base.Init(_hipsTarget);
     optitrackTracker = hipsTarget.humanoid.optitrack;
     tracker          = optitrackTracker;
 }
Ejemplo n.º 30
0
 public HipsTargetProps(SerializedObject serializedObject, HipsTarget hipsTarget)
     : base(serializedObject, hipsTarget.kinect, hipsTarget, "kinect")
 {
 }