设置用Kinect SDK手控的光标
本文关键字:光标 SDK Kinect 设置 | 更新日期: 2023-09-27 18:05:15
我正在开发一个基本的应用程序来加载和修改分子,我的主要目标是实现Kinect来做到这一点。到目前为止,我已经可以用鼠标来做了,我想用我的右手来做。搜索了一段时间后,我尝试在屏幕环境中定义光标,就像用鼠标(Input.mousePosition.x)代替手坐标(为屏幕尺寸标准化)一样。我不能让光标跟随我的手,但我可以让游戏对象跟随我身体的任何部分,这样我的坐标就会被导入,当我调试控制变量时,它们会返回一个包含在屏幕尺寸中的值。我想我的错误是在OnGUI()。有谁能帮帮我吗?
提前感谢。
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);
}
}