Esempio n. 1
0
    // Use this for initialization
    void Start()
    {
        //km = GetComponent<KinectManager>();
        km = camera.GetComponent <KinectManager>();
        //mrenderer = GetComponent<MeshRenderer>();
        meshrenderer = GetComponent <MeshRenderer>();
        meshfilter   = GetComponent <MeshFilter>();
        mesh         = new Mesh();
        Vector3 pos = new Vector3(0, 0, 0);

        meshfilter.mesh = mesh;

        depth_data    = km.GetRawDepthMap();
        canvas_height = KinectWrapper.GetDepthHeight() / 2;
        canvas_width  = KinectWrapper.GetDepthWidth() / 2;
        //numPoints = canvas_height * canvas_width;
        numPoints = canvas_height * canvas_width;


        meshrenderer.material = new Material(Shader.Find("Tim/PointShader"));
        //meshrenderer.SetWidth(0.02f, 0.02f); //thickness of line
        Color c_white = Color.white;
        Color c_red   = Color.red;


        for (int i = 0; i < canvas_width; i++)
        {
            for (int j = 0; j < canvas_height; j++)
            {
                vertices.Add(new Vector3(i * 0.01f, j * 0.01f, 0));
            }
        }

        points = new Vector3[numPoints];
        int[]   indicies = new int[numPoints];
        Color[] colors   = new Color[numPoints];
        for (int i = 0; i < points.Length; ++i)
        {
            points[i]   = new Vector3(UnityEngine.Random.Range(-10, 10), UnityEngine.Random.Range(-10, 10), UnityEngine.Random.Range(-10, 10));
            indicies[i] = i;
            colors[i]   = new Color(UnityEngine.Random.Range(0.0f, 1.0f), UnityEngine.Random.Range(0.0f, 1.0f), UnityEngine.Random.Range(0.0f, 1.0f), 1.0f);
            //mesh.normals[i] = new Vector3(0, 0, 1);
        }

        mesh.vertices = points;
        mesh.colors   = colors;
        mesh.SetIndices(indicies, MeshTopology.Points, 0);


        //depth, width, height

        Debug.Log(numPoints);
        Debug.Log(canvas_width);
        Debug.Log(canvas_height);

        Debug.Log(depth_data.Length);
    }
Esempio n. 2
0
    // Use this for initialization
    void Start()
    {
        kinectManagerInstance = KinectManager.Instance;

        WidthBuffer  = finalWidth;
        HeightBuffer = finalHeight;

        MyMesh = new Mesh();
        GetComponent <MeshFilter>().mesh = MyMesh;

        SetupArrays();

        mappingCorrections = new ushort[KinectWrapper.GetDepthHeight() * KinectWrapper.GetDepthWidth()];
    }
Esempio n. 3
0
    // Use this for initialization
    void Start()
    {
        //km = GetComponent<KinectManager>();
        km        = camera.GetComponent <KinectManager>();
        mrenderer = GetComponent <MeshRenderer>();


        //depth, width, height
        depth_data    = km.GetRawDepthMap();
        canvas_height = KinectWrapper.GetDepthHeight();
        canvas_width  = KinectWrapper.GetDepthWidth();
        // Debug.Log(canvas_height);
        // Debug.Log(canvas_width);
        // Debug.Log(depth_data.Length);
    }
Esempio n. 4
0
    // Use this for initialization
    void Start()
    {
        km           = camera.GetComponent <KinectManager>();
        meshrenderer = GetComponent <MeshRenderer>();
        meshfilter   = GetComponent <MeshFilter>();
        mesh         = new Mesh();

        meshfilter.mesh = mesh;

        depth_data    = km.GetRawDepthMap();
        canvas_height = KinectWrapper.GetDepthHeight() / 2;
        canvas_width  = KinectWrapper.GetDepthWidth() / 2;

        meshrenderer.material = new Material(Shader.Find("Tim/PointShader"));
    }
Esempio n. 5
0
    void Awake()
    {
        int hr = 0;

        try
        {
            hr = KinectWrapper.NuiInitialize(KinectWrapper.NuiInitializeFlags.UsesSkeleton |
                                             KinectWrapper.NuiInitializeFlags.UsesDepthAndPlayerIndex |
                                             (ComputeColorMap ? KinectWrapper.NuiInitializeFlags.UsesColor : 0));
            if (hr != 0)
            {
                throw new Exception("NuiInitialize Failed");
            }

            hr = KinectWrapper.NuiSkeletonTrackingEnable(IntPtr.Zero,
                                                         8);              // 0, 12,8
            if (hr != 0)
            {
                throw new Exception("Cannot initialize Skeleton Data");
            }

            depthStreamHandle = IntPtr.Zero;
            if (ComputeUserMap)
            {
                hr = KinectWrapper.NuiImageStreamOpen
                         (KinectWrapper.NuiImageType.DepthAndPlayerIndex,

                         KinectWrapper.Constants.DepthImageResolution, 0, 2, IntPtr.Zero, ref
                         depthStreamHandle);
                if (hr != 0)
                {
                    throw new Exception("Cannot open depth stream");
                }
            }

            colorStreamHandle = IntPtr.Zero;
            if (ComputeColorMap)
            {
                hr = KinectWrapper.NuiImageStreamOpen
                         (KinectWrapper.NuiImageType.Color,

                         KinectWrapper.Constants.ColorImageResolution, 0, 2, IntPtr.Zero, ref
                         colorStreamHandle);
                if (hr != 0)
                {
                    throw new Exception("Cannot open color stream");
                }
            }

            // set kinect elevation angle
            KinectWrapper.NuiCameraElevationSetAngle(SensorAngle);

            // init skeleton structures
            skeletonFrame = new KinectWrapper.NuiSkeletonFrame()
            {
                SkeletonData = new KinectWrapper.NuiSkeletonData
                               [KinectWrapper.Constants.NuiSkeletonCount]
            };

            // values used to pass to smoothing function
            smoothParameters = new
                               KinectWrapper.NuiTransformSmoothParameters();

            switch (smoothing)
            {
            case Smoothing.Default:
                smoothParameters.fSmoothing          = 0.5f;
                smoothParameters.fCorrection         = 0.5f;
                smoothParameters.fPrediction         = 0.5f;
                smoothParameters.fJitterRadius       = 0.05f;
                smoothParameters.fMaxDeviationRadius = 0.04f;
                break;

            case Smoothing.Medium:
                smoothParameters.fSmoothing          = 0.5f;
                smoothParameters.fCorrection         = 0.1f;
                smoothParameters.fPrediction         = 0.5f;
                smoothParameters.fJitterRadius       = 0.1f;
                smoothParameters.fMaxDeviationRadius = 0.1f;
                break;

            case Smoothing.Aggressive:
                smoothParameters.fSmoothing          = 0.7f;
                smoothParameters.fCorrection         = 0.3f;
                smoothParameters.fPrediction         = 1.0f;
                smoothParameters.fJitterRadius       = 1.0f;
                smoothParameters.fMaxDeviationRadius = 1.0f;
                break;
            }

            // create arrays for joint positions and joint orientations
            int skeletonJointsCount = (int)
                                      KinectWrapper.NuiSkeletonPositionIndex.Count;

            playerJointsTracked = new bool[skeletonJointsCount];
            playerPrevTracked   = new bool[skeletonJointsCount];

            playerJointsPos = new Vector3[skeletonJointsCount];


            //create the transform matrix that converts from kinect-space to world-space
            Quaternion quatTiltAngle = new Quaternion();
            quatTiltAngle.eulerAngles = new Vector3(-SensorAngle, 0.0f, 0.0f);

            //float heightAboveHips = SensorHeight - 1.0f;

            // transform matrix - kinect to world
            //kinectToWorld.SetTRS(new Vector3(0.0f, heightAboveHips, 0.0f), quatTiltAngle, Vector3.one);
            kinectToWorld.SetTRS(new Vector3(0.0f, SensorHeight, 0.0f), quatTiltAngle, Vector3.one);
            flipMatrix       = Matrix4x4.identity;
            flipMatrix[2, 2] = -1;

            instance = this;
        }
        catch (DllNotFoundException e)
        {
            string message = "Please check the Kinect SDK installation.";
            Debug.LogError(message);
            Debug.LogError(e.ToString());
            return;
        }
        catch (Exception e)
        {
            string message = e.Message + " - " +
                             KinectWrapper.GetNuiErrorString(hr);
            Debug.LogError(message);
            Debug.LogError(e.ToString());
            return;
        }

        if (ComputeUserMap)
        {
            // Initialize depth & label map related stuff
            usersMapSize = KinectWrapper.GetDepthWidth() *
                           KinectWrapper.GetDepthHeight();
            usersLblTex = new Texture2D(KinectWrapper.GetDepthWidth(),
                                        KinectWrapper.GetDepthHeight());
            usersMapColors = new Color32[usersMapSize];
            usersPrevState = new ushort[usersMapSize];

            usersDepthMap     = new ushort[usersMapSize];
            usersHistogramMap = new float[8192];
        }

        if (ComputeColorMap)
        {
            // Initialize color map related stuff
            usersClrTex = new Texture2D(KinectWrapper.GetColorWidth(),
                                        KinectWrapper.GetColorHeight());

            colorImage = new Color32[KinectWrapper.GetColorWidth() *
                                     KinectWrapper.GetColorHeight()];
            usersColorMap = new byte[colorImage.Length << 2];
        }


        // Initialize user list to contain ALL users.
        allUsers = new List <uint>();

        KinectInitialized = true;
    }
Esempio n. 6
0
    /// <summary>
    /// This function usually works up to a second =)
    /// </summary>
    void InitializeKinect()
    {
        if (KinectText != null)
        {
            KinectText.text = "Starting Kinect initialization";
        }

        // Error code.
        int hr = 0;

        try
        {
            // Initialize kinect for skeleton tracking and video
            hr = KinectWrapper.NuiInitialize(KinectWrapper.NuiInitializeFlags.UsesColor | KinectWrapper.NuiInitializeFlags.UsesSkeleton);
            if (hr != 0)
            {
                if (KinectText != null)
                {
                    KinectText.text = "Failed to Initialize Kinect";
                }
                throw new Exception("NuiInitialize Failed");
            }

            // Initialize video
            colorStreamHandle = IntPtr.Zero;
            hr = KinectWrapper.NuiImageStreamOpen(KinectWrapper.NuiImageType.Color,
                                                  KinectWrapper.Constants.ImageResolution, 0, 2, IntPtr.Zero, ref colorStreamHandle);
            if (hr != 0)
            {
                if (KinectText != null)
                {
                    KinectText.text = "Failed to Initialize Color Stream";
                }
                throw new Exception("Cannot open color stream");
            }

            // Initialize skeleton
            hr = KinectWrapper.NuiSkeletonTrackingEnable(IntPtr.Zero, 8);              // 0, 12,8
            if (hr != 0)
            {
                if (KinectText != null)
                {
                    KinectText.text = "Failed to Initialize Skeleton Stream";
                }
                throw new Exception("Cannot initialize Skeleton Data");
            }

            // init skeleton structures
            skeletonFrame = new KinectWrapper.NuiSkeletonFrame()
            {
                SkeletonData = new KinectWrapper.NuiSkeletonData[KinectWrapper.Constants.NuiSkeletonCount]
            };

            // values used to pass to smoothing function
            smoothParameters                     = new KinectWrapper.NuiTransformSmoothParameters();
            smoothParameters.fSmoothing          = 0.5f;
            smoothParameters.fCorrection         = 0.5f;
            smoothParameters.fPrediction         = 0.5f;
            smoothParameters.fJitterRadius       = 0.05f;
            smoothParameters.fMaxDeviationRadius = 0.04f;

            // set kinect elevation angle
            //KinectWrapper.NuiCameraElevationSetAngle(15);
        }
        catch (DllNotFoundException e)
        {
            if (KinectText != null)
            {
                KinectText.text = "Please check the Kinect SDK installation.";
            }
            string message = "Please check the Kinect SDK installation.";
            Debug.LogError(message);
            Debug.LogError(e.ToString());
            return;
        }
        catch (Exception e)
        {
            string message = e.Message + " - " + KinectWrapper.GetNuiErrorString(hr);
            if (KinectText != null)
            {
                if ((uint)hr == (uint)KinectWrapper.NuiErrorCodes.DeviceNotConnected)
                {
                    KinectText.text = "O-ops, where is my kinect";//"Упс, де мій Кінект ???";
                }
                else if ((uint)hr == (uint)KinectWrapper.NuiErrorCodes.DeviceNotPowered)
                {
                    KinectText.text = "O-ops, why my kinect is not powered";//"Упс, чому Кінект не у розетці ???";
                }
                else if ((uint)hr == (uint)KinectWrapper.NuiErrorCodes.DeviceInUse)
                {
                    KinectText.text = "O-ops, why someone else is using my kinect";//"Упс, хто використовує мій Кінект ???";
                }
                else
                {
                    KinectText.text = message;
                }
            }
            Debug.LogError(message);
            return;
        }


        // Initialize color map related stuff
        usersClrTex = new Texture2D(KinectWrapper.GetDepthWidth(), KinectWrapper.GetDepthHeight());
        if (SmallVideo)
        {
            usersClrRect = new Rect(10, 10, 160, 120);
        }
        else
        {
            usersClrRect = new Rect(10, 10, usersClrTex.width, usersClrTex.height);
        }

        colorImage = new Color32[KinectWrapper.GetDepthWidth() * KinectWrapper.GetDepthHeight()];

        KinectInitialized = true;
        if (KinectText != null)
        {
            KinectText.text = "Kinect initialization successful";
        }
    }
Esempio n. 7
0
    void Start()
    {
        //_adaptiveUi = GameObject.Find("Cube").GetComponent<AdaptiveUi>(); // Waterstrong

        int hr = 0;

        try
        {
            hr = KinectWrapper.NuiInitialize(KinectWrapper.NuiInitializeFlags.UsesDepthAndPlayerIndex |
                                             KinectWrapper.NuiInitializeFlags.UsesSkeleton |
                                             KinectWrapper.NuiInitializeFlags.UsesColor);
            if (hr != 0)
            {
                throw new Exception("NuiInitialize Failed.");
            }

            hr = KinectWrapper.NuiSkeletonTrackingEnable(IntPtr.Zero, 8);              // 0, 12,8
            if (hr != 0)
            {
                throw new Exception("Cannot initialize Skeleton Data.");
            }

            depthStreamHandle = IntPtr.Zero;
            hr = KinectWrapper.NuiImageStreamOpen(KinectWrapper.NuiImageType.DepthAndPlayerIndex,
                                                  KinectWrapper.Constants.ImageResolution, 0, 2, IntPtr.Zero, ref depthStreamHandle);
//			if (hr != 0)
//			{
//				throw new Exception("Cannot open depth stream.");
//			}

            colorStreamHandle = IntPtr.Zero;
            hr = KinectWrapper.NuiImageStreamOpen(KinectWrapper.NuiImageType.Color,
                                                  KinectWrapper.Constants.ImageResolution, 0, 2, IntPtr.Zero, ref colorStreamHandle);
//			if (hr != 0)
//			{
//				throw new Exception("Cannot open color stream.");
//			}

            // set kinect elevation angle
            KinectWrapper.NuiCameraSetAngle((long)KinectAngle);

            // init skeleton structures
            skeletonFrame = new KinectWrapper.NuiSkeletonFrame()
            {
                SkeletonData = new KinectWrapper.NuiSkeletonData[KinectWrapper.Constants.NuiSkeletonCount]
            };

            // values used to pass to smoothing function
            smoothParameters                     = new KinectWrapper.NuiTransformSmoothParameters();
            smoothParameters.fSmoothing          = 0.7f;  //0.5f;
            smoothParameters.fCorrection         = 0.0f;  //0.5f;
            smoothParameters.fPrediction         = 0.0f;  //0.5f;
            smoothParameters.fJitterRadius       = 0.05f;
            smoothParameters.fMaxDeviationRadius = 0.05f; // 0.04f;

            //                 Smoothing = 0.3f,
            //                 Correction = 0.0f,
            //                 Prediction = 0.0f,
            //                 JitterRadius = 1.0f,
            //                 MaxDeviationRadius = 0.5f
            //Smoothing = 1.0f,
            //Correction = 0.1f,
            //Prediction = 0.1f,
            //JitterRadius = 0.05f,
            //MaxDeviationRadius = 0.05f



            // create arrays for joint positions and joint orientations
            int skeletonJointsCount = (int)KinectWrapper.NuiSkeletonPositionIndex.Count;
            player1JointsTracked = new bool[skeletonJointsCount];
            player2JointsTracked = new bool[skeletonJointsCount];

            player1JointsPos = new Vector3[skeletonJointsCount];
            player2JointsPos = new Vector3[skeletonJointsCount];

            player1JointsOri = new Matrix4x4[skeletonJointsCount];
            player2JointsOri = new Matrix4x4[skeletonJointsCount];

            //create the transform matrix that converts from kinect-space to world-space
            Quaternion quat = new Quaternion();
            quat.eulerAngles = new Vector3(-KinectAngle, 0.0f, 0.0f);

            // transform matrix - kinect to world
            kinectToWorld.SetTRS(new Vector3(0.0f, SensorHeight, 0.0f), quat, Vector3.one);
            //quatToWorld = Quaternion.LookRotation(kinectToWorld.GetColumn(2), kinectToWorld.GetColumn(1));

            instance = this;
            DontDestroyOnLoad(gameObject);
            Debug.Log("success kinect.");

            // Waterstrong Add
            _motion = MotionFactory.CreateHandleChain();
            Debug.Log("motion chain success.");
        }
        catch (Exception e)
        {
            Debug.Log(e.Message + ", hr=" + hr.ToString());
            return;
        }

        // Initialize depth & label map related stuff
        usersMapSize   = KinectWrapper.GetDepthWidth() * KinectWrapper.GetDepthHeight();
        usersLblTex    = new Texture2D(KinectWrapper.GetDepthWidth(), KinectWrapper.GetDepthHeight());
        usersMapColors = new Color[usersMapSize];

        /*
         * usersMapRect = new Rect(Screen.width - usersLblTex.width / 2,
         *                          Screen.height - usersLblTex.height / 2,
         *                          -usersLblTex.width / 2,
         *                          usersLblTex.height / 2);
         */

        usersMapRect = new Rect(Screen.width - 10,
                                Screen.height - 240 - 10,
                                -320,
                                240);

        if (DisplayColorMap)
        {
            usersClrTex = new Texture2D(KinectWrapper.GetDepthWidth(), KinectWrapper.GetDepthHeight());
            //usersClrColors = new Color[usersMapSize];
            usersClrRect    = new Rect(Screen.width /**- usersLblTex.width / 2*/ /**- usersClrTex.width / 2*/, Screen.height - usersClrTex.height / 2, -usersClrTex.width / 2, usersClrTex.height / 2);
            usersMapRect.x -= usersLblTex.width / 2;

            colorImage = new Color32[usersMapSize];
        }

        usersDepthMap     = new short[usersMapSize];
        usersHistogramMap = new float[5000];

        // Initialize user list to contain ALL users.
        allUsers = new List <uint>();

        // Pull the AvatarController from each of the players Avatars.
        Player1Controllers = new List <AvatarController>();
        Player2Controllers = new List <AvatarController>();

        // Add each of the avatars' controllers into a list for each player.
        foreach (GameObject avatar in Player1Avatars)
        {
            Player1Controllers.Add(avatar.GetComponent <AvatarController>());
        }

        foreach (GameObject avatar in Player2Avatars)
        {
            Player2Controllers.Add(avatar.GetComponent <AvatarController>());
        }

        // GUI Text.
        CalibrationText = GameObject.Find("CalibrationText");
        if (CalibrationText != null)
        {
            CalibrationText.guiText.text = "WAITING FOR USERS";
        }

        Debug.Log("Waiting for users.");

        KinectInitialized = true;
    }
Esempio n. 8
0
    //----------------------------------- end of public functions --------------------------------------//

    void Start()
    {
        //CalibrationText = GameObject.Find("CalibrationText");
        int hr = 0;

        try
        {
            hr = KinectWrapper.NuiInitialize(KinectWrapper.NuiInitializeFlags.UsesSkeleton |
                                             KinectWrapper.NuiInitializeFlags.UsesDepthAndPlayerIndex |
                                             (ComputeColorMap ? KinectWrapper.NuiInitializeFlags.UsesColor : 0));
            if (hr != 0)
            {
                throw new Exception("NuiInitialize Failed");
            }

            hr = KinectWrapper.NuiSkeletonTrackingEnable(IntPtr.Zero, 8);              // 0, 12,8
            if (hr != 0)
            {
                throw new Exception("Cannot initialize Skeleton Data");
            }

            _depthStreamHandle = IntPtr.Zero;
            if (ComputeUserMap)
            {
                hr = KinectWrapper.NuiImageStreamOpen(KinectWrapper.NuiImageType.DepthAndPlayerIndex,
                                                      KinectWrapper.Constants.DepthImageResolution, 0, 2, IntPtr.Zero, ref _depthStreamHandle);
                if (hr != 0)
                {
                    throw new Exception("Cannot open depth stream");
                }
            }

            _colorStreamHandle = IntPtr.Zero;
            if (ComputeColorMap)
            {
                hr = KinectWrapper.NuiImageStreamOpen(KinectWrapper.NuiImageType.Color,
                                                      KinectWrapper.Constants.ColorImageResolution, 0, 2, IntPtr.Zero, ref _colorStreamHandle);
                if (hr != 0)
                {
                    throw new Exception("Cannot open color stream");
                }
            }

            // set kinect elevation angle
            KinectWrapper.NuiCameraElevationSetAngle(SensorAngle);

            // init skeleton structures
            _skeletonFrame = new KinectWrapper.NuiSkeletonFrame()
            {
                SkeletonData = new KinectWrapper.NuiSkeletonData[KinectWrapper.Constants.NuiSkeletonCount]
            };

            // values used to pass to smoothing function
            _smoothParameters = new KinectWrapper.NuiTransformSmoothParameters();

            switch (smoothing)
            {
            case Smoothing.Default:
                _smoothParameters.fSmoothing          = 0.5f;
                _smoothParameters.fCorrection         = 0.5f;
                _smoothParameters.fPrediction         = 0.5f;
                _smoothParameters.fJitterRadius       = 0.05f;
                _smoothParameters.fMaxDeviationRadius = 0.04f;
                break;

            case Smoothing.Medium:
                _smoothParameters.fSmoothing          = 0.5f;
                _smoothParameters.fCorrection         = 0.1f;
                _smoothParameters.fPrediction         = 0.5f;
                _smoothParameters.fJitterRadius       = 0.1f;
                _smoothParameters.fMaxDeviationRadius = 0.1f;
                break;

            case Smoothing.Aggressive:
                _smoothParameters.fSmoothing          = 0.7f;
                _smoothParameters.fCorrection         = 0.3f;
                _smoothParameters.fPrediction         = 1.0f;
                _smoothParameters.fJitterRadius       = 1.0f;
                _smoothParameters.fMaxDeviationRadius = 1.0f;
                break;
            }

            // create arrays for joint positions and joint orientations
            int skeletonJointsCount = (int)KinectWrapper.NuiSkeletonPositionIndex.Count;

            _player1JointsTracked = new bool[skeletonJointsCount];
            _player2JointsTracked = new bool[skeletonJointsCount];
            _player1PrevTracked   = new bool[skeletonJointsCount];
            _player2PrevTracked   = new bool[skeletonJointsCount];

            _player1JointsPos = new Vector3[skeletonJointsCount];
            _player2JointsPos = new Vector3[skeletonJointsCount];

            _player1JointsOri = new Matrix4x4[skeletonJointsCount];
            _player2JointsOri = new Matrix4x4[skeletonJointsCount];

            //create the transform matrix that converts from kinect-space to world-space
            Quaternion quatTiltAngle = new Quaternion();
            quatTiltAngle.eulerAngles = new Vector3(-SensorAngle, 0.0f, 0.0f);

            // transform matrix - kinect to world
            _kinectToWorld.SetTRS(new Vector3(0.0f, SensorHeight, 0.0f), quatTiltAngle, Vector3.one);
            _flipMatrix       = Matrix4x4.identity;
            _flipMatrix[2, 2] = -1;

            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        catch (DllNotFoundException e)
        {
            string message = "Please check the Kinect SDK installation.";
            Debug.LogError(message);
            Debug.LogError(e.ToString());

            return;
        }
        catch (Exception e)
        {
            string message = e.Message + " - " + KinectWrapper.GetNuiErrorString(hr);
            Debug.LogError(message);
            Debug.LogError(e.ToString());

            return;
        }

        // get the main camera rectangle
        Rect cameraRect = Camera.main.pixelRect;

        if (ComputeUserMap)
        {
            var displayMapsWidthPercent  = DisplayMapsWidthPercent / 100f;
            var displayMapsHeightPercent = displayMapsWidthPercent * KinectWrapper.GetDepthHeight() / KinectWrapper.GetDepthWidth();

            var displayWidth  = cameraRect.width * displayMapsWidthPercent;
            var displayHeight = cameraRect.width * displayMapsHeightPercent;

            // Initialize depth & label map related stuff
            _usersMapSize   = KinectWrapper.GetDepthWidth() * KinectWrapper.GetDepthHeight();
            _usersLblTex    = new Texture2D(KinectWrapper.GetDepthWidth(), KinectWrapper.GetDepthHeight());
            _usersMapColors = new Color32[_usersMapSize];
            _usersPrevState = new ushort[_usersMapSize];
            _usersMapRect   = new Rect(cameraRect.width - displayWidth, cameraRect.height, displayWidth, -displayHeight);

            _usersDepthMap     = new ushort[_usersMapSize];
            _usersHistogramMap = new float[8192];
        }

        if (ComputeColorMap)
        {
            var displayMapsWidthPercent  = DisplayMapsWidthPercent / 100f;
            var displayMapsHeightPercent = displayMapsWidthPercent * KinectWrapper.GetColorHeight() / KinectWrapper.GetColorWidth();

            var displayWidth  = cameraRect.width * displayMapsWidthPercent;
            var displayHeight = cameraRect.width * displayMapsHeightPercent;

            // Initialize color map related stuff
            _usersClrTex  = new Texture2D(KinectWrapper.GetColorWidth(), KinectWrapper.GetColorHeight());
            _usersClrRect = new Rect(cameraRect.width - displayWidth, cameraRect.height, displayWidth, -displayHeight);

            _colorImage    = new Color32[KinectWrapper.GetColorWidth() * KinectWrapper.GetColorHeight()];
            _usersColorMap = new byte[_colorImage.Length << 2];
        }


        // Initialize user list to contain ALL users.
        _allUsers = new List <uint>();

        Debug.Log("Waiting for users.");

        _kinectInitialized = true;
    }
Esempio n. 9
0
    void Start()
    {
        CalibrationText = GameObject.Find("CalibrationText");

        try
        {
            // Make sure we have the Open NI file.
            uint rc = KinectWrapper.Init(new StringBuilder("OpenNI.xml"));

            if (rc != 0)
            {
                throw new Exception(String.Format("Error initing OpenNI: {0}", Marshal.PtrToStringAnsi(KinectWrapper.GetStatusString(rc))));
            }

            // Initialize depth & label map related stuff
            usersMapSize      = KinectWrapper.GetDepthWidth() * KinectWrapper.GetDepthHeight();
            usersLblTex       = new Texture2D(KinectWrapper.GetDepthWidth(), KinectWrapper.GetDepthHeight());
            usersMapColors    = new Color[usersMapSize];
            usersMapRect      = new Rect(Screen.width - usersLblTex.width / 2, Screen.height - usersLblTex.height / 2, usersLblTex.width / 2, usersLblTex.height / 2);
            usersLabelMap     = new short[usersMapSize];
            usersDepthMap     = new short[usersMapSize];
            usersHistogramMap = new float[5000];

            // Initialize user list to contain ALL users.
            allUsers = new List <uint>();

            // Initialize user callbacks.
            NewUser            = new KinectWrapper.UserDelegate(OnNewUser);
            CalibrationStarted = new KinectWrapper.UserDelegate(OnCalibrationStarted);
            CalibrationFailed  = new KinectWrapper.UserDelegate(OnCalibrationFailed);
            CalibrationSuccess = new KinectWrapper.UserDelegate(OnCalibrationSuccess);
            UserLost           = new KinectWrapper.UserDelegate(OnUserLost);

            // Pull the AvatarController from each of the players Avatars.
            Player1Controllers = new List <AvatarController>();
            Player2Controllers = new List <AvatarController>();

            // Add each of the avatars' controllers into a list for each player.
            foreach (GameObject avatar in Player1Avatars)
            {
                Player1Controllers.Add(avatar.GetComponent <AvatarController>());
            }

            foreach (GameObject avatar in Player2Avatars)
            {
                Player2Controllers.Add(avatar.GetComponent <AvatarController>());
            }

            // GUI Text.
            if (CalibrationText != null)
            {
                CalibrationText.guiText.text = "MATCH POSE TO CALIBRATE";
            }

            // Start looking for users.
            KinectWrapper.StartLookingForUsers(NewUser, CalibrationStarted, CalibrationFailed, CalibrationSuccess, UserLost);
            Debug.Log("Waiting for users to calibrate");

            // Set the default smoothing for the Kinect.
            KinectWrapper.SetSkeletonSmoothing(0.7);

            instance          = this;
            OpenNIInitialized = true;
        }
        catch (DllNotFoundException ex)
        {
            Debug.LogError(ex.ToString());
            if (CalibrationText != null)
            {
                CalibrationText.guiText.text = "Please check the OpenNI and NITE installations.";
            }
        }
        catch (Exception ex)
        {
            Debug.LogError(ex.ToString());
            if (CalibrationText != null)
            {
                CalibrationText.guiText.text = ex.Message;
            }
        }
    }