欢迎使用CSDN-markdown编辑器

如何实现一个人走近体感摄像头,物体变大,远离体感摄像头,物体变小呢??如果做出这样的控制,那么就会比用手势来缩放物体更能体现科技含量。
相信很多人有很多的做法,我就用最简单的方式,深度数据。 在这里说几句题外话,体感的精髓并不是彩色数据流,而是深度数据流,这里面包含了太多你有用的东西。

好了,思路如下:
首先定义两个变量,一个是 记录上一次的 人身体的深度数据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 就写到这里。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值