void Awake() { try { // bool bOnceRestarted = false; // if(System.IO.File.Exists("SCrestart.txt")) // { // bOnceRestarted = true; // // try // { // System.IO.File.Delete("SCrestart.txt"); // } // catch(Exception ex) // { // Debug.LogError("Error deleting SCrestart.txt"); // Debug.LogError(ex.ToString()); // } // } // init the available sensor interfaces sensorInterface = new Kinect2Interface(); bool bNeedRestart = false; if (sensorInterface.InitSensorInterface(true, ref bNeedRestart)) { if (bNeedRestart) { System.IO.File.WriteAllText("SCrestart.txt", "Restarting level..."); KinectInterop.RestartLevel(gameObject, "SC"); return; } else { // check if a sensor is connected bSensorAvailable = sensorInterface.GetSensorsCount() > 0; if (infoText != null) { infoText.text = bSensorAvailable ? "Sensor is connected." : "No sensor is connected."; } } } else { sensorInterface.FreeSensorInterface(true); sensorInterface = null; } } catch (Exception ex) { Debug.LogError(ex.ToString()); if (infoText != null) { infoText.text = ex.Message; } } }
void OnDestroy() { if (instance == null || instance != this) { return; } //Debug.Log("KM was destroyed"); // shut down the polling threads and stop the sensors if (kinectInitialized) { // close the opened sensors and release respective data for (int i = sensorDatas.Count - 1; i >= 0; i--) { KinectInterop.SensorData sensorData = sensorDatas[i]; DepthSensorInterface sensorInt = sensorData.sensorInterface; Debug.Log(string.Format("Closing S{0}: {1}", i, sensorInt.GetType().Name)); if (sensorData.pollFramesThread != null) { // stop the frame-polling thread sensorData.threadStopEvent.Set(); sensorData.pollFramesThread.Join(); sensorData.pollFramesThread = null; sensorData.threadStopEvent.Dispose(); sensorData.threadStopEvent = null; } // close the sensor KinectInterop.CloseSensor(sensorData); sensorDatas.RemoveAt(i); sensorInterfaces.RemoveAt(i); } kinectInitialized = false; } instance = null; }
// opens the default sensor and needed readers public static SensorData OpenDefaultSensor(DepthSensorInterface sensorInt, FrameSource dwFlags, float sensorAngle, bool bUseMultiSource) { SensorData sensorData = null; if (sensorInt == null) { return(sensorData); } try { if (sensorData == null) { sensorData = sensorInt.OpenDefaultSensor(dwFlags, sensorAngle, bUseMultiSource); if (sensorData != null) { sensorData.sensorInterface = sensorInt; sensorData.sensorIntPlatform = sensorInt.GetSensorPlatform(); Debug.Log("Interface used: " + sensorInt.GetType().Name); Debug.Log("Shader level: " + SystemInfo.graphicsShaderLevel); } } else { sensorInt.FreeSensorInterface(false); } } catch (Exception ex) { Debug.LogError("Initialization of the sensor failed."); Debug.LogError(ex.ToString()); try { sensorInt.FreeSensorInterface(false); } catch (Exception) { // do nothing } } return(sensorData); }
// initializes the available sensor interfaces public static List <DepthSensorInterface> InitSensorInterfaces(ref bool bNeedRestart) { List <DepthSensorInterface> listInterfaces = new List <DepthSensorInterface>(); var typeInterface = typeof(DepthSensorInterface); Type[] typesAvailable = typeInterface.Assembly.GetTypes(); foreach (Type type in typesAvailable) { if (typeInterface.IsAssignableFrom(type) && type != typeInterface) { DepthSensorInterface sensorInt = null; try { sensorInt = (DepthSensorInterface)Activator.CreateInstance(type); bool bIntNeedRestart = false; if (sensorInt.InitSensorInterface(ref bIntNeedRestart)) { bNeedRestart |= bIntNeedRestart; } else { sensorInt.FreeSensorInterface(); sensorInt = null; continue; } if (sensorInt.GetSensorsCount() <= 0) { sensorInt.FreeSensorInterface(); sensorInt = null; } } catch (Exception) { if (sensorInt != null) { try { sensorInt.FreeSensorInterface(); } catch (Exception) { // do nothing } finally { sensorInt = null; } } } if (sensorInt != null) { listInterfaces.Add(sensorInt); } } } return(listInterfaces); }
void Awake() { try { // bool bOnceRestarted = false; // if(System.IO.File.Exists("SCrestart.txt")) // { // bOnceRestarted = true; // // try // { // System.IO.File.Delete("SCrestart.txt"); // } // catch(Exception ex) // { // Debug.LogError("Error deleting SCrestart.txt"); // Debug.LogError(ex.ToString()); // } // } // init the available sensor interfaces sensorInterface = new Kinect2Interface(); bool bNeedRestart = false; if(sensorInterface.InitSensorInterface(true, ref bNeedRestart)) { if(bNeedRestart) { System.IO.File.WriteAllText("SCrestart.txt", "Restarting level..."); KinectInterop.RestartLevel(gameObject, "SC"); return; } else { // check if a sensor is connected bSensorAvailable = sensorInterface.GetSensorsCount() > 0; if(infoText != null) { infoText.GetComponent<GUIText>().text = bSensorAvailable ? "Sensor is connected." : "No sensor is connected."; } } } else { sensorInterface.FreeSensorInterface(true); sensorInterface = null; } } catch (Exception ex) { Debug.LogError(ex.ToString()); if(infoText != null) { infoText.GetComponent<GUIText>().text = ex.Message; } } }
// calculates joint orientations of the body joints public void CalculateJointOrients() { if (depthSensorInterface == null) depthSensorInterface = new Kinect2Interface(); // (goe): I dont know if this is a correct initialization int jointCount = bodyData.joint.Length; for (int j = 0; j < jointCount; j++) { int joint = j; KinectInterop.JointData jointData = bodyData.joint[joint]; bool bJointValid = ignoreInferredJoints ? jointData.trackingState == KinectInterop.TrackingState.Tracked : jointData.trackingState != KinectInterop.TrackingState.NotTracked; if (bJointValid) { int nextJoint = (int)depthSensorInterface.GetNextJoint((KinectInterop.JointType)joint); // (goe): I dont know if this call is posible if (nextJoint != joint && nextJoint >= 0 && nextJoint < bodyData.joint.Length) { KinectInterop.JointData nextJointData = bodyData.joint[nextJoint]; bool bNextJointValid = ignoreInferredJoints ? nextJointData.trackingState == KinectInterop.TrackingState.Tracked : nextJointData.trackingState != KinectInterop.TrackingState.NotTracked; if (bNextJointValid) { Vector3 baseDir = KinectInterop.JointBaseDir[nextJoint]; Vector3 jointDir = nextJointData.direction; jointDir.z = -jointDir.z; if ((joint == (int)KinectInterop.JointType.ShoulderLeft) || (joint == (int)KinectInterop.JointType.ShoulderRight)) { float angle = -bodyData.bodyTurnAngle; Vector3 axis = jointDir; Quaternion armTurnRotation = Quaternion.AngleAxis(angle, axis); jointData.normalRotation = armTurnRotation * Quaternion.FromToRotation(baseDir, jointDir); } else if ((joint == (int)KinectInterop.JointType.ElbowLeft) || (joint == (int)KinectInterop.JointType.WristLeft) || (joint == (int)KinectInterop.JointType.HandLeft)) { bool bRotated = false; if (allowHandRotations && (joint != (int)KinectInterop.JointType.ElbowLeft)) { KinectInterop.JointData handData = bodyData.joint[(int)KinectInterop.JointType.HandLeft]; KinectInterop.JointData handTipData = bodyData.joint[(int)KinectInterop.JointType.HandTipLeft]; KinectInterop.JointData thumbData = bodyData.joint[(int)KinectInterop.JointType.ThumbLeft]; if (handData.trackingState != KinectInterop.TrackingState.NotTracked && handTipData.trackingState != KinectInterop.TrackingState.NotTracked && thumbData.trackingState != KinectInterop.TrackingState.NotTracked) { Vector3 rightDir = -(handData.direction + handTipData.direction); rightDir.z = -rightDir.z; Vector3 fwdDir = thumbData.direction; fwdDir.z = -fwdDir.z; if (rightDir.sqrMagnitude >= 0.01f && fwdDir.sqrMagnitude >= 0.01f) { Vector3 upDir = Vector3.Cross(fwdDir, rightDir); fwdDir = Vector3.Cross(rightDir, upDir); Quaternion handRotation = Quaternion.LookRotation(fwdDir, upDir); jointData.normalRotation = handRotation; //bRotated = true; } } bRotated = true; } if (!bRotated) { Quaternion armTurnRotation = Quaternion.identity; if (bodyData.leftThumbDirection != Vector3.zero && bodyData.leftThumbForward != Vector3.zero) // && //Vector3.Angle(bodyData.leftThumbForward, bodyData.leftThumbDirection) <= 90f) { armTurnRotation = Quaternion.FromToRotation(bodyData.leftThumbForward, bodyData.leftThumbDirection); } else { float angle = -bodyData.bodyTurnAngle; Vector3 axis = jointDir; armTurnRotation = Quaternion.AngleAxis(angle, axis); } jointData.normalRotation = armTurnRotation * Quaternion.FromToRotation(baseDir, jointDir); } } else if ((joint == (int)KinectInterop.JointType.ElbowRight) || (joint == (int)KinectInterop.JointType.WristRight) || (joint == (int)KinectInterop.JointType.HandRight)) { bool bRotated = false; if (allowHandRotations && (joint != (int)KinectInterop.JointType.ElbowRight)) { KinectInterop.JointData handData = bodyData.joint[(int)KinectInterop.JointType.HandRight]; KinectInterop.JointData handTipData = bodyData.joint[(int)KinectInterop.JointType.HandTipRight]; KinectInterop.JointData thumbData = bodyData.joint[(int)KinectInterop.JointType.ThumbRight]; if (handData.trackingState != KinectInterop.TrackingState.NotTracked && handTipData.trackingState != KinectInterop.TrackingState.NotTracked && thumbData.trackingState != KinectInterop.TrackingState.NotTracked) { Vector3 rightDir = handData.direction + handTipData.direction; rightDir.z = -rightDir.z; Vector3 fwdDir = thumbData.direction; fwdDir.z = -fwdDir.z; if (rightDir.sqrMagnitude >= 0.01f && fwdDir.sqrMagnitude >= 0.01f) { Vector3 upDir = Vector3.Cross(fwdDir, rightDir); fwdDir = Vector3.Cross(rightDir, upDir); Quaternion handRotation = Quaternion.LookRotation(fwdDir, upDir); // Vector3.up); jointData.normalRotation = handRotation; //bRotated = true; } } bRotated = true; } if (!bRotated) { Quaternion armTurnRotation = Quaternion.identity; if (bodyData.rightThumbDirection != Vector3.zero && bodyData.rightThumbForward != Vector3.zero) // && //Vector3.Angle(bodyData.rightThumbForward, bodyData.rightThumbDirection) <= 90f) { armTurnRotation = Quaternion.FromToRotation(bodyData.rightThumbForward, bodyData.rightThumbDirection); } else { float angle = -bodyData.bodyTurnAngle; Vector3 axis = jointDir; armTurnRotation = Quaternion.AngleAxis(angle, axis); } jointData.normalRotation = armTurnRotation * Quaternion.FromToRotation(baseDir, jointDir); } } else { jointData.normalRotation = Quaternion.FromToRotation(baseDir, jointDir); } if ((joint == (int)KinectInterop.JointType.SpineBase) || (joint == (int)KinectInterop.JointType.SpineMid) || (joint == (int)KinectInterop.JointType.SpineShoulder) || (joint == (int)KinectInterop.JointType.Neck) || (joint == (int)KinectInterop.JointType.HipLeft) || (joint == (int)KinectInterop.JointType.HipRight) || (joint == (int)KinectInterop.JointType.KneeLeft) || (joint == (int)KinectInterop.JointType.KneeRight) || (joint == (int)KinectInterop.JointType.AnkleLeft) || (joint == (int)KinectInterop.JointType.AnkleRight)) { baseDir = Vector3.right; jointDir = bodyData.shouldersDirection; jointDir.z = -jointDir.z; jointData.normalRotation *= Quaternion.FromToRotation(baseDir, jointDir); } // (goe): This is erased because the stored data has not face tracking data // if(joint == (int)KinectInterop.JointType.Neck && // sensorData != null && // depthSensorInterface != null) // { // if(sensorData.sensorInterface.IsFaceTrackingInitialized() && // sensorData.sensorInterface.IsFaceTracked(bodyData.liTrackingID)) // { // Quaternion headRotation = Quaternion.identity; // // if(sensorData.sensorInterface.GetHeadRotation(bodyData.liTrackingID, ref headRotation)) // { // Vector3 rotAngles = headRotation.eulerAngles; // rotAngles.x = -rotAngles.x; // rotAngles.y = -rotAngles.y; // // jointData.normalRotation = Quaternion.Euler(rotAngles); // } // } // } Vector3 mirroredAngles = jointData.normalRotation.eulerAngles; mirroredAngles.y = -mirroredAngles.y; mirroredAngles.z = -mirroredAngles.z; jointData.mirroredRotation = Quaternion.Euler(mirroredAngles); } } else { jointData.normalRotation = Quaternion.identity; jointData.mirroredRotation = Quaternion.identity; } } bodyData.joint[joint] = jointData; if (joint == (int)KinectInterop.JointType.SpineBase) { bodyData.normalRotation = jointData.normalRotation; bodyData.mirroredRotation = jointData.mirroredRotation; } } }