- Home /
why does my temp camera only renders transparent objects ?
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 ?
Answer by Steven-1 · Apr 11, 2012 at 01:16 PM
Apparently this is fixed by writing: var tex:RenderTexture = RenderTexture.GetTemporary(400,400,24); instead of var tex:RenderTexture = RenderTexture.GetTemporary(400,400);
I have no idea why though.
Thanks to Steva for answering in the other forum
Your answer
Follow this Question
Related Questions
Render a GameObject/mesh to a -transparent- texture at run-time 2 Answers
what does Camera.targetTexture do? 1 Answer
Error node.m_Bounds.IsValid() on UnityEngine.Camera:Render() 1 Answer
Capture RenderTexture pixels multiple times in one frame? 1 Answer
Howto render camera into texture while still rendering into scene view? 1 Answer