Rendering to offscreen buffer and retrieving the buffer

Hello!

I know how to do what is mentioned in the title, using native openGL calls, but still havent figured out the best way to do this in Unity. I’m playing with the qualcomm lib, have access to the camera frame, want to downsample it to a buffer using custom shaders, and then get access to that buffers pixels.

I’ve looked at rendertargets and and graphics.blit but havent really figured out how to do this in unity. Anyone else done it? care to push me in the right direction?

In unity you do that through RenderTextures (Pro only) and setting it as render texture for a camera to render to.

There is no other way to do it, its the only ‘buffer’ that exists beside the backbuffer.

If you want to read the pixels for cpu processing through normal code, then the backbuffer (or a rendertexture set as active on a camera, which is basically the same) is the only place you can read it from. Texture2Ds ReadPixels is what you would use then.
If its for shader usage, then rendertextures are fine and the way to go, you would then use the GrabPass. (GrapPass in shaderlab is RenderTexture Pro only too)

1 Like

Check the SSAO.cs script included in the Pro version. Are you using Pro?

The SSAO script uses blit to downsample the buffer for blurring.

I do have Pro version, I think I’d seen the blur script, which gave me the way to render offscreen, but I need access to the buffer after i’ve rendered to it.

I thought I’d read that ReadPixels was very very slow…or was that SetPixel?

SetPixels is really slow.

Create a texture, and render into it, then use ReadPixels to read from it and do what you like.

You can use ReadPixels32 instead of ReadPixels, it should be faster.

This should get you started:

	mainCamera.targetTexture = renderTexture;
	
	RenderTexture.active = renderTexture; 

	mainCamera.Render();

	screenTexture.ReadPixels( new Rect(0, 0, width , height ), 0, 0 );
	screenTexture.Apply(false);
	textureColors = screenTexture.GetPixels();
			
	RenderTexture.active = null;

	mainCamera.targetTexture = null;
1 Like

bueno!

Thank you very much…(i think i was making it harder than it had to be)