void Update() { if (kinectManager && kinectManager.IsInitialized() && kinectManager.IsUserDetected()) { // 检测站在指定位置的人是否离开指定区域 if (kinectManager.GetAllUserIds().Contains(oldUserID)) { Vector3 SpineBasePos = kinectManager.GetJointKinectPosition(oldUserID, jointIndex); if (!IsInRect(SpineBasePos)) { //Debug.Log("**********不在指定区域"); if (PlayerNotInRect != null) { PlayerNotInRect(oldUserID); } } } foreach (var userId in kinectManager.GetAllUserIds()) { // 追踪当前用户骨骼 if (kinectManager.IsJointTracked(userId, jointIndex)) { Vector3 SpineBasePos = kinectManager.GetJointKinectPosition(userId, jointIndex); if (IsInRect(SpineBasePos)) { //var posX = SpineBasePos.x.ToString("F2"); //var posZ = SpineBasePos.z.ToString("F2"); //Debug.Log(string.Format("*********Pos: [{0}, {1}]" , posX,posZ)); oldUserID = userId; if (PlayerInRect != null) { PlayerInRect(userId); } } } } } if (Input.GetKeyDown(KeyCode.Tab)) { foreach (var item in kinectManager.GetAllUserIds()) { if (kinectManager.IsJointTracked(item, jointIndex)) { Vector3 SpineBasePos = kinectManager.GetJointKinectPosition(item, jointIndex); kinectConfig.PlayerPosX = SpineBasePos.x; kinectConfig.PlayerPosZ = SpineBasePos.z; Debug.Log("PosX:" + kinectConfig.PlayerPosX); Debug.Log("PosZ:" + kinectConfig.PlayerPosZ); DataManager.Instance.SaveKinectConfig(kinectConfig); } } } }
//----------------------------------------------------------------------------- // returns the checksum of current users protected long GetUserChecksum(out int userCount) { userCount = 0; long checksum = 0; //bool foundUserZero = false; if (mKinectManager && mKinectManager.IsInitialized()) { userCount = mKinectManager.GetUsersCount(); //for (int i = 0; i < userCount; i++) //{ // a trick to avoid dismissing userID 0 //long userID = mKinectManager.GetUserIdByIndex(i); //if (userID == 0) //{ // foundUserZero = true; // Debug.Log("Found user zero! "); //} //checksum ^= userID; //} ////Debug.Log("\r\n==== foundUserZero ======"); //checksum = 0; string debubgTextString = ""; List <long> ids = mKinectManager.GetAllUserIds(); ////Debug.Log("\r\n==== start of list ======"); foreach (long id in ids) { //checksum ^= id; checksum += id; debubgTextString += "id-> " + id + "\n\r"; //Debug.Log("id-> " + id); } //Debug.Log("\r\n==== end of list ======"); if (userDebugText) { userDebugText.text = debubgTextString + " CHECKSUM: " + checksum; } } return(checksum); }
void Update() { // 初期化 _NearPlayer = null; _NearPlayerHead = null; // Kinectの認識されているユーザー数を読み込み List <long> userList = new List <long>(); try { userList = kinectManager.GetAllUserIds(); } catch { // NiceCatch; } if (userList.Count != 0) { // 初期化 float _NearPlayerPos = 1000; // if文用 // 認識されているプレイヤーを探索 for (int i = 0; i < userList.Count; i++) { // 正確に認識されているか if (KinectInterop.TrackingState.Tracked == kinectManager.GetJointTrackingState(userList[i], (int)KinectInterop.JointType.SpineBase)) { // 一番近くいるプレイヤー情報だけ格納 if (_CubemanList[i].transform.position.z < _NearPlayerPos) { userID = userList[i]; userNumber = i; _NearPlayerPos = _CubemanList[i].transform.position.z; _NearPlayer = _CubemanList[i]; _NearPlayerHead = _CubemanHeadList[i]; } } } } else { // 初期化 if (_NearPlayer) { _NearPlayer = null; } } }
void Update() { // get the face-tracking manager instance if (faceManager == null) { kinectManager = KinectManager.Instance; faceManager = FacetrackingManager.Instance; } // get user-id by user-index long userId = kinectManager ? kinectManager.GetUserIdByIndex(playerIndex) : 0; if (kinectManager && kinectManager.IsInitialized() && userId != 0 && faceManager && faceManager.IsTrackingFace(userId) && foregroundCamera) { // get head position Vector3 newPosition = faceManager.GetHeadPosition(userId, true); // get head rotation Quaternion newRotation = initialRotation * faceManager.GetHeadRotation(userId, true); // rotational fix, provided by Richard Borys: // The added rotation fixes rotational error that occurs when person is not centered in the middle of the kinect Vector3 addedRotation = newPosition.z != 0f ? new Vector3(Mathf.Rad2Deg * (Mathf.Tan(newPosition.y) / newPosition.z), Mathf.Rad2Deg * (Mathf.Tan(newPosition.x) / newPosition.z), 0) : Vector3.zero; addedRotation.x = newRotation.eulerAngles.x + addedRotation.x; addedRotation.y = newRotation.eulerAngles.y + addedRotation.y; addedRotation.z = newRotation.eulerAngles.z + addedRotation.z; newRotation = Quaternion.Euler(-addedRotation.x, addedRotation.y, -addedRotation.z); // end of rotational fix if (smoothFactorRotation != 0f) { transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, smoothFactorRotation * Time.deltaTime); } else { transform.rotation = newRotation; } // get the background rectangle (use the portrait background, if available) Rect backgroundRect = foregroundCamera.pixelRect; PortraitBackground portraitBack = PortraitBackground.Instance; if (portraitBack && portraitBack.enabled) { backgroundRect = portraitBack.GetBackgroundRect(); } // model position newPosition = kinectManager.GetJointPosColorOverlay(userId, (int)KinectInterop.JointType.Head, foregroundCamera, backgroundRect); if (newPosition == Vector3.zero) { // hide the model behind the camera newPosition.z = -10f; } if (verticalOffset != 0f) { // add the vertical offset Vector3 dirHead = new Vector3(0, verticalOffset, 0); dirHead = transform.InverseTransformDirection(dirHead); newPosition += dirHead; } // go to the new position if (smoothFactorMovement != 0f && transform.position.z >= 0f) { transform.position = Vector3.Lerp(transform.position, newPosition, smoothFactorMovement * Time.deltaTime); } else { transform.position = newPosition; } // scale the model if needed if (!IsForceScale && Math.Abs(transform.localScale.x - modelScaleFactor) > 0.00001f) { transform.localScale = new Vector3(modelScaleFactor, modelScaleFactor, modelScaleFactor); } } else { // hide the model behind the camera //if(transform.position.z >= 0f) //{ // transform.position = new Vector3(0f, 0f, -10f); //} if (kinectManager.GetAllUserIds().Count <= 0) { // transform.position = new Vector3(0f, 0f, -10f); } } }
public void Monitoring() { if (!isInit) { return; } if (kinectManager == null) { kinectManager = KinectManager.Instance; } //生成体感卡片 List <long> ids = kinectManager.GetAllUserIds(); for (int i = 0; i < ids.Count; i++) { long userid = ids[i]; //获取关节 int jointIndex = (int)KinectInterop.JointType.Head; //if (!kinectManager.IsJointTracked(userid, jointIndex)) //{ // continue; //} //当检测到用户时,就获取到用户的位置信息 Vector3 userPos = kinectManager.GetUserPosition(userid); //Vector3 userPos = kinectManager.GetJointKinectPosition(userid, jointIndex); //print(userid + "===" + userPos); //kinect在背后,x正负值颠倒y //userPos = new Vector3(-userPos.x, userPos.y, userPos.z); //保留?位小数 //float x = float.Parse(string.Format("{0:f3}", userPos.x)); //float y = float.Parse(string.Format("{0:f3}", userPos.y)); //if (x<minX) //{ // minX = x; // print("minX"+minX + "---" + userPos.z); //} //if (x>maxX) //{ // maxX = x; // print("maxX" + maxX + "---" + userPos.z); //} //屏幕中心点屏幕坐标 Vector2 origin = new Vector2(Screen.width / 2, Screen.height / 2); //Vector2 userScreenPos = new Vector2(origin.x + basicDistance / 2 / userPos.z * x * Screen.width / 2, origin.y + basicDistance / userPos.z * y * Screen.height / 2); // 正式环境删除400 Vector2 userScreenPos = new Vector2(origin.x + userPos.x / 4f * Screen.width, userPos.y * Screen.height / 2); //Vector2 userScreenPos = new Vector2(origin.x + userPos.x; KinectAgent kinectAgent = _manager.kinectManager.GetAgentById(userid); if (!InEffectiveRange(new Vector3(userScreenPos.x, userScreenPos.y, userPos.z))) { //print("超出边界"); if (kinectAgent != null) { kinectAgent.Close(); } continue; } //添加逻辑 if (kinectAgent == null) { if (_manager.kinectManager.CalScreenPositionIsAvailable(userScreenPos)) { _manager.kinectManager.AddKinectAgents(userScreenPos, userid); } } else { //移动体感卡片,不进行上下移动 Vector2 rectPosition = new Vector2(); RectTransformUtility.ScreenPointToLocalPointInRectangle(_parentRectTransform, userScreenPos, null, out rectPosition); float currentY = kinectAgent.GetComponent <RectTransform>().anchoredPosition.y; var to = new Vector2(rectPosition.x, currentY); Debug.Log("@@@ kinectAgent 进行移动"); kinectAgent.UpdatePos(to); //kinectAgent.UpdatePos(rectPosition); if (_manager.kinectManager.HasEnterCardRange(kinectAgent)) { kinectAgent.Close(); } KinectAgent target = _manager.kinectManager.HasEnterKinectCardRange(kinectAgent); if (target) { if (kinectAgent.createTime < target.createTime) { kinectAgent.Close(); } } } } // 那 ids 去进行校对,返回需要删除的遮罩 var existAgents = _manager.kinectManager.kinectAgents; // 比对ids 和 existagents.userid foreach (var item in existAgents) { if (!ids.Contains(item.userId) && item.status != KinectAgentStatusEnum.Destoring && item.status != KinectAgentStatusEnum.Obsolete) { //print("目标被移除:" + item.userId); item.Close(); } } // 得出结果,调用kinectAgent.Close(); }