Example #1
0
    void Update()
    {
        if (texOutput)
        {
            // blit the output texture
            BackgroundRemovalManager backManager = BackgroundRemovalManager.Instance;
            if (backManager && backManager.IsBackgroundRemovalInitialized())
            {
                if (foregroundMaterial)
                {
                    foregroundMaterial.SetTexture("_BodyTex", backManager.GetAlphaBodyTex());
                    Graphics.Blit(null, texOutput, foregroundMaterial, 0);
                }
                else
                {
                    Graphics.Blit(backManager.GetAlphaBodyTex(), texOutput);
                }
            }

            // set the gui texture, if needed
            GUITexture guiTexture = GetComponent <GUITexture>();
            if (guiTexture && guiTexture.texture == null)
            {
                guiTexture.texture = texOutput;
            }
        }
    }
    void Update()
    {
        BackgroundRemovalManager backManager = BackgroundRemovalManager.Instance;

        if (backManager && backManager.IsBackgroundRemovalInitialized())
        {
            Texture foregroundTex = backManager.GetForegroundTex();

            if (backgroundImage && (backgroundImage.texture == null) && foregroundTex)
            {
                backgroundImage.texture = foregroundTex;
            }
        }

        KinectManager manager = KinectManager.Instance;

        if (manager && manager.IsInitialized())
        {
            if (currentCameraOffset != adjustedCameraOffset)
            {
                // update the camera automatically, according to the current sensor height and angle
                KinectInterop.SensorData sensorData = manager.GetSensorData();

                if (foregroundCamera != null && sensorData != null)
                {
                    Vector3 fgCameraPos = initialCameraPos;
                    fgCameraPos.x += sensorData.faceOverlayOffset + adjustedCameraOffset;
                    foregroundCamera.transform.position = fgCameraPos;
                    currentCameraOffset = adjustedCameraOffset;
                }
            }
        }
    }
    void Update()
    {
        BackgroundRemovalManager backManager = BackgroundRemovalManager.Instance;

        if (thisRenderer && backManager && backManager.IsBackgroundRemovalInitialized())
        {
            if (thisRenderer.sharedMaterial.mainTexture == null)
            {
                thisRenderer.sharedMaterial.mainTexture = backManager.GetForegroundTex();
            }
        }
    }
Example #4
0
    void Update()
    {
        BackgroundRemovalManager backManager = BackgroundRemovalManager.Instance;

        if (backManager && backManager.IsBackgroundRemovalInitialized())
        {
            if (img && img.texture == null)
            {
                //img.texture = backManager.GetForegroundTex();
                img.texture = backManager.GetAlphaBodyTex();
            }
        }
    }
Example #5
0
    void Update()
    {
        BackgroundRemovalManager backManager = BackgroundRemovalManager.Instance;

        if (backManager && backManager.IsBackgroundRemovalInitialized())
        {
            GUITexture guiTexture = GetComponent <GUITexture>();

            if (guiTexture && guiTexture.texture == null)
            {
                guiTexture.texture = backManager.GetForegroundTex();
            }
        }
    }
    void Update()
    {
        transform.localPosition = Player.JointOffset;
        transform.localScale    = new Vector3(Player.AspectScale.x * Player.JointScale, -Player.AspectScale.y * Player.JointScale, Player.AspectScale.z * Player.JointScale);

        // Render the kinect color image onto a quad
        BackgroundRemovalManager backManager = BackgroundRemovalManager.Instance;

        if (backManager && backManager.IsBackgroundRemovalInitialized())
        {
            var tex = backManager.GetForegroundTex();
            MeshRenderer.material.SetTexture("_MainTex", tex);
        }
    }
    void Update()
    {
        if (foregroundBlendMat && backgroundTexture &&
            kinectManager && kinectManager.IsInitialized())
        {
            if (!backManager)
            {
                backManager = BackgroundRemovalManager.Instance;
            }

            Texture alphaBodyTex = backManager ? backManager.GetAlphaBodyTex() : null;
            KinectInterop.SensorData sensorData = kinectManager.GetSensorData();

            if (backManager && backManager.IsBackgroundRemovalInitialized() &&
                alphaBodyTex && backgroundTexture && lastDepthFrameTime != sensorData.lastDepthFrameTime)
            {
                lastDepthFrameTime = sensorData.lastDepthFrameTime;
                foregroundBlendMat.SetTexture("_BodyTex", alphaBodyTex);
            }
        }
    }
Example #8
0
    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 (manager == null || !manager.IsInitialized())
        {
            return;
        }

        // get user texture
        Renderer renderer = GetComponent <Renderer>();

        if (renderer && renderer.material && renderer.material.mainTexture == null)
        {
            BackgroundRemovalManager backManager = BackgroundRemovalManager.Instance;
            renderer.material.mainTexture = backManager && backManager.IsBackgroundRemovalInitialized() ?
                                            (Texture)sensorData.color2DepthTexture : (Texture)manager.GetUsersLblTex();
        }

        // get kinect-to-world matrix
        kinectToWorld = manager.GetKinectToWorldMatrix();

        if (playerIndex >= 0)
        {
            long lastUserId = userId;
            userId = manager.GetUserIdByIndex(playerIndex);

            userBodyIndex = (byte)manager.GetBodyIndexByUserId(userId);
            if (userBodyIndex == 255)
            {
                userBodyIndex = 222;
            }

            // check for color overlay
            if (foregroundCamera)
            {
                // 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();
                }

                // get user position
                userMeshPos = manager.GetJointPosColorOverlay(userId, (int)KinectInterop.JointType.SpineBase, foregroundCamera, backgroundRect);
            }
            else
            {
                // get user position
                userMeshPos = manager.GetJointKinectPosition(userId, (int)KinectInterop.JointType.SpineBase);
            }

            if (!mirroredMovement)
            {
                //userMeshPos.x = -userMeshPos.x;
                userMeshPos.x = 0f;
            }

            if (foregroundCamera == null)
            {
                // convert kinect pos to world coords, when there is no color overlay
                userMeshPos = kinectToWorld.MultiplyPoint3x4(userMeshPos);
            }

            // set transform position
            Vector3 newUserPos = userMeshPos + originPosition;             // manager.GetJointPosition(userId, (int)KinectInterop.JointType.SpineBase) + originPosition;

            if (invertedZMovement)
            {
                newUserPos.z = -newUserPos.z;
            }

            transform.position = lastUserId != 0 && smoothFactor != 0f ? Vector3.Lerp(transform.position, newUserPos, smoothFactor * Time.deltaTime) : newUserPos;
        }
        else
        {
            userId        = 0;
            userBodyIndex = 255;
            userMeshPos   = Vector3.zero;
        }

        // update the mesh
        UpdateMesh();
    }
    private void UpdateMesh()
    {
        if (sensorData.depthImage != null && sensorData.bodyIndexImage != null &&
            sensorData.depth2SpaceCoords != null && lastSpaceCoordsTime != sensorData.lastDepth2SpaceCoordsTime)
        {
            int vCount = 0, tCount = 0;
            EstimateUserVertices(out vCount, out tCount);

            vertices  = new Vector3[vCount];
            uvs       = new Vector2[vCount];
            triangles = new int[6 * tCount];

            BackgroundRemovalManager backManager = BackgroundRemovalManager.Instance;
            bool isBackManagerOn = backManager && backManager.IsBackgroundRemovalInitialized();

            int index = 0, vIndex = 0, tIndex = 0, xyIndex = 0;
            for (int y = 0; y < depthHeight; y += sampleSize)
            {
                int xyStartIndex = xyIndex;

                for (int x = 0; x < depthWidth; x += sampleSize)
                {
                    //Vector3 vSpacePos = spaceCoords[xyIndex];
                    Vector3 vSpacePos = sensorData.depth2SpaceCoords[xyIndex];

                    if (vertexType[index] != 0 &&
                        !float.IsInfinity(vSpacePos.x) && !float.IsInfinity(vSpacePos.y) && !float.IsInfinity(vSpacePos.z))
                    {
                        // check for color overlay
                        if (foregroundCamera)
                        {
                            // 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();
                            }

                            Vector2 vColorPos  = sensorData.depth2ColorCoords[xyIndex];
                            ushort  depthValue = sensorData.depthImage[xyIndex];

                            if (!float.IsInfinity(vColorPos.x) && !float.IsInfinity(vColorPos.y) && depthValue > 0)
                            {
                                float xScaled = (float)vColorPos.x * backgroundRect.width / sensorData.colorImageWidth;
                                float yScaled = (float)vColorPos.y * backgroundRect.height / sensorData.colorImageHeight;

                                float xScreen   = backgroundRect.x + xScaled;
                                float yScreen   = backgroundRect.y + backgroundRect.height - yScaled;
                                float zDistance = (float)depthValue / 1000f;

                                vSpacePos = foregroundCamera.ScreenToWorldPoint(new Vector3(xScreen, yScreen, zDistance));
                            }
                        }

                        if (!mirroredMovement)
                        {
                            vSpacePos.x = -vSpacePos.x;
                        }

                        if (foregroundCamera == null)
                        {
                            // convert space to world coords, when there is no color overlay
                            vSpacePos = kinectToWorld.MultiplyPoint3x4(vSpacePos);
                        }

                        float u = (float)(isBackManagerOn && sensorData.colorImageScale.x < 0f ? depthWidth - x - 1 : x);
                        float v = (float)(isBackManagerOn && sensorData.colorImageScale.y > 0f ? depthHeight - y - 1 : y);

                        vertices[vIndex] = vSpacePos - userMeshPos;
                        uvs[vIndex]      = new Vector2(u / depthWidth, v / depthHeight);
                        vIndex++;

                        if (vertexType[index] == 3)
                        {
                            if (mirroredMovement)
                            {
                                triangles[tIndex++] = vertexIndex[index];                                  // top left
                                triangles[tIndex++] = vertexIndex[index + 1];                              // top right
                                triangles[tIndex++] = vertexIndex[index + sampledWidth];                   // bottom left

                                triangles[tIndex++] = vertexIndex[index + sampledWidth];                   // bottom left
                                triangles[tIndex++] = vertexIndex[index + 1];                              // top right
                                triangles[tIndex++] = vertexIndex[index + sampledWidth + 1];               // bottom right
                            }
                            else
                            {
                                triangles[tIndex++] = vertexIndex[index + 1];                              // top left
                                triangles[tIndex++] = vertexIndex[index];                                  // top right
                                triangles[tIndex++] = vertexIndex[index + sampledWidth + 1];               // bottom left

                                triangles[tIndex++] = vertexIndex[index + sampledWidth + 1];               // bottom left
                                triangles[tIndex++] = vertexIndex[index];                                  // top right
                                triangles[tIndex++] = vertexIndex[index + sampledWidth];                   // bottom right
                            }
                        }
                    }

                    index++;
                    xyIndex += sampleSize;
                }

                xyIndex = xyStartIndex + sampleSize * depthWidth;
            }

            // buffer is released
            lastSpaceCoordsTime = sensorData.lastDepth2SpaceCoordsTime;

//			lock(sensorData.spaceCoordsBufferLock)
//			{
//				sensorData.spaceCoordsBufferReady = false;
//			}

            mesh.Clear();
            mesh.vertices = vertices;
            mesh.uv       = uvs;
            //mesh.normals = normals;
            mesh.triangles = triangles;
            mesh.RecalculateNormals();
            mesh.RecalculateBounds();

            if (updateMeshCollider)
            {
                MeshCollider meshCollider = GetComponent <MeshCollider>();

                if (meshCollider)
                {
                    meshCollider.sharedMesh = null;
                    meshCollider.sharedMesh = mesh;
                }
            }
        }
    }
Example #11
0
    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;
            }
        }
    }