使用 Kinect SDK 设置手控光标

Set a cursor hand controlled with Kinect SDK

我正在开发一个加载和修改分子的基本应用程序,我的主要目标是实现 Kinect 来完成它。 到目前为止,我已经能够用鼠标来完成,我想用右手来完成。 搜索了一段时间后,我尝试在屏幕环境中定义我的光标,就像用鼠标 (Input.mousePosition.x) 代替手坐标(针对屏幕尺寸进行标准化)一样。 我不能让光标跟随我的手,但我可以让游戏 object 跟随我的 body 的任何部分所以我的坐标被导入并且当我调试我的控制变量时它们 return 屏幕尺寸中包含的值。 我想我的错误在 OnGUI() 中。 谁能body帮帮我

提前致谢。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class handPointer_01 : MonoBehaviour
{
    public Texture2D cursorImage_00;
    public Texture2D cursorImage_01;
    public Texture2D cursorImage_02;
    private Texture2D cursorImage;
    private int cursorWidth = 16;
    private int cursorHeight = 16;
    private string defaultResource = "MousePointer";
    private GameObject target;

    public GameObject leftHandPos;
    public GameObject rightHandPos;
    public GameObject leftShoulderPos;
    public GameObject rightShoulderPos;
    public GameObject leftHipPos;
    public GameObject rightHipPos;

    private float RightHandX;
    private float RightHandY;
    private float xPrevious;
    private float yPrevious;
    private double MoveThreshold = 0.01;
    void Start()
    {
        //Switch off default cursor

        if(!cursorImage_00 || !cursorImage_01 || !cursorImage_02)
        {
            cursorImage = (Texture2D) Resources.Load(defaultResource);
            Debug.Log(cursorImage);
        }
        else Cursor.visible = false;
        //cursorImage = (Texture2D) Instantiate(cursorImage);
    }
    void Update()
    {

        if (rightShoulderPos.transform.position.z - rightHandPos.transform.position.z > 0.01)
        {
            float xScaled = Mathf.Abs((rightHandPos.transform.position.x - rightShoulderPos.transform.position.x) / ((rightShoulderPos.transform.position.x - leftShoulderPos.transform.position.x) * 2)) * Screen.width;
            float yScaled = Mathf.Abs((rightHandPos.transform.position.y - rightHipPos.transform.position.y) / ((rightShoulderPos.transform.position.y - rightHipPos.transform.position.y) * 2)) * Screen.height;

            // the hand has moved enough to update screen position (jitter control / smoothing)
            if (Mathf.Abs(rightHandPos.transform.position.x - xPrevious) > MoveThreshold || Mathf.Abs(rightHandPos.transform.position.y - yPrevious) > MoveThreshold)
            {
                RightHandX = Mathf.Min(Mathf.Max(xScaled,Screen.width),0);
                RightHandY = Mathf.Min(Mathf.Max(yScaled,Screen.height),0);

                xPrevious = rightHandPos.transform.position.x;
                yPrevious = rightHandPos.transform.position.y;

                // reset the tracking timer
                //trackingTimerCounter = 10;
            }
        }

        // // Get the left mouse button
        // if(Input.GetMouseButtonDown(0))
        // {          
            // RaycastHit hitInfo;
            // target = GetClickedObject (out hitInfo);

            // if (target != null && target.gameObject.tag =="Draggable")
            // {
                // cursorImage = cursorImage_02;
                // Debug.Log("Hit");
            // }
            // else
            // {
                // cursorImage = cursorImage_01;
                // Debug.Log("Miss");
            // }
        // }

        // // Disable movements on button release
        // if (!Input.GetMouseButton(0))
        // {
            // cursorImage = cursorImage_00;
        // }
        cursorImage = cursorImage_00;
    }

    void OnGUI()
    {
        //GUI.DrawTexture(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, cursorWidth, cursorHeight), cursorImage);
        GUI.DrawTexture(new Rect(RightHandX ,RightHandY , cursorWidth, cursorHeight), cursorImage);
    }

    GameObject GetClickedObject (out RaycastHit hit)
    {
        GameObject target = null;
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        if (Physics.Raycast (ray.origin, ray.direction * 10, out hit)) {
            target = hit.collider.gameObject;
        }
        return target;
    }
}

我已经设法找到基于试错的解决方案。 :) 删除了额外的代码(注释代码)以避免误解。 可能感兴趣的人:

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

public class HandModifier_03 : MonoBehaviour
{
 //Cursor Variables
 public Texture2D cursorImage_00;
 private Texture2D r_cursorImage;

 private int cursorWidth = 32;
 private int cursorHeight = 32;
 private string defaultResource = "MousePointer";

 //Hand Variables
 public Vector3 screenSpace;
 private Vector3 last_right;

 public Transform referenceCamera; //Camera that acts as a point of view to act on the object relative to.

 // Kinect data receiver
 public UDPReceive receiver;

 private Vector3 headPos; 
 private Vector3 leftHandPos;
 //private Vector3 leftWristPos;
 //private Vector3 leftElbowPos;
 private Vector3 leftShoulderPos;

 private Vector3 rightHandPos;
 //private Vector3 rightWristPos;
 //private Vector3 rightElbowPos;
 private Vector3 rightShoulderPos;

 //private Vector3 leftThumbPos;
 //private Vector3 rightThumbPos;
 //private Vector3 leftHandTipPos;
 //private Vector3 rightHandTipPos;

 private Vector3 leftHipPos;
 private Vector3 rightHipPos;

 private string lHandState;
 private string rHandState;

 //Cursor auxiliar variables
 private float xScaled;
 private float yScaled;  

 private Vector2 rHandScreen;

 void Start()
 {
  r_cursorImage = cursorImage_00;
 }

 // Update is called once per frame
 void Update ()
 {
  //Coordinate Update
  headPos = receiver.headPos;

  leftHandPos = receiver.leftHandPos;
  //leftWristPos = receiver.leftWristPos;
  //  leftElbowPos = receiver.leftElbowPos;
  leftShoulderPos = receiver.leftShoulderPos;

  rightHandPos = receiver.rightHandPos;
  //  rightWristPos = receiver.rightWristPos;
  //  rightElbowPos = receiver.rightElbowPos;
  rightShoulderPos = receiver.rightShoulderPos;

  //  leftThumbPos = receiver.leftThumbPos;
  //  rightThumbPos = receiver.rightThumbPos;
  //  leftHandTipPos = receiver.leftHandTipPos;
  //  rightHandTipPos = receiver.rightHandTipPos;

  lHandState = receiver.lHandState;
  rHandState = receiver.rHandState;


  leftHipPos = receiver.leftHipPos;
  rightHipPos = receiver.rightHipPos;

  //Right Hand Screen Position   
  xScaled = Mathf.Abs ((rightHandPos.x - rightShoulderPos.x)) / Mathf.Abs ((rightShoulderPos.x - leftShoulderPos.x) * 1.75f) * Screen.width;
  yScaled = Mathf.Abs ((rightHandPos.y - rightHipPos.y)) / Mathf.Abs ((rightShoulderPos.y - rightHipPos.y) * 2) * Screen.height;


  xScaled = Mathf.Max (Mathf.Min (xScaled, Screen.width * 0.98f), 0.02f);
  yScaled = Screen.height - Mathf.Max (Mathf.Min (yScaled, Screen.height * 0.98f), 0.02f); // Subtracting Screen Height required

  Vector2 rHandScreen = new Vector2 (xScaled, yScaled);

  //  Vector2 rHandScreen = HandOnScreenPosition (out rightHandPos, leftShoulderPos, rightShoulderPos, rightHipPos);

  //  UnityEngine.Debug.Log(_mode);


 }
  
 void OnGUI() 
 {
  //GUI.DrawTexture(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, cursorWidth, cursorHeight), cursorImage);
  GUI.DrawTexture(new Rect(xScaled ,yScaled , cursorWidth, cursorHeight), r_cursorImage);
 }

}