Unity Editor crashes when I do readpixels

I seem to be having a problem getting Texture2D.ReadPixels() to work (Unity Indie).

I added the below simple code to my project, referring to a 512x512 RGB24-bit readable texture. I drag the texture to the unFillTexture field in the inspector.

I expected to see the screen be grabbed into the texture - the texture would be used in the next frame to render objects. But as soon as I run the project, the Unity Editor crashes, exits immediately and brings up a bug reporting window.

How do I get this to grab into a texture?

var unFillTexture : Texture2D;
private var grabRect : Rect = Rect(0,0,512,512);

function LateUpdate () {
unFillTexture.ReadPixels (grabRect, 0, 0, false);
}

Attached I’ve made a very simple Unity project with a textured cube and an attempted ReadPixels() call. In this example it doesn’t crash, but I also don’t see anything grabbed into the texture… it remains unchanged. What am I missing?

327256–11546–$temp_182.zip (1.75 MB)

Anyone have any idea how to get ReadPixels working?

At my home computer it seemed as though it was grabbing a copy of what looked like the Unity editor screen, as if that was something left over in the backbuffer, having recently closed the Unity editor. I was totally amazed to see a section of the unity editor showing up on my cube!

So in a way maybe the ReadPixels is working but it’s definitely not grabbing what I just rendered, so how do I do that?

I fixed it!!!

It really boils down to my lack of experience/understanding with Unity. I was trying to do a grab in a camera OnPostRender() function but this was not working well at all, if at all.

But what I now understand is that I need to use yield WaitForEndOfFrame(); to wait until the whole frame is finished being rendered and then read the pixels back. It works! … my only issue now is that I need to actually do two sets of grabs, ie render, grab, render, grab, within one frame, so not sure if the waiting for end frame will work out that way?. … but maybe if I force a hidden camera to render each time before I grab?

Anyway… working code:
var unFillTexture : Texture2D;
private var grabRect : Rect = new Rect(0,0,512,512);

function Update(){
	if (Input.GetKey(KeyCode.LeftArrow)) {transform.position.x+=0.01;}
	if (Input.GetKey(KeyCode.RightArrow)) {transform.position.x-=0.01;}
	if (Input.GetKey(KeyCode.UpArrow)) {transform.position.y-=0.01;}
	if (Input.GetKey(KeyCode.DownArrow)) {transform.position.y+=0.01;}
	
	GrabToTexture();
}

function GrabToTexture(){
	yield new WaitForEndOfFrame ();

    unFillTexture.ReadPixels (grabRect, 0, 0, false);
	unFillTexture.Apply();
}

[/code]

Just a note… in the Unity online script documentation for ReadPixels, it seems to suggest that if you don’t want to use MipMaps then you don’t need to call Apply() … but maybe I’m reading into it?

“f recalculateMipMaps is set to true, the mip maps of the texture will also be updated. If recalculateMipMaps is set to false, you must call Apply to recalculate them.”

It doesn’t say that you need to Apply() when NOT using mipmaps, or not to, it just doesn’t mention it. So I think I read this to mean it was not needed. Upon removing Apply() there is only one grab and no subsequent grabs. Adding Apply grabs every frame. I did uncheck the mipmaps checkbox in the inspector for the texture, so I hope that means mipmaps are not being recalculated? (I said false in the final field of readpixels?

Doh, ok, several posts talking to myself, whatever.

I obviously misunderstood about the Apply() being needed, because this simple code works, attached to the camera, meaning you can let multiple cameras render in order and grab from each after they finish, yay!:

function OnPostRender(){
    unFillTexture.ReadPixels (grabRect, 0, 0, false);
	unFillTexture.Apply();
}

just wanted to let you kwon that your posts talking to urself actually helped me a lot! Keep that attitude, it might seem worthless, but after so long, ends up helping someone!

Me 2,Thanks a lot