Demo entry 6626055

1111

   

Submitted by anonymous on Jun 23, 2017 at 06:57
Language: C#. Code size: 10.3 kB.

    using UnityEngine;  
    using System.Collections.Generic;  
    using IMIForUnity;  
    using UnityEngine.UI;  
    using System.Runtime.InteropServices;  
    using System;  
      
    public class ImiExplorer : MonoBehaviour  
    {  
        private int errorCode = 0;  
      
    #if UNITY_ANDROID && !UNITY_EDITOR  
        private bool IsPerformance = false;  
    #endif  
      
        public GameObject skeletons;  
      
        public RawImage colorView;  
        public RawImage depthView;  
        public GameObject cube;  
        public Text label;  
      
      
        public PlayerControlMode playerControlMode = PlayerControlMode.NEAREST;  
      
        private List<GameObject> skeletonList = new List<GameObject>();  
        private int skeletonCount = (int)ImiSkeleton.Index.COUNT;  
      
        private IManager iManager = IManagerFactory.GetInstance();  
      
        private ImiTexture2D texture2d = null;  
        private ImiTexture2D texture2dDepth = null;  
      
        // 彩色视图上显示的图像类型  
        private FrameType CVFrameType = FrameType.COLOR_TEXTURE;  
        private UserExtractMode CVUserExtractMode = UserExtractMode.NO_EXTRACT;  
      
      
        // 深度视图上显示的图像类型  
        private FrameType DVFrameType = FrameType.DEPTH_TEXTURE;  
        private UserExtractMode DVUserExtractMode = UserExtractMode.NO_EXTRACT;  
      
        private IGesture imiGestureManager = IManagerFactory.GetInstance().IEGetImiGestureInstance();  
      
        // Use this for initialization  
        void Start()  
        {  
            Debug.Log("skeletonCount = " + skeletonCount);  
      
            int skeletonsNum = skeletonCount;  
            for (int i = 0; i < skeletonsNum; i++)  
            {  
                GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);  
                sphere.transform.parent = skeletons.transform;  
                sphere.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);  
                skeletonList.Add(sphere);  
            }  
      
            for (int i = 0; i < skeletonsNum; i++)  
            {  
                GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Cube);  
                sphere.transform.parent = skeletons.transform;  
                sphere.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);  
                skeletonList.Add(sphere);  
            }  
      
            Debug.Log("Create Manager");  
      
            iManager.IEAddFrameSource(CVFrameType, CVUserExtractMode);  
            iManager.IEAddFrameSource(DVFrameType, DVUserExtractMode);  
            iManager.IEAddFrameSource(FrameType.SKELETON, UserExtractMode.NO_EXTRACT);  
      
            iManager.IESetResolution(FrameType.COLOR_TEXTURE, 640, 480);  
            iManager.IESetResolution(FrameType.DEPTH_TEXTURE, 320, 240);  
             
     iManager.IEInit(this);  
      
    #if UNITY_ANDROID && !UNITY_EDITOR  
            if (IsPerformance)  
            {  
                //enable performance for color 720p/1080p  
                iManager.IEEnablePerformance(CVFrameType, CVUserExtractMode);  
            }  
    #endif  
              
            errorCode = iManager.IEOpenDevice();  
        
            texture2d = new ImiTexture2D(FrameType.COLOR_TEXTURE, 640, 480);  
            texture2dDepth = new ImiTexture2D(FrameType.DEPTH_TEXTURE, 320, 240);  
      
            colorView.texture = texture2d.Texture;  
            depthView.texture = texture2dDepth.Texture;  
      
            initGestures();  
        }  
      
        void initGestures()  
        {  
            if (imiGestureManager != null)  
            {  
                imiGestureManager.AddGesture(ImiGesture.GestureType.IMI_GESTURE_RIGHT_HAND_WAVE, (gesture, player, gevent) =>  
                {  
                    if (gevent == ImiGesture.GestureEvent.Complete)  
                    {  
                        Debug.Log("IMI_GESTURE_RIGHT_HAND_WAVE Detected!");  
                    }  
                });  
            }  
        }  
      
        private void showTexture()  
        {  
            iManager.IEUpdateTextureFrame(CVFrameType, CVUserExtractMode, ref texture2d);  
            iManager.IEUpdateTextureFrame(DVFrameType, DVUserExtractMode, ref texture2dDepth);  
        }  
      
      
        private void showSyncTexture()  
        {  
            if (lastmainColor == IntPtr.Zero)  
            {  
                lastmainColor = ImiEngineWrapper.IEReadImiFrame(DVFrameType, DVUserExtractMode);  
                if (lastmainColor != IntPtr.Zero)  
                {  
                    ImiEngineWrapper.IEImageFrame stImageFrame2 = (ImiEngineWrapper.IEImageFrame)Marshal.PtrToStructure(lastmainColor, typeof(ImiEngineWrapper.IEImageFrame));  
                    mainColorTimeStamp = stImageFrame2.timeStamp / 1000;  
                    //Debug.Log("[Richard] MainUser timeStemp = " + mainColorTimeStamp);  
                }  
            }  
      
            if (lastColor == IntPtr.Zero)  
            {  
                lastColor = ImiEngineWrapper.IEReadImiFrame(CVFrameType, CVUserExtractMode);  
                if (lastColor != IntPtr.Zero)  
                {  
                    ImiEngineWrapper.IEImageFrame stImageFrame1 = (ImiEngineWrapper.IEImageFrame)Marshal.PtrToStructure(lastColor, typeof(ImiEngineWrapper.IEImageFrame));  
                    colorTimeStamp = stImageFrame1.timeStamp / 1000;  
                    //Debug.Log("[Richard] Color timeStemp = " + colorTimeStamp);  
                }  
            }  
      
            if (lastColor != IntPtr.Zero && lastmainColor != IntPtr.Zero)  
            {  
                long timeStemp = colorTimeStamp - mainColorTimeStamp;  
                Debug.Log("[Richard] sub timeStemp = " + timeStemp);  
                lastmainColor = IntPtr.Zero;  
                lastColor = IntPtr.Zero;  
            }  
        }  
      
        void testGrasp()  
        {  
    #if UNITY_ANDROID && !UNITY_EDITOR  
            Dictionary<int, IPlayerInfo> playerInfos = iManager.IEUpdatePlayerInfos();  
      
            if (playerInfos == null || playerInfos.Count <= 0)  
            {  
                return;  
            }  
      
            foreach (int key in playerInfos.Keys)  
            {  
                // Display main player skeleton  
                if (key == iManager.IEGetMainPlayerId())  
                {  
                    iManager.IESetUserProperty(key, ImiEngineWrapper.PlayerProperty.BOTH_HAND_GRASP, true);  
                    ImiEngineWrapper.UserGraspState userGS = playerInfos[key].GetUserGraspState();  
                    ImiSkeleton[] skeletons = playerInfos[key].GetSkeletons();  
                    for (int j = 0; j < skeletonCount; j++)  
                    {  
                        //Debug.Log("player pos is " + skeletons[j].position.ToString());  
                        skeletonList[j].transform.position = skeletons[j].position;  
                    }  
      
                    label.text = "lc=" + userGS.leftConfidence + ", lg=" + userGS.leftGesturestate + ", rc=" + userGS.rightConfidence + ", rg=" + userGS.rightGesturestate;  
                }  
            }  
    #endif  
        }  
      
        void testPlayerInfos()  
        {  
            Dictionary<int, IPlayerInfo> playerInfos = iManager.IEUpdatePlayerInfos();  
      
            if (playerInfos == null || playerInfos.Count <= 0)  
            {  
                return;  
            }  
      
            foreach (int key in playerInfos.Keys)  
            {  
                // Display main player skeleton  
                if (key == iManager.IEGetMainPlayerId())  
                {  
                    ImiSkeleton[] skeletons = playerInfos[key].GetSkeletons();  
                    for (int j = 0; j < skeletonCount; j++)  
                    {  
                        skeletonList[j].transform.position = skeletons[j].position;  
                    }  
                    if (skeletons[(int)ImiSkeleton.Index.HIP_CENTER].position.y - playerInfos[key].GetJointInfo().fixHipPosY > 0.1f)  
                    {  
                        label.text = "Jump";  
                    }  
                    else if (skeletons[(int)ImiSkeleton.Index.HIP_CENTER].position.y - playerInfos[key].GetJointInfo().fixHipPosY < -0.2f)  
                    {  
                        label.text = "Crouch";  
                    }  
                    else  
                    {  
                        label.text = "Stand";  
                    }  
                }  
      
             
            }  
      
            imiGestureManager.AddGestureUser(iManager.IEGetMainPlayerId());  
            imiGestureManager.UpdateGestures(playerInfos, true);  
        }  
      
        // Update is called once per frame  
        void Update()  
        {  
    #if UNITY_ANDROID && !UNITY_EDITOR  
            if (IsPerformance)  
            {  
                if (iManager.A100M)  
                {  
                    colorView.transform.localScale = new Vector3(-1, 1, 1);  
                }  
                else  
                {  
                    colorView.transform.localScale = new Vector3(1, 1, 1);  
                }  
                  
            }  
    #endif  
            if (Input.GetKey(KeyCode.Escape))  
            {  
                Application.Quit();  
            }  
      
            cube.transform.Rotate(1, 1, 1);  
            if (!iManager.IsDeviceWorking())  
            {  
                return;  
            }  
      
            iManager.IEGetMainPlayerId();  
      
            showTexture();  
      
            //showSyncTexture();  
      
            testPlayerInfos();  
      
            iManager.IEGetSubPlayerId();  
      
            iManager.IESetGroundJudge(true, true);  
        }  
      
        void OnApplicationQuit()  
        {  
            iManager.IECloseDevice();  
        }  
    }  

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).