- Home /
Using Texture2D.ReadPixels with a RenderTexture that has AntialiasingAsPostEffect
I am trying to save .png's from a RenderTexture. For my purposes, I need antialiasing so I've got the AntialiasingAsPostEffect on the Camera that the RenderTexture is attached to. I have the render texture showing on a Plane in my Unity scene just so I can keep an eye on what's being rendered to it. The PostEffect AA looks good so it seems like I'm ready to go, but I find that when I set RenderTexture.active to that render texture, the files I save are blank.
Here is what my code is like on a script on the Camera with the RenderTexture:
void Start () {
camera = transform.camera;
renderTexture = camera.targetTexture;
}
void OnPostRender() {
RenderTexture.active = renderTexture;
Texture2D frame = new Texture2D( renderTexture.width, renderTexture.height );
frame.ReadPixels( new Rect( 0, 0, renderTexture.width, renderTexture.height ), 0, 0, false );
frame.Apply();
string fileName = "frame_" + framesCaptured.ToString( "000" ) + ".png";
string workingDir;
if ( Application.isEditor )
workingDir = System.IO.Directory.GetParent( Application.dataPath ).ToString();
else
workingDir = System.IO.Directory.GetParent( Application.dataPath ).Parent.ToString();
byte[] bytes = frame.EncodeToPNG();
FileStream file = File.Open( workingDir + "/Saved/" + fileName, FileMode.Create );
BinaryWriter binary = new BinaryWriter( file );
binary.Write( bytes );
file.Close();
}
If I comment out the first line of the OnPostRender function and just read from whatever is already the current active RenderTexture, it seems to read from a TemBuffer from the AA PostEffect and it doesn't have the AA applied yet, so its still all the jaggies. If I leave it in, the files I write are empty.
Can anyone help? I need to save files from this RenderTexture without jaggies!
where you able to solve this issue... I also have the same problem.. the file output is visible on rendertexture in unity but the files are empty.
Your answer
Follow this Question
Related Questions
Texture.ReadPixels() pixelated 0 Answers
Can a Texture2D be created at runtime from a snapshot of a RenderTexture? 3 Answers
Rendering an HD PNG off-screen? 0 Answers
ReadPixels generates corrupted textures on certain Android devices 0 Answers
Why is ReadPixels on my RenderTexture creating a pure gray texture? 1 Answer