Hi all, I'm making a game where you play as a photographer and you can show your photos to people. I needed to set the camera to a specific aspect ratio so your photos would fit the UI properly no matter the resolution and I decided to capture the screen with a Texure2D since RenderTexture wasn't working well.
The problem I'm having is that the Texture2D is not capturing any image effects. I've read that image effects are rendered in the order they're stacked in the camera GameObject, so I put my screenshot script at the bottom and even tried the top. No such luck.
Here's the main part of my script:
void SnapPhoto() {
//yield return new WaitForEndOfFrame();
Texture2D tex = new Texture2D(screenWidth,screenHeight, TextureFormat.RGBAFloat, false);
tex.ReadPixels(new Rect(0,0,screenWidth,screenHeight),0,0, false);
//tex.ReadPixels(screenRect,0,0, false);
tex.Apply();
//tex.EncodeToPNG();
byte[] bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
//Application.CaptureScreenshot(Application.dataPath + "/../SavedScreenBIG.png");
//bytes = File.ReadAllBytes(
tex.LoadImage(bytes);
//rawPhotoImg.texture = tex;
photoImg.overrideSprite = Sprite.Create(tex,new Rect(0,0,tex.width,tex.height),new Vector2(0f,0f), 100);
shutterImg.enabled = false;
/*//float originalAspect = Camera.main.aspect;
//Camera.main.aspect = 1.5f;
RenderTexture rt = new RenderTexture(screenWidth,screenHeight,24,RenderTextureFormat.ARGBFloat);
//rt.useMipMap = false;
rt.Create();
Camera.main.targetTexture = rt;
//Camera.main.Render();
RenderTexture.active = rt;
Texture2D tex = new Texture2D(rt.width,rt.height, TextureFormat.RGBAFloat, false);
tex.ReadPixels(new Rect(0,0,rt.width,rt.height),0,0, false);
RenderTexture.active = null;
Camera.main.targetTexture = null;
byte[] bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
tex.LoadImage(bytes);
photoImg.overrideSprite = Sprite.Create(tex,new Rect(0,0,tex.width,tex.height),new Vector2(0f,0f), 100);
shutterImg.enabled = false;
//Camera.main.aspect = originalAspect;*/
}
void OnPostRender() {
if (doSnap) {
doSnap = false;
//Camera.main.Render();
//StartCoroutine("SnapPhoto");
SnapPhoto();
}
//SnapPhoto();
}
I'm calling SnapPhoto() in OnPostRender() which should be doing the ReadPixels after the last camera render? I've tried calling it in LateUpdate() and Update() but nothing seems to capture the image effects. Here's a picture:
The script isn't super advanced yet, later I'm going to add a thing to create a new Photo object and add it to a UI panel with all the information needed. For now it just replaces a UI image.
I've been wrestling with this for a week and I'm all out of ideas. The thing is I think it was working before when I only had an SSAO effect. That was when the game was still in Gamma color space and the camera didn't have HDR. I had to change the Texture2D TextureFormat to accommodate the Linear HDR or else I got some pretty glitchy images.