Search Unity

Question Unity RenderTexture Black when Saving PNG on Oculus Quest/Android

Discussion in 'VR' started by samuelesm, Aug 7, 2021.

  1. samuelesm

    samuelesm

    Joined:
    Jun 24, 2016
    Posts:
    1
    Hi, I've scoured the web for a possible solution to my problem with no avail.

    I'm currently trying to add a screenshot function on the Oculus Quest where a player can take a picture from a secondary camera.

    When I run my program from the editor, with the exception of lag from taking the picture, it works perfectly fine. However, when I push to app lab to run on the Oculus natively, I always get a blank image. I've messed around with the color encoding, flag settings, and using OnRenderImage.

    I've also tried several encoding options, so I think the problem lies in the RenderTexure.

    Apparently, having black images or RenderTextures is a common problem with Android or mobile devices, and I've seen some threads on them. As the Quest is an Android based device, I tried as many suggestions as I could find to no avail.

    Here's one of the threads I looked at:
    https://stackoverflow.com/questions/35196619/render-texture-black-on-android-only

    If anyone has a clue to what may be wrong, I'll be very grateful to hear your suggestions.

    Below is the section of code responsible for taking the picture.
    Thanks!

    Code (CSharp):
    1.     private IEnumerator TakePhoto()
    2.     {
    3.        
    4.         yield return new WaitForEndOfFrame();
    5.         string sceneName = SceneManager.GetActiveScene().name;
    6.         if (sceneName.Equals("HomeScene"))
    7.         {
    8.             cameraFlash.SetActive(true);
    9.         }
    10.         Camera cam = cameraTransform.GetComponent<Camera>();
    11.         RenderTexture renderText = RenderTexture.active;
    12.         cam.Render();
    13.         RenderTexture.active = cam.targetTexture;
    14.  
    15.  
    16.         Texture2D Im = new Texture2D(cam.targetTexture.width, cam.targetTexture.height, TextureFormat.RGB24, false);
    17.  
    18.         // adding wait here, makes error
    19.         Im.ReadPixels(new Rect(0, 0, cam.targetTexture.width, cam.targetTexture.height), 0, 0);
    20.         yield return new WaitForEndOfFrame();
    21.         yield return new WaitForEndOfFrame();
    22.         Im.Apply();
    23.         yield return Im;
    24.  
    25.         byte[] Bytes = ImageConversion.EncodeArrayToPNG(Im.GetRawTextureData(), Im.graphicsFormat, (uint)cam.targetTexture.width, (uint)cam.targetTexture.height);
    26.  
    27.         string photoTime = GetTimeStamp("");
    28.        
    29.         print("SceneName: " + sceneName);
    30.  
    31.         if (Directory.Exists(Application.persistentDataPath))
    32.         {
    33.             print("Capture Path: " + Application.persistentDataPath);
    34.             NativeGallery.Permission permission = NativeGallery.SaveImageToGallery(Im, "gameName", sceneName + "_" + photoTime + "_ - n" + FileCounter + ".png");
    35.             File.WriteAllBytes(Application.persistentDataPath + "\\" + sceneName + "_" + photoTime + "_-n" + FileCounter + ".png", Bytes);
    36.             FileCounter++;
    37.         }      
    38.         cameraFlash.SetActive(false);
    39.         Destroy(Im);
    40.     }
     
  2. mrstruijk

    mrstruijk

    Joined:
    Jan 24, 2018
    Posts:
    52
    Have you managed to find a fix for this?
     
  3. pch4n

    pch4n

    Joined:
    Aug 15, 2019
    Posts:
    1
    In my case I solved it by using OpenGLES3 instead of Vulkan
     
  4. hgulgen

    hgulgen

    Joined:
    Nov 29, 2016
    Posts:
    54
    2021.3.9f1 LTS version is problematic at render texture in android build. I tried a lot of things but then I switched old version unity LTS 2020. It works fine for me now. Do not waste your time :/ If you have a problem.
     
  5. weiner_monkey

    weiner_monkey

    Joined:
    Aug 1, 2022
    Posts:
    49
  6. DanielSafs

    DanielSafs

    Joined:
    Nov 22, 2016
    Posts:
    11
    Same problem here. Just disabled Autographics API and set OpenGL as first option. Thanks guys, lost a entire day trying other solutions.
     
  7. elvis-satta

    elvis-satta

    Joined:
    Nov 13, 2013
    Posts:
    19
    Hi, does anyone know if by any chance this Vulkan issue has been fixed in recent 2021 LTS versions?
     
  8. weiner_monkey

    weiner_monkey

    Joined:
    Aug 1, 2022
    Posts:
    49