多媒体互动:用Unity开发体感接物

需求:挥手或者翻页笔下开启下一页 左右移动让小人左右移动接道具,用户在30s内吃道具,道具总共有7种类型,每种类型吃了后 点亮一个图片。

sdk:kinectv2

挥手核心逻辑 使用KinectGestures.Gestures.SwipeDown

public class PlayerGestureListener : Singleton<PlayerGestureListener>, KinectGestures.GestureListenerInterface
{

    public bool IsSwipeDown()
    {
        if (swipeDown)
        {
            swipeDown = false;
            return true;
        }
        return false;
    }

    public bool swipeDown = false;

    private bool progressDisplayed;
    private float progressGestureTime;


    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (progressDisplayed && ((Time.realtimeSinceStartup - progressGestureTime) > 0.05f))
        {
            progressDisplayed = false;

            Debug.Log("Forced progress to end.");
        }
    }


    public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint)
    {
        if (progressDisplayed)
        {
            progressDisplayed = false;
        }


        return true;
    }

    public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos)
    {
         if (gesture == KinectGestures.Gestures.SwipeDown)
            swipeDown = true;

        return true;
    }

    public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture, float progress, KinectInterop.JointType joint, Vector3 screenPos)
    {
        if ((gesture == KinectGestures.Gestures.ZoomOut || gesture == KinectGestures.Gestures.ZoomIn) && progress > 0.5f)
        {
                progressDisplayed = true;
                progressGestureTime = Time.realtimeSinceStartup;
        }
        else if ((gesture == KinectGestures.Gestures.Wheel || gesture == KinectGestures.Gestures.LeanLeft || gesture == KinectGestures.Gestures.LeanRight ||
            gesture == KinectGestures.Gestures.LeanForward || gesture == KinectGestures.Gestures.LeanBack) && progress > 0.5f)
        {

                progressDisplayed = true;
                progressGestureTime = Time.realtimeSinceStartup;
        }
        else if (gesture == KinectGestures.Gestures.Run && progress > 0.5f)
        {
                progressDisplayed = true;
                progressGestureTime = Time.realtimeSinceStartup;
        }
    }

    public void UserDetected(long userId, int userIndex)
    {
        KinectManager manager = KinectManager.Instance;
        manager.DetectGesture(userId, KinectGestures.Gestures.SwipeDown);
    }

    public void UserLost(long userId, int userIndex)
    {
        Debug.Log("UserLost");
    }

   
}
 

左右移动的核心逻辑:

  void Update()
    {
        KinectManager manager = KinectManager.Instance;

        if (manager && manager.IsInitialized() && foregroundCamera)
        {
            Rect backgroundRect = foregroundCamera.pixelRect;
            PortraitBackground portraitBack = PortraitBackground.Instance;

            if (portraitBack && portraitBack.enabled)
            {
                backgroundRect = portraitBack.GetBackgroundRect();
            }

            int joint = (int)KinectInterop.JointType.SpineMid;
            // overlay all joints in the skeleton
            if (manager.IsUserDetected())
            {
                long userId = manager.GetUserIdByIndex(playerIndex);

                if (manager.IsJointTracked(userId, joint))
                {
                    posJoint = manager.GetJointPosColorOverlay(userId, joint, foregroundCamera, backgroundRect);
                    //posJoint = manager.GetJointKinectPosition(userId, joint);
                    //posJoint = manager.GetJointPosition(userId, joint);
                    //Debug.Log("GetJointPosition:" + posJoint);
                    //Debug.Log("转换成屏幕坐标:" + Camera.main.WorldToScreenPoint(posJoint));

                    //Debug.Log("GetJointPosition:" + manager.GetJointPosition(userId, joint));
                    //Debug.Log("GetJointKinectPosition:" + manager.GetJointKinectPosition(userId, joint));
                    //Debug.Log("GetJointPosColorOverlay:" + manager.GetJointPosColorOverlay(userId, joint, foregroundCamera, backgroundRect));
                    //Debug.Log("GetJointPosDepthOverlay:" + manager.GetJointPosDepthOverlay(userId, joint, foregroundCamera, backgroundRect));

                    if (posJoint != Vector3.zero)
                    {
                        Vector3 screenPoint= Camera.main.WorldToScreenPoint(posJoint);

                        rectTran.anchoredPosition=new Vector2(screenPoint.x, -352F);
                    }

                }

            }

        }

        if(Input.GetKey(KeyCode.UpArrow))
        {
            float x=rectTran.anchoredPosition.x;
            x -= 10;
            x = Mathf.Clamp(x,44,700);
            rectTran.anchoredPosition = new Vector2(x, rectTran.anchoredPosition.y);
        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            float x = rectTran.anchoredPosition.x;
            x += 10;
            x = Mathf.Clamp(x, 44, 700);
            rectTran.anchoredPosition = new Vector2(x, rectTran.anchoredPosition.y);
        }


    }

 

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Unity互动投影(TUIO)是一种集成在Unity引擎中的交互式投影解决方案。TUIO可以识别和跟踪使用者的手势,借助投影设备将图像投影到真实世界的物体表面上。 使用Unity互动投影可以创建各种令人惊叹的互动投影项目。在Unity开发环境中,可以通过添加TUIO插件来实现不同手势的识别和追踪,例如手指点击、拖动、缩放等。这些手势可以用于操控投影表面上的虚拟对象,创造出与物体互动的效果。 Unity互动投影的应用场景非常广泛。它可以用于展览、商业演示、游戏娱乐等领域。例如,在一个展览中,使用者可以通过手势在投影表面上遥控操作一个虚拟汽车,感受驾驶的乐趣;在商业演示中,使用者可以通过手势操作虚拟产品模型,展示其功能和特点;在游戏娱乐中,玩家可以通过手势控制角色在投影表面上移动和攻击敌人。 Unity互动投影的优势在于其简单易用和灵活性。借助Unity引擎的强大功能和可视化编辑器,开发者可以轻松创建交互式投影项目,并根据需要进行定制和扩展。同时,Unity互动投影也支持多点触控和多人同时互动,使得用户可以在同一个投影表面上共同操作。 总而言之,Unity互动投影(TUIO)是一种强大的交互式投影解决方案,可通过Unity引擎实现各种令人惊叹的投影项目。它可以广泛应用于展览、商业演示和游戏娱乐等领域,为用户提供独特、沉浸式的互动体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值