1. 程式人生 > >在Kinect體感裝置中檢測UI或者2D物體的方法(與追蹤判斷圖片位置思路不同)

在Kinect體感裝置中檢測UI或者2D物體的方法(與追蹤判斷圖片位置思路不同)

第一步:建立一個工程,匯入好kinect sdk ,做好前期準備工作;

第二步:找到Main Camera或者其他的只要是渲染UI的相機即可,將其Projection修改為Orthography。如圖位置

第三步:建立一個按鈕,設定tag為“button”,並繫結Box Collieder 2D。

第四步:修改Canvas的Cavas元件中的Render Mode為”Screen Space-Camera“,併為其tian新增Render Camera。如圖所示:

第五步:直接上程式碼了,註釋不多,只有射線部分有點,勉強看吧,相信kinect追蹤大家能看懂的,都是直接套用。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class manager : MonoBehaviour {
    [Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")]
    public int player1Index = 0;
    public int player2Index = 1;
   
    [Tooltip("Smooth factor for cursor movement.")]
    public float smoothFactor = 10f;
    bool CastRay = false;

    [Tooltip("Camera that will be used to overlay the 3D-objects over the background.")]
    public Camera foregroundCamera;

    public GameObject[] firstPeopleHands;

    // Use this for initialization
    void Start() {

    }

    // Update is called once per frame
    void Update()
    {
        KinectManager manager = KinectManager.Instance;
        if (manager && manager.IsInitialized())
        {

            int rightHandIndex = (int)KinectInterop.JointType.HandRight;
            int leftHandIndex = (int)KinectInterop.JointType.HandLeft;

            if (manager.IsUserDetected())
            {
              
                List<int> indexList = manager.GetTrackedBodyIndices();
              
                if (indexList.Count == 1)//One people
                {
                    long userId_1 = manager.GetUserIdByIndex(player1Index);
                    if (manager.IsJointTracked(userId_1, rightHandIndex))
                    {
                        Vector3 rightHandRawPos = manager.GetJointKinectPosition(userId_1, rightHandIndex);
                        Vector3 leftHandRawPos = manager.GetJointKinectPosition(userId_1, leftHandIndex);
                        
                        if (rightHandRawPos != Vector3.zero)
                        {
                            CastRay = true;
                           
                            if (CastRay)
                            {//獲取追蹤 並進行ui檢測
                                Rect backgroundRect = foregroundCamera.pixelRect;
                                Vector3 posJointRightHand = manager.GetJointPosColorOverlay(userId_1, rightHandIndex, foregroundCamera, backgroundRect);
                                Vector3 posJointLeftHand = manager.GetJointPosColorOverlay(userId_1,
                                    (int)KinectInterop.JointType.HandLeft, foregroundCamera, backgroundRect);
                                firstPeopleHands[0].transform.localPosition = posJointRightHand;
                                firstPeopleHands[1].transform.localPosition = posJointLeftHand;

                                CheckRay(posJointRightHand);                         

                            }
                        }
                    }
                }
            }
        }
    }


    bool ison=false;
    private void CheckRay(Vector3 startPos)
    {//右手點選UI       
        Vector2 mousePos2D = new Vector2(startPos.x,startPos.y);
        RaycastHit2D hits = Physics2D.Raycast(mousePos2D, Vector2.up);
        if (hits.collider!=null&& hits.collider.gameObject.tag == "button")
        {
            if (ison==false)
            {
                ison = true;
                print("點選了按鈕!!!!");           
            }     
        }



    }
}

另外閒扯一下(嘿嘿):

接觸Kinect時間不長,希望能多多與大家交流,學習之路還很漫長,菜鳥在路上,向那些大佬膜拜,希望大家也能多分享,為了大家學習,少才一些坑,推薦一個qq交流群吧,身邊一個大佬的:806091680,進來互相交流經驗,共同進步