Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
3.04k stars 1.14k forks source link

[Bug] Get the ARCamera image to a RenderTexture, ReadPixels from the RenderTexture then set to a RawImage , flickers color ??? #985

Closed hasionerin closed 2 years ago

hasionerin commented 2 years ago

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:

`

public Camera cam; 
public RawImage img;

private Texture2D renderResult = null;

public RenderTexture camRT;

#region GPU 

private RenderTexture mRenderTexture;

private void Update()
{
    if (mRenderTexture == null)
    {
        RenderTextureDescriptor renderTextureDes = new RenderTextureDescriptor(480, 640, RenderTextureFormat.BGRA32);

        mRenderTexture = RenderTexture.GetTemporary(renderTextureDes);
    }

    cam.targetTexture = mRenderTexture;
    cam.Render();

    RenderTexture.active = mRenderTexture;

    if (renderResult == null)
        renderResult = new Texture2D(480, 640, TextureFormat.RGB24, false);

    renderResult.ReadPixels(new Rect(0, 0, mRenderTexture.width, mRenderTexture.height), 0, 0);
    renderResult.Apply();

    img.texture = renderResult;

    cam.targetTexture = null;
    RenderTexture.active = null;

}

#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 :

微信图片_20220614155737

微信图片_20220614155745 微信图片_20220614155754

hasionerin commented 2 years ago

@tdmowrer please helps me . This problem has puzzled me for a long time. thanks a lot !

DavidMohrhardt commented 2 years ago

I can't help but wonder if this might be a timing issue.

To validate it's not can you try just using Graphics.Blit or use a blit command buffer and add it to the CameraEvent.AfterEverything event like so:

commandBuffer.clear();
commandBuffer.ClearRenderTarget(true, false, Color.clear);
commandBuffer.Blit(null, myRenderTarget, material);
Camera.main.AddCommandBuffer(CameraEvent.AfterEverything, commandBuffer);

then read the texture OnPostRender.

andyb-unity commented 2 years ago

Closing due to inactivity