Search Unity

Bug Get the ARCamera image to a RenderTexture, ReadPixels from the RenderTexture then set to a RawImage

Discussion in 'AR' started by hasion01, Jun 14, 2022.

  1. hasion01

    hasion01

    Joined:
    Nov 11, 2021
    Posts:
    4
    Unity Version : Unity2020.3.18
    ARFoundation Version : 4.2.1
    ARCore XR Plugin 4.2.1
    Platform : Android

    I Get the ARCamera Image Real-time, Render to a RenderTexture, then ReadPixels from the RenderTexture ,and set to a RawImage , unfortunately, the RawImage flickers color .

    follow is my code:
    Code (CSharp):
    1. public Camera cam;
    2. public RawImage img;
    3.  
    4. private Texture2D renderResult = null;
    5.  
    6. public RenderTexture camRT;
    7.  
    8. #region GPU
    9.  
    10. private RenderTexture mRenderTexture;
    11.  
    12. private void Update()
    13. {
    14.    if (mRenderTexture == null)
    15.    {
    16.        RenderTextureDescriptor renderTextureDes = new RenderTextureDescriptor(480, 640, RenderTextureFormat.BGRA32);
    17.  
    18.        mRenderTexture = RenderTexture.GetTemporary(renderTextureDes);
    19.    }
    20.  
    21.    cam.targetTexture = mRenderTexture;
    22.    cam.Render();
    23.  
    24.    RenderTexture.active = mRenderTexture;
    25.  
    26.    if (renderResult == null)
    27.        renderResult = new Texture2D(480, 640, TextureFormat.RGB24, false);
    28.  
    29.    renderResult.ReadPixels(new Rect(0, 0, mRenderTexture.width, mRenderTexture.height), 0, 0);
    30.    renderResult.Apply();
    31.  
    32.    img.texture = renderResult;
    33.  
    34.    cam.targetTexture = null;
    35.    RenderTexture.active = null;
    36.  
    37. }
    38.  
    39. #endregion
    it like flicker every few frames . It's too strange . and it works fine on ios devices. just error on android devices . has anyone had this problem? and how to solve this problem?

    i take some photos :
     

    Attached Files: