private void Awake() { KinectWrapper.NuiCameraElevationGetAngle(out angle); Debug.Log("Start up"); Debug.Log("Current angle = " + angle); KinectWrapper.NuiCameraElevationSetAngle(0); }
void OnGUI() { // Kinect angle controller is a useful thing =) GUI.Label(new Rect(Screen.width - 95, 15, 150, 25), "Kinect Angle"); if (GUI.Button(new Rect(Screen.width - 85, 40, 50, 25), "20")) { KinectWrapper.NuiCameraElevationSetAngle(20); } if (GUI.Button(new Rect(Screen.width - 85, 70, 50, 25), "10")) { KinectWrapper.NuiCameraElevationSetAngle(10); } if (GUI.Button(new Rect(Screen.width - 85, 100, 50, 25), "0")) { KinectWrapper.NuiCameraElevationSetAngle(0); } if (GUI.Button(new Rect(Screen.width - 85, 130, 50, 25), "-10")) { KinectWrapper.NuiCameraElevationSetAngle(-10); } }
void Awake() { int hr = 0; try { hr = KinectWrapper.NuiInitialize(KinectWrapper.NuiInitializeFlags.UsesSkeleton | KinectWrapper.NuiInitializeFlags.UsesDepthAndPlayerIndex | (ComputeColorMap ? KinectWrapper.NuiInitializeFlags.UsesColor : 0)); if (hr != 0) { throw new Exception("NuiInitialize Failed"); } hr = KinectWrapper.NuiSkeletonTrackingEnable(IntPtr.Zero, 8); // 0, 12,8 if (hr != 0) { throw new Exception("Cannot initialize Skeleton Data"); } depthStreamHandle = IntPtr.Zero; if (ComputeUserMap) { hr = KinectWrapper.NuiImageStreamOpen (KinectWrapper.NuiImageType.DepthAndPlayerIndex, KinectWrapper.Constants.DepthImageResolution, 0, 2, IntPtr.Zero, ref depthStreamHandle); if (hr != 0) { throw new Exception("Cannot open depth stream"); } } colorStreamHandle = IntPtr.Zero; if (ComputeColorMap) { hr = KinectWrapper.NuiImageStreamOpen (KinectWrapper.NuiImageType.Color, KinectWrapper.Constants.ColorImageResolution, 0, 2, IntPtr.Zero, ref colorStreamHandle); if (hr != 0) { throw new Exception("Cannot open color stream"); } } // set kinect elevation angle KinectWrapper.NuiCameraElevationSetAngle(SensorAngle); // init skeleton structures skeletonFrame = new KinectWrapper.NuiSkeletonFrame() { SkeletonData = new KinectWrapper.NuiSkeletonData [KinectWrapper.Constants.NuiSkeletonCount] }; // values used to pass to smoothing function smoothParameters = new KinectWrapper.NuiTransformSmoothParameters(); switch (smoothing) { case Smoothing.Default: smoothParameters.fSmoothing = 0.5f; smoothParameters.fCorrection = 0.5f; smoothParameters.fPrediction = 0.5f; smoothParameters.fJitterRadius = 0.05f; smoothParameters.fMaxDeviationRadius = 0.04f; break; case Smoothing.Medium: smoothParameters.fSmoothing = 0.5f; smoothParameters.fCorrection = 0.1f; smoothParameters.fPrediction = 0.5f; smoothParameters.fJitterRadius = 0.1f; smoothParameters.fMaxDeviationRadius = 0.1f; break; case Smoothing.Aggressive: smoothParameters.fSmoothing = 0.7f; smoothParameters.fCorrection = 0.3f; smoothParameters.fPrediction = 1.0f; smoothParameters.fJitterRadius = 1.0f; smoothParameters.fMaxDeviationRadius = 1.0f; break; } // create arrays for joint positions and joint orientations int skeletonJointsCount = (int) KinectWrapper.NuiSkeletonPositionIndex.Count; playerJointsTracked = new bool[skeletonJointsCount]; playerPrevTracked = new bool[skeletonJointsCount]; playerJointsPos = new Vector3[skeletonJointsCount]; //create the transform matrix that converts from kinect-space to world-space Quaternion quatTiltAngle = new Quaternion(); quatTiltAngle.eulerAngles = new Vector3(-SensorAngle, 0.0f, 0.0f); //float heightAboveHips = SensorHeight - 1.0f; // transform matrix - kinect to world //kinectToWorld.SetTRS(new Vector3(0.0f, heightAboveHips, 0.0f), quatTiltAngle, Vector3.one); kinectToWorld.SetTRS(new Vector3(0.0f, SensorHeight, 0.0f), quatTiltAngle, Vector3.one); flipMatrix = Matrix4x4.identity; flipMatrix[2, 2] = -1; instance = this; } catch (DllNotFoundException e) { string message = "Please check the Kinect SDK installation."; Debug.LogError(message); Debug.LogError(e.ToString()); return; } catch (Exception e) { string message = e.Message + " - " + KinectWrapper.GetNuiErrorString(hr); Debug.LogError(message); Debug.LogError(e.ToString()); return; } if (ComputeUserMap) { // Initialize depth & label map related stuff usersMapSize = KinectWrapper.GetDepthWidth() * KinectWrapper.GetDepthHeight(); usersLblTex = new Texture2D(KinectWrapper.GetDepthWidth(), KinectWrapper.GetDepthHeight()); usersMapColors = new Color32[usersMapSize]; usersPrevState = new ushort[usersMapSize]; usersDepthMap = new ushort[usersMapSize]; usersHistogramMap = new float[8192]; } if (ComputeColorMap) { // Initialize color map related stuff usersClrTex = new Texture2D(KinectWrapper.GetColorWidth(), KinectWrapper.GetColorHeight()); colorImage = new Color32[KinectWrapper.GetColorWidth() * KinectWrapper.GetColorHeight()]; usersColorMap = new byte[colorImage.Length << 2]; } // Initialize user list to contain ALL users. allUsers = new List <uint>(); KinectInitialized = true; }
void Awake() { WrapperTools.EnsureKinectWrapperPresence(); int hr = 0; try { hr = KinectWrapper.NuiInitialize(KinectWrapper.NuiInitializeFlags.UsesSkeleton | KinectWrapper.NuiInitializeFlags.UsesDepthAndPlayerIndex); if (hr != 0) { throw new Exception("NuiInitialize Failed"); } hr = KinectWrapper.NuiSkeletonTrackingEnable(IntPtr.Zero, 8); // 0, 12,8 if (hr != 0) { throw new Exception("Cannot initialize Skeleton Data"); } depthStreamHandle = IntPtr.Zero; // set kinect elevation angle KinectWrapper.NuiCameraElevationSetAngle(SensorAngle); // init skeleton structures skeletonFrame = new KinectWrapper.NuiSkeletonFrame() { SkeletonData = new KinectWrapper.NuiSkeletonData[KinectWrapper.Constants.NuiSkeletonCount] }; // values used to pass to smoothing function smoothParameters = new KinectWrapper.NuiTransformSmoothParameters(); switch (smoothing) { case Smoothing.Default: smoothParameters.fSmoothing = 0.5f; smoothParameters.fCorrection = 0.5f; smoothParameters.fPrediction = 0.5f; smoothParameters.fJitterRadius = 0.05f; smoothParameters.fMaxDeviationRadius = 0.04f; break; case Smoothing.Medium: smoothParameters.fSmoothing = 0.5f; smoothParameters.fCorrection = 0.1f; smoothParameters.fPrediction = 0.5f; smoothParameters.fJitterRadius = 0.1f; smoothParameters.fMaxDeviationRadius = 0.1f; break; case Smoothing.Aggressive: smoothParameters.fSmoothing = 0.7f; smoothParameters.fCorrection = 0.3f; smoothParameters.fPrediction = 1.0f; smoothParameters.fJitterRadius = 1.0f; smoothParameters.fMaxDeviationRadius = 1.0f; break; } // init the tracking state filter trackingStateFilter = new TrackingStateFilter[KinectWrapper.Constants.NuiSkeletonMaxTracked]; for (int i = 0; i < trackingStateFilter.Length; i++) { trackingStateFilter[i] = new TrackingStateFilter(); trackingStateFilter[i].Init(); } // init the bone orientation filter boneOrientationFilter = new BoneOrientationsFilter[KinectWrapper.Constants.NuiSkeletonMaxTracked]; for (int i = 0; i < boneOrientationFilter.Length; i++) { boneOrientationFilter[i] = new BoneOrientationsFilter(); boneOrientationFilter[i].Init(); } // init the clipped legs filter clippedLegsFilter = new ClippedLegsFilter[KinectWrapper.Constants.NuiSkeletonMaxTracked]; for (int i = 0; i < clippedLegsFilter.Length; i++) { clippedLegsFilter[i] = new ClippedLegsFilter(); } // init the bone orientation constraints boneConstraintsFilter = new BoneOrientationsConstraint(); boneConstraintsFilter.AddDefaultConstraints(); // init the self intersection constraints selfIntersectionConstraint = new SelfIntersectionConstraint(); // create arrays for joint positions and joint orientations int skeletonJointsCount = (int)KinectWrapper.NuiSkeletonPositionIndex.Count; player1JointsTracked = new bool[skeletonJointsCount]; player1PrevTracked = new bool[skeletonJointsCount]; player1JointsPos = new Vector3[skeletonJointsCount]; player1JointsOri = new Matrix4x4[skeletonJointsCount]; gestureTrackingAtTime = new float[KinectWrapper.Constants.NuiSkeletonMaxTracked]; //create the transform matrix that converts from kinect-space to world-space Quaternion quatTiltAngle = new Quaternion(); quatTiltAngle.eulerAngles = new Vector3(-SensorAngle, 0.0f, 0.0f); //float heightAboveHips = SensorHeight - 1.0f; // transform matrix - kinect to world kinectToWorld.SetTRS(new Vector3(0.0f, SensorHeight, 0.0f), quatTiltAngle, Vector3.one); flipMatrix = Matrix4x4.identity; flipMatrix[2, 2] = -1; } catch (DllNotFoundException e) { string message = "Please check the Kinect SDK installation."; Debug.LogError(message); Debug.LogError(e.ToString()); return; } catch (Exception e) { string message = e.Message + " - " + KinectWrapper.GetNuiErrorString(hr); Debug.LogError(message); Debug.LogError(e.ToString()); return; } // Initialize user list to contain ALL users. allUsers = new List <uint>(); KinectInitialized = true; }
//----------------------------------- end of public functions --------------------------------------// void Start() { //CalibrationText = GameObject.Find("CalibrationText"); int hr = 0; try { hr = KinectWrapper.NuiInitialize(KinectWrapper.NuiInitializeFlags.UsesSkeleton | KinectWrapper.NuiInitializeFlags.UsesDepthAndPlayerIndex | (ComputeColorMap ? KinectWrapper.NuiInitializeFlags.UsesColor : 0)); if (hr != 0) { throw new Exception("NuiInitialize Failed"); } hr = KinectWrapper.NuiSkeletonTrackingEnable(IntPtr.Zero, 8); // 0, 12,8 if (hr != 0) { throw new Exception("Cannot initialize Skeleton Data"); } _depthStreamHandle = IntPtr.Zero; if (ComputeUserMap) { hr = KinectWrapper.NuiImageStreamOpen(KinectWrapper.NuiImageType.DepthAndPlayerIndex, KinectWrapper.Constants.DepthImageResolution, 0, 2, IntPtr.Zero, ref _depthStreamHandle); if (hr != 0) { throw new Exception("Cannot open depth stream"); } } _colorStreamHandle = IntPtr.Zero; if (ComputeColorMap) { hr = KinectWrapper.NuiImageStreamOpen(KinectWrapper.NuiImageType.Color, KinectWrapper.Constants.ColorImageResolution, 0, 2, IntPtr.Zero, ref _colorStreamHandle); if (hr != 0) { throw new Exception("Cannot open color stream"); } } // set kinect elevation angle KinectWrapper.NuiCameraElevationSetAngle(SensorAngle); // init skeleton structures _skeletonFrame = new KinectWrapper.NuiSkeletonFrame() { SkeletonData = new KinectWrapper.NuiSkeletonData[KinectWrapper.Constants.NuiSkeletonCount] }; // values used to pass to smoothing function _smoothParameters = new KinectWrapper.NuiTransformSmoothParameters(); switch (smoothing) { case Smoothing.Default: _smoothParameters.fSmoothing = 0.5f; _smoothParameters.fCorrection = 0.5f; _smoothParameters.fPrediction = 0.5f; _smoothParameters.fJitterRadius = 0.05f; _smoothParameters.fMaxDeviationRadius = 0.04f; break; case Smoothing.Medium: _smoothParameters.fSmoothing = 0.5f; _smoothParameters.fCorrection = 0.1f; _smoothParameters.fPrediction = 0.5f; _smoothParameters.fJitterRadius = 0.1f; _smoothParameters.fMaxDeviationRadius = 0.1f; break; case Smoothing.Aggressive: _smoothParameters.fSmoothing = 0.7f; _smoothParameters.fCorrection = 0.3f; _smoothParameters.fPrediction = 1.0f; _smoothParameters.fJitterRadius = 1.0f; _smoothParameters.fMaxDeviationRadius = 1.0f; break; } // create arrays for joint positions and joint orientations int skeletonJointsCount = (int)KinectWrapper.NuiSkeletonPositionIndex.Count; _player1JointsTracked = new bool[skeletonJointsCount]; _player2JointsTracked = new bool[skeletonJointsCount]; _player1PrevTracked = new bool[skeletonJointsCount]; _player2PrevTracked = new bool[skeletonJointsCount]; _player1JointsPos = new Vector3[skeletonJointsCount]; _player2JointsPos = new Vector3[skeletonJointsCount]; _player1JointsOri = new Matrix4x4[skeletonJointsCount]; _player2JointsOri = new Matrix4x4[skeletonJointsCount]; //create the transform matrix that converts from kinect-space to world-space Quaternion quatTiltAngle = new Quaternion(); quatTiltAngle.eulerAngles = new Vector3(-SensorAngle, 0.0f, 0.0f); // transform matrix - kinect to world _kinectToWorld.SetTRS(new Vector3(0.0f, SensorHeight, 0.0f), quatTiltAngle, Vector3.one); _flipMatrix = Matrix4x4.identity; _flipMatrix[2, 2] = -1; Instance = this; DontDestroyOnLoad(gameObject); } catch (DllNotFoundException e) { string message = "Please check the Kinect SDK installation."; Debug.LogError(message); Debug.LogError(e.ToString()); return; } catch (Exception e) { string message = e.Message + " - " + KinectWrapper.GetNuiErrorString(hr); Debug.LogError(message); Debug.LogError(e.ToString()); return; } // get the main camera rectangle Rect cameraRect = Camera.main.pixelRect; if (ComputeUserMap) { var displayMapsWidthPercent = DisplayMapsWidthPercent / 100f; var displayMapsHeightPercent = displayMapsWidthPercent * KinectWrapper.GetDepthHeight() / KinectWrapper.GetDepthWidth(); var displayWidth = cameraRect.width * displayMapsWidthPercent; var displayHeight = cameraRect.width * displayMapsHeightPercent; // Initialize depth & label map related stuff _usersMapSize = KinectWrapper.GetDepthWidth() * KinectWrapper.GetDepthHeight(); _usersLblTex = new Texture2D(KinectWrapper.GetDepthWidth(), KinectWrapper.GetDepthHeight()); _usersMapColors = new Color32[_usersMapSize]; _usersPrevState = new ushort[_usersMapSize]; _usersMapRect = new Rect(cameraRect.width - displayWidth, cameraRect.height, displayWidth, -displayHeight); _usersDepthMap = new ushort[_usersMapSize]; _usersHistogramMap = new float[8192]; } if (ComputeColorMap) { var displayMapsWidthPercent = DisplayMapsWidthPercent / 100f; var displayMapsHeightPercent = displayMapsWidthPercent * KinectWrapper.GetColorHeight() / KinectWrapper.GetColorWidth(); var displayWidth = cameraRect.width * displayMapsWidthPercent; var displayHeight = cameraRect.width * displayMapsHeightPercent; // Initialize color map related stuff _usersClrTex = new Texture2D(KinectWrapper.GetColorWidth(), KinectWrapper.GetColorHeight()); _usersClrRect = new Rect(cameraRect.width - displayWidth, cameraRect.height, displayWidth, -displayHeight); _colorImage = new Color32[KinectWrapper.GetColorWidth() * KinectWrapper.GetColorHeight()]; _usersColorMap = new byte[_colorImage.Length << 2]; } // Initialize user list to contain ALL users. _allUsers = new List <uint>(); Debug.Log("Waiting for users."); _kinectInitialized = true; }
void Update() { KinectManager manager = KinectManager.Instance; KinectWrapper.NuiCameraElevationGetAngle(out angle); Debug.Log("Current angle = " + angle); if (manager && manager.IsInitialized()) { if (angle < 4 && !manager.IsUserDetected()) { Debug.Log("Current angle = " + angle); KinectWrapper.NuiCameraElevationSetAngle(5); } //backgroundImage.renderer.material.mainTexture = manager.GetUsersClrTex(); if (backgroundImage && (backgroundImage.material.GetTexture(0) == null)) { backgroundImage.material.SetTexture(0, manager.GetUsersClrTex()); } // Vector3 vRight = BottomRight - BottomLeft; // Vector3 vUp = TopLeft - BottomLeft; int iJointIndex = (int)TrackedJoint; if (manager.IsUserDetected()) { uint userId = manager.GetPlayer1ID(); if (manager.IsJointTracked(userId, iJointIndex)) { Vector3 posJoint = manager.GetRawSkeletonJointPos(userId, iJointIndex); if (posJoint != Vector3.zero) { // 3d position to depth Vector2 posDepth = manager.GetDepthMapPosForJointPos(posJoint); // depth pos to color pos Vector2 posColor = manager.GetColorMapPosForDepthPos(posDepth); float scaleX = (float)posColor.x / KinectWrapper.Constants.ColorImageWidth; float scaleY = 1.0f - (float)posColor.y / KinectWrapper.Constants.ColorImageHeight; // Vector3 localPos = new Vector3(scaleX * 10f - 5f, 0f, scaleY * 10f - 5f); // 5f is 1/2 of 10f - size of the plane // Vector3 vPosOverlay = backgroundImage.transform.TransformPoint(localPos); //Vector3 vPosOverlay = BottomLeft + ((vRight * scaleX) + (vUp * scaleY)); Debug.Log(scaleY); if (OverlayObject) { Vector3 vPosOverlay = Camera.main.ViewportToWorldPoint(new Vector3(scaleX, scaleY, distanceToCamera)); OverlayObject.transform.position = Vector3.Lerp(OverlayObject.transform.position, vPosOverlay, smoothFactor * Time.deltaTime); } if (OverlayObject) { Vector3 vPosOverlay = Camera.main.ViewportToWorldPoint(new Vector3(scaleX, scaleY, distanceToCamera)); OverlayObject.transform.position = Vector3.Lerp(OverlayObject.transform.position, vPosOverlay, smoothFactor * Time.deltaTime); } } } } } }
//---------------------------------- END OF PUBLIC FUNCTIONS -----------------------------------------------------------// void Awake() { KinectCoordinatesAdjustment = new KinectWrapper.NuiImageViewArea { eDigitalZoom = 0, lCenterX = 0, lCenterY = 0 }; int hr = 0; try { hr = KinectWrapper.NuiInitialize(KinectWrapper.NuiInitializeFlags.UsesSkeleton | KinectWrapper.NuiInitializeFlags.UsesDepthAndPlayerIndex | KinectWrapper.NuiInitializeFlags.UsesColor); if (hr != 0) { throw new Exception("NuiInitialize Failed"); } depthStreamHandle = IntPtr.Zero; hr = KinectWrapper.NuiImageStreamOpen(KinectWrapper.NuiImageType.DepthAndPlayerIndex, KinectWrapper.Constants.DepthImageResolution, 0, 2, IntPtr.Zero, ref depthStreamHandle); if (hr != 0) { throw new Exception("Cannot open depth stream"); } colorStreamHandle = IntPtr.Zero; hr = KinectWrapper.NuiImageStreamOpen(KinectWrapper.NuiImageType.Color, KinectWrapper.Constants.ColorImageResolution, 0, 2, IntPtr.Zero, ref colorStreamHandle); if (hr != 0) { throw new Exception("Cannot open color stream"); } // set kinect elevation angle KinectWrapper.NuiCameraElevationSetAngle(kinectMotorAngle); //create the transform matrix that converts from kinect-space to world-space Quaternion quatTiltAngle = new Quaternion(); quatTiltAngle.eulerAngles = new Vector3(-kinectMotorAngle, 0.0f, 0.0f); // transform matrix - kinect to world kinectToWorld.SetTRS(new Vector3(0.0f, kinectPlacementHeight, 0.0f), quatTiltAngle, Vector3.one); flipMatrix = Matrix4x4.identity; flipMatrix[2, 2] = -1; DontDestroyOnLoad(gameObject); } catch (DllNotFoundException e) { string message = "Please check the Kinect SDK installation."; Debug.LogError(message); Debug.LogError(e.ToString()); if (UILog != null) { UILog.text = message; } return; } catch (Exception e) { string message = e.Message + " - " + KinectWrapper.GetNuiErrorString(hr); Debug.LogError(message); Debug.LogError(e.ToString()); if (UILog != null) { UILog.text = message; } return; } InitializeFeeds(); InitializeGradedDepthStreamColors(); instance = this; if (UILog != null) { UILog.text = "Kinect is initialized"; } Debug.Log("Kinect is initialized"); kinectInitialized = true; }