Search Unity

RenderTexture not working on iOS and Android Unity 4.2.0f4

Discussion in 'Editor & General Support' started by The-IT664, Jul 26, 2013.

  1. The-IT664

    The-IT664

    Joined:
    Jul 28, 2009
    Posts:
    29
    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.
     
    Last edited: Jul 26, 2013
  2. costathi

    costathi

    Joined:
    Jul 3, 2013
    Posts:
    5
    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
     
  3. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    and the case number is?
     
  4. costathi

    costathi

    Joined:
    Jul 3, 2013
    Posts:
    5
    I've reported the bug today (Case 555847)
     
  5. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    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]
     
  6. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    the problem found and fixed; though i have no ETA of it going live should be in 4.2.something, sure
     
  7. costathi

    costathi

    Joined:
    Jul 3, 2013
    Posts:
    5
    Thank you very much!

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

    devbr

    Joined:
    Dec 26, 2011
    Posts:
    65
    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?
     
  9. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    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
     
  10. devbr

    devbr

    Joined:
    Dec 26, 2011
    Posts:
    65
    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?
     
  11. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    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)
     
  12. devbr

    devbr

    Joined:
    Dec 26, 2011
    Posts:
    65
    Thanks for the help Alexey, it's working with your hint!
     
  13. okhan1980

    okhan1980

    Joined:
    Mar 5, 2013
    Posts:
    4
    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:
    Code (csharp):
    1.     private IEnumerator PrintScreen()
    2.     {
    3.         // wait for graphics to render
    4.         yield return new WaitForEndOfFrame();
    5.        
    6.         //m_Modal.SetActive(true);
    7.         camera.enabled = true;
    8.        
    9.         // Find out if we need to scale the screenshot to the minimum size
    10.         float scale = 1;
    11.         if(Screen.width < MIN_WIDTH) {
    12.             scale = MIN_WIDTH / Screen.width;
    13.         }
    14.         int width = (int) (Screen.width * scale);
    15.         int height = (int) (Screen.height * scale);
    16.        
    17.         // create a texture to pass to encoding
    18.         Texture2D texture = new Texture2D(width, height, TextureFormat.RGB24, false);
    19.        
    20.         // create the camera
    21.         camera.CopyFrom(Camera.mainCamera);
    22.        
    23.         // Get the screenshot
    24.         RenderTexture rt = new RenderTexture(width, height, 24);
    25.         camera.targetTexture = rt;
    26.         camera.Render();
    27.         RenderTexture.active = rt;
    28.         texture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    29.         camera.targetTexture = null;
    30.         RenderTexture.active = null; // JC: added to avoid errors
    31.         texture.Apply();
    32.        
    33.        
    34.         // split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
    35.         yield return 0;
    36.        
    37.         // Apply the screenshot to the texture of the game object
    38.         //Destroy(m_TextureObject.GetComponent<UITexture>().mainTexture);
    39.         m_Modal.SetActive(true);
    40.         _modal.SetScreenshot(texture);
    41.  
    42.         // Save the file
    43.         _image = texture.EncodeToPNG();
    44.  
    45.         //File.WriteAllBytes(Application.dataPath + "/../screenshot.png", bytes);
    46.         camera.enabled = false;
    47.        
    48.         Destroy(rt);
    49.     }
    Any additional help in order to resolve the texture rendering issue would be most appreciated! Thanks!
     
  14. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    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)
     
  15. okhan1980

    okhan1980

    Joined:
    Mar 5, 2013
    Posts:
    4
    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.
     
  16. macdude2

    macdude2

    Joined:
    Sep 22, 2010
    Posts:
    686
    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?
     
  17. LoftyTheMetroid

    LoftyTheMetroid

    Joined:
    Oct 26, 2012
    Posts:
    57
    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:

    Code (csharp):
    1.  
    2.     public Camera cam;
    3.     public Material testMat;
    4.  
    5. public void TakeShot()
    6.     {
    7.         // if (!RenderTexture.active.IsCreated())
    8.         //  RenderTexture.active.Create();
    9.        
    10.         RenderTexture.active = cam.targetTexture;
    11.         cam.Render();
    12.         Debug.Log("Name: " + cam.targetTexture.name);
    13.         Debug.Log("Texture created...? " + cam.targetTexture.IsCreated());
    14.        
    15.         Texture2D image = new Texture2D(cam.targetTexture.width, cam.targetTexture.height);
    16.         image.ReadPixels(new Rect(0, 0, cam.targetTexture.width, cam.targetTexture.height), 0, 0);
    17.         testMat.mainTexture = image;
    18.     }
    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.
     
  18. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    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?
     
  19. GonzoCubFan

    GonzoCubFan

    Joined:
    Oct 21, 2011
    Posts:
    70
    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 =
     
  20. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    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)
     
  21. LoftyTheMetroid

    LoftyTheMetroid

    Joined:
    Oct 26, 2012
    Posts:
    57
    I went ahead and submitted a bug report using a simple project reproducing my problem. I've also attached it here for convenience: View attachment $RenderTextureTest.zip

    I hope I'm not just simply misunderstanding RenderTextures. Like I said, this is my first time using them, so I can't tell what is my fault and what is an actual bug.
     
  22. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    >>I hope I'm not just simply misunderstanding RenderTextures.
    oh yes you do.
    first:
    RenderTexture.active = cam.targetTexture;
    no need - RenderTexture.active is here if you need to do some crazyness. If you want to render camera to texture - just set camera's targetTexture. It will do stuff itself

    second:
    RenderTexture is your normal texture. So if you simply want to render with it - no need to do ReadPixels - you can assign it to material directly.

    third:
    you alloc new texture2d in every OnPostRender. riiight

    fourth:
    you need to call Texture2D.Apple to upload your changes to gpu.

    fifth
    after camera render, active RT is reset to prev one. So you need to redo your active dance:

    RenderTexture curActive = RenderTexture.active;
    RenderTexture.active = cam.targetTexture;

    image.ReadPixels(new Rect(0, 0, cam.targetTexture.width, cam.targetTexture.height), 0, 0);
    image.Apply();

    RenderTexture.active = curActive;

    sixth:
    you should never whine about "bugs" before you read the manual/docs and fully understand what you're doing

    ;-)
     
  23. LoftyTheMetroid

    LoftyTheMetroid

    Joined:
    Oct 26, 2012
    Posts:
    57
    Okay, first, you were the one that suggested I submit a bug report based solely on the code I presented in this thread:

    The code in the attached project and submitted bug report is practically the same as what I posted earlier, with only just the slight swap of the RenderTexture.Create() that you suggested. (Which, by the way, was beside the point; RenderTexture.Create() is still giving NullReferenceExceptions, as is IsCreated().) I wouldn't have submitted the bug report if you hadn't suggested I do so.

    Second, given that this is a reproduction of the bug, I included everything in my code that reproduced the bug, much of which was taken from examples in the docs. Yes, some of it was redundant, but given that I did not know what was causing the bug and Unity's examples are supposed to be bug-free, I thought it was best to follow the relevant examples as best I could.

    Furthermore, this still doesn't resolve the issue completely. In the test project I supplied, you can set the RenderTextureMat's texture to the RenderTexture and comment out the active RT and image code (thus leaving only cam.Render(), which you say should work if the Camera's RenderTarget is set appropriately) and it still results in a black texture. In fact, only when using the image code do I get any results. (And somehow cam.Render() is also rendering to my game view in addition to my RenderTexture. Is this intentional, or is this also part of the bug? I want cam.Render() to only render to the RenderTexture.)

    Like I said, I don't have much experience with RenderTextures, but you don't need to chastise me for following your advice, especially when it's already been established there are bugs in the rendering code and I can't be sure which pieces of "unnecessary" code are helping or hurting my efforts. Just like how this whole OnPostRender() workaround should be "unnecessary", I'm just trying to figure out how to avoid the current bugs.
     
  24. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    ok, lets start anew
    what you should do:
    first and foremost your setup in scene itself is not that good for checking the results. Do the following:
    on your second camera (rendering to RT) set clear to color (DO NOT EVER select dont clear unless you know what you are doing)
    change cube material to Unlit/Texture (otherwise lighting will be calculated, so your cube might be drawn too close to black)

    if you want intermediate texture
    if you want just rt
    that should do it
     
  25. LoftyTheMetroid

    LoftyTheMetroid

    Joined:
    Oct 26, 2012
    Posts:
    57
    Thanks for the help, but that code still doesn't work completely. I've posted my results below in case you're interested, but I think at this point I'm either going to wait for the Unity update or just use my slightly less ideal solution for my game mechanic (but hey, at least it means I won't need to depend on a Pro-exclusive feature).

    First, I implemented all of your scene setup suggestions, although they resulted in the same results as my previous scene. Second camera was set to Solid Color, cube material was changed to Unlit/Texture, etc.

    Using the second, simpler method with no intermediate texture, I got this:

    $xkYA7VZ.png

    Since that cube has a material with the RT, I should see a picture of the sphere when it takes a shot, but it remains black.

    As an aside, here's an image of the instant the camera is rendering:

    $3ZHXydk.png

    You can see that the RT camera's image is being drawn in the lower-left of the game editor window, which I'm not sure why...

    Your first method, using the intermediate texture, provided better results:

    $u1Ehvzm.png

    (Note: During this photo, I reduced the RT camera's size while I was testing different things. That's why the sphere's size is different.)

    At least here the RT is actually being applied to the material. However, as you can see, the RT is still being drawn in the corner for a brief moment, and I also don't understand why the circle isn't being framed in the center of the texture since both the sphere and RT camera are at (0,0) on the XY plane, with no rotations of the camera or anything to cause the sphere to be off-center. (It looks like the clipping matches how the lower-left image is being clipped in the game editor window...?)

    For reference, here's the RT camera preview:

    $8Q0RqHO.png

    Anyway, that's all I've got. Hope the Unity update is soon...!
     
  26. GonzoCubFan

    GonzoCubFan

    Joined:
    Oct 21, 2011
    Posts:
    70
    OK,

    Don't know if this should be in a new thread or not. We just upgraded to 4.2.1 as advised by Alexey. We've never had problems with RenderToTexture except on Android. Always worked fine for iOS, Editor and Web, but gave us black on Android. After upgrading and building for Android, the Rendered textures just displays a somewhat random texture file as opposed to a rendered texture. On a Galaxy tablet I am seeing one of the textures used on one of the models being rendered to texture (just the whole texture sheet). On a Nexus 7, I am seeing one of the normal maps displayed instead of the scene being rendered.

    Apparently the touted fix for Render To Texture for 4.2.1 is not quite there yet for Android.

    Suggestions?

    = Ed =
     
  27. Ogien

    Ogien

    Joined:
    Nov 21, 2012
    Posts:
    165
    To my horror I just discovered the same issue on my app. My production app that has over 10K downloads a day. I am about to jump out of my seat, I need help please. I am getting black textures in some of my users on Android just as described above. Why are more people not complaining about this?
     
  28. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    now wait a second - it seems we get two issues lumped in there
    wrong. what was described here initially is that on ANY android phone rendering to rt out of drawing (say in update) will result in black rt
    can we separate this stuff?
     
  29. GonzoCubFan

    GonzoCubFan

    Joined:
    Oct 21, 2011
    Posts:
    70
    I can only speak to my current project. RT, while it works just fine on iOS, Editor and Web, produces incorrect results on Android as I described. Prior to 4.2.1 (we went directly there from 4.1.4f1), we saw black being rendered on Android while the other platforms worked fine. With 4.2.1 we are seeing various texture sheets as described above.

    We set up our cameras and textures as described in the documentation and just let the camera(s) rendering to texture update normally as well as the objects that need to animate. Nothing associated with RT is handled in any sort of unusual fashion.

    Please advise if there are any work-arounds for this. We can provide a version of our code if necessary. We are getting very close to release for this product and would like to get this addressed quickly.

    Thanks in advance,

    = Ed =
     
    entropicjoey1 likes this.
  30. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    that screams bug report. One thing you can check before doing that is that you DONT use RT as both material texture and render target at same draw call (i saw that plenty of times)
     
  31. GonzoCubFan

    GonzoCubFan

    Joined:
    Oct 21, 2011
    Posts:
    70
    What is the best way to ensure that this is not what is happening? Like I said, we are not doing anything special. I would think that since the source textures are different for each of those renders (i.e. using it as both a target and a source), that this would necessitate different draw calls, but I could be mistaken. As I said, we are not doing anything special here.

    = Ed =
     
  32. GonzoCubFan

    GonzoCubFan

    Joined:
    Oct 21, 2011
    Posts:
    70
    Ping! It's quiet here. Too damn quiet. The silence is getting deafening...
     
  33. _Domi_

    _Domi_

    Joined:
    Jul 25, 2013
    Posts:
    10
    I'm also watching this thread as I have the same issue, works on PC but not on Android. Tried 4.2.1, even beta 4.3 does not fix this.
     
  34. jdesantos

    jdesantos

    Joined:
    May 24, 2013
    Posts:
    312
    Are we the only that still have this problem?

    Even after upgrading to 4.3, our render textures never get created (native pointer is always null).

    Working in Windows, OSX and Linux.
    Failing in ios and android.

    Any update about this please?
     
  35. GonzoCubFan

    GonzoCubFan

    Joined:
    Oct 21, 2011
    Posts:
    70
    You must have an Android Pro license for this to work. It is now working for us.

    = Ed =
     
  36. _Domi_

    _Domi_

    Joined:
    Jul 25, 2013
    Posts:
    10
    It's not working for us. Can you post some solution/example?
     
  37. jdesantos

    jdesantos

    Joined:
    May 24, 2013
    Posts:
    312
    Hmm... If I understand them, our pro license (desktop) does not cover that feature. Is that correct?

    What is really strange is that after upgrading to 4.3 the feature worked (ios) and two hour later if stopped from working in the device, and hours later it stopped working in the editor (with platform ios selected).

    Really, suspicious. :)

    By the way, an error or a warning instead of doing nothing would have save us a lot of hours.
     
  38. Brickit

    Brickit

    Joined:
    Jul 4, 2012
    Posts:
    30
    Getting same problem with android. R2T is black in editor when android selected and is not working in the phone either.
    Is this because This pro-version version of unity does not have the Android pro?
     
  39. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Most likely. It will only use Unity features if the android license is Unity and not Unity Pro
     
  40. Brickit

    Brickit

    Joined:
    Jul 4, 2012
    Posts:
    30
    Yep that was it, i was using my other pro licence without the android pro . :D

    Works fine now.
     
  41. Maku

    Maku

    Joined:
    Feb 24, 2014
    Posts:
    2
    I'm not sure if it's the same problem, but if I set a skybox on my main camera, linked to a material containing a simple diffuse texture, it appears in black on Android.

    If I move to a mobile/skybox and use the same texture on the front, it works. Well I have to set the position with x;y to get something correct but it's better than nothing.
     
  42. Oaking

    Oaking

    Joined:
    Jun 19, 2014
    Posts:
    3
    Yes, It's the same problem, I Create a RT when open UI. Then Create a Camera render a plane which has MainTex of the RT, and make the 3D main camera not enable to combine the background 3D Scene(The plane RT) with UI, using this to improve performance on IOS. This world on PC, but on IOS, the RenderTexture is black.
     
  43. Oaking

    Oaking

    Joined:
    Jun 19, 2014
    Posts:
    3
    and My Unity version is 4.5 pro, Is this problem caused by Single-Thread on IOS, and with no problem on Multi-Thread Platform?
     
  44. srmojuze

    srmojuze

    Joined:
    Mar 18, 2013
    Posts:
    127
    FWIW on Mac OS X 10.9 and Unity 4.5 if the camera rendering to render texture has Antialiasing as a component then render texture to GUITexture does not work. Render texture to material works.
     
  45. sschoellhammer

    sschoellhammer

    Joined:
    Feb 15, 2013
    Posts:
    46
    Just out of interest, is this bug you were talking about fixed Alexey? On the latest version I still have to call Render in OnPostRender or I get a black image on IOS.. :/
     
  46. paskuniak

    paskuniak

    Joined:
    Aug 16, 2014
    Posts:
    17
    Finally managed to make it work...
    Try changing your RT's shader to unlit/texture
     
    Sixbraker likes this.
  47. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,279
    STILL an issue for us on android (4.5.3) - at least I got work around for my issue (reported Case 626849) by doing render texture stuff in OnPostRender as mentioned here
     
  48. bererton

    bererton

    Joined:
    Jun 20, 2014
    Posts:
    34
    I'm running into the same thing on Unity 5.02f1 running on iphone5. Works in the simulator, but I only get a black texture on the phone. Using OnPostRender doesn't seem to fit it for me, so it might be something else as well....
     
    giacomamba likes this.
  49. Suguma

    Suguma

    Joined:
    May 29, 2015
    Posts:
    26
    I'm having the same problem on 5.1.0f3. RenderTexture works perfectly on the editor, but all I get are black squares and complete garbage on the iPad.
    The OnPostRender workaround works, but having the camera enabled is not an option. I have multiple cameras that render things outside of the player view, so enableling them all is absolutely not an option.

    Anybody have a clue? Will test on 5.0.2 to see.
     
  50. Suguma

    Suguma

    Joined:
    May 29, 2015
    Posts:
    26
    If somebody still seeing this, I had the problem on 5.1 and rebuilding for 5.0.2 fixed it by itself. No workarounds needed.