├── .gitignore ├── Assets ├── AvatarsDemo │ ├── Materials │ │ └── FloorMaterial.mat │ ├── PointMan │ │ ├── ColorCube.fbm │ │ │ └── CubeMap.png │ │ ├── ColorCube.fbx │ │ ├── ColorMaterial.mat │ │ ├── CubeMap.png │ │ ├── Materials │ │ │ ├── CubeMap.mat │ │ │ ├── CubeMapCenter.mat │ │ │ ├── SkeletonLine.mat │ │ │ └── YellowCube.mat │ │ ├── PointMan.prefab │ │ ├── PointManController.cs │ │ ├── PointManYellow.prefab │ │ ├── SkeletonLine.prefab │ │ └── YellowCube.prefab │ └── Textures │ │ └── grid.png ├── Kinect2UnityExample.unity ├── KinectScripts │ ├── KinectManager.cs │ ├── KinectServer.cs │ └── KinectWrapper.cs └── Resources │ ├── GripCursor32.png │ ├── HandCursor32.png │ ├── Kinect2UnityClient.dll.bytes │ ├── Kinect2UnityServer.exe.bytes │ ├── ReleaseCursor32.png │ ├── msvcp110d.dll.i386.bytes │ ├── msvcp110d.dll.x64.bytes │ ├── msvcr110d.dll.i386.bytes │ └── msvcr110d.dll.x64.bytes ├── Library ├── .gitignore ├── AnnotationManager ├── AssetImportState ├── AssetServerCacheV3 ├── AssetVersioning.db ├── BuildPlayer.prefs ├── BuildSettings.asset ├── EditorUserBuildSettings.asset ├── EditorUserSettings.asset ├── FailedAssetImports.txt ├── InspectorExpandedItems.asset ├── MonoManager.asset ├── ProjectSettings.asset ├── ScriptMapper ├── assetDatabase3 ├── expandedItems ├── guidmapper └── metadata │ ├── 19 │ └── 191807f1008f6d747a7f05f6a28b1472 │ ├── 26 │ └── 26d013e3b64b1b049996b316995cf9d7 │ ├── 28 │ └── 28179444c3c7af744954d04207020229 │ ├── 36 │ └── 366e99b07d99f174087de49a4045a4d0 │ ├── 37 │ └── 372af108bb0e90245bebe24c4afa5fc8 │ ├── 39 │ └── 39cd60652f1f10944b3b1f8cafeb28ae │ ├── 40 │ └── 40f3aea2f9108b4498810fd583ac77ff │ ├── 46 │ └── 46dcfeef00f96a2438cdab823172efda │ ├── 47 │ └── 475d892be250472448901e2ac7e4f3da │ ├── 55 │ └── 55b9da46e4eef3f4cafab36e66796aeb │ ├── 56 │ └── 56c7813e5eff2d64894f4c5a209ab220 │ ├── 73 │ ├── 733fb2e84d195e1459a8a2bbc31707ae │ └── 73edc93f32ffcfa4ab8cc7f9e14500ba │ ├── 77 │ └── 77e94628ea257894eb0b75f7171d13ad │ ├── 85 │ └── 85ff23489b1399e468f2c6fafe1e63ac │ ├── 86 │ └── 86e10b2b763c9b04c8dd83e42a8f50fc │ ├── 90 │ └── 90fe4c2409694094faf2b29325e2d5bc │ ├── 96 │ └── 96fab79365c641c47b45548e8e42d086 │ ├── 00 │ ├── 00000000000000001000000000000000 │ ├── 00000000000000002000000000000000 │ ├── 00000000000000003000000000000000 │ ├── 00000000000000004000000000000000 │ ├── 00000000000000004100000000000000 │ ├── 00000000000000005000000000000000 │ ├── 00000000000000006000000000000000 │ ├── 00000000000000007000000000000000 │ ├── 00000000000000008000000000000000 │ ├── 00000000000000009000000000000000 │ ├── 0000000000000000a000000000000000 │ ├── 0000000000000000b000000000000000 │ └── 0000000000000000c000000000000000 │ ├── 2c │ └── 2c5097180052eba44b7944372ead4ccd │ ├── 2e │ └── 2e0ca68029ae7694dbe740d5e533c8d2 │ ├── 4d │ ├── 4d589fa57be210d48ad7046a1af0d672 │ └── 4d6bc47498368c141bc5aea37fd355f7 │ ├── 5a │ └── 5aa6f36584b40454d9d7f115bcfacad9 │ ├── 6b │ └── 6b531f336dfee5f438f94ed501caefc7 │ ├── 9a │ └── 9a219312cdda5ff4bb2b5510d90c3b0d │ ├── 9f │ └── 9f828727d4cfb7a4b8984b4e0310ae67 │ ├── b0 │ └── b02178bd62ae4f644bee73df22356c24 │ ├── bd │ └── bdd4729ec55a86a4dbf10a24293b9f23 │ ├── c1 │ └── c19ffeb0f090eac4bbcdf1ab23a9ff66 │ ├── c2 │ └── c25d226819a00a449bfa8b247eac07a7 │ ├── d1 │ └── d178c33d67caf1f48b73e155e2d5010b │ ├── de │ └── deab68507f779a144b807ddc9587f2fe │ ├── eb │ └── ebd602dc5968e0743b5c5fa9cd5268ee │ ├── ed │ └── ed1c50b3cebc9374f9da79ca55dd4e4b │ ├── f5 │ └── f578c9700f0b728429f030aa09476426 │ └── fe │ └── fe88cedb1a2428d44afa801a6b6b3d98 ├── ProjectSettings ├── AudioManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── EditorSettings.asset ├── InputManager.asset ├── NavMeshLayers.asset ├── NetworkManager.asset ├── ProjectSettings.asset ├── QualitySettings.asset ├── TagManager.asset └── TimeManager.asset └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.csproj 2 | *.pidb 3 | *.sln 4 | *.userprefs 5 | /Kinect20.dll 6 | /KinectServer/ 7 | /Kinect2UnityClient.dll 8 | /msvcp110d.dll 9 | /msvcr110d.dll 10 | -------------------------------------------------------------------------------- /Assets/AvatarsDemo/Materials/FloorMaterial.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/AvatarsDemo/Materials/FloorMaterial.mat -------------------------------------------------------------------------------- /Assets/AvatarsDemo/PointMan/ColorCube.fbm/CubeMap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/AvatarsDemo/PointMan/ColorCube.fbm/CubeMap.png -------------------------------------------------------------------------------- /Assets/AvatarsDemo/PointMan/ColorCube.fbx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/AvatarsDemo/PointMan/ColorCube.fbx -------------------------------------------------------------------------------- /Assets/AvatarsDemo/PointMan/ColorMaterial.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/AvatarsDemo/PointMan/ColorMaterial.mat -------------------------------------------------------------------------------- /Assets/AvatarsDemo/PointMan/CubeMap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/AvatarsDemo/PointMan/CubeMap.png -------------------------------------------------------------------------------- /Assets/AvatarsDemo/PointMan/Materials/CubeMap.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/AvatarsDemo/PointMan/Materials/CubeMap.mat -------------------------------------------------------------------------------- /Assets/AvatarsDemo/PointMan/Materials/CubeMapCenter.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/AvatarsDemo/PointMan/Materials/CubeMapCenter.mat -------------------------------------------------------------------------------- /Assets/AvatarsDemo/PointMan/Materials/SkeletonLine.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/AvatarsDemo/PointMan/Materials/SkeletonLine.mat -------------------------------------------------------------------------------- /Assets/AvatarsDemo/PointMan/Materials/YellowCube.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/AvatarsDemo/PointMan/Materials/YellowCube.mat -------------------------------------------------------------------------------- /Assets/AvatarsDemo/PointMan/PointMan.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/AvatarsDemo/PointMan/PointMan.prefab -------------------------------------------------------------------------------- /Assets/AvatarsDemo/PointMan/PointManController.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System; 3 | using System.Collections; 4 | 5 | public class PointManController : MonoBehaviour 6 | { 7 | public bool MoveVertically = false; 8 | public bool MirroredMovement = false; 9 | 10 | //public GameObject debugText; 11 | 12 | public GameObject Hip_Center; 13 | public GameObject Spine; 14 | public GameObject Neck; 15 | public GameObject Head; 16 | public GameObject Shoulder_Left; 17 | public GameObject Elbow_Left; 18 | public GameObject Wrist_Left; 19 | public GameObject Hand_Left; 20 | public GameObject Shoulder_Right; 21 | public GameObject Elbow_Right; 22 | public GameObject Wrist_Right; 23 | public GameObject Hand_Right; 24 | public GameObject Hip_Left; 25 | public GameObject Knee_Left; 26 | public GameObject Ankle_Left; 27 | public GameObject Foot_Left; 28 | public GameObject Hip_Right; 29 | public GameObject Knee_Right; 30 | public GameObject Ankle_Right; 31 | public GameObject Foot_Right; 32 | public GameObject Spine_Shoulder; 33 | public GameObject Hand_Tip_Left; 34 | public GameObject Thumb_Left; 35 | public GameObject Hand_Tip_Right; 36 | public GameObject Thumb_Right; 37 | 38 | public LineRenderer LinePrefab; 39 | 40 | private GameObject[] bones; 41 | private LineRenderer[] lines; 42 | 43 | private Vector3 initialPosition; 44 | private Quaternion initialRotation; 45 | private Vector3 initialPosOffset = Vector3.zero; 46 | private Int64 initialPosUserID = 0; 47 | 48 | 49 | void Start () 50 | { 51 | //store bones in a list for easier access 52 | bones = new GameObject[] { 53 | Hip_Center, 54 | Spine, 55 | Neck, 56 | Head, 57 | Shoulder_Left, 58 | Elbow_Left, 59 | Wrist_Left, 60 | Hand_Left, 61 | Shoulder_Right, 62 | Elbow_Right, 63 | Wrist_Right, 64 | Hand_Right, 65 | Hip_Left, 66 | Knee_Left, 67 | Ankle_Left, 68 | Foot_Left, 69 | Hip_Right, 70 | Knee_Right, 71 | Ankle_Right, 72 | Foot_Right, 73 | Spine_Shoulder, 74 | Hand_Tip_Left, 75 | Thumb_Left, 76 | Hand_Tip_Right, 77 | Thumb_Right 78 | }; 79 | 80 | // array holding the skeleton lines 81 | lines = new LineRenderer[bones.Length]; 82 | 83 | if(LinePrefab) 84 | { 85 | for(int i = 0; i < lines.Length; i++) 86 | { 87 | lines[i] = Instantiate(LinePrefab) as LineRenderer; 88 | } 89 | } 90 | 91 | initialPosition = transform.position; 92 | initialRotation = transform.rotation; 93 | } 94 | 95 | 96 | void Update () 97 | { 98 | KinectManager manager = KinectManager.Instance; 99 | 100 | // get 1st player 101 | Int64 userID = manager ? manager.GetPrimaryUser() : 0; 102 | 103 | if(userID <= 0) 104 | { 105 | // reset the pointman position and rotation 106 | if(transform.position != initialPosition) 107 | transform.position = initialPosition; 108 | 109 | if(transform.rotation != initialRotation) 110 | transform.rotation = initialRotation; 111 | 112 | for(int i = 0; i < bones.Length; i++) 113 | { 114 | bones[i].gameObject.SetActive(true); 115 | 116 | bones[i].transform.localPosition = Vector3.zero; 117 | bones[i].transform.localRotation = Quaternion.identity; 118 | 119 | if(LinePrefab) 120 | { 121 | lines[i].gameObject.SetActive(false); 122 | } 123 | } 124 | 125 | return; 126 | } 127 | 128 | // set the position in space 129 | Vector3 posPointMan = manager.GetUserPosition(userID); 130 | posPointMan.z = !MirroredMovement ? -posPointMan.z : posPointMan.z; 131 | 132 | // store the initial position 133 | if(initialPosUserID != userID) 134 | { 135 | initialPosUserID = userID; 136 | initialPosOffset = transform.position - (MoveVertically ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z)); 137 | } 138 | 139 | transform.position = initialPosOffset + (MoveVertically ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z)); 140 | 141 | // update the local positions of the bones 142 | for(int i = 0; i < bones.Length; i++) 143 | { 144 | if(bones[i] != null) 145 | { 146 | int joint = !MirroredMovement ? i : (int)KinectWrapper.GetMirrorJoint((KinectWrapper.JointType)i); 147 | 148 | if(manager.IsJointTracked(userID, joint)) 149 | { 150 | bones[i].gameObject.SetActive(true); 151 | 152 | Vector3 posJoint = manager.GetJointPosition(userID, joint); 153 | posJoint.z = !MirroredMovement ? -posJoint.z : posJoint.z; 154 | 155 | Quaternion rotJoint = manager.GetJointOrientation(userID, joint, !MirroredMovement); 156 | 157 | posJoint -= posPointMan; 158 | 159 | if(MirroredMovement) 160 | { 161 | posJoint.x = -posJoint.x; 162 | posJoint.z = -posJoint.z; 163 | } 164 | 165 | bones[i].transform.localPosition = posJoint; 166 | bones[i].transform.localRotation = rotJoint; 167 | 168 | if(LinePrefab) 169 | { 170 | lines[i].gameObject.SetActive(true); 171 | Vector3 posJoint2 = bones[i].transform.position; 172 | 173 | Vector3 dirFromParent = manager.GetJointDirection(userID, joint); 174 | dirFromParent.z = !MirroredMovement ? -dirFromParent.z : dirFromParent.z; 175 | Vector3 posParent = posJoint2 - dirFromParent; 176 | 177 | //lines[i].SetVertexCount(2); 178 | lines[i].SetPosition(0, posParent); 179 | lines[i].SetPosition(1, posJoint2); 180 | } 181 | } 182 | else 183 | { 184 | bones[i].gameObject.SetActive(false); 185 | 186 | if(LinePrefab) 187 | { 188 | lines[i].gameObject.SetActive(false); 189 | } 190 | } 191 | } 192 | } 193 | } 194 | 195 | } 196 | -------------------------------------------------------------------------------- /Assets/AvatarsDemo/PointMan/PointManYellow.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/AvatarsDemo/PointMan/PointManYellow.prefab -------------------------------------------------------------------------------- /Assets/AvatarsDemo/PointMan/SkeletonLine.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/AvatarsDemo/PointMan/SkeletonLine.prefab -------------------------------------------------------------------------------- /Assets/AvatarsDemo/PointMan/YellowCube.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/AvatarsDemo/PointMan/YellowCube.prefab -------------------------------------------------------------------------------- /Assets/AvatarsDemo/Textures/grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/AvatarsDemo/Textures/grid.png -------------------------------------------------------------------------------- /Assets/Kinect2UnityExample.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/Kinect2UnityExample.unity -------------------------------------------------------------------------------- /Assets/KinectScripts/KinectManager.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System; 3 | using System.Collections; 4 | using System.Collections.Generic; 5 | 6 | public class KinectManager : MonoBehaviour 7 | { 8 | // Bool to control whether or not to start Kinect2Server internally 9 | public bool startKinectServer = true; 10 | 11 | // How high off the ground is the sensor (in meters). 12 | public float sensorHeight = 1.0f; 13 | 14 | // Public Bool to determine whether to receive and compute the user map 15 | public bool computeUserMap = false; 16 | 17 | // Public Bool to determine whether to receive and compute the color map 18 | public bool computeColorMap = false; 19 | 20 | // Public Bool to determine whether to display user map on the GUI 21 | public bool displayUserMap = false; 22 | 23 | // Public Bool to determine whether to display color map on the GUI 24 | public bool displayColorMap = false; 25 | 26 | // Public Bool to determine whether to display the skeleton lines on user map 27 | public bool displaySkeletonLines = false; 28 | 29 | // Public Floats to specify the width and height of the depth and color maps as % of the camera width and height 30 | // if percents are zero, they are calculated based on actual Kinect image´s width and height 31 | private float MapsPercentWidth = 0f; 32 | private float MapsPercentHeight = 0f; 33 | 34 | // Minimum user distance in order to process skeleton data 35 | public float minUserDistance = 0.5f; 36 | 37 | // Public Bool to determine whether to detect only the closest user or not 38 | public bool detectClosestUser = true; 39 | 40 | // Public Bool to determine whether to use only the tracked joints (and ignore the inferred ones) 41 | public bool ignoreInferredJoints = true; 42 | 43 | // GUI Text to show messages. 44 | public GUIText calibrationText; 45 | 46 | 47 | // Bool to keep track of whether Kinect has been initialized 48 | private bool kinectInitialized = false; 49 | 50 | // The singleton instance of KinectManager 51 | private static KinectManager instance = null; 52 | 53 | // KinectServer instance 54 | private KinectServer kinectServer = null; 55 | 56 | // Depth and user maps 57 | private KinectWrapper.DepthBuffer depthImage; 58 | private KinectWrapper.BodyIndexBuffer bodyIndexImage; 59 | private KinectWrapper.UserHistogramBuffer userHistogramImage; 60 | private Texture2D usersLblTex; 61 | private Rect usersMapRect; 62 | // private Color32[] usersMapColors; 63 | // private ushort[] usersPrevState; 64 | // private float[] usersHistogramMap; 65 | // private int usersMapSize; 66 | private int minDepth; 67 | private int maxDepth; 68 | 69 | // Color map 70 | private KinectWrapper.ColorBuffer colorImage; 71 | private Texture2D usersClrTex; 72 | private Rect usersClrRect; 73 | 74 | // Kinect body frame data 75 | private KinectWrapper.BodyFrame bodyFrame; 76 | private Int64 lastFrameTime = 0; 77 | 78 | // List of all users 79 | private List alUserIds; 80 | private Dictionary dictUserIdToIndex; 81 | 82 | // Primary (first or closest) user ID 83 | private Int64 liPrimaryUserId = 0; 84 | 85 | // Kinect to world matrix 86 | private Matrix4x4 kinectToWorld; 87 | 88 | 89 | // returns the single KinectManager instance 90 | public static KinectManager Instance 91 | { 92 | get 93 | { 94 | return instance; 95 | } 96 | } 97 | 98 | // checks if Kinect is initialized and ready to use. If not, there was an error during Kinect-sensor initialization 99 | public static bool IsKinectInitialized() 100 | { 101 | return instance != null ? instance.kinectInitialized : false; 102 | } 103 | 104 | // checks if Kinect is initialized and ready to use. If not, there was an error during Kinect-sensor initialization 105 | public bool IsInitialized() 106 | { 107 | return kinectInitialized; 108 | } 109 | 110 | // returns the depth image/users histogram texture,if ComputeUserMap is true 111 | public Texture2D GetUsersLblTex() 112 | { 113 | return usersLblTex; 114 | } 115 | 116 | // returns the color image texture,if ComputeColorMap is true 117 | public Texture2D GetUsersClrTex() 118 | { 119 | return usersClrTex; 120 | } 121 | 122 | // returns true if at least one user is currently detected by the sensor 123 | public bool IsUserDetected() 124 | { 125 | return kinectInitialized && (alUserIds.Count > 0); 126 | } 127 | 128 | // returns true if the User is calibrated and ready to use 129 | public bool IsUserCalibrated(Int64 userId) 130 | { 131 | return dictUserIdToIndex.ContainsKey(userId); 132 | } 133 | 134 | // returns the number of currently detected users 135 | public int GetUsersCount() 136 | { 137 | return alUserIds.Count; 138 | } 139 | 140 | // returns the UserID by the given index 141 | public Int64 GetUserIdByIndex(int i) 142 | { 143 | if(i >= 0 && i < alUserIds.Count) 144 | { 145 | return alUserIds[i]; 146 | } 147 | 148 | return 0; 149 | } 150 | 151 | // returns the UserID of the primary user (the first or the closest one), if there is any 152 | public Int64 GetPrimaryUser() 153 | { 154 | return liPrimaryUserId; 155 | } 156 | 157 | // returns the User position, relative to the Kinect-sensor, in meters 158 | public Vector3 GetUserPosition(Int64 userId) 159 | { 160 | if(dictUserIdToIndex.ContainsKey(userId)) 161 | { 162 | int index = dictUserIdToIndex[userId]; 163 | 164 | if(index >= 0 && index < KinectWrapper.Constants.BodyCount && 165 | bodyFrame.bodyData[index].bIsTracked != 0) 166 | { 167 | return bodyFrame.bodyData[index].position; 168 | } 169 | } 170 | 171 | return Vector3.zero; 172 | } 173 | 174 | // returns the User rotation, relative to the Kinect-sensor 175 | public Quaternion GetUserOrientation(Int64 userId, bool flip) 176 | { 177 | if(dictUserIdToIndex.ContainsKey(userId)) 178 | { 179 | int index = dictUserIdToIndex[userId]; 180 | 181 | if(index >= 0 && index < KinectWrapper.Constants.BodyCount && 182 | bodyFrame.bodyData[index].bIsTracked != 0) 183 | { 184 | return bodyFrame.bodyData[index].orientation; 185 | } 186 | } 187 | 188 | return Quaternion.identity; 189 | } 190 | 191 | // returns true if the given joint of the specified user is being tracked 192 | public bool IsJointTracked(Int64 userId, int joint) 193 | { 194 | if(dictUserIdToIndex.ContainsKey(userId)) 195 | { 196 | int index = dictUserIdToIndex[userId]; 197 | 198 | if(index >= 0 && index < KinectWrapper.Constants.BodyCount && 199 | bodyFrame.bodyData[index].bIsTracked != 0) 200 | { 201 | if(joint >= 0 && joint < KinectWrapper.Constants.JointCount) 202 | { 203 | KinectWrapper.Joint jointData = bodyFrame.bodyData[index].joint[joint]; 204 | 205 | return ignoreInferredJoints ? (jointData.trackingState == KinectWrapper.TrackingState.Tracked) : 206 | (jointData.trackingState != KinectWrapper.TrackingState.NotTracked); 207 | } 208 | } 209 | } 210 | 211 | return false; 212 | } 213 | 214 | // returns the joint position of the specified user, relative to the Kinect-sensor, in meters 215 | public Vector3 GetJointPosition(Int64 userId, int joint) 216 | { 217 | if(dictUserIdToIndex.ContainsKey(userId)) 218 | { 219 | int index = dictUserIdToIndex[userId]; 220 | 221 | if(index >= 0 && index < KinectWrapper.Constants.BodyCount && 222 | bodyFrame.bodyData[index].bIsTracked != 0) 223 | { 224 | if(joint >= 0 && joint < KinectWrapper.Constants.JointCount) 225 | { 226 | KinectWrapper.Joint jointData = bodyFrame.bodyData[index].joint[joint]; 227 | return jointData.position; 228 | } 229 | } 230 | } 231 | 232 | return Vector3.zero; 233 | } 234 | 235 | // returns the joint direction of the specified user, relative to the parent joint 236 | public Vector3 GetJointDirection(Int64 userId, int joint) 237 | { 238 | if(dictUserIdToIndex.ContainsKey(userId)) 239 | { 240 | int index = dictUserIdToIndex[userId]; 241 | 242 | if(index >= 0 && index < KinectWrapper.Constants.BodyCount && 243 | bodyFrame.bodyData[index].bIsTracked != 0) 244 | { 245 | if(joint >= 0 && joint < KinectWrapper.Constants.JointCount) 246 | { 247 | KinectWrapper.Joint jointData = bodyFrame.bodyData[index].joint[joint]; 248 | return jointData.direction; 249 | } 250 | } 251 | } 252 | 253 | return Vector3.zero; 254 | } 255 | 256 | // returns the joint rotation of the specified user, relative to the Kinect-sensor 257 | public Quaternion GetJointOrientation(Int64 userId, int joint, bool flip) 258 | { 259 | if(dictUserIdToIndex.ContainsKey(userId)) 260 | { 261 | int index = dictUserIdToIndex[userId]; 262 | 263 | if(index >= 0 && index < KinectWrapper.Constants.BodyCount && 264 | bodyFrame.bodyData[index].bIsTracked != 0) 265 | { 266 | if(joint >= 0 && joint < KinectWrapper.Constants.JointCount) 267 | { 268 | KinectWrapper.Joint jointData = bodyFrame.bodyData[index].joint[joint]; 269 | return jointData.orientation; 270 | } 271 | } 272 | } 273 | 274 | return Quaternion.identity; 275 | } 276 | 277 | // checks if the left hand confidence for a user is high 278 | // returns true if the confidence is high, false if it is low or user is not found 279 | public bool IsLeftHandConfidenceHigh(Int64 userId) 280 | { 281 | if(dictUserIdToIndex.ContainsKey(userId)) 282 | { 283 | int index = dictUserIdToIndex[userId]; 284 | 285 | if(index >= 0 && index < KinectWrapper.Constants.BodyCount && 286 | bodyFrame.bodyData[index].bIsTracked != 0) 287 | { 288 | return (bodyFrame.bodyData[index].leftHandConfidence == KinectWrapper.TrackingConfidence.High); 289 | } 290 | } 291 | 292 | return false; 293 | } 294 | 295 | // checks if the right hand confidence for a user is high 296 | // returns true if the confidence is high, false if it is low or user is not found 297 | public bool IsRightHandConfidenceHigh(Int64 userId) 298 | { 299 | if(dictUserIdToIndex.ContainsKey(userId)) 300 | { 301 | int index = dictUserIdToIndex[userId]; 302 | 303 | if(index >= 0 && index < KinectWrapper.Constants.BodyCount && 304 | bodyFrame.bodyData[index].bIsTracked != 0) 305 | { 306 | return (bodyFrame.bodyData[index].rightHandConfidence == KinectWrapper.TrackingConfidence.High); 307 | } 308 | } 309 | 310 | return false; 311 | } 312 | 313 | // returns the left hand state for a user 314 | public KinectWrapper.HandState GetLeftHandState(Int64 userId) 315 | { 316 | if(dictUserIdToIndex.ContainsKey(userId)) 317 | { 318 | int index = dictUserIdToIndex[userId]; 319 | 320 | if(index >= 0 && index < KinectWrapper.Constants.BodyCount && 321 | bodyFrame.bodyData[index].bIsTracked != 0) 322 | { 323 | return bodyFrame.bodyData[index].leftHandState; 324 | } 325 | } 326 | 327 | return KinectWrapper.HandState.NotTracked; 328 | } 329 | 330 | // returns the right hand state for a user 331 | public KinectWrapper.HandState GetRightHandState(Int64 userId) 332 | { 333 | if(dictUserIdToIndex.ContainsKey(userId)) 334 | { 335 | int index = dictUserIdToIndex[userId]; 336 | 337 | if(index >= 0 && index < KinectWrapper.Constants.BodyCount && 338 | bodyFrame.bodyData[index].bIsTracked != 0) 339 | { 340 | return bodyFrame.bodyData[index].rightHandState; 341 | } 342 | } 343 | 344 | return KinectWrapper.HandState.NotTracked; 345 | } 346 | 347 | // returns the interaction box for the left hand of the specified user, in meters 348 | public bool GetLeftHandInteractionBox(Int64 userId, ref Vector3 leftBotBack, ref Vector3 rightTopFront, bool bValidBox) 349 | { 350 | if(dictUserIdToIndex.ContainsKey(userId)) 351 | { 352 | int index = dictUserIdToIndex[userId]; 353 | 354 | if(index >= 0 && index < KinectWrapper.Constants.BodyCount && 355 | bodyFrame.bodyData[index].bIsTracked != 0) 356 | { 357 | KinectWrapper.BodyData bodyData = bodyFrame.bodyData[index]; 358 | bool bResult = true; 359 | 360 | if(bodyData.joint[(int)KinectWrapper.JointType.ShoulderRight].trackingState == KinectWrapper.TrackingState.Tracked && 361 | bodyData.joint[(int)KinectWrapper.JointType.HipLeft].trackingState == KinectWrapper.TrackingState.Tracked) 362 | { 363 | rightTopFront.x = bodyData.joint[(int)KinectWrapper.JointType.ShoulderRight].position.x; 364 | leftBotBack.x = rightTopFront.x - 2 * (rightTopFront.x - bodyData.joint[(int)KinectWrapper.JointType.HipLeft].position.x); 365 | } 366 | else 367 | { 368 | bResult = bValidBox; 369 | } 370 | 371 | if(bodyData.joint[(int)KinectWrapper.JointType.HipRight].trackingState == KinectWrapper.TrackingState.Tracked && 372 | bodyData.joint[(int)KinectWrapper.JointType.ShoulderRight].trackingState == KinectWrapper.TrackingState.Tracked) 373 | { 374 | leftBotBack.y = bodyData.joint[(int)KinectWrapper.JointType.HipRight].position.y; 375 | rightTopFront.y = bodyData.joint[(int)KinectWrapper.JointType.ShoulderRight].position.y; 376 | 377 | float fDelta = (rightTopFront.y - leftBotBack.y) * 2 / 3; 378 | leftBotBack.y += fDelta; 379 | rightTopFront.y += fDelta; 380 | } 381 | else 382 | { 383 | bResult = bValidBox; 384 | } 385 | 386 | if(bodyData.joint[(int)KinectWrapper.JointType.HipCenter].trackingState == KinectWrapper.TrackingState.Tracked) 387 | { 388 | leftBotBack.z = bodyData.joint[(int)KinectWrapper.JointType.HipCenter].position.z; 389 | rightTopFront.z = leftBotBack.z - 0.5f; 390 | } 391 | else 392 | { 393 | bResult = bValidBox; 394 | } 395 | 396 | return bResult; 397 | } 398 | } 399 | 400 | return false; 401 | } 402 | 403 | // returns the interaction box for the right hand of the specified user, in meters 404 | public bool GetRightHandInteractionBox(Int64 userId, ref Vector3 leftBotBack, ref Vector3 rightTopFront, bool bValidBox) 405 | { 406 | if(dictUserIdToIndex.ContainsKey(userId)) 407 | { 408 | int index = dictUserIdToIndex[userId]; 409 | 410 | if(index >= 0 && index < KinectWrapper.Constants.BodyCount && 411 | bodyFrame.bodyData[index].bIsTracked != 0) 412 | { 413 | KinectWrapper.BodyData bodyData = bodyFrame.bodyData[index]; 414 | bool bResult = true; 415 | 416 | if(bodyData.joint[(int)KinectWrapper.JointType.ShoulderLeft].trackingState == KinectWrapper.TrackingState.Tracked && 417 | bodyData.joint[(int)KinectWrapper.JointType.HipRight].trackingState == KinectWrapper.TrackingState.Tracked) 418 | { 419 | leftBotBack.x = bodyData.joint[(int)KinectWrapper.JointType.ShoulderLeft].position.x; 420 | rightTopFront.x = leftBotBack.x + 2 * (bodyData.joint[(int)KinectWrapper.JointType.HipRight].position.x - leftBotBack.x); 421 | } 422 | else 423 | { 424 | bResult = bValidBox; 425 | } 426 | 427 | if(bodyData.joint[(int)KinectWrapper.JointType.HipLeft].trackingState == KinectWrapper.TrackingState.Tracked && 428 | bodyData.joint[(int)KinectWrapper.JointType.ShoulderLeft].trackingState == KinectWrapper.TrackingState.Tracked) 429 | { 430 | leftBotBack.y = bodyData.joint[(int)KinectWrapper.JointType.HipLeft].position.y; 431 | rightTopFront.y = bodyData.joint[(int)KinectWrapper.JointType.ShoulderLeft].position.y; 432 | 433 | float fDelta = (rightTopFront.y - leftBotBack.y) * 2 / 3; 434 | leftBotBack.y += fDelta; 435 | rightTopFront.y += fDelta; 436 | } 437 | else 438 | { 439 | bResult = bValidBox; 440 | } 441 | 442 | if(bodyData.joint[(int)KinectWrapper.JointType.HipCenter].trackingState == KinectWrapper.TrackingState.Tracked) 443 | { 444 | leftBotBack.z = bodyData.joint[(int)KinectWrapper.JointType.HipCenter].position.z; 445 | rightTopFront.z = leftBotBack.z - 0.5f; 446 | } 447 | else 448 | { 449 | bResult = bValidBox; 450 | } 451 | 452 | return bResult; 453 | } 454 | } 455 | 456 | return false; 457 | } 458 | 459 | 460 | // KinectManager's Internal Methods 461 | 462 | void Awake() 463 | { 464 | try 465 | { 466 | if(KinectWrapper.EnsureKinectWrapperPresence()) 467 | { 468 | // reload the same level 469 | Application.LoadLevel(Application.loadedLevel); 470 | } 471 | } 472 | catch (Exception ex) 473 | { 474 | Debug.LogError(ex.ToString()); 475 | 476 | if(calibrationText != null) 477 | { 478 | calibrationText.guiText.text = ex.Message; 479 | } 480 | } 481 | } 482 | 483 | void Start() 484 | { 485 | int hr = 0; 486 | 487 | try 488 | { 489 | if(startKinectServer) 490 | { 491 | // start the Kinect-server app 492 | hr = StartKinectServer(); 493 | if (hr != 0) 494 | { 495 | throw new Exception("Kinect2Server not started"); 496 | } 497 | } 498 | 499 | // try to initialize the default Kinect2 sensor 500 | KinectWrapper.FrameSource dwFlags = KinectWrapper.FrameSource.TypeBody; 501 | if(computeColorMap) 502 | dwFlags |= KinectWrapper.FrameSource.TypeColor; 503 | if(computeUserMap) 504 | dwFlags |= KinectWrapper.FrameSource.TypeDepth | KinectWrapper.FrameSource.TypeBodyIndex | KinectWrapper.FrameSource.TypeInfrared; 505 | 506 | hr = KinectWrapper.InitDefaultKinectSensor(dwFlags, KinectWrapper.Constants.ColorImageWidth, KinectWrapper.Constants.ColorImageHeight); 507 | if (hr != 0) 508 | { 509 | throw new Exception("InitDefaultKinectSensor failed"); 510 | } 511 | 512 | // transform matrix - kinect to world 513 | kinectToWorld.SetTRS(new Vector3(0.0f, sensorHeight, 0.0f), Quaternion.identity, Vector3.one); 514 | } 515 | catch(Exception ex) 516 | { 517 | string message = ex.Message + " - " + KinectWrapper.GetSystemErrorMessage(hr); 518 | Debug.LogError(message); 519 | 520 | Debug.LogException(ex); 521 | 522 | if(calibrationText != null) 523 | { 524 | calibrationText.guiText.text = message; 525 | } 526 | 527 | return; 528 | } 529 | 530 | // init skeleton structures 531 | bodyFrame = new KinectWrapper.BodyFrame(true); 532 | 533 | // get the main camera rectangle 534 | Rect cameraRect = Camera.main.pixelRect; 535 | 536 | // calculate map width and height in percent, if needed 537 | if(MapsPercentWidth == 0f) 538 | MapsPercentWidth = (KinectWrapper.Constants.DepthImageWidth / 2) / cameraRect.width; 539 | if(MapsPercentHeight == 0f) 540 | MapsPercentHeight = (KinectWrapper.Constants.DepthImageHeight / 2) / cameraRect.height; 541 | 542 | if(computeUserMap) 543 | { 544 | // init user-depth structures 545 | //depthImage = new KinectWrapper.DepthBuffer(true); 546 | //bodyIndexImage = new KinectWrapper.BodyIndexBuffer(true); 547 | 548 | // Initialize depth & label map related stuff 549 | // usersMapSize = KinectWrapper.Constants.DepthImageWidth * KinectWrapper.Constants.DepthImageHeight; 550 | usersLblTex = new Texture2D(KinectWrapper.Constants.DepthImageWidth, KinectWrapper.Constants.DepthImageHeight); 551 | // usersMapColors = new Color32[usersMapSize]; 552 | // usersPrevState = new ushort[usersMapSize]; 553 | usersMapRect = new Rect(cameraRect.width - cameraRect.width * MapsPercentWidth, cameraRect.height, cameraRect.width * MapsPercentWidth, -cameraRect.height * MapsPercentHeight); 554 | // usersHistogramMap = new float[8192]; 555 | } 556 | 557 | if(computeColorMap) 558 | { 559 | // init color image structures 560 | //colorImage = new KinectWrapper.ColorBuffer(true); 561 | 562 | // Initialize color map related stuff 563 | usersClrTex = new Texture2D(KinectWrapper.Constants.ColorImageWidth, KinectWrapper.Constants.ColorImageHeight); 564 | usersClrRect = new Rect(cameraRect.width - cameraRect.width * MapsPercentWidth, cameraRect.height, cameraRect.width * MapsPercentWidth, -cameraRect.height * MapsPercentHeight); 565 | 566 | if(computeUserMap) 567 | { 568 | usersMapRect.x -= cameraRect.width * MapsPercentWidth; //usersClrTex.width / 2; 569 | } 570 | } 571 | 572 | // Initialize user list to contain all users. 573 | alUserIds = new List(); 574 | dictUserIdToIndex = new Dictionary(); 575 | 576 | kinectInitialized = true; 577 | instance = this; 578 | 579 | DontDestroyOnLoad(gameObject); 580 | 581 | // GUI Text. 582 | if(calibrationText != null) 583 | { 584 | calibrationText.guiText.text = "WAITING FOR USERS"; 585 | } 586 | 587 | Debug.Log("Waiting for users."); 588 | } 589 | 590 | // Start the Kinect2Server app 591 | private int StartKinectServer() 592 | { 593 | // start Kinect Server 594 | kinectServer = new KinectServer(); 595 | kinectServer.RunKinectServer(); 596 | 597 | float fTimeToWait = Time.realtimeSinceStartup + 10f; // allow 10 seconds time-out 598 | int iPing = 0; 599 | 600 | while(Time.realtimeSinceStartup < fTimeToWait) 601 | { 602 | iPing = KinectWrapper.PingKinect2Server(); 603 | if(iPing == 12345678) 604 | break; 605 | } 606 | 607 | if(iPing == 12345678) 608 | { 609 | iPing = 0; 610 | } 611 | 612 | return iPing; 613 | } 614 | 615 | void OnApplicationQuit() 616 | { 617 | // shut down the Kinect on quitting. 618 | if(kinectInitialized) 619 | { 620 | KinectWrapper.ShutdownKinectSensor(); 621 | instance = null; 622 | } 623 | 624 | // shut down the server, if any 625 | if(kinectServer != null) 626 | { 627 | kinectServer.ShutdownKinectServer(); 628 | } 629 | } 630 | 631 | void OnGUI() 632 | { 633 | if(kinectInitialized) 634 | { 635 | if(computeUserMap && displayUserMap) 636 | { 637 | GUI.DrawTexture(usersMapRect, usersLblTex); 638 | } 639 | 640 | if(computeColorMap && displayColorMap) 641 | { 642 | GUI.DrawTexture(usersClrRect, usersClrTex); 643 | } 644 | } 645 | } 646 | 647 | void Update() 648 | { 649 | if(kinectInitialized) 650 | { 651 | if(KinectWrapper.PollSkeleton(ref bodyFrame, lastFrameTime)) 652 | { 653 | lastFrameTime = bodyFrame.liRelativeTime; 654 | ProcessBodyFrameData(); 655 | } 656 | 657 | if(computeColorMap) 658 | { 659 | if(KinectWrapper.PollColorFrame(ref colorImage)) 660 | { 661 | UpdateColorMap(); 662 | } 663 | } 664 | 665 | if(computeUserMap) 666 | { 667 | if(KinectWrapper.PollDepthFrame(ref depthImage, ref bodyIndexImage, ref minDepth, ref maxDepth)) 668 | { 669 | UpdateUserMap(); 670 | } 671 | } 672 | 673 | } 674 | } 675 | 676 | // Update the user histogram 677 | void UpdateUserMap() 678 | { 679 | if(KinectWrapper.PollUserHistogramFrame(ref userHistogramImage, computeColorMap)) 680 | { 681 | // draw user histogram 682 | usersLblTex.SetPixels32(userHistogramImage.pixels); 683 | 684 | // draw skeleton lines 685 | if(displaySkeletonLines) 686 | { 687 | for(int i = 0; i < alUserIds.Count; i++) 688 | { 689 | Int64 liUserId = alUserIds[i]; 690 | int index = dictUserIdToIndex[liUserId]; 691 | 692 | if(index >= 0 && index < KinectWrapper.Constants.BodyCount) 693 | { 694 | DrawSkeleton(usersLblTex, ref bodyFrame.bodyData[index]); 695 | } 696 | } 697 | } 698 | 699 | usersLblTex.Apply(); 700 | } 701 | } 702 | 703 | // Update the color image 704 | void UpdateColorMap() 705 | { 706 | usersClrTex.SetPixels32(colorImage.pixels); 707 | usersClrTex.Apply(); 708 | } 709 | 710 | // Processes body frame data 711 | private void ProcessBodyFrameData() 712 | { 713 | List lostUsers = new List(); 714 | lostUsers.AddRange(alUserIds); 715 | 716 | for(int i = 0; i < KinectWrapper.Constants.BodyCount; i++) 717 | { 718 | KinectWrapper.BodyData bodyData = bodyFrame.bodyData[i]; 719 | Int64 userId = bodyData.liTrackingID; 720 | 721 | if(bodyData.bIsTracked != 0 && Mathf.Abs(kinectToWorld.MultiplyPoint3x4(bodyData.position).z) >= minUserDistance) 722 | { 723 | // get the body position 724 | Vector3 bodyPos = kinectToWorld.MultiplyPoint3x4(bodyData.position); 725 | 726 | if(liPrimaryUserId == 0) 727 | { 728 | // check if this is the closest user 729 | bool bClosestUser = true; 730 | int iClosestUserIndex = i; 731 | 732 | if(detectClosestUser) 733 | { 734 | for(int j = 0; j < KinectWrapper.Constants.BodyCount; j++) 735 | { 736 | if(j != i) 737 | { 738 | KinectWrapper.BodyData bodyDataOther = bodyFrame.bodyData[j]; 739 | 740 | if((bodyDataOther.bIsTracked != 0) && 741 | (Mathf.Abs(kinectToWorld.MultiplyPoint3x4(bodyDataOther.position).z) < Mathf.Abs(bodyPos.z))) 742 | { 743 | bClosestUser = false; 744 | iClosestUserIndex = j; 745 | break; 746 | } 747 | } 748 | } 749 | } 750 | 751 | if(bClosestUser) 752 | { 753 | // calibrate the first or closest user 754 | CalibrateUser(userId, iClosestUserIndex); 755 | } 756 | } 757 | 758 | // calibrate current user 759 | CalibrateUser(userId, i); 760 | 761 | // convert Kinect positions to world positions 762 | bodyFrame.bodyData[i].position = bodyPos; 763 | 764 | for (int j = 0; j < KinectWrapper.Constants.JointCount; j++) 765 | { 766 | bodyData.joint[j].position = kinectToWorld.MultiplyPoint3x4(bodyData.joint[j].position); 767 | 768 | if((bodyData.liTrackingID == liPrimaryUserId) && (j == (int)KinectWrapper.JointType.HipCenter) && 769 | bodyData.joint[j].trackingState == KinectWrapper.TrackingState.Tracked) 770 | { 771 | string debugText = String.Format("Body Pos: {0}", bodyData.joint[j].position); 772 | 773 | if(calibrationText) 774 | { 775 | calibrationText.guiText.text = debugText; 776 | } 777 | } 778 | } 779 | 780 | lostUsers.Remove(userId); 781 | } 782 | } 783 | 784 | // remove the lost users if any 785 | if(lostUsers.Count > 0) 786 | { 787 | foreach(Int64 userId in lostUsers) 788 | { 789 | RemoveUser(userId); 790 | } 791 | 792 | lostUsers.Clear(); 793 | } 794 | } 795 | 796 | // Adds UserId to the list of users 797 | void CalibrateUser(Int64 userId, int userIndex) 798 | { 799 | if(!alUserIds.Contains(userId)) 800 | { 801 | alUserIds.Add(userId); 802 | dictUserIdToIndex[userId] = userIndex; 803 | 804 | if(liPrimaryUserId == 0) 805 | { 806 | liPrimaryUserId = userId; 807 | 808 | if(liPrimaryUserId != 0) 809 | { 810 | if(calibrationText != null && calibrationText.guiText.text != "") 811 | { 812 | calibrationText.guiText.text = ""; 813 | } 814 | } 815 | } 816 | } 817 | } 818 | 819 | // Remove a lost UserId 820 | void RemoveUser(Int64 userId) 821 | { 822 | // remove from global users list 823 | alUserIds.Remove(userId); 824 | dictUserIdToIndex.Remove(userId); 825 | 826 | if(liPrimaryUserId == userId) 827 | { 828 | if(alUserIds.Count > 0) 829 | { 830 | liPrimaryUserId = alUserIds[0]; 831 | } 832 | else 833 | { 834 | liPrimaryUserId = 0; 835 | } 836 | } 837 | 838 | if(liPrimaryUserId == 0) 839 | { 840 | //Debug.Log("Waiting for users."); 841 | 842 | if(calibrationText != null && calibrationText.guiText.text == "") 843 | { 844 | calibrationText.guiText.text = "WAITING FOR USERS"; 845 | } 846 | } 847 | } 848 | 849 | // draws the skeleton in the given texture 850 | private void DrawSkeleton(Texture2D aTexture, ref KinectWrapper.BodyData bodyData) 851 | { 852 | int jointsCount = KinectWrapper.Constants.JointCount; 853 | 854 | for(int i = 0; i < jointsCount; i++) 855 | { 856 | int parent = (int)KinectWrapper.GetParentJoint((KinectWrapper.JointType)i); 857 | 858 | if(bodyData.joint[i].trackingState == KinectWrapper.TrackingState.Tracked && bodyData.joint[parent].trackingState == KinectWrapper.TrackingState.Tracked) 859 | { 860 | Vector2 posParent = KinectWrapper.GetKinectPointDepthCoords(bodyData.joint[parent].kinectPos); 861 | Vector2 posJoint = KinectWrapper.GetKinectPointDepthCoords(bodyData.joint[i].kinectPos); 862 | 863 | // posParent.y = KinectWrapper.Constants.ImageHeight - posParent.y - 1; 864 | // posJoint.y = KinectWrapper.Constants.ImageHeight - posJoint.y - 1; 865 | // posParent.x = KinectWrapper.Constants.ImageWidth - posParent.x - 1; 866 | // posJoint.x = KinectWrapper.Constants.ImageWidth - posJoint.x - 1; 867 | 868 | //Color lineColor = playerJointsTracked[i] && playerJointsTracked[parent] ? Color.red : Color.yellow; 869 | DrawLine(aTexture, (int)posParent.x, (int)posParent.y, (int)posJoint.x, (int)posJoint.y, Color.yellow); 870 | } 871 | } 872 | 873 | //aTexture.Apply(); 874 | } 875 | 876 | // draws a line in a texture 877 | private void DrawLine(Texture2D a_Texture, int x1, int y1, int x2, int y2, Color a_Color) 878 | { 879 | int width = KinectWrapper.Constants.DepthImageWidth; 880 | int height = KinectWrapper.Constants.DepthImageHeight; 881 | 882 | int dy = y2 - y1; 883 | int dx = x2 - x1; 884 | 885 | int stepy = 1; 886 | if (dy < 0) 887 | { 888 | dy = -dy; 889 | stepy = -1; 890 | } 891 | 892 | int stepx = 1; 893 | if (dx < 0) 894 | { 895 | dx = -dx; 896 | stepx = -1; 897 | } 898 | 899 | dy <<= 1; 900 | dx <<= 1; 901 | 902 | if(x1 >= 0 && x1 < width && y1 >= 0 && y1 < height) 903 | for(int x = -1; x <= 1; x++) 904 | for(int y = -1; y <= 1; y++) 905 | a_Texture.SetPixel(x1 + x, y1 + y, a_Color); 906 | 907 | if (dx > dy) 908 | { 909 | int fraction = dy - (dx >> 1); 910 | 911 | while (x1 != x2) 912 | { 913 | if (fraction >= 0) 914 | { 915 | y1 += stepy; 916 | fraction -= dx; 917 | } 918 | 919 | x1 += stepx; 920 | fraction += dy; 921 | 922 | if(x1 >= 0 && x1 < width && y1 >= 0 && y1 < height) 923 | for(int x = -1; x <= 1; x++) 924 | for(int y = -1; y <= 1; y++) 925 | a_Texture.SetPixel(x1 + x, y1 + y, a_Color); 926 | } 927 | } 928 | else 929 | { 930 | int fraction = dx - (dy >> 1); 931 | 932 | while (y1 != y2) 933 | { 934 | if (fraction >= 0) 935 | { 936 | x1 += stepx; 937 | fraction -= dy; 938 | } 939 | 940 | y1 += stepy; 941 | fraction += dx; 942 | 943 | if(x1 >= 0 && x1 < width && y1 >= 0 && y1 < height) 944 | for(int x = -1; x <= 1; x++) 945 | for(int y = -1; y <= 1; y++) 946 | a_Texture.SetPixel(x1 + x, y1 + y, a_Color); 947 | } 948 | } 949 | 950 | } 951 | 952 | } 953 | -------------------------------------------------------------------------------- /Assets/KinectScripts/KinectServer.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System; 3 | using System.Runtime.InteropServices; 4 | //using System.Diagnostics; 5 | 6 | public class KinectServer 7 | { 8 | private System.Diagnostics.Process procServer; 9 | 10 | [DllImport("kernel32.dll", SetLastError=true)] 11 | public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr); 12 | 13 | [DllImport("kernel32.dll", SetLastError=true)] 14 | public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr); 15 | 16 | 17 | public void RunKinectServer() 18 | { 19 | procServer = new System.Diagnostics.Process(); 20 | procServer.StartInfo.FileName = Application.dataPath + @"/../KinectServer/Kinect2UnityServer.exe"; 21 | procServer.StartInfo.WorkingDirectory = Application.dataPath + @"/../KinectServer"; 22 | procServer.StartInfo.UseShellExecute = false; 23 | procServer.StartInfo.CreateNoWindow = false; 24 | procServer.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized; 25 | procServer.StartInfo.RedirectStandardInput = true; 26 | // procServer.StartInfo.RedirectStandardOutput = true; 27 | // procServer.StartInfo.RedirectStandardError = true; 28 | // procServer.OutputDataReceived += (sender, args) => Debug.Log(args.Data); 29 | // procServer.ErrorDataReceived += (sender, args) => Debug.LogError(args.Data); 30 | 31 | try 32 | { 33 | //IntPtr ptr = new IntPtr(); 34 | //Wow64DisableWow64FsRedirection(ref ptr); 35 | procServer.Start(); 36 | //Wow64RevertWow64FsRedirection(ptr); 37 | } 38 | catch(Exception e) 39 | { 40 | Debug.LogError("Could not find Kinect2UnityServer.exe"); 41 | Debug.LogException(e); 42 | procServer = null; 43 | return; 44 | } 45 | 46 | //procServer.BeginOutputReadLine(); 47 | ////procServer.StandardInput.Write("0"); // gets rid of the Byte-order mark in the pipe. 48 | } 49 | 50 | public void ShutdownKinectServer() 51 | { 52 | if (procServer == null) 53 | return; 54 | 55 | try 56 | { 57 | System.Diagnostics.Process.GetProcessById(procServer.Id); 58 | } 59 | catch (ArgumentException) 60 | { 61 | // The other app might have been shut down externally 62 | return; 63 | } 64 | 65 | try 66 | { 67 | procServer.StandardInput.WriteLine("exit"); 68 | } 69 | catch (InvalidOperationException) 70 | { 71 | // The other app might have been shut down externally already. 72 | } 73 | } 74 | } 75 | 76 | -------------------------------------------------------------------------------- /Assets/KinectScripts/KinectWrapper.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | using System; 6 | using System.IO; 7 | 8 | 9 | public class KinectWrapper 10 | { 11 | // constants 12 | public static class Constants 13 | { 14 | public const int BodyCount = 6; 15 | public const int JointCount = 25; 16 | 17 | public const int ColorImageWidth = 480; // 960; // 1920; 18 | public const int ColorImageHeight = 270; // 540; // 1080; 19 | public const int DepthImageWidth = 512; 20 | public const int DepthImageHeight = 424; 21 | } 22 | 23 | /// Data structures for interfacing C# with the native wrapper 24 | 25 | [Flags] 26 | public enum FrameSource : uint 27 | { 28 | TypeColor = 0x1, 29 | TypeInfrared = 0x2, 30 | TypeDepth = 0x8, 31 | TypeBodyIndex = 0x10, 32 | TypeBody = 0x20, 33 | TypeAudio = 0x40 34 | } 35 | 36 | public enum NuiErrorCodes : uint 37 | { 38 | FrameNoData = 0x83010001, 39 | StreamNotEnabled = 0x83010002, 40 | ImageStreamInUse = 0x83010003, 41 | FrameLimitExceeded = 0x83010004, 42 | FeatureNotInitialized = 0x83010005, 43 | DeviceNotGenuine = 0x83010006, 44 | InsufficientBandwidth = 0x83010007, 45 | DeviceNotSupported = 0x83010008, 46 | DeviceInUse = 0x83010009, 47 | 48 | DatabaseNotFound = 0x8301000D, 49 | DatabaseVersionMismatch = 0x8301000E, 50 | HardwareFeatureUnavailable = 0x8301000F, 51 | 52 | DeviceNotConnected = 0x83010014, 53 | DeviceNotReady = 0x83010015, 54 | SkeletalEngineBusy = 0x830100AA, 55 | DeviceNotPowered = 0x8301027F, 56 | } 57 | 58 | public enum JointType : int 59 | { 60 | HipCenter = 0, 61 | Spine = 1, 62 | Neck = 2, 63 | Head = 3, 64 | ShoulderLeft = 4, 65 | ElbowLeft = 5, 66 | WristLeft = 6, 67 | HandLeft = 7, 68 | ShoulderRight = 8, 69 | ElbowRight = 9, 70 | WristRight = 10, 71 | HandRight = 11, 72 | HipLeft = 12, 73 | KneeLeft = 13, 74 | AnkleLeft = 14, 75 | FootLeft = 15, 76 | HipRight = 16, 77 | KneeRight = 17, 78 | AnkleRight = 18, 79 | FootRight = 19, 80 | SpineShoulder = 20, 81 | HandTipLeft = 21, 82 | ThumbLeft = 22, 83 | HandTipRight = 23, 84 | ThumbRight = 24, 85 | Count = 25 86 | } 87 | 88 | public enum TrackingState 89 | { 90 | NotTracked = 0, 91 | Inferred = 1, 92 | Tracked = 2 93 | } 94 | 95 | public enum HandState 96 | { 97 | Unknown = 0, 98 | NotTracked = 1, 99 | Open = 2, 100 | Closed = 3, 101 | Lasso = 4 102 | } 103 | 104 | public enum TrackingConfidence 105 | { 106 | Low = 0, 107 | High = 1 108 | } 109 | 110 | [Flags] 111 | public enum ClippedEdges 112 | { 113 | None = 0, 114 | Right = 1, 115 | Left = 2, 116 | Top = 4, 117 | Bottom = 8 118 | } 119 | 120 | [StructLayout(LayoutKind.Sequential)] 121 | public struct Joint 122 | { 123 | public JointType jointType; 124 | public TrackingState trackingState; 125 | public Vector3 kinectPos; 126 | public Vector3 position; 127 | public Vector3 direction; 128 | public Quaternion orientation; 129 | } 130 | 131 | [StructLayout(LayoutKind.Sequential)] 132 | public struct BodyData 133 | { 134 | public Int64 liTrackingID; 135 | public Vector3 position; 136 | public Quaternion orientation; 137 | 138 | [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 25, ArraySubType = UnmanagedType.Struct)] 139 | public Joint[] joint; 140 | 141 | public HandState leftHandState; 142 | public TrackingConfidence leftHandConfidence; 143 | public HandState rightHandState; 144 | public TrackingConfidence rightHandConfidence; 145 | 146 | public uint dwClippedEdges; 147 | public short bIsTracked; 148 | public short bIsRestricted; 149 | } 150 | 151 | [StructLayout(LayoutKind.Sequential)] 152 | public struct BodyFrame 153 | { 154 | public Int64 liRelativeTime; 155 | [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 6, ArraySubType = UnmanagedType.Struct)] 156 | public BodyData[] bodyData; 157 | public Vector4 floorClipPlane; 158 | 159 | public BodyFrame(bool bInit) 160 | { 161 | liRelativeTime = 0; 162 | floorClipPlane = Vector4.zero; 163 | 164 | bodyData = new BodyData[Constants.BodyCount]; 165 | 166 | for(int i = 0; i < Constants.BodyCount; i++) 167 | { 168 | bodyData[i].joint = new Joint[Constants.JointCount]; 169 | } 170 | } 171 | } 172 | 173 | public struct ColorBuffer 174 | { 175 | [MarshalAs(UnmanagedType.ByValArray, SizeConst = Constants.ColorImageWidth * Constants.ColorImageHeight, ArraySubType = UnmanagedType.U4)] 176 | public Color32[] pixels; 177 | 178 | public ColorBuffer(bool bInit) 179 | { 180 | pixels = new Color32[Constants.ColorImageWidth * Constants.ColorImageHeight]; 181 | } 182 | } 183 | 184 | public struct DepthBuffer 185 | { 186 | [MarshalAs(UnmanagedType.ByValArray, SizeConst = Constants.DepthImageWidth * Constants.DepthImageHeight, ArraySubType = UnmanagedType.U2)] 187 | public ushort[] pixels; 188 | 189 | public DepthBuffer(bool bInit) 190 | { 191 | pixels = new ushort[Constants.ColorImageWidth * Constants.ColorImageHeight]; 192 | } 193 | } 194 | 195 | public struct BodyIndexBuffer 196 | { 197 | [MarshalAs(UnmanagedType.ByValArray, SizeConst = Constants.DepthImageWidth * Constants.DepthImageHeight, ArraySubType = UnmanagedType.U1)] 198 | public byte[] pixels; 199 | 200 | public BodyIndexBuffer(bool bInit) 201 | { 202 | pixels = new byte[Constants.ColorImageWidth * Constants.ColorImageHeight]; 203 | } 204 | } 205 | 206 | public struct UserHistogramBuffer 207 | { 208 | [MarshalAs(UnmanagedType.ByValArray, SizeConst = Constants.DepthImageWidth * Constants.DepthImageHeight, ArraySubType = UnmanagedType.U4)] 209 | public Color32[] pixels; 210 | 211 | public UserHistogramBuffer(bool bInit) 212 | { 213 | pixels = new Color32[Constants.DepthImageWidth * Constants.DepthImageHeight]; 214 | } 215 | } 216 | 217 | 218 | // Wrapped native functions 219 | // the version, the sample is built upon: 220 | [DllImport("Kernel32.dll", SetLastError = true)] 221 | static extern uint FormatMessage( uint dwFlags, IntPtr lpSource, uint dwMessageId, uint dwLanguageId, ref IntPtr lpBuffer, uint nSize, IntPtr pArguments); 222 | 223 | [DllImport("kernel32.dll", SetLastError = true)] 224 | static extern IntPtr LocalFree(IntPtr hMem); 225 | 226 | // Pings the server 227 | [DllImportAttribute(@"Kinect2UnityClient.dll")] 228 | public static extern int PingKinect2Server(); 229 | 230 | // Initializes the default Kinect sensor 231 | [DllImportAttribute(@"Kinect2UnityClient.dll")] 232 | public static extern int InitDefaultKinectSensor(FrameSource dwFlags, int iColorWidth, int iColorHeight); 233 | 234 | // Shuts down the opened Kinect2 sensor 235 | [DllImportAttribute(@"Kinect2UnityClient.dll")] 236 | public static extern void ShutdownKinectSensor(); 237 | 238 | // Returns the maximum number of the bodies 239 | [DllImportAttribute(@"Kinect2UnityClient.dll")] 240 | public static extern int GetBodyCount(); 241 | 242 | // Returns the latest body frame data available 243 | [DllImportAttribute(@"Kinect2UnityClient.dll")] 244 | public static extern int GetBodyFrameData(ref BodyFrame pBodyFrame, bool bGetOrientations, bool bGetHandStates); 245 | 246 | // // Returns the latest color frame data, if available 247 | // [DllImportAttribute(@"Kinect2UnityClient.dll")] 248 | // public static extern int GetColorFrameData(int iWidth, int iHeight, uint iBufferLength, ref ColorBuffer pColorFrame, ref Int64 liRelativeTime); 249 | // 250 | // // Returns the latest depth frame data, if available 251 | // [DllImportAttribute(@"Kinect2UnityClient.dll")] 252 | // public static extern int GetDepthFrameData(ref DepthBuffer pDepthFrame, ref int iMinDepthDistance, ref int iMaxDepthDistance, ref Int64 liRelativeTime); 253 | // 254 | // // Returns the latest infrared frame data, if available 255 | // [DllImportAttribute(@"Kinect2UnityClient.dll")] 256 | // public static extern int GetInfraredFrameData(ref DepthBuffer pInfraredFrame, ref Int64 liRelativeTime); 257 | // 258 | // // Returns the latest body-index frame data, if available 259 | // [DllImportAttribute(@"Kinect2UnityClient.dll")] 260 | // public static extern int GetBodyIndexFrameData(ref BodyIndexBuffer pBodyIndexFrame, ref Int64 liRelativeTime); 261 | 262 | 263 | // Polls frame data from all opened image streams 264 | [DllImportAttribute(@"Kinect2UnityClient.dll")] 265 | public static extern int PollImageFrameData(FrameSource dwFlags); 266 | 267 | // Gets the new color frame, if one is available. Returns S_OK if a new frame is available 268 | [DllImportAttribute(@"Kinect2UnityClient.dll")] 269 | public static extern int GetNewColorFrameData(ref IntPtr pColorFrame, ref Int64 liFrameTime); 270 | 271 | // Gets the new depth frame, if one available. Returns S_OK if a new frame is available 272 | [DllImportAttribute(@"Kinect2UnityClient.dll")] 273 | public static extern int GetNewDepthFrameData(ref IntPtr pDepthFrame, ref Int64 liFrameTime, ref int iMinDepth, ref int iMaxDepth); 274 | 275 | // Gets the new infrared frame, if one available. Returns S_OK if a new frame is available 276 | [DllImportAttribute(@"Kinect2UnityClient.dll")] 277 | public static extern int GetNewInfraredFrameData(ref IntPtr pInfraredFrame, ref Int64 liFrameTime); 278 | 279 | // Gets the new body index frame, if one available. Returns S_OK if a new frame is available 280 | [DllImportAttribute(@"Kinect2UnityClient.dll")] 281 | public static extern int GetNewBodyIndexFrameData(ref IntPtr pBodyIndexFrame, ref Int64 liFrameTime); 282 | 283 | // Gets the new users' histogram frame, if one available. Returns S_OK if a new frame is available 284 | [DllImportAttribute(@"Kinect2UnityClient.dll")] 285 | public static extern int GetUserHistogramFrameData(ref IntPtr pUserHistogramFrame, ref Int64 liFrameTime, bool bUseColorData); 286 | 287 | 288 | // Returns color frame coordinates of a 3d Kinect-point 289 | [DllImportAttribute(@"Kinect2UnityClient.dll")] 290 | public static extern int GetKinectPointColorCoords(float x, float y, float z, out int cx, out int cy); 291 | 292 | // Returns depth frame coordinates of a 3d Kinect-point 293 | [DllImportAttribute(@"Kinect2UnityClient.dll")] 294 | public static extern int GetKinectPointDepthCoords(float x, float y, float z, out int dx, out int dy); 295 | 296 | // Returns Kinect 3d-coordinates of a depth point 297 | [DllImportAttribute(@"Kinect2UnityClient.dll")] 298 | public static extern int GetDepthPointKinectCoords(int dx, int dy, ushort depthValue, out float kx, out float ky, out float kz); 299 | 300 | // Returns color frame coordinates of a depth point 301 | [DllImportAttribute(@"Kinect2UnityClient.dll")] 302 | public static extern int GetDepthPointColorCoords(int dx, int dy, ushort depthValue, out int cx, out int cy); 303 | 304 | 305 | // Public unility functions 306 | 307 | // Returns the system error message 308 | public static string GetSystemErrorMessage(int hr) 309 | { 310 | string message = string.Empty; 311 | uint uhr = (uint)hr; 312 | 313 | const uint FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100; 314 | const uint FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; 315 | const uint FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000; 316 | 317 | IntPtr lpMsgBuf = IntPtr.Zero; 318 | 319 | uint dwChars = FormatMessage( 320 | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 321 | IntPtr.Zero, 322 | (uint)hr, 323 | 0, // Default language 324 | ref lpMsgBuf, 325 | 0, 326 | IntPtr.Zero); 327 | 328 | if (dwChars > 0) 329 | { 330 | message = Marshal.PtrToStringAnsi(lpMsgBuf).Trim(); 331 | 332 | // Free the buffer. 333 | LocalFree(lpMsgBuf); 334 | } 335 | else 336 | { 337 | // Handle the error. 338 | message = "hr=0x" + uhr.ToString("X"); 339 | } 340 | 341 | return message; 342 | } 343 | 344 | // returns the parent joint of the given joint 345 | public static JointType GetParentJoint(JointType joint) 346 | { 347 | switch(joint) 348 | { 349 | case JointType.HipCenter: 350 | return JointType.HipCenter; 351 | 352 | case JointType.Neck: 353 | return JointType.SpineShoulder; 354 | 355 | case JointType.SpineShoulder: 356 | return JointType.Spine; 357 | 358 | case JointType.ShoulderLeft: 359 | case JointType.ShoulderRight: 360 | return JointType.SpineShoulder; 361 | 362 | case JointType.HipLeft: 363 | case JointType.HipRight: 364 | return JointType.HipCenter; 365 | 366 | case JointType.HandTipLeft: 367 | case JointType.ThumbLeft: 368 | return JointType.HandLeft; 369 | 370 | case JointType.HandTipRight: 371 | case JointType.ThumbRight: 372 | return JointType.HandRight; 373 | } 374 | 375 | return (JointType)((int)joint - 1); 376 | } 377 | 378 | // returns the mirror joint of the given joint 379 | public static JointType GetMirrorJoint(JointType joint) 380 | { 381 | switch(joint) 382 | { 383 | case JointType.ShoulderLeft: 384 | return JointType.ShoulderRight; 385 | case JointType.ElbowLeft: 386 | return JointType.ElbowRight; 387 | case JointType.WristLeft: 388 | return JointType.WristRight; 389 | case JointType.HandLeft: 390 | return JointType.HandRight; 391 | 392 | case JointType.ShoulderRight: 393 | return JointType.ShoulderLeft; 394 | case JointType.ElbowRight: 395 | return JointType.ElbowLeft; 396 | case JointType.WristRight: 397 | return JointType.WristLeft; 398 | case JointType.HandRight: 399 | return JointType.HandLeft; 400 | 401 | case JointType.HipLeft: 402 | return JointType.HipRight; 403 | case JointType.KneeLeft: 404 | return JointType.KneeRight; 405 | case JointType.AnkleLeft: 406 | return JointType.AnkleRight; 407 | case JointType.FootLeft: 408 | return JointType.FootRight; 409 | 410 | case JointType.HipRight: 411 | return JointType.HipLeft; 412 | case JointType.KneeRight: 413 | return JointType.KneeLeft; 414 | case JointType.AnkleRight: 415 | return JointType.AnkleLeft; 416 | case JointType.FootRight: 417 | return JointType.FootLeft; 418 | 419 | case JointType.HandTipLeft: 420 | return JointType.HandTipRight; 421 | case JointType.ThumbLeft: 422 | return JointType.ThumbRight; 423 | 424 | case JointType.HandTipRight: 425 | return JointType.HandTipLeft; 426 | case JointType.ThumbRight: 427 | return JointType.ThumbLeft; 428 | } 429 | 430 | return joint; 431 | } 432 | 433 | // Polls for new skeleton data 434 | public static bool PollSkeleton(ref BodyFrame bodyFrame, Int64 lastFrameTime) 435 | { 436 | bool newSkeleton = false; 437 | 438 | int hr = KinectWrapper.GetBodyFrameData(ref bodyFrame, true, true); 439 | if(hr == 0 && (bodyFrame.liRelativeTime > lastFrameTime)) 440 | { 441 | newSkeleton = true; 442 | } 443 | 444 | return newSkeleton; 445 | } 446 | 447 | // Polls for new color frame data 448 | public static bool PollColorFrame(ref ColorBuffer colorImage) 449 | { 450 | bool bNewFrame = false; 451 | 452 | IntPtr imagePtr = IntPtr.Zero; 453 | Int64 liFrameTime = 0; 454 | 455 | int hr = PollImageFrameData(FrameSource.TypeColor); 456 | if (hr == 0) 457 | { 458 | hr = GetNewColorFrameData(ref imagePtr, ref liFrameTime); 459 | 460 | if(hr == 0) 461 | { 462 | colorImage = (ColorBuffer)Marshal.PtrToStructure(imagePtr, typeof(ColorBuffer)); 463 | bNewFrame = true; 464 | } 465 | } 466 | 467 | return bNewFrame; 468 | } 469 | 470 | // Polls for new depth frame data 471 | public static bool PollDepthFrame(ref DepthBuffer depthImage, ref BodyIndexBuffer bodyIndexImage, ref int minDepth, ref int maxDepth) 472 | { 473 | bool bNewFrame = false; 474 | 475 | IntPtr imagePtr = IntPtr.Zero; 476 | Int64 liFrameTime = 0; 477 | 478 | int hr = PollImageFrameData(FrameSource.TypeDepth | FrameSource.TypeBodyIndex); 479 | if (hr == 0) 480 | { 481 | hr = GetNewDepthFrameData(ref imagePtr, ref liFrameTime, ref minDepth, ref maxDepth); 482 | 483 | if(hr == 0) 484 | { 485 | depthImage = (DepthBuffer)Marshal.PtrToStructure(imagePtr, typeof(DepthBuffer)); 486 | 487 | hr = GetNewBodyIndexFrameData(ref imagePtr, ref liFrameTime); 488 | 489 | if(hr == 0) 490 | { 491 | bodyIndexImage = (BodyIndexBuffer)Marshal.PtrToStructure(imagePtr, typeof(BodyIndexBuffer)); 492 | bNewFrame = true; 493 | } 494 | } 495 | } 496 | 497 | return bNewFrame; 498 | } 499 | 500 | // Polls for new infrared frame data 501 | public static bool PollInfraredFrame(ref DepthBuffer infraredImage) 502 | { 503 | bool bNewFrame = false; 504 | 505 | IntPtr imagePtr = IntPtr.Zero; 506 | Int64 liFrameTime = 0; 507 | 508 | int hr = PollImageFrameData(FrameSource.TypeInfrared); 509 | if (hr == 0) 510 | { 511 | hr = GetNewInfraredFrameData(ref imagePtr, ref liFrameTime); 512 | 513 | if(hr == 0) 514 | { 515 | infraredImage = (DepthBuffer)Marshal.PtrToStructure(imagePtr, typeof(DepthBuffer)); 516 | bNewFrame = true; 517 | } 518 | } 519 | 520 | return bNewFrame; 521 | } 522 | 523 | // Polls for new color histogram frame data 524 | public static bool PollUserHistogramFrame(ref UserHistogramBuffer userHistImage, bool bUseColorData) 525 | { 526 | bool bNewFrame = false; 527 | 528 | IntPtr imagePtr = IntPtr.Zero; 529 | Int64 liFrameTime = 0; 530 | 531 | int hr = GetUserHistogramFrameData(ref imagePtr, ref liFrameTime, bUseColorData); 532 | 533 | if(hr == 0) 534 | { 535 | userHistImage = (UserHistogramBuffer)Marshal.PtrToStructure(imagePtr, typeof(UserHistogramBuffer)); 536 | bNewFrame = true; 537 | } 538 | 539 | return bNewFrame; 540 | } 541 | 542 | // returns depth frame coordinates for the given 3d Kinect-space point 543 | public static Vector2 GetKinectPointDepthCoords(Vector3 kinectPos) 544 | { 545 | int dx = 0, dy = 0; 546 | int hr = GetKinectPointDepthCoords(kinectPos.x, kinectPos.y, kinectPos.z, out dx, out dy); 547 | 548 | if(hr == 0) 549 | { 550 | return new Vector2(dx, dy); 551 | } 552 | 553 | return Vector2.zero; 554 | } 555 | 556 | 557 | // Copies the needed resources into the project directory 558 | public static bool EnsureKinectWrapperPresence() 559 | { 560 | bool bOneCopied = false, bAllCopied = true; 561 | 562 | CopyResourceFile("KinectServer/Kinect2UnityServer.exe", "Kinect2UnityServer.exe", ref bOneCopied, ref bAllCopied); 563 | CopyResourceFile("KinectServer/msvcp110d.dll", "msvcp110d.dll.x64", ref bOneCopied, ref bAllCopied); 564 | CopyResourceFile("KinectServer/msvcr110d.dll", "msvcr110d.dll.x64", ref bOneCopied, ref bAllCopied); 565 | 566 | CopyResourceFile("Kinect2UnityClient.dll", "Kinect2UnityClient.dll", ref bOneCopied, ref bAllCopied); 567 | CopyResourceFile("msvcp110d.dll", "msvcp110d.dll.i386", ref bOneCopied, ref bAllCopied); 568 | CopyResourceFile("msvcr110d.dll", "msvcr110d.dll.i386", ref bOneCopied, ref bAllCopied); 569 | 570 | return bOneCopied && bAllCopied; 571 | } 572 | 573 | // Copy a resource file to the target 574 | private static bool CopyResourceFile(string targetFilePath, string resFileName, ref bool bOneCopied, ref bool bAllCopied) 575 | { 576 | TextAsset textRes = Resources.Load(resFileName, typeof(TextAsset)) as TextAsset; 577 | if(textRes == null) 578 | { 579 | bOneCopied = false; 580 | bAllCopied = false; 581 | 582 | return false; 583 | } 584 | 585 | FileInfo targetFile = new FileInfo(targetFilePath); 586 | if(!targetFile.Directory.Exists) 587 | { 588 | targetFile.Directory.Create(); 589 | } 590 | 591 | if(!targetFile.Exists || targetFile.Length != textRes.bytes.Length) 592 | { 593 | if(textRes != null) 594 | { 595 | using (FileStream fileStream = new FileStream (targetFilePath, FileMode.Create, FileAccess.Write, FileShare.Read)) 596 | { 597 | fileStream.Write(textRes.bytes, 0, textRes.bytes.Length); 598 | } 599 | 600 | bool bFileCopied = File.Exists(targetFilePath); 601 | 602 | bOneCopied = bOneCopied || bFileCopied; 603 | bAllCopied = bAllCopied && bFileCopied; 604 | 605 | return bFileCopied; 606 | } 607 | } 608 | 609 | return false; 610 | } 611 | 612 | } -------------------------------------------------------------------------------- /Assets/Resources/GripCursor32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/Resources/GripCursor32.png -------------------------------------------------------------------------------- /Assets/Resources/HandCursor32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/Resources/HandCursor32.png -------------------------------------------------------------------------------- /Assets/Resources/Kinect2UnityClient.dll.bytes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/Resources/Kinect2UnityClient.dll.bytes -------------------------------------------------------------------------------- /Assets/Resources/Kinect2UnityServer.exe.bytes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/Resources/Kinect2UnityServer.exe.bytes -------------------------------------------------------------------------------- /Assets/Resources/ReleaseCursor32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/Resources/ReleaseCursor32.png -------------------------------------------------------------------------------- /Assets/Resources/msvcp110d.dll.i386.bytes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/Resources/msvcp110d.dll.i386.bytes -------------------------------------------------------------------------------- /Assets/Resources/msvcp110d.dll.x64.bytes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/Resources/msvcp110d.dll.x64.bytes -------------------------------------------------------------------------------- /Assets/Resources/msvcr110d.dll.i386.bytes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/Resources/msvcr110d.dll.i386.bytes -------------------------------------------------------------------------------- /Assets/Resources/msvcr110d.dll.x64.bytes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Assets/Resources/msvcr110d.dll.x64.bytes -------------------------------------------------------------------------------- /Library/.gitignore: -------------------------------------------------------------------------------- 1 | /ScriptAssemblies/ 2 | -------------------------------------------------------------------------------- /Library/AnnotationManager: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/AnnotationManager -------------------------------------------------------------------------------- /Library/AssetImportState: -------------------------------------------------------------------------------- 1 | 5;0;-1 -------------------------------------------------------------------------------- /Library/AssetServerCacheV3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/AssetServerCacheV3 -------------------------------------------------------------------------------- /Library/AssetVersioning.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/AssetVersioning.db -------------------------------------------------------------------------------- /Library/BuildPlayer.prefs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/BuildPlayer.prefs -------------------------------------------------------------------------------- /Library/BuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/BuildSettings.asset -------------------------------------------------------------------------------- /Library/EditorUserBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/EditorUserBuildSettings.asset -------------------------------------------------------------------------------- /Library/EditorUserSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/EditorUserSettings.asset -------------------------------------------------------------------------------- /Library/FailedAssetImports.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/FailedAssetImports.txt -------------------------------------------------------------------------------- /Library/InspectorExpandedItems.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/InspectorExpandedItems.asset -------------------------------------------------------------------------------- /Library/MonoManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/MonoManager.asset -------------------------------------------------------------------------------- /Library/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/ProjectSettings.asset -------------------------------------------------------------------------------- /Library/ScriptMapper: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/ScriptMapper -------------------------------------------------------------------------------- /Library/assetDatabase3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/assetDatabase3 -------------------------------------------------------------------------------- /Library/expandedItems: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/expandedItems -------------------------------------------------------------------------------- /Library/guidmapper: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/guidmapper -------------------------------------------------------------------------------- /Library/metadata/00/00000000000000001000000000000000: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/00/00000000000000001000000000000000 -------------------------------------------------------------------------------- /Library/metadata/00/00000000000000002000000000000000: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/00/00000000000000002000000000000000 -------------------------------------------------------------------------------- /Library/metadata/00/00000000000000003000000000000000: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/00/00000000000000003000000000000000 -------------------------------------------------------------------------------- /Library/metadata/00/00000000000000004000000000000000: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/00/00000000000000004000000000000000 -------------------------------------------------------------------------------- /Library/metadata/00/00000000000000004100000000000000: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/00/00000000000000004100000000000000 -------------------------------------------------------------------------------- /Library/metadata/00/00000000000000005000000000000000: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/00/00000000000000005000000000000000 -------------------------------------------------------------------------------- /Library/metadata/00/00000000000000006000000000000000: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/00/00000000000000006000000000000000 -------------------------------------------------------------------------------- /Library/metadata/00/00000000000000007000000000000000: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/00/00000000000000007000000000000000 -------------------------------------------------------------------------------- /Library/metadata/00/00000000000000008000000000000000: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/00/00000000000000008000000000000000 -------------------------------------------------------------------------------- /Library/metadata/00/00000000000000009000000000000000: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/00/00000000000000009000000000000000 -------------------------------------------------------------------------------- /Library/metadata/00/0000000000000000a000000000000000: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/00/0000000000000000a000000000000000 -------------------------------------------------------------------------------- /Library/metadata/00/0000000000000000b000000000000000: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/00/0000000000000000b000000000000000 -------------------------------------------------------------------------------- /Library/metadata/00/0000000000000000c000000000000000: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/00/0000000000000000c000000000000000 -------------------------------------------------------------------------------- /Library/metadata/19/191807f1008f6d747a7f05f6a28b1472: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/19/191807f1008f6d747a7f05f6a28b1472 -------------------------------------------------------------------------------- /Library/metadata/26/26d013e3b64b1b049996b316995cf9d7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/26/26d013e3b64b1b049996b316995cf9d7 -------------------------------------------------------------------------------- /Library/metadata/28/28179444c3c7af744954d04207020229: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/28/28179444c3c7af744954d04207020229 -------------------------------------------------------------------------------- /Library/metadata/2c/2c5097180052eba44b7944372ead4ccd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/2c/2c5097180052eba44b7944372ead4ccd -------------------------------------------------------------------------------- /Library/metadata/2e/2e0ca68029ae7694dbe740d5e533c8d2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/2e/2e0ca68029ae7694dbe740d5e533c8d2 -------------------------------------------------------------------------------- /Library/metadata/36/366e99b07d99f174087de49a4045a4d0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/36/366e99b07d99f174087de49a4045a4d0 -------------------------------------------------------------------------------- /Library/metadata/37/372af108bb0e90245bebe24c4afa5fc8: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/37/372af108bb0e90245bebe24c4afa5fc8 -------------------------------------------------------------------------------- /Library/metadata/39/39cd60652f1f10944b3b1f8cafeb28ae: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/39/39cd60652f1f10944b3b1f8cafeb28ae -------------------------------------------------------------------------------- /Library/metadata/40/40f3aea2f9108b4498810fd583ac77ff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/40/40f3aea2f9108b4498810fd583ac77ff -------------------------------------------------------------------------------- /Library/metadata/46/46dcfeef00f96a2438cdab823172efda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/46/46dcfeef00f96a2438cdab823172efda -------------------------------------------------------------------------------- /Library/metadata/47/475d892be250472448901e2ac7e4f3da: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/47/475d892be250472448901e2ac7e4f3da -------------------------------------------------------------------------------- /Library/metadata/4d/4d589fa57be210d48ad7046a1af0d672: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/4d/4d589fa57be210d48ad7046a1af0d672 -------------------------------------------------------------------------------- /Library/metadata/4d/4d6bc47498368c141bc5aea37fd355f7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/4d/4d6bc47498368c141bc5aea37fd355f7 -------------------------------------------------------------------------------- /Library/metadata/55/55b9da46e4eef3f4cafab36e66796aeb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/55/55b9da46e4eef3f4cafab36e66796aeb -------------------------------------------------------------------------------- /Library/metadata/56/56c7813e5eff2d64894f4c5a209ab220: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/56/56c7813e5eff2d64894f4c5a209ab220 -------------------------------------------------------------------------------- /Library/metadata/5a/5aa6f36584b40454d9d7f115bcfacad9: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/5a/5aa6f36584b40454d9d7f115bcfacad9 -------------------------------------------------------------------------------- /Library/metadata/6b/6b531f336dfee5f438f94ed501caefc7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/6b/6b531f336dfee5f438f94ed501caefc7 -------------------------------------------------------------------------------- /Library/metadata/73/733fb2e84d195e1459a8a2bbc31707ae: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/73/733fb2e84d195e1459a8a2bbc31707ae -------------------------------------------------------------------------------- /Library/metadata/73/73edc93f32ffcfa4ab8cc7f9e14500ba: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/73/73edc93f32ffcfa4ab8cc7f9e14500ba -------------------------------------------------------------------------------- /Library/metadata/77/77e94628ea257894eb0b75f7171d13ad: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/77/77e94628ea257894eb0b75f7171d13ad -------------------------------------------------------------------------------- /Library/metadata/85/85ff23489b1399e468f2c6fafe1e63ac: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/85/85ff23489b1399e468f2c6fafe1e63ac -------------------------------------------------------------------------------- /Library/metadata/86/86e10b2b763c9b04c8dd83e42a8f50fc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/86/86e10b2b763c9b04c8dd83e42a8f50fc -------------------------------------------------------------------------------- /Library/metadata/90/90fe4c2409694094faf2b29325e2d5bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/90/90fe4c2409694094faf2b29325e2d5bc -------------------------------------------------------------------------------- /Library/metadata/96/96fab79365c641c47b45548e8e42d086: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/96/96fab79365c641c47b45548e8e42d086 -------------------------------------------------------------------------------- /Library/metadata/9a/9a219312cdda5ff4bb2b5510d90c3b0d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/9a/9a219312cdda5ff4bb2b5510d90c3b0d -------------------------------------------------------------------------------- /Library/metadata/9f/9f828727d4cfb7a4b8984b4e0310ae67: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/9f/9f828727d4cfb7a4b8984b4e0310ae67 -------------------------------------------------------------------------------- /Library/metadata/b0/b02178bd62ae4f644bee73df22356c24: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/b0/b02178bd62ae4f644bee73df22356c24 -------------------------------------------------------------------------------- /Library/metadata/bd/bdd4729ec55a86a4dbf10a24293b9f23: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/bd/bdd4729ec55a86a4dbf10a24293b9f23 -------------------------------------------------------------------------------- /Library/metadata/c1/c19ffeb0f090eac4bbcdf1ab23a9ff66: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/c1/c19ffeb0f090eac4bbcdf1ab23a9ff66 -------------------------------------------------------------------------------- /Library/metadata/c2/c25d226819a00a449bfa8b247eac07a7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/c2/c25d226819a00a449bfa8b247eac07a7 -------------------------------------------------------------------------------- /Library/metadata/d1/d178c33d67caf1f48b73e155e2d5010b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/d1/d178c33d67caf1f48b73e155e2d5010b -------------------------------------------------------------------------------- /Library/metadata/de/deab68507f779a144b807ddc9587f2fe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/de/deab68507f779a144b807ddc9587f2fe -------------------------------------------------------------------------------- /Library/metadata/eb/ebd602dc5968e0743b5c5fa9cd5268ee: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/eb/ebd602dc5968e0743b5c5fa9cd5268ee -------------------------------------------------------------------------------- /Library/metadata/ed/ed1c50b3cebc9374f9da79ca55dd4e4b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/ed/ed1c50b3cebc9374f9da79ca55dd4e4b -------------------------------------------------------------------------------- /Library/metadata/f5/f578c9700f0b728429f030aa09476426: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/f5/f578c9700f0b728429f030aa09476426 -------------------------------------------------------------------------------- /Library/metadata/fe/fe88cedb1a2428d44afa801a6b6b3d98: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/Library/metadata/fe/fe88cedb1a2428d44afa801a6b6b3d98 -------------------------------------------------------------------------------- /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshLayers.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/ProjectSettings/NavMeshLayers.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfilkov/kinect2-unity-example-with-ms-sdk/2a4f451c660c6d48baf5b49f354a3fce7d95e1c8/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Kinect2 Unity Example with MS-SDK 2 | ================================= 3 | 4 | This is preliminary software and subject to change. 5 | 6 | Kinect2 Unity Example is a test-project for the Kinect2 native wrapper for Unity I started developing. The wrapper and the Unity-package are my contributions to the Kinect2 Developer Preview Program, run by Microsoft. 7 | 8 | How It Operates 9 | --------------- 10 | 11 | As the Kinect2 SDK/Runtime are 64-bit only at this time, but Unity development environment is 32-bit, I considered it reasonable to develop the wrapper/plugin in two parts - Kinect2UnityServer (64-bit exe) and Kinect2UnityClient (32-bit dll). Kinect2 client and server parts communicate using the RPC-mechanism over named pipes. The flow is like this: 12 | 13 | KinectManager.cs (.Net) -> KinectWrapper.cs (.Net wrapper) -> Kinect2UnityClient.dll (32b) -> Kinect2UnityServer.exe (64b) -> Kinect2.dll (64b) -> Kinect2UnityServer.exe (64b) -> Kinect2UnityClient.dll (32b) -> KinectWrapper.cs (.Net wrapper) -> KinectManager.cs (.Net) 14 | 15 | How to Start It 16 | --------------- 17 | 18 | In order to get the Unity-package working, you need to open Kinect2UnityExample-scene and start it. It uses KinectServer-script to run Kinect2UnityServer.exe automatically at the start of the scene and stop it when the application stops. If server start is successful, the Kinect-server window is displayed minimized (intentionally not hidden), so you could see the server error messages there. If Unity editor freezes at scene start-up, please start the Kinect2 server manually. To do this, first disable 'Start Kinect Server'-setting of KinectManager. The KinectManager is a component of MainCamera-game object in the scene. Then go to '[project-folder]/KinectServer' and double click 'Kinect2UnityServer.exe'. 19 | 20 | What Is In There 21 | ---------------- 22 | 23 | The project is in development. At the moment implemented are these features: the transfer of the raw camera images (color, depth, IR and body-index), the transfer of body data and the coordinate mapping among the Kinect 3d-space and image 2d-spaces. You will see the transfer of the body data in real-time on the cube man in the scene. The depth+bodyindex-data and color data may be seen as GUI-textures at the bottom right of the screen. By default the UserMap (depth and body-index data) is enabled and the ColorMap (color camera images) is disabled. You can enable or disable the transfer and display of these images by enabling or disabling 'Compute User Map' and 'Compute Color Map'-parameters of the KinectManager. This will affect FPS. KinectManager is component of the MainCamera game object. 24 | 25 | How to Switch from Mirrored to Non-Mirrored Movement 26 | ---------------------------------------------------- 27 | To switch from mirrored to non mirrored cube-man, you need to do these two things: 1. Set the Y-rotation of PointManCtrl game object to 0, and 2. Disable 'Mirrored Movement'-parameter of the PointManController. PointManController is component of the PointMan game object (parented to PointManCtrl). To switch back to mirrored movement do the opposite: 1. Set the Y-rotation of PointManCtrl game object to 180 degrees, and 2. Enable 'Mirrored Movement'-parameter of the PointManController. 28 | 29 | The Current State 30 | ----------------- 31 | Here is the actual Unity-asset: https://www.assetstore.unity3d.com/en/#!/content/18708 32 | Sorry for the price tag, but I have to compensate somehow the time I spend for support, research and development. 33 | Here you can find more info about the project: http://rfilkov.com/2014/08/01/kinect-v2-with-ms-sdk/ 34 | And here are some useful tips and tricks regarding it: http://rfilkov.com/2015/01/25/kinect-v2-tips-tricks-examples/ 35 | 36 | Thank You! 37 | ----------- 38 | My sincere thanks to all that have contributed to the development of this project, tested it or gave feedback. Special thanks to these guys for their collaboration: Davy Loots, Robert Cornfield and Andres Soechting! 39 | 40 | Enjoy and greetings, 41 | Rumen 42 | 43 | --------------------------------------------------------------------------------