/// <summary> /// Sets up and starts the kinect thread, such that input can be processed, if the input /// is not initialized already /// </summary> public void Initialize() { if (!initialized) { kinectMgr = new KinectManager(); kinectThread = new KinectReaderThread(kinectMgr); kinectThread.Start(); initialized = true; } }
IEnumerator WaitOnStartMGC() { float timeOfWaiting = Time.time; while ((MGC.Instance != null && MGC.Instance.kinectManagerInstance != null)) { if ((Time.time - timeOfWaiting) > 10) { Debug.LogWarning ("MGC or Kineck is not created"); break; } yield return null; } KManager = MGC.Instance.kinectManagerInstance; }
// Use this for initialization void Start() { SocialGame.LevelManager.gameSelected = 0; normal = player2.material; KManager = MGC.Instance.kinectManagerInstance; }
/// <summary> /// Constructor: initialize the kinectManager. /// </summary> /// <param name="kinectManager">KinectManager used to communicate with</param> public KinectReaderThread(KinectManager kinectManager) : base() { this.kinectManager = kinectManager; }
/// <summary> /// Gets the list of gesture joint indexes. /// </summary> /// <returns>The needed joint indexes.</returns> /// <param name="manager">The KinectManager instance</param> public static int[] GetNeededJointIndexes(KinectManager manager) { leftHandIndex = manager.GetJointIndex(KinectInterop.JointType.HandLeft); rightHandIndex = manager.GetJointIndex(KinectInterop.JointType.HandRight); leftElbowIndex = manager.GetJointIndex(KinectInterop.JointType.ElbowLeft); rightElbowIndex = manager.GetJointIndex(KinectInterop.JointType.ElbowRight); leftShoulderIndex = manager.GetJointIndex(KinectInterop.JointType.ShoulderLeft); rightShoulderIndex = manager.GetJointIndex(KinectInterop.JointType.ShoulderRight); hipCenterIndex = manager.GetJointIndex(KinectInterop.JointType.SpineBase); shoulderCenterIndex = manager.GetJointIndex(KinectInterop.JointType.SpineShoulder); leftHipIndex = manager.GetJointIndex(KinectInterop.JointType.HipLeft); rightHipIndex = manager.GetJointIndex(KinectInterop.JointType.HipRight); leftAnkleIndex = manager.GetJointIndex(KinectInterop.JointType.AnkleLeft); rightAnkleIndex = manager.GetJointIndex(KinectInterop.JointType.AnkleRight); neck = manager.GetJointIndex(KinectInterop.JointType.Neck); hipsIndex = manager.GetJointIndex(KinectInterop.JointType.SpineBase); int[] neededJointIndexes = { leftHandIndex, rightHandIndex, leftElbowIndex, rightElbowIndex, leftShoulderIndex, rightShoulderIndex, hipCenterIndex, shoulderCenterIndex, leftHipIndex, rightHipIndex, leftAnkleIndex, rightAnkleIndex }; return neededJointIndexes; }
// Update the avatar each frame. public virtual void UpdateAvatar(Int64 UserID) { if (!gameObject.activeInHierarchy) return; // Get the KinectManager instance if (kinectManager == null) { kinectManager = KinectManager.Instance; } // move the avatar to its Kinect position MoveAvatar(UserID); for (var boneIndex = 0; boneIndex < bones.Length; boneIndex++) { if (!bones[boneIndex]) continue; if (boneIndex2JointMap.ContainsKey(boneIndex)) { KinectInterop.JointType joint = !mirroredMovement ? boneIndex2JointMap[boneIndex] : boneIndex2MirrorJointMap[boneIndex]; TransformBone(UserID, joint, boneIndex, !mirroredMovement); } else if (specIndex2JointMap.ContainsKey(boneIndex)) { // special bones (clavicles) List<KinectInterop.JointType> alJoints = !mirroredMovement ? specIndex2JointMap[boneIndex] : specIndex2MirrorJointMap[boneIndex]; if (alJoints.Count >= 2) { //Debug.Log(alJoints[0].ToString()); Vector3 baseDir = alJoints[0].ToString().EndsWith("Left") ? Vector3.left : Vector3.right; TransformSpecialBone(UserID, alJoints[0], alJoints[1], boneIndex, baseDir, !mirroredMovement); } } } }
//void OnApplicationQuit() void OnDestroy() { //Debug.Log("KM was destroyed"); // shut down the Kinect on quitting. if (kinectInitialized) { KinectInterop.CloseSensor(sensorData); // KinectInterop.ShutdownKinectSensor(); instance = null; } }
public void StartKinect() { try { // try to initialize the default Kinect2 sensor KinectInterop.FrameSource dwFlags = KinectInterop.FrameSource.TypeBody; if (computeUserMap) dwFlags |= KinectInterop.FrameSource.TypeDepth | KinectInterop.FrameSource.TypeBodyIndex; if (computeColorMap) dwFlags |= KinectInterop.FrameSource.TypeColor; if (computeInfraredMap) dwFlags |= KinectInterop.FrameSource.TypeInfrared; // if(useAudioSource) // dwFlags |= KinectInterop.FrameSource.TypeAudio; // open the default sensor sensorData = KinectInterop.OpenDefaultSensor(sensorInterfaces, dwFlags, sensorAngle, useMultiSourceReader); if (sensorData == null) { if (sensorInterfaces == null || sensorInterfaces.Count == 0) { transform.parent.gameObject.SetActive(false); //throw new Exception("No sensor found. Make sure you have installed the SDK and the sensor is connected."); return; } else throw new Exception("OpenDefaultSensor failed."); } // enable or disable getting height and angle info sensorData.hintHeightAngle = (autoHeightAngle != AutoHeightAngle.DontUse); //create the transform matrix - kinect to world Quaternion quatTiltAngle = Quaternion.Euler(-sensorAngle, 0.0f, 0.0f); kinectToWorld.SetTRS(new Vector3(0.0f, sensorHeight, 0.0f), quatTiltAngle, Vector3.one); } catch (DllNotFoundException ex) { string message = ex.Message + " cannot be loaded. Please check the Kinect SDK installation."; //Debug.LogError(message); Debug.LogException(ex); if (calibrationText != null) { calibrationText.GetComponent<GUIText>().text = message; } return; } catch (Exception ex) { string message = ex.Message; //Debug.LogError(message); Debug.LogException(ex); if (calibrationText != null) { calibrationText.GetComponent<GUIText>().text = message; } return; } // set the singleton instance instance = this; // init skeleton structures bodyFrame = new KinectInterop.BodyFrameData(sensorData.bodyCount, KinectInterop.Constants.JointCount); // sensorData.jointCount bodyFrame.bTurnAnalisys = allowTurnArounds; KinectInterop.SmoothParameters smoothParameters = new KinectInterop.SmoothParameters(); switch (smoothing) { case Smoothing.Default: smoothParameters.smoothing = 0.5f; smoothParameters.correction = 0.5f; smoothParameters.prediction = 0.5f; smoothParameters.jitterRadius = 0.05f; smoothParameters.maxDeviationRadius = 0.04f; break; case Smoothing.Medium: smoothParameters.smoothing = 0.5f; smoothParameters.correction = 0.1f; smoothParameters.prediction = 0.5f; smoothParameters.jitterRadius = 0.1f; smoothParameters.maxDeviationRadius = 0.1f; break; case Smoothing.Aggressive: smoothParameters.smoothing = 0.7f; smoothParameters.correction = 0.3f; smoothParameters.prediction = 1.0f; smoothParameters.jitterRadius = 1.0f; smoothParameters.maxDeviationRadius = 1.0f; break; } // init data filters jointPositionFilter = new JointPositionsFilter(); jointPositionFilter.Init(smoothParameters); // init the bone orientation constraints if (useBoneOrientationConstraints) { boneConstraintsFilter = new BoneOrientationsConstraint(); boneConstraintsFilter.AddDefaultConstraints(); boneConstraintsFilter.SetDebugText(calibrationText); } if (computeUserMap) { // Initialize depth & label map related stuff usersLblTex = new Texture2D(sensorData.depthImageWidth, sensorData.depthImageHeight, TextureFormat.ARGB32, false); usersMapSize = sensorData.depthImageWidth * sensorData.depthImageHeight; usersHistogramImage = new Color32[usersMapSize]; usersPrevState = new ushort[usersMapSize]; usersHistogramMap = new float[5001]; } if (computeColorMap) { // Initialize color map related stuff //usersClrTex = new Texture2D(sensorData.colorImageWidth, sensorData.colorImageHeight, TextureFormat.RGBA32, false); usersClrSize = sensorData.colorImageWidth * sensorData.colorImageHeight; } // try to automatically use the available avatar controllers in the scene if (avatarControllers.Count == 0) { MonoBehaviour[] monoScripts = FindObjectsOfType(typeof(MonoBehaviour)) as MonoBehaviour[]; foreach (MonoBehaviour monoScript in monoScripts) { if (typeof(AvatarController).IsAssignableFrom(monoScript.GetType()) && monoScript.enabled) { AvatarController avatar = (AvatarController)monoScript; avatarControllers.Add(avatar); } } } // try to automatically use the available gesture listeners in the scene if (gestureListeners.Count == 0) { MonoBehaviour[] monoScripts = FindObjectsOfType(typeof(MonoBehaviour)) as MonoBehaviour[]; foreach (MonoBehaviour monoScript in monoScripts) { if (typeof(KinectGestures.GestureListenerInterface).IsAssignableFrom(monoScript.GetType()) && monoScript.enabled) { //KinectGestures.GestureListenerInterface gl = (KinectGestures.GestureListenerInterface)monoScript; gestureListeners.Add(monoScript); } } } // Initialize user list to contain all users. //alUserIds = new List<Int64>(); //dictUserIdToIndex = new Dictionary<Int64, int>(); kinectInitialized = true; #if USE_SINGLE_KM_IN_MULTIPLE_SCENES //DontDestroyOnLoad(gameObject); #endif // GUI Text. if (calibrationText != null) { calibrationText.GetComponent<GUIText>().text = "WAITING FOR USERS"; } Debug.Log("Waiting for users."); }