RenderTexture not working on iOS and Android Unity 4.2.0f4

Render Textures do not seem to be working correctly for Android and iOS builds. They always appear black.
Render Texture functionality works on the Windows and MacOS editors.

Problem tested on Nexus 4, Galaxy Note 10.1 and iPhone 4S.

I have sent through an error report with a reproducible scene.

In case anyone is also experiencing the same issue, feel free to let us know of any more details or any work arounds.

I'll update this post if I get any more information.

Hi!

I'm experiencing the same issue.
I've also made a very simple scene that reproduce the issue.
When compiled with Unity 4.1.5, my textures are well captured.
But if I compile using Unity 4.2, my textures are black...

Did you have more informations about it?

Thierry

[quote]
I have sent through an error report with a reproducible scene.
[/quote]
and the case number is?

I've reported the bug today (Case 555847)

argh, yeah there were lots of changes regarding render loop handling, e.g. disabling render to backbuffer in update. You are doing everything quite correct (render to RT is/should be allowed at any point). While i'm digging your easiest workaround would be to move you "capture" (where you render camera to rt) from Update to OnPostRender (on your main camera) [as in - extract to function and call from camera script, or smth along these lines]

the problem found and fixed; though i have no ETA of it going live should be in 4.2.something, sure

Thank you very much!

I'll use the workaround while waiting for the fix ;)

Hello Alexey!

I have this problem too. In my case it's done programatically:

var cam:Transform=transform.Find("Camera");
cam.camera.targetTexture=new RenderTexture(512, 512, 24);
cam.camera.targetTexture.Create();
cam.gameObject.active=true;
cam.camera.enabled=true;
RenderTexture.active = cam.camera.targetTexture;
cam.camera.Render();
cam.camera.enabled=false;
return cam.camera.targetTexture;

Before updating to 4.2 it was working fine. Right now it's returning a black texture. Is there anyway to workaround until 4.2 is not fixed?

again, as i said, just move the code that does camera render from Update to OnPostRender. That's it. It should be fixed in 4.2 sure, but for now you can workaround it this way

I'm sorry if I did not understand, but this code is called from a button click. It returns a single RenderTexture each time it's clicked. It's not running inside a Update.

function OnClick()
{
renderer.material.mainTexture = takeAPicture();
}

function takeAPicture()
{
var cam:Transform=transform.Find("Camera");
cam.camera.targetTexture=new RenderTexture(512, 512, 24);
cam.camera.targetTexture.Create();
cam.gameObject.active=true;
cam.camera.enabled=true;
RenderTexture.active = cam.camera.targetTexture;
cam.camera.Render();
cam.camera.enabled=false;
return cam.camera.targetTexture;
}

How to implement OnPostRender in this scenario?

oh
well, i would add some var to set to true on onclick and actually take picture in OnPostRender.
But again, unless that functionality is needed NAOW!!11eleven, you can wait for the fix to go live (i hope it will be 4.2.1, yes)

Thanks for the help Alexey, it's working with your hint!

So in our app we are trying to start a CoRoutine to kick off a screen shot as soon as a prefab has been enabled.

In our case, we are waiting for the end of frame before performing the render texture behavior, which should be equivalent to a OnPostRender. However, we are still seeing garbled pictures in the editor and black or garbled images on devices. Here is the code being used:

    private IEnumerator PrintScreen()
    {
        // wait for graphics to render
        yield return new WaitForEndOfFrame();

        //m_Modal.SetActive(true);
        camera.enabled = true;

        // Find out if we need to scale the screenshot to the minimum size
        float scale = 1;
        if(Screen.width < MIN_WIDTH) {
            scale = MIN_WIDTH / Screen.width;
        }
        int width = (int) (Screen.width * scale);
        int height = (int) (Screen.height * scale);

        // create a texture to pass to encoding
        Texture2D texture = new Texture2D(width, height, TextureFormat.RGB24, false);

        // create the camera
        camera.CopyFrom(Camera.mainCamera);

        // Get the screenshot
        RenderTexture rt = new RenderTexture(width, height, 24);
        camera.targetTexture = rt;
        camera.Render();
        RenderTexture.active = rt;
        texture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        camera.targetTexture = null;
        RenderTexture.active = null; // JC: added to avoid errors
        texture.Apply();


        // split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
        yield return 0;

        // Apply the screenshot to the texture of the game object
        //Destroy(m_TextureObject.GetComponent<UITexture>().mainTexture);
        m_Modal.SetActive(true);
        _modal.SetScreenshot(texture);

        // Save the file
        _image = texture.EncodeToPNG();

        //File.WriteAllBytes(Application.dataPath + "/../screenshot.png", bytes);
        camera.enabled = false;

        Destroy(rt);
    }

Any additional help in order to resolve the texture rendering issue would be most appreciated! Thanks!

it is same issue as before. At the end-of-frame drawing loop is already ended, so you are hit by the same bug.
And i understand it is a bit more complicated to workaround in your case. So, unless you are on tight deadline, please wait for 4.2.1 (we really-really delayed it too much but it should be out soon)

Okay thank you very much! Looking forward to the patch. I will try to rewrite the code to utilize OnPostRender just to see if there is any improvement to the images being stored.

I was also having a problem rendering the texture of an anti-aliased render texture to a png. Was anyone else having this problem? Could this bug be related the one posted above?

Gah, this is so frustrating. I am getting all sorts of errors no matter what I try to do. Like others here, I'm not getting my camera to render to its RenderTexture target (resulting in a black texture), but Alexey's OnPostRender() workaround is not working for me. In fact, it seems like my RenderTexture is not being created or something.

So, I have the MainCamera telling my RenderTexture camera to render during the MainCamera's OnPostRender(). Here's the RenderTexture camera's relevant code:

    public Camera cam;
    public Material testMat;

public void TakeShot()
    {
        // if (!RenderTexture.active.IsCreated())
        //  RenderTexture.active.Create();

        RenderTexture.active = cam.targetTexture;
        cam.Render();
        Debug.Log("Name: " + cam.targetTexture.name);
        Debug.Log("Texture created...? " + cam.targetTexture.IsCreated());

        Texture2D image = new Texture2D(cam.targetTexture.width, cam.targetTexture.height);
        image.ReadPixels(new Rect(0, 0, cam.targetTexture.width, cam.targetTexture.height), 0, 0);
        testMat.mainTexture = image;
    }

The first debug line returns the name of the RenderTexture I've assigned the camera, so I know the reference is correct. But the second line always returns false. In fact, if I uncomment the first two lines, I get a null reference exception on RenderTexture.IsCreated().

I've never messed with RenderTextures before, so I have no idea if what I'm doing is right or wrong, or just part of an unresolved bug.

well, the proper way would be
first do
RenderTexture.active = cam.targetTexture;
and then
if (!RenderTexture.active.IsCreated())
RenderTexture.active.Create();
but then, camera should actually "create" rt by itself when rendering to it
can you please bug report with small repro project?

I'm watching this thread as well. We are still on 4.1.4f1 and won't switch until the RenderTexture issue is fixed. I'm marking this to follow it in hopes that there will be more information soon. This was posted a week ago, so "soon" is realtive - but I understand how that goes.

Fingers crossed for 4.2.1.

= Ed =

well, the fix is already in 4.2.1. The "problem" is 4.2.1 started as hot-fix release and then it started to grow due to various stuff. So yeah, wait for 4.2.1 (i really do hope it will be out soon, so we can finally move on)