Beispiel #1
0
    public override void RenderOutput(WebCamScreenController screenController, PixelData pixelData)
    {
        var texture = screenController.GetScreen();

        if (!outputStreamPoller.Next(outputPacket))
        {
            Debug.LogWarning("Failed to fetch an output packet, rendering the input image");
            texture.SetPixels32(pixelData.Colors);
            texture.Apply();
            return;
        }

        ImageFrame outputFrame = null;

        var status = gpuHelper.RunInGlContext(() => {
            var gpuFrame       = outputPacket.Get();
            var gpuFrameFormat = gpuFrame.Format();
            var sourceTexture  = gpuHelper.CreateSourceTexture(gpuFrame);

            outputFrame = new ImageFrame(
                gpuFrameFormat.ImageFormatFor(), gpuFrame.Width(), gpuFrame.Height(), ImageFrame.kGlDefaultAlignmentBoundary);

            gpuHelper.BindFramebuffer(sourceTexture);
            var info = gpuFrameFormat.GlTextureInfoFor(0);

            Gl.ReadPixels(0, 0, sourceTexture.width, sourceTexture.height, info.glFormat, info.glType, outputFrame.MutablePixelData());
            Gl.Flush();

            sourceTexture.Release();

            return(Status.Ok(false));
        });

        if (status.ok)
        {
            texture.SetPixels32(outputFrame.GetColor32s());
        }
        else
        {
            Debug.LogError(status.ToString());
            texture.SetPixels32(pixelData.Colors);
        }

        texture.Apply();
    }
    public override void RenderOutput(WebCamScreenController screenController, TextureFrame textureFrame)
    {
#if UNITY_ANDROID
        // MediaPipe renders to the texture directly.
        return;
#else
        if (!outputStreamPoller.Next(outputPacket))
        {
            Debug.LogWarning("Failed to fetch an output packet, rendering the input image");
            screenController.DrawScreen(textureFrame);
            return;
        }

        using (var gpuBuffer = outputPacket.Get()) {
            ImageFrame imageFrame = null;

            gpuHelper.RunInGlContext(() => {
                var gpuBufferFormat = gpuBuffer.Format();
                var sourceTexture   = gpuHelper.CreateSourceTexture(gpuBuffer);

                imageFrame = new ImageFrame(
                    gpuBufferFormat.ImageFormatFor(), gpuBuffer.Width(), gpuBuffer.Height(), ImageFrame.kGlDefaultAlignmentBoundary);

                gpuHelper.BindFramebuffer(sourceTexture);
                var info = gpuBufferFormat.GlTextureInfoFor(0);

                Gl.ReadPixels(0, 0, sourceTexture.width, sourceTexture.height, info.glFormat, info.glType, imageFrame.MutablePixelData());
                Gl.Flush();

                sourceTexture.Release();

                return(Status.Ok(false));
            }).AssertOk();

            if (imageFrame != null) // always true
            {
                screenController.DrawScreen(imageFrame);
            }
        }
#endif
    }
    private ImageFrame FetchNextHairMask()
    {
        if (!hairMaskStreamPoller.Next(hairMaskPacket))
        {
            Debug.LogWarning($"Failed to fetch next packet from {hairMaskStream}");
            return(null);
        }

        ImageFrame outputFrame = null;

        var status = gpuHelper.RunInGlContext(() => {
            var gpuFrame       = hairMaskPacket.Get();
            var gpuFrameFormat = gpuFrame.Format();
            var sourceTexture  = gpuHelper.CreateSourceTexture(gpuFrame);

            outputFrame = new ImageFrame(
                gpuFrameFormat.ImageFormatFor(), gpuFrame.Width(), gpuFrame.Height(), ImageFrame.kGlDefaultAlignmentBoundary);

            gpuHelper.BindFramebuffer(sourceTexture);
            var info = gpuFrameFormat.GlTextureInfoFor(0);

            Gl.ReadPixels(0, 0, sourceTexture.width, sourceTexture.height, info.glFormat, info.glType, outputFrame.MutablePixelData());
            Gl.Flush();

            sourceTexture.Release();

            return(Status.Ok(false));
        });

        if (!status.ok)
        {
            Debug.LogError(status.ToString());
        }

        return(outputFrame);
    }