void Update() { if (faceManager == null) { faceManager = FacetrackingManager.Instance; } //unsafe for editor but can optimise if commented if (!kinectManager || !kinectManager.IsInitialized()) { return; } if (!faceManager || !faceManager.IsFaceTrackingInitialized()) { return; } long userId = kinectManager.GetUserIdByIndex(faceManager.playerIndex); if (userId == 0) { return; } if (!backManager) { backManager = BackgroundRemovalManager.Instance; if (backManager) { // re-initialize the texture colorTex = null; } } if (backManager && backManager.IsBackgroundRemovalInitialized()) { // use foreground image if (!colorTex) { colorTex = new Texture2D(kinectManager.GetColorImageWidth(), kinectManager.GetColorImageHeight(), TextureFormat.ARGB32, false); } try { RenderTexture foregroundTex = (RenderTexture)backManager.GetForegroundTex(); KinectInterop.RenderTex2Tex2D(foregroundTex, ref colorTex); } catch (System.Exception) { colorTex = (Texture2D)backManager.GetForegroundTex(); } } else { // use color camera image colorTex = kinectManager.GetUsersClrTex(); } //faceRect = faceManager.GetFaceColorRect(userId); faceRect = GetHeadJointFaceRect(userId); if (!colorTex || faceRect.width <= 0 || faceRect.height <= 0) { return; } int faceX = (int)faceRect.x; int faceY = (int)faceRect.y; int faceW = (int)faceRect.width; int faceH = (int)faceRect.height; if (faceX < 0) { faceX = 0; } if (faceY < 0) { faceY = 0; } if ((faceX + faceW) > colorTex.width) { faceW = colorTex.width - faceX; } if ((faceY + faceH) > colorTex.height) { faceH = colorTex.height - faceY; } if (faceTex.width != faceW || faceTex.height != faceH) { faceTex.Resize(faceW, faceH); } Color[] colorPixels = colorTex.GetPixels(faceX, faceY, faceW, faceH, 0); faceTex.SetPixels(colorPixels); faceTex.Apply(); if (targetRenderer && targetRenderer.material) { targetRenderer.material.mainTexture = faceTex; } }
void Update() { if (faceManager == null) { faceManager = FacetrackingManager.Instance; } if (!kinectManager || !kinectManager.IsInitialized()) { return; } if (!faceManager || !faceManager.IsFaceTrackingInitialized()) { return; } long userId = kinectManager.GetUserIdByIndex(playerIndex); if (userId == 0) { if (targetObject && targetObject.material && targetObject.material.mainTexture != null) { targetObject.material.mainTexture = null; } return; } if (!backManager) { backManager = BackgroundRemovalManager.Instance; if (backManager) { // re-initialize the texture colorTex = null; } } if (!bSkipUpdatingForegroundTex && backManager && backManager.IsBackgroundRemovalInitialized()) { // use foreground image Texture bmForeTex = backManager.GetForegroundTex(); if (bmForeTex is RenderTexture) { foregroundTex = (RenderTexture)bmForeTex; bSkipUpdatingForegroundTex = (foregroundTex != null); } else { if (foregroundTex == null) { foregroundTex = new RenderTexture(bmForeTex.width, bmForeTex.height, 0); } Graphics.Blit(bmForeTex, foregroundTex); bSkipUpdatingForegroundTex = false; } // if (!colorTex) // { // colorTex = new Texture2D(kinectManager.GetColorImageWidth(), kinectManager.GetColorImageHeight(), TextureFormat.ARGB32, false); // } // // try // { // foregroundTex = (RenderTexture)backManager.GetForegroundTex (); // //KinectInterop.RenderTex2Tex2D (foregroundTex, ref colorTex); // } // catch (System.Exception) // { // colorTex = (Texture2D)backManager.GetForegroundTex (); // } } else { // use color camera image if (!colorTex) { colorTex = kinectManager.GetUsersClrTex2D(); } } //faceRect = faceManager.GetFaceColorRect(userId); faceRect = GetHeadJointFaceRect(userId); if (faceRect.width > 0 && faceRect.height > 0) { int faceX = (int)faceRect.x; int faceY = (int)faceRect.y; int faceW = (int)faceRect.width; int faceH = (int)faceRect.height; if (faceX < 0) { faceX = 0; } if (faceY < 0) { faceY = 0; } if (foregroundTex) { if ((faceX + faceW) > foregroundTex.width) { faceW = foregroundTex.width - faceX; } if ((faceY + faceH) > foregroundTex.height) { faceH = foregroundTex.height - faceY; } } else if (colorTex) { if ((faceX + faceW) > colorTex.width) { faceW = colorTex.width - faceX; } if ((faceY + faceH) > colorTex.height) { faceH = colorTex.height - faceY; } } if (faceTex.width != faceW || faceTex.height != faceH) { faceTex.Resize(faceW, faceH); } if (foregroundTex) { KinectInterop.RenderTex2Tex2D(foregroundTex, faceX, foregroundTex.height - faceY - faceH, faceW, faceH, ref faceTex); } else if (colorTex) { Color[] colorPixels = colorTex.GetPixels(faceX, faceY, faceW, faceH, 0); faceTex.SetPixels(colorPixels); faceTex.Apply(); } if (targetObject && !targetObject.gameObject.activeSelf) { targetObject.gameObject.SetActive(true); } if (targetObject && targetObject.material) { targetObject.material.mainTexture = faceTex; } // don't rotate the transform - mesh follows the head rotation if (targetObject.transform.rotation != Quaternion.identity) { targetObject.transform.rotation = Quaternion.identity; } } else { if (targetObject && targetObject.gameObject.activeSelf) { targetObject.gameObject.SetActive(false); } if (targetObject && targetObject.material && targetObject.material.mainTexture != null) { targetObject.material.mainTexture = null; } } }