Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Location cards are rendered incorrectly #346

Open
sebastientromp opened this issue Nov 4, 2024 · 1 comment
Open

Location cards are rendered incorrectly #346

sebastientromp opened this issue Nov 4, 2024 · 1 comment

Comments

@sebastientromp
Copy link
Contributor

(putting it here so that there is a single place I can refer to).

kLnBEyUg

This is what the final location card looks like.
The way I render cards is to load them up in Unity, then take a screenshot:

public IEnumerator CaptureScreenshot(
            GameObject targetObject, string destPath, int width = 500, int height = 650, int targetWidth = 512, int startX = 617, int startY = 310)
        {
            yield return new WaitForEndOfFrame();
            captureCamera.cullingMask = 1 << targetObject.layer; // Only render the layer the target object is on

            RenderTexture renderTexture = new RenderTexture(Screen.width, Screen.height, 24);
            captureCamera.targetTexture = renderTexture;
            captureCamera.Render();

            RenderTexture.active = renderTexture;
            Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false);
            Rect rex = new Rect(startX, startY, Math.Min(Screen.width - startX, width), Math.Min(Screen.height - startY, height));
            tex.ReadPixels(rex, 0, 0);
            tex.Apply();

            // Calculate new height to maintain aspect ratio
            float aspectRatio = (float)height / width;
            int targetHeight = Mathf.RoundToInt(targetWidth * aspectRatio);

            // Scale the captured texture
            Texture2D scaledTex = ScaleTexture(tex, targetWidth, targetHeight);

            var bytes = scaledTex.EncodeToPNG();
            File.WriteAllBytes(destPath, bytes);

            // Clean up
            captureCamera.targetTexture = null;
            RenderTexture.active = null;
            Destroy(renderTexture);
            Destroy(scaledTex);
            Destroy(tex);
        }

When the object is displayed in-game, it looks correct, with all its parts properly rendered, but the issue comes when rendering it to a texture with transparency.
If, instead of TextureFormat.ARGB32 I use a format without transparency, like TextureFormat.RGB24, it renders correctly (though obviously without a transparent background, which is an issue).

Moreover, looking at the texture files, this is what it looks like with the alpha channel off:
84FHfPy9

When turning the alpha channel on:
4_iDfGuU

The missing part clearly corresponds to the part in the texture where the alpha channel is set.

So what I'm looking for now is:

  • can I turn off transparency altogether in my game objects?
  • since this happens only when rendering to a texture, and not with the in-game camera, maybe there is a way to tune the camera to ignore this transparency setting (but while keeping the transparent background).
@sebastientromp
Copy link
Contributor Author

There are also other smaller transparency issues, which are assumed are linked. Putting the image against a red background reveals them quite clearly:
eSC4Vd9c

See this as well to reveal the transparent edges (thanks vGhost!):
42eRv9hE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant