diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Unity/Experimental/TextureFrame.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Unity/Experimental/TextureFrame.cs index 176b0b5cd..145082c58 100644 --- a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Unity/Experimental/TextureFrame.cs +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Unity/Experimental/TextureFrame.cs @@ -108,34 +108,15 @@ public void Dispose() /// If format is different from , it converts the format. /// /// - /// After calling it, pixel data can't be read from CPU safely. + /// After calling it, pixel data can't be read on CPU safely. + /// To read pixel data on CPU, use . /// - public bool ReadTextureOnGPU(Texture src) + public void ReadTextureOnGPU(Texture src, bool flipHorizontally, bool flipVertically) { - if (GetTextureFormat(src) != format) - { - return Graphics.ConvertTexture(src, _texture); - } - Graphics.CopyTexture(src, _texture); - return true; - } + ReadTextureInternal(src, flipHorizontally, flipVertically); - public AsyncGPUReadbackRequest ReadTextureAsync(Texture src) - { - if (!ReadTextureOnGPU(src)) - { - throw new InvalidOperationException("Failed to read texture on GPU"); - } - - return AsyncGPUReadback.Request(_texture, 0, (req) => - { - if (_texture == null) - { - return; - } - _texture.LoadRawTextureData(req.GetData()); - _texture.Apply(); - }); + Graphics.CopyTexture(_tmpRenderTexture, _texture); + RenderTexture.ReleaseTemporary(_tmpRenderTexture); } /// @@ -143,6 +124,12 @@ public AsyncGPUReadbackRequest ReadTextureAsync(Texture src) /// Avoid calling this method again before the returned is . /// public AsyncGPUReadbackRequest ReadTextureAsync(Texture src, bool flipHorizontally, bool flipVertically) + { + ReadTextureInternal(src, flipHorizontally, flipVertically); + return AsyncGPUReadback.Request(_tmpRenderTexture, 0, _onReadBackRenderTexture); + } + + private void ReadTextureInternal(Texture src, bool flipHorizontally, bool flipVertically) { var graphicsFormat = GraphicsFormatUtility.GetGraphicsFormat(format, true); _tmpRenderTexture = RenderTexture.GetTemporary(src.width, src.height, 32, graphicsFormat); @@ -163,8 +150,6 @@ public AsyncGPUReadbackRequest ReadTextureAsync(Texture src, bool flipHorizontal } Graphics.Blit(src, _tmpRenderTexture, scale, offset); RenderTexture.active = currentRenderTexture; - - return AsyncGPUReadback.Request(_tmpRenderTexture, 0, _onReadBackRenderTexture); } private readonly Action _onReadBackRenderTexture;