예제 #1
0
        public virtual HandleOrientation GetNearestOrientation(Transform grip, Side side)
        {
            float             higherDot         = -Mathf.Infinity;
            HandleOrientation orientationResult = null;

            foreach (HandleOrientation orientation in orientations)
            {
                if (orientation.side == side)
                {
                    float dot = Vector3.Dot(grip.forward, orientation.transform.rotation * Vector3.forward) + Vector3.Dot(grip.up, orientation.transform.rotation * Vector3.up);
                    if (dot > higherDot)
                    {
                        higherDot         = dot;
                        orientationResult = orientation;
                    }
                }
            }
            return(orientationResult);
        }
예제 #2
0
 public virtual void CheckOrientations()
 {
     orientations = new List <HandleOrientation>(this.GetComponentsInChildren <HandleOrientation>());
     if (orientations.Count == 0)
     {
         if (allowedOrientations.Count > 0)
         {
             foreach (Orientation allowedOrientation in allowedOrientations)
             {
                 if (allowedOrientation.allowedHand == HandSide.Both || allowedOrientation.allowedHand == HandSide.Right)
                 {
                     HandleOrientation handleOrientation = AddOrientation(Side.Right, allowedOrientation.positionOffset, Quaternion.Euler(allowedOrientation.rotation));
                     if (orientationDefaultRight == null && (allowedOrientation.isDefault == HandSide.Both || allowedOrientation.isDefault == HandSide.Right))
                     {
                         orientationDefaultRight = handleOrientation;
                     }
                 }
                 if (allowedOrientation.allowedHand == HandSide.Both || allowedOrientation.allowedHand == HandSide.Left)
                 {
                     HandleOrientation handleOrientation = AddOrientation(Side.Left, allowedOrientation.positionOffset, Quaternion.Euler(allowedOrientation.rotation));
                     if (orientationDefaultLeft == null && (allowedOrientation.isDefault == HandSide.Both || allowedOrientation.isDefault == HandSide.Left))
                     {
                         orientationDefaultLeft = handleOrientation;
                     }
                 }
             }
         }
         else
         {
             if (!Application.isPlaying)
             {
                 orientationDefaultRight = AddOrientation(Side.Right, Vector3.zero, Quaternion.identity);
                 orientationDefaultLeft  = AddOrientation(Side.Left, Vector3.zero, Quaternion.identity);
             }
         }
     }
 }
예제 #3
0
            public void AlignTestItem()
            {
                if (grabItem)
                {
                    grabHandle = ragdollHand.side == Side.Right ? grabItem.mainHandleRight : grabItem.mainHandleLeft;
                }
                if (!grabHandle)
                {
                    return;
                }

                if (previousHandle && previousHandle != grabHandle)
                {
                    Item previousItem = previousHandle.GetComponentInParent <Item>();
                    if (previousItem)
                    {
                        previousItem.transform.localPosition = new Vector3(0, 100, 0);
                        previousItem.transform.localRotation = Quaternion.identity;
                    }
                    else
                    {
                        previousHandle.transform.root.localPosition = new Vector3(0, 100, 0);
                        previousHandle.transform.root.localRotation = Quaternion.identity;
                    }
                }

                if (useGripFrom == Grip.OpenPose)
                {
                    ragdollHand.SetGripFromOpenPose();
                }
                else if (useGripFrom == Grip.ClosePose)
                {
                    ragdollHand.SetGripFromClosePose();
                }

                grabHandle.CheckOrientations();
                HandleOrientation handleOrientation = grabHandle.orientationDefaultRight;

                if (ragdollHand.side == Side.Left)
                {
                    handleOrientation = grabHandle.orientationDefaultLeft;
                }

                if (handleOrientationIndex > 0)
                {
                    int i = 0;
                    foreach (HandleOrientation ho in grabHandle.orientations)
                    {
                        if (grabHandle.orientationDefaultLeft == ho)
                        {
                            continue;
                        }
                        if (grabHandle.orientationDefaultRight == ho)
                        {
                            continue;
                        }
                        if (ho.side == ragdollHand.side)
                        {
                            if ((handleOrientationIndex - 1) == i)
                            {
                                handleOrientation = ho;
                            }
                            i++;
                        }
                    }
                }

                Item      item        = grabHandle.GetComponentInParent <Item>();
                Transform alignObject = grabHandle.transform.root;

                if (item != null)
                {
                    alignObject = item.transform;
                }

                Transform objectGrip = new GameObject("ObjectGrip").transform;

                objectGrip.SetParent(item ? item.transform : grabHandle.transform.root);
                objectGrip.position = handleOrientation.transform.position + (handleOrientation.handle.transform.up * handleOrientation.handle.GetDefaultAxisLocalPosition());
                objectGrip.rotation = handleOrientation.transform.rotation;

                alignObject.MoveAlign(objectGrip, ragdollHand.grip.position, ragdollHand.grip.rotation);

                DestroyImmediate(objectGrip.gameObject);
                previousHandle = grabHandle;
            }
예제 #4
0
        public void AlignObject()
        {
            Animator  animator      = this.GetComponentInParent <Animator>();
            Transform rightHandBone = animator.GetBoneTransform(HumanBodyBones.RightHand);
            Transform leftHandBone  = animator.GetBoneTransform(HumanBodyBones.LeftHand);

            if (this.transform.parent.parent != rightHandBone)
            {
                Debug.LogError("HandPoseDefinition is not correctly placed, should be HandRightBone/BodyHand/HandPoseDefinition");
                return;
            }

            handleReference.CheckOrientations();
            HandleOrientation handleOrientation = handleReference.orientationDefaultRight;

            if (side == Side.Left)
            {
                handleOrientation = handleReference.orientationDefaultLeft;
            }

            if (handleOrientationIndex > 0)
            {
                int i = 0;
                foreach (HandleOrientation ho in handleReference.orientations)
                {
                    if (handleReference.orientationDefaultLeft == ho)
                    {
                        continue;
                    }
                    if (handleReference.orientationDefaultRight == ho)
                    {
                        continue;
                    }
                    if (ho.side == side)
                    {
                        if ((handleOrientationIndex - 1) == i)
                        {
                            handleOrientation = ho;
                        }
                        i++;
                    }
                }
            }

            if (handleOrientation)
            {
                ItemDefinition objectDefinition = handleReference.GetComponentInParent <ItemDefinition>();

                Transform objectGrip = new GameObject("ObjectGrip").transform;
                objectGrip.SetParent(objectDefinition ? objectDefinition.transform : handleReference.transform.root);
                objectGrip.position = handleOrientation.transform.position + (handleOrientation.handleDefinition.transform.up * handleOrientation.handleDefinition.GetDefaultAxisLocalPosition());
                objectGrip.rotation = handleOrientation.transform.rotation;

                Transform alignObject = handleReference.transform.root;
                if (objectDefinition != null)
                {
                    alignObject = objectDefinition.transform;
                }

                Transform handGripRight = this.transform.parent.Find("HandGrip");
                if (!handGripRight)
                {
                    handGripRight = new GameObject("HandGrip").transform;
                    handGripRight.SetParent(this.transform.parent);
                }
                handGripRight.localPosition = gripLocalPosition;
                handGripRight.localRotation = Quaternion.Euler(gripLocalRotation);

                if (side == Side.Right)
                {
                    alignObject.MoveAlign(objectGrip, handGripRight.position, handGripRight.rotation);
                }
                else
                {
                    Transform bodyHandLeft = leftHandBone.Find("BodyHand");
                    if (!bodyHandLeft)
                    {
                        bodyHandLeft = new GameObject("BodyHand").transform;
                        bodyHandLeft.SetParent(leftHandBone);
                        bodyHandLeft.localPosition = Vector3.zero;
                        bodyHandLeft.localRotation = Quaternion.Euler(90, 0, 0);
                    }
                    Transform handGripLeft = bodyHandLeft.Find("HandGrip");
                    if (!handGripLeft)
                    {
                        handGripLeft = new GameObject("HandGrip").transform;
                        handGripLeft.SetParent(bodyHandLeft);
                    }
                    handGripLeft.localPosition = gripLocalPosition;
                    handGripLeft.localRotation = Quaternion.Euler(gripLocalRotation);
                    handGripLeft.MirrorRelativeToParent(new Vector3(-1, 1, 1));
                    handGripLeft.localScale = Vector3.one;
                    alignObject.MoveAlign(objectGrip, handGripLeft.position, handGripLeft.rotation);
                }

                DestroyImmediate(objectGrip.gameObject);
            }
            else
            {
                Debug.LogError("No orientation set on the handle!");
            }
        }