欢迎使用CSDN-markdown编辑器
2017-11-11 11:37
295 查看
如何实现一个人走近体感摄像头,物体变大,远离体感摄像头,物体变小呢??如果做出这样的控制,那么就会比用手势来缩放物体更能体现科技含量。
相信很多人有很多的做法,我就用最简单的方式,深度数据。 在这里说几句题外话,体感的精髓并不是彩色数据流,而是深度数据流,这里面包含了太多你有用的东西。
好了,思路如下:
首先定义两个变量,一个是 记录上一次的 人身体的深度数据Z 轴值, 然后每一帧都用新获取的人身体的Z轴深度数据值与老的比较,如果小于0 那就是靠近了,如果大于0,那就远离。
这只是简单思路,如果要是做的平滑的缩放物体,还要做些优化。
代码如下:
using UnityEngine;
using System.Collections;
//using Windows.Kinect;
public class JointOverlayer : MonoBehaviour
{
[Tooltip(“GUI-texture used to display the color camera feed on the scene background.”)]
public GUITexture backgroundImage;
[Tooltip(“Camera that will be used to overlay the 3D-objects over the background.”)]
public Camera foregroundCamera;
}
这个代码是在 kinect 2.0 的小球跟着你的手移动的基础上修改的。 在做物体的缩放优化 这块,我借鉴了 Touch 用两个手指缩放物体的代码。 不得不说, 其实算法都是相同的。ok 就写到这里。
相信很多人有很多的做法,我就用最简单的方式,深度数据。 在这里说几句题外话,体感的精髓并不是彩色数据流,而是深度数据流,这里面包含了太多你有用的东西。
好了,思路如下:
首先定义两个变量,一个是 记录上一次的 人身体的深度数据Z 轴值, 然后每一帧都用新获取的人身体的Z轴深度数据值与老的比较,如果小于0 那就是靠近了,如果大于0,那就远离。
这只是简单思路,如果要是做的平滑的缩放物体,还要做些优化。
代码如下:
using UnityEngine;
using System.Collections;
//using Windows.Kinect;
public class JointOverlayer : MonoBehaviour
{
[Tooltip(“GUI-texture used to display the color camera feed on the scene background.”)]
public GUITexture backgroundImage;
[Tooltip(“Camera that will be used to overlay the 3D-objects over the background.”)]
public Camera foregroundCamera;
[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 playerIndex = 0; [Tooltip("Kinect joint that is going to be overlayed.")] public KinectInterop.JointType trackedJoint = KinectInterop.JointType.Head; [Tooltip("Game object used to overlay the joint.")] public Transform overlayObject; //public float smoothFactor = 10f; //public GUIText debugText; private Quaternion initialRotation = Quaternion.identity; private Vector3 oldeDepth; private Vector3 newDepth; void Start() { if(overlayObject) { // always mirrored initialRotation = Quaternion.Euler(new Vector3(0f, 180f, 0f)); overlayObject.rotation = Quaternion.identity; } } void Update () { KinectManager manager = KinectManager.Instance; if(manager && manager.IsInitialized() && foregroundCamera) { //backgroundImage.renderer.material.mainTexture = manager.GetUsersClrTex(); if(backgroundImage && (backgroundImage.texture == null)) { backgroundImage.texture = manager.GetUsersClrTex(); } // get the background rectangle (use the portrait background, if available) Rect backgroundRect = foregroundCamera.pixelRect; PortraitBackground portraitBack = PortraitBackground.Instance; if(portraitBack && portraitBack.enabled) { backgroundRect = portraitBack.GetBackgroundRect(); } // overlay the joint int iJointIndex = (int)trackedJoint; if(manager.IsUserDetected()) { long userId = manager.GetUserIdByIndex(playerIndex); if(manager.IsJointTracked(userId, iJointIndex)) { Vector3 posJoint = manager.GetJointPosColorOverlay(userId, iJointIndex, foregroundCamera, backgroundRect); newDepth = posJoint; //Debug.Log(newDepth.z - oldeDepth.z); //if(newDepth.z-oldeDepth.z<-0.1f) //{ // Debug.Log("near"); //} //else if(newDepth.z - oldeDepth.z > 0.1f) //{ // Debug.Log("far"); //} //else //{ // //Debug.Log("stay"); //} float offset = newDepth.z - oldeDepth.z; float scaleFactor = offset / 10f; Vector3 localScale = transform.localScale; Vector3 scale = new Vector3(localScale.x + scaleFactor, localScale.y + scaleFactor, localScale.z + scaleFactor); overlayObject.localScale = scale; oldeDepth = newDepth; //Debug.Log(posJoint.z); if (posJoint != Vector3.zero) { if(overlayObject) { overlayObject.position = posJoint; Quaternion rotJoint = manager.GetJointOrientation(userId, iJointIndex, false); rotJoint = initialRotation * rotJoint; overlayObject.rotation = rotJoint; } } } } } }
}
这个代码是在 kinect 2.0 的小球跟着你的手移动的基础上修改的。 在做物体的缩放优化 这块,我借鉴了 Touch 用两个手指缩放物体的代码。 不得不说, 其实算法都是相同的。ok 就写到这里。
相关文章推荐
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器