Sometimes in game I have to get pixel data from the screen,
to do this, I render the scene again to a small rendertexture, from which I then read the pixels
(I would be better and easier if I could just get the render from previous frame, but ah well)
the code is this (in a script attached to the main camera):
var tex:RenderTexture = RenderTexture.GetTemporary(400,400);
RenderTexture.active = tex;
var thiscam:Camera = GetComponent(Camera);
var cam:Camera = (new GameObject( "tempCamera", Camera )).GetComponent(Camera);
cam.CopyFrom(thiscam);
cam.targetTexture = tex;
cam.Render();
var tex2d:Texture2D = new Texture2D(400, 400, TextureFormat.RGB24, false);
tex2d.ReadPixels(new Rect(0,0,400,400), 0, 0);
var pngShot:byte[] = tex2d.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/testColor.png", pngShot);
RenderTexture.active = null;
cam.targetTexture = null;
DestroyImmediate(cam);
RenderTexture.ReleaseTemporary(tex);
return tex2d.GetPixel(200,200);
for testing purposes, I made the texture bigger and save it on desk,
and so I noticed that only objects with transparent shaders are rendered.
Any idea why this is ?