Ejemplo n.º 1
0
    public override void RenderOutput(WebCamScreenController screenController, Color32[] pixelData)
    {
        var texture = screenController.GetScreen();

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

        ImageFrame outputFrame = null;

        var status = gpuHelper.RunInGlContext(() => {
            var gpuFrame       = outputPacket.GetValue();
            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);

            UnsafeNativeMethods.GlReadPixels(0, 0, sourceTexture.Width(), sourceTexture.Height(), info.GlFormat(), info.GlType(), outputFrame.PixelDataPtr());
            UnsafeNativeMethods.GlFlush();

            sourceTexture.Release();

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

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

        texture.Apply();
    }
    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.GetValue();
            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.PixelDataPtr());
            GL.Flush();

            sourceTexture.Release();

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

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

        return(outputFrame);
    }