- Home /
RenderTexture to capture an area bigger than Screen on IOS
I'm trying to use RenderTexture to capture a 1280x720 texture on the iPhone4, and the output seems to be limited to the screen proportions. in pseudo code what i'm doing is:
create a RenderTexture 1280x720 (called rt from now on)
change the ortho size of the camera to 360
set targetTexture for Camera to rt.
call Camera.Render() (this renders a 1280x720 plane set at position 0,0 with the video texture on it)
set RenderTexture.active to rt
Create a Texture2D (call it tex) of size 1280x720
call tex.ReadPixels(Rect(1280,720),0,0)
tex.Apply
and then EncodeToPNG
and basically the output i'm getting is that only 960x640 (iphone 4 screen size) pixels of the output texture are filled in. The image i've uploaded is the output from the device - the output from the player is roughly the same though slightly more garbled.) Is there some limitation that won't allow the camera to render bigger than the screen size? Anybody know-
btw i tried with RenderTextures of sizes 2048x1024 and this worked fine on the Player - but still produced the result as seem in the image when i tried it from the device.
EDIT: Okay I've tried changing to use an Untagged camera as Fattie suggested but I'm still seeing the same result so I'm posting the code here:
IEnumerator TakeShotCo ()
{
yield return new WaitForEndOfFrame();
RenderTexture rt = new RenderTexture((int)(1280.0f),(int)(720.0f),24);
Vector3 newScale = new Vector3(-128,1,-72);
Globals.g_mainLoop.cameraPlane.transform.localScale = newScale;
UnityEngine.Camera virtuCam = GameObject.Find ("virtuCam").camera;
virtuCam.targetTexture = rt;
virtuCam.Render();
RenderTexture.active = rt;
byte[] pngShot = null;
Texture2D tex = new Texture2D(1280,720,TextureFormat.RGB24,false);
tex.ReadPixels(new Rect(0,0,1280,720), 0, 0);
tex.Apply();
RenderTexture.active = null;
Destroy(rt);
pngShot = tex.EncodeToPNG();
virtuCam.targetTexture = null;
System.IO.File.WriteAllBytes(Utilties.pathForDocumentsFile("texture1280x720"), pngShot );
}
any Ideas appreciated. Thanks
here's what the virtuCam looks like...
Hmm something is wrong, perhaps you should post your code ??
Is your problem this: (sorry I'm a bit friend from coding all day and I can't follow you exactly)
You shouldn't be using the actual camera you use to see stuff in the game.
$$anonymous$$ake a new camera in the scene called virtuCam (you must use that name - because it's funny)
Flag it as untagged, and everywhere else in your scene never ever just implictly find your scene camera (with hat "main camera" rubbish) find it explicitly. And find your virtuCam explicitly
virtualCam = FindUnderHereNamed("virtuCam").camera;
then just follow the usual approach ..
function _freakyExampleCode( )
{
// let's go....
var tempRT:RenderTexture = new RenderTexture(picW,picH, 24 );
// the thingy on the end can be 0,16,24
// formats like RenderTextureFormat.Default, ARGB32 blah blah
virtualCam.targetTexture = tempRT;
var virtualPhoto:Texture2D =
new Texture2D( picW,picH, TextureFormat.RGB24, false);
// false, meaning no need for mipmaps. mipmaps can go to hell
virtualCam.Render();
RenderTexture.active = tempRT;
virtualPhoto.ReadPixels(Rect(0, 0, picW,picH), 0, 0);
RenderTexture.active = null; // redundant code is bad code
Destroy(tempRT);
var bytes:byte[];
bytes = virtualPhoto.EncodeToPNG();
System.IO.File.WriteAllBytes( some filename here, bytes );
// virtualCam.SetActive(false); ...no great need for this
}
again you may have to post your code.
and you can not use the same camera. (Like you report it works in the editor ... because you can just set the size of the game window to whatever. A very confusing aspect of Unity. I find one should always set the game windo to an actual iPad (/ phone) size to avoid horror.
Thanks Fattie, I am using one of my in-game cameras so I'm going to try adding an untagged virtuCam as you suggest and see how we go... if still no dice then i'll post the code... :)
Hi Fattie, hmm I'm still seeing the same thing - i've posted the code i'm using above .. the camera is definitely untagged. any clues in there?
Yes, I'm pretty sure it's using the virtuCam - the code I posted above is the code that's being run and i put in a breakpoint and the virtuCam pointer there shows an orthographic size of 360 which is only true for that untagged cam. I also noticed when inspecting it though that the virtuCam's pixelRect was 960x640 ... i tried to manually change that with the code -
Rect camRect = new Rect(0,0,1280,720);
virtuCam.pixelRect = camRect;
but on inspection that appeared to be completely ignored - maybe its max rect is tied in to the Screen. width/height parameters?