在Unity3D中使用DV&Kinect

kinect的就是去看看脚本,基本用到的也就是些get开头的方法

 If you need to invoke a public function from KinectManager in your scripts, use the following line to get reference to KinectManager: ‘ K inectManager kinectManager = KinectManager.Instance ;’. You can get reference to the the InteractionManager, SpeechManager or FacetrackingManager in the same way. 

1.kinect 
1.KinectManager 主要获取人体信息,坐标变化,深度变化,显示图信息。
调度Kinect的dll文件运行Kinect和处理人体动作
2.gesturesListener 根据相关动作执行相关函数跳转。
监听人体动作,做出指定行为

2.Kinect Camera and AVPro Live Camera 
1.添加AVPro Live Camera Manager and Kinect Manager 到Main Camera.其中Compute Color Map需要勾选,因为需要调用Kinect摄取的彩色图像。
  
2.制作一个板子Quad ,使其材质(命名为KinectClrTex)

3.附加脚本文件(KinectBG)
using UnityEngine;
using System.Collections;

//第三幕显示kinect相机画面
public class KinectBG : MonoBehaviour {

 
 // Update is called once per frame
 void Update () {
        renderer.material.mainTexture = KinectManager.Instance. GetUsersClrTex();
 }
}
4.为使板子适配屏幕尺寸比例,也需要附加脚本(FitScreen)
using UnityEngine;
using System.Collections;

//Quad 自动适配屏幕大小
public class FitScreen : MonoBehaviour {

    //对应相机
    public Camera cam;
    //距相机位置距离
    public float distance;
    //分辨率
    public Vector2 aspect;
    //是否使用屏幕分辨率,开启则忽视上一项分辨率设定
    public bool useScreenAspect;

    // Use this for initialization
    void Start () {
        float height = 0f;
        float width = 0f;
        if (useScreenAspect)
        {
            //根据相机模式计算高度(正交,投影)
            if (cam.orthographic)
            {
                height = cam.orthographicSize * 2.01f;
            }
            else
            {
                height = Mathf.Tan(cam.fieldOfView / 360f * Mathf.PI) * distance * 1.01f * 2f;
            }

            width = (float)Screen.width / (float)Screen.height * height;
            transform.localPosition = new Vector3(0f, 0f, distance);
            //使Quad暂时脱离父物体,修正因父物体累计的缩放因子造成的误差
            Transform parent = transform.parent;
            transform.parent = null;
            transform.localScale = new Vector3(width, height, 1f);
            transform.parent = parent;
        }
        else
        {
            //根据视频长宽比与屏幕长宽比的对比选择不同的计算方式
            if (((float)Screen.width / (float)Screen.height) > (aspect.x / aspect.y))
            {
                if (cam.orthographic)
                {
                    width = cam.orthographicSize * 2.01f * (float)Screen.width / (float)Screen.height;
                }
                else
                {
                    width = Mathf.Tan(cam.fieldOfView / 360f * Mathf.PI) * distance * 1.01f * 2f * (float)Screen.width / (float)Screen.height;
                }

                height = width * aspect.y / aspect.x;
                transform.localPosition = new Vector3(0f, 0f, distance);
                Transform parent = transform.parent;
                transform.parent = null;
                transform.localScale = new Vector3(width, height, 1f);
                transform.parent = parent;
            }
            else
            {
                if (cam.orthographic)
                {
                    height = cam.orthographicSize * 2.01f;
                }
                else
                {
                    height = Mathf.Tan(cam.fieldOfView / 360f * Mathf.PI) * distance * 1.01f * 2f;
                }

                width = aspect.x / aspect.y * height;
                transform.localPosition = new Vector3(0f, 0f, distance);
                Transform parent = transform.parent;
                transform.parent = null;
                transform.localScale = new Vector3(width, height, 1f);
                transform.parent = parent;
            }
        }
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}


quad自动适配屏幕大小,相机模式(正交,投影),视频长宽比与屏幕长宽比。 PhotoShooting.txt
3.Kinect控制图标手柄移动。
1.KinectManager跟MainCamera有联系,有MainCamera才会有KinectManager的实例   KinectManager m_KinectManagerInstance =   KinectManager.Instance;
2.MainCamera 的属性Projection且是Orthographic,调整Clipping Planes.

4.Capture ScreenShoot
using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class CaptureCamera : MonoBehaviour {
    public Texture2D screenshoot;
    public byte[] pngData;
 
 // Use this for initialization
 void Start () {
 }
 
 // Update is called once per frame
 void Update () {
       
        if (Input.GetKeyDown(KeyCode.Mouse1))
        {
              WatchPhoto();     
        }          
 }
    void TakePhoto()
    {
        StartCoroutine(OnTakePhoto());
   
    }
    //Screen shoot
    IEnumerator OnTakePhoto()
    {

         yield return new WaitForEndOfFrame();
        screenshoot = new Texture2D(Screen.width,Screen.height);
        screenshoot.ReadPixels(new Rect(0f, 0f, Screen.width, Screen.height), 0, 0);
        screenshoot.Apply();
          pngData = screenshoot.EncodeToPNG();
       
       
    }
    void ShowCapture(Texture2D photo)
    {
        renderer.material.mainTexture = photo;
        renderer.enabled = true;      
    }
    void WatchPhoto()
    {
        TakePhoto();
        ShowCapture(screenshoot);
    }
}
1.其中 pngData = screenshoot.EncodeToPNG(); 功能是生成PNG图片。 其中pngData算出来PNG数据数组很大,会导致Unity卡顿,注意关掉其下拉列表。
2.material的Shader为Unlit/Texture
3.yield return new WaitForEndOfFrame();完成一帧的渲染,再截图。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值