Quick ReadPixels() question (simple noob question)

Hi!

According to the script reference on ReadPixels(): http://unity3d.com/support/documentation/ScriptReference/Texture2D.ReadPixels.html

“This will copy a rectangular pixel area from the currently active RenderTexture or the view (specified by /source/”)…

My question; how would you specify which RenderTexture to pull from if you have several active in your scene? Or, if you have active rendertextures and you still want to pull from a camera’s view, how would you specify this?

or is this something you would do from inside one of the camera functions, like OnPostRender, etc?

Thanks-

Put in a:

RenderTexture.active = <your render texture>

before you do a read pixels. Set it to null to pull from the camera.

thank you very much for your help here and before. I’m got this kind of working, but I’ve got two remaining problems:

  1. I’ve got the ReadPixels() operation happening during Update, but it only seems to update the texture right when I start playing, and right after I stop. It’s not updating every frame, like I had hoped, and I don’t know why. Is this something that can only update while the game isn’t running?

  2. When it does update, it’s only pulling a small portion of the view. the camera is focused on a sphere, and the render texture shows the sphere right in the middle, fully visible, but in the resulting Texture2D, the sphere is in the top right, being cut off by the edge of the texture. What am I missing here?

Here’s my code (both my RenderTexture and Texture2d are both 256x256):

Thanks!

using UnityEngine;
using System.Collections;

public class Camera128 : MonoBehaviour
{
	Texture2D TextureTarget;
	Rect RenderTextureResolution;
	
	void Awake()
	{
		// Set inital fields
		TextureTarget = Resources.Load("128") as Texture2D;
		RenderTextureResolution = new Rect(0, 0, 128, 128);
	}

	void Update()
	{
		// Assigning the active RenderTexture
		RenderTexture.active = Resources.Load("RT128") as RenderTexture;
		
		// ReadPixels()	
		TextureTarget.ReadPixels(RenderTextureResolution, 0, 0 ,false);
		TextureTarget.Apply();
		
		// Dropping the active RenderTexture	
		RenderTexture.active = null;
		
	}
}
RenderTextureResolution = new Rect(0, 0, 128, 128);

If they’re both 256x256 then that should be as well.

My mistake- they are actually both 128x128. If you have time, any idea about either of the 2 issues from above?

Any reason you’re reloading the RenderTexture every frame on Update? Wouldn’t it be better to load in Awake like the others?

no reason, but for the record, this didn’t work:

using UnityEngine;
using System.Collections;

public class Camera128 : MonoBehaviour
{
	Texture2D TextureTarget;
	Rect RenderTextureResolution;
	
	void Awake()
	{
		// Set inital fields
		TextureTarget = Resources.Load("128") as Texture2D;
		RenderTextureResolution = new Rect(0, 0, 128, 128);
		
		// Assigning the active RenderTexture
		RenderTexture.active = Resources.Load("RT128") as RenderTexture;
	}

	void Update()
	{
		// ReadPixels()	
		TextureTarget.ReadPixels(RenderTextureResolution, 0, 0 ,false);
		TextureTarget.Apply();
	}
}

Any idea about why it’s only updating when I start and end runtime (instead of updating the texture every frame)?