unity与Kinect开发之3d物体跟随人物指定关节匹配移动

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


//Kinect跟随的实现
//如何准确让物体跟随图像的手
//在场景新建一个空物体名为KinectManager,
//挂在组件KinectManager 修改Compute User Map为BodyTexture,勾选Compute Color Map
//挂在组件BackgroundRemovalManager  设置ForegroundCamera为主相机,取消勾选ColorCameraResolution
//新建一个材质,修改shader为Unlit/transparent
//设置参数为  Tiling  x 1.5     Y -1
//            Offset  X -0.25   Y  1
//新建一个canvas  修改RenderMode 为  ScreenSpace-Camera ,然后挂载此组件
//新建一个RawImage  将新建的材质赋给他,


public class CanvasHandTrack : MonoBehaviour {
    [SerializeField] private Camera camera;//必须是主相机
    [SerializeField] private RawImage humanImage;
    private float width = 1920f;
    private float high = 1080f;

    private float depthWidth = 512f;
//Debug.Log("11" + mBackgroundRemovalManager.GetForegroundTex().width);
    private float depthHigh = 424f; 
//Debug.Log("11" + mBackgroundRemovalManager.GetForegroundTex().height);
    //512/424=1.207   需要保持图片的比例一致

    private KinectManager kinectManager;
    private long userID;
    private Rect rect = new Rect(307.92f, 1080f, 1304.15f, -1080f);
   //转换的目标尺寸
            //1304.15/1080=1.207
                      //(x:-960.00, y:-540.00, width:1920.00, height:1080.00)
    private BackgroundRemovalManager mBackgroundRemovalManager;
    private void Awake()
    {
        //固定参数很重要
        rect = new Rect(Screen.width * 0.16f, Screen.height, Screen.width * 0.67924f, -Screen.height);
    }
    // Use this for initialization
    void Start () {
        kinectManager = KinectManager.Instance;
        mBackgroundRemovalManager = BackgroundRemovalManager.Instance;
    }
	
	// Update is called once per frame
	void Update () {
        if (kinectManager.IsInitialized())
        {
            SetHand(kinectManager.GetUserIdByIndex(0), hand1, KinectInterop.JointType.HandRight, out rightHandCameraPos1st);
            SetHand(kinectManager.GetUserIdByIndex(0), hand2, KinectInterop.JointType.HandLeft, out lefthandCameraPos1st);

            SetHumanImage();
        }
    }
    [SerializeField] private Transform hand1;
    [SerializeField] private Transform hand2;
    private Vector3 rightHandCameraPos1st;
    private Vector3 lefthandCameraPos1st;
    private Vector3 rightHandCameraPos2nd;
    private Vector3 lefthandCameraPos2nd;private void SetHand(long userID, Transform hand, KinectInterop.JointType jointType, out Vector3 cameraPos)
    {
        Vector3 point;
        point = kinectManager.GetJointPosDepthOverlay(userID, (int)jointType, camera, rect);
        point = camera.WorldToScreenPoint(point);//世界坐标转成屏幕坐标
        point.z = 0;//固定z值
        point.y = Screen.height - point.y;//翻转Y值
        cameraPos = point;
        point.z = transform.position.z;
        point = camera.ScreenToWorldPoint(point);//屏幕坐标转成世界坐标
        point.z = transform.position.z;
        hand.position = point;
    }

    //获取人物图片
    private void SetHumanImage()
    {
        humanImage.texture = mBackgroundRemovalManager.GetForegroundTex();

    }
}

如何移动图片之后仍然跟随移动

//移动控制图片的时候需要将手的匹配位置也要移动
public class WorldHandController : MonoBehaviour
{
    [SerializeField] private Transform rightHand1st;
    [SerializeField] private Transform leftHand1st;
    [SerializeField] private Transform CucumberFactoryTrans;
    [SerializeField] private CanvasHandTrack canvasHandTrack;
    [SerializeField] private Transform humanImageTrans;
    private Camera mCamera;
    private float initY;
    void Start()
    {
        mCamera = Camera.main;
        initY = humanImageTrans.position.y;
    }
    //此脚本的作用就是固定手的Z值
    void Update()
    {
        Vector3 right1stTempPoint = canvasHandTrack.RightHandCameraPos1st;
        Vector3 left1stTempPoint = canvasHandTrack.LefthandCameraPos1st;   

        //固定Z值平面
        left1stTempPoint.z = right1stTempPoint.z
            = CucumberFactoryTrans.position.z;

        right1stTempPoint = mCamera.ScreenToWorldPoint(right1stTempPoint);
        left1stTempPoint = mCamera.ScreenToWorldPoint(left1stTempPoint);
     
        //固定Z值平面
        left1stTempPoint.z = right1stTempPoint.z
            = CucumberFactoryTrans.position.z;

        //在移动人物图片上下的时候,保持手的Y值跟随没问题
        right1stTempPoint.y -= (initY - humanImageTrans.position.y);
        //Debug.Log(" right1stTempPoint.y:"+(initY - humanImageTrans.position.y));
        left1stTempPoint.y -= (initY - humanImageTrans.position.y);
        //Debug.Log("  left1stTempPoint.y" + (initY - humanImageTrans.position.y));

        rightHand1st.position = right1stTempPoint;
        leftHand1st.position = left1stTempPoint;
    }
}

看不到请联系我qq1186908998

你可能感兴趣的:(总栏目)