简体   繁体   English

设置用Kinect SDK控制的光标指针

[英]Set a cursor hand controlled with Kinect SDK

I'm developing a basic application to load and modify molecules and my main goal is to implement Kinect to do it. 我正在开发一个用于加载和修改分子的基本应用程序,而我的主要目标是实现Kinect来实现。 So far I've been able to do it with a mouse and I would like to do it with my right hand. 到目前为止,我已经能够使用鼠标来做到这一点,而我想用右手来做到。 After searching for a while I tried to define my cursor in a screen environment like if it would be done with a mouse (Input.mousePosition.x) substituting this for the hands coordinates (normalized for the screen dimensions). 搜索了一会儿之后,我尝试在屏幕环境中定义光标,例如是否可以使用鼠标(Input.mousePosition.x)将其替换为手坐标(针对屏幕尺寸进行标准化)。 I can't make the cursor follow my hand, but I can make a game object follow any part of my body so my coordinates are being imported and when I debug my control variables they return a value contained in the screen dimensions. 我不能使光标跟随我的手,但是我可以使游戏对象跟随我身体的任何部分,以便导入我的坐标,并且当我调试控制变量时,它们将返回包含在屏幕尺寸中的值。 I guess my error is in OnGUI(). 我猜我的错误是在OnGUI()中。 Can anybody help me. 有谁能够帮助我。

Thanks in advance. 提前致谢。

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;
    }
}

I have managed to find the sollution based on trial error attempts. 我设法根据尝试的错误尝试找到了解决方案。 :) Removed the extra code (commented code) to avoid misunderstandings. :)删除了多余的代码(带注释的代码),以免造成误解。 For whom it may interest: 它可能对谁感兴趣:

 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); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM