- Home /
How to render a camera image to png
Hi, I'm trying (unsuccessfully so far) to take a camera image and get a RenderTexture from it. Once I have the RenderTexture, I need to use Texture2D.ReadPixels to read the RenderTexture, so that it can be saved out as a PNG file.
I have this code to get the initial RT from the camera...
RenderTexture c1render = new RenderTexture(80, 80, 24);
c1.targetTexture = c1render;
c1.Render();
c1.targetTexture = null;
and the following to save a Texture2D...
File.WriteAllBytes("mypath/somefile.png", texture2d.EncodeToPNG());
The bit I'm having trouble with is getting a Texture2D from the RenderTexture of my camera.
Any help would be gratefully received
Please format your code. If you don't know how visit the FAQ and watch the Video
$$anonymous$$y apologies, I used the < code > tag, which apparently isn't the right way.
Answer by Benproductions1 · Apr 02, 2013 at 11:51 PM
Hi! there,
You can cast textures like this:
//The variables
var image:Texture;
var image2d:Texture2D;
//Convert the texture to a Texture2D
image2d = image as Texture2D;
Hope this helps, Benproductions1
Thanks :) It was actually more complex than that. I added RenderTextures to each camera, then used ReadPixels to get the RTs into Texture2Ds and save them to PNG files. $$anonymous$$y client then uploads the PNGs to the masterserver - so they are available for use by other clients, in the forums, etc. Took me a day to figure this all out, but it's good now.