videokit-ai / natshare

Cross-platform social sharing for Unity Engine.
https://github.com/natmlx/videokit
Apache License 2.0
134 stars 28 forks source link

Payload API corrupting screenshots on iOS #66

Closed ideka closed 4 years ago

ideka commented 5 years ago

This is how I capture the screenshot. I can't use ScreenCapture because I specifically need to capture a single camera.

private static Texture2D CaptureCamera(Camera camera)
{
    var (width, height) = (camera.pixelWidth, camera.pixelHeight);

    var renderTexture = new RenderTexture(width, height, 24);
    var texture = new Texture2D(width, height, TextureFormat.RGB24, false);

    {
        var oldCameraTarget = camera.targetTexture;
        camera.targetTexture = renderTexture;
        var oldActiveTexture = RenderTexture.active;
        RenderTexture.active = renderTexture;

        camera.Render();
        texture.ReadPixels(new Rect(0, 0, width, height), 0, 0, false);
        texture.Apply(false);

        RenderTexture.active = oldActiveTexture;
        camera.targetTexture = oldCameraTarget;
    }

    Destroy(renderTexture);

    return texture;
}

The resulting Texture2D is then displayed, and looks fine in-game. But when sharing or saving it with NatShare, like so

using (var save = new SavePayload())
{
    save.AddImage(screenshot);
}

The resulting image is badly corrupted. The top 685125 pixels seem to show up fine, but the rest of the image looks gray and vertically interlaced 4+ times. The full size of the tested image is 1125x2436.

olokobayusuf commented 5 years ago

The texture format must be RGBA32.

olokobayusuf commented 5 years ago

Reopening this because it's too much of a requirement to impose. NatShare should support any Texture2D regardless of format.

olokobayusuf commented 4 years ago

Fixed in ff8a82e255bb65f70f72886be2af093839169739. Feel free to test it out.