I need to look up the color on a RenderTexture where a RayCast hits it. Of course, it’s not possible to that directly. RenderTextures don’t have a getPixel
method. So, every frame, I save the RenderTexture to a Texture2D. On Start I call:
public Texture2D tex = null;
public RenderTexture myRenderTexture; // Assigned in the inspector.
void Start () {
RenderTexture.active = myRenderTexture;
tex = new Texture2D(myRenderTexture.width, myRenderTexture.height, TextureFormat.ARGB32, false);
}
Then, on update, I call this from another script:
public IEnumerator refresh () {
yield return new WaitForEndOfFrame();
tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0, true);
tex.Apply ();
}
Then, my plan was to just use tex
as the Texture2D of my RenderTexture.
But what actually happens is that tex
only contains a big square of grey, specifically, in RGB, it’s all (205,205,205). I can see the Texture2D in the inspector and it’s all grey. And the pixel colors I gather from the RayCast are all (205,205,205).
Sometimes, by tweaking some aspects, such as the TextureFormat or changing the wrap mode on the Texture2D, I get a copy of the “Game” frame in the lower left corner of the 2D Texture, but that is not reliable and does not happen often. The Game frame does not show the RenderTexture but does show an object that uses the RenderTexture.
The RenderTexture is actually applied as expected on a plane, so I’m confident that the RenderTexture works as expected and that the problem resides in the way the data is copied to the Texture2D.
Why isn’t my RenderTexture copied to the Texture2D?