Texture2D.ReadPixels doesn't capture image effects.
Hi all, I'm making a game where you play as a photographer and you can show your photos to people, so 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 on the bottom and even tried the top. No such luck.
Here's 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();
}
The main part is SnapPhoto(). I'm calling it in OnPostRender(). 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.
Thanks!
Answer by Armageddon104 · Aug 31, 2015 at 10:43 PM
Okay so I've found these:
http://docs.unity3d.com/ScriptReference/Camera.OnRenderImage.html
http://answers.unity3d.com/questions/29267/texture2dreadpixels-on-rendertexture-in-depth-mode.html
Also the reason my image is darker than the game appears is because of this I think?
http://forum.unity3d.com/threads/reading-pixelsfrom-argbhalf-render-texture.77280/
Anyways OnRenderImage displays the Unity UI well enough, but everything else is black, maybe it's because the default RenderTextures are null? I don't know what to set the material too. If I set the RenderTextures to the Camera.main.targetTexture it show the UI, if I don't the screen is just grey.
So no closer really but at least this is called after the post effects are done.