Search Unity

ARFoundation Black screen when trying to get CPU camera

Discussion in 'AR' started by Miaounathor, Oct 21, 2019.

  1. Miaounathor

    Miaounathor

    Joined:
    May 22, 2019
    Posts:
    2
    Hey guys

    I've followed this page to get the picture from the camera using this tutorial. One of the problem I encounter is that whenever I call the function to call the image 2 different situations happen:

    1. If Multithreading rendering is enabled, the FPS will drop all the way down to 10 FPS
    2. If Multithreading rendering is disabled, I have a full black screen

    Is there any other parameter I should change?

    Unity 2019.1.14.f1
    ARFoundation 2.1.4
    Arcore XRPlugin 2.1.2

    Tested on a Xiaomi mix 2


    The code to get the camera image in CPU is the following

    Code (CSharp):
    1. private unsafe void GetImage()
    2.     {
    3.         XRCameraImage image;
    4.         if (m_CameraManager.TryGetLatestImage(out image))
    5.         {
    6.             var conversionParams = new XRCameraImageConversionParams
    7.             {
    8.                 // Get the entire image
    9.                 inputRect = new RectInt(0, 0, image.width, image.height),
    10.  
    11.                 // Downsample by 2
    12.                 outputDimensions = new Vector2Int(image.width / 2, image.height / 2),
    13.  
    14.                 // Choose RGBA format
    15.                 outputFormat = TextureFormat.RGBA32,
    16.  
    17.                 // Flip across the vertical axis (mirror image)
    18.                 transformation = CameraImageTransformation.MirrorY
    19.             };
    20.  
    21.             // See how many bytes we need to store the final image.
    22.             int size = image.GetConvertedDataSize(conversionParams);
    23.  
    24.             // Allocate a buffer to store the image
    25.             var buffer = new NativeArray<byte>(size, Allocator.Temp);
    26.  
    27.             // Extract the image data
    28.             image.Convert(conversionParams, new IntPtr(buffer.GetUnsafePtr()), buffer.Length);
    29.  
    30.             // The image was converted to RGBA32 format and written into the provided buffer
    31.             // so we can dispose of the CameraImage. We must do this or it will leak resources.
    32.             image.Dispose();
    33.  
    34.             // At this point, we could process the image, pass it to a computer vision algorithm, etc.
    35.             // In this example, we'll just apply it to a texture to visualize it.
    36.  
    37.             // We've got the data; let's put it into a texture so we can visualize it.
    38.             m_Texture = new Texture2D(
    39.                 conversionParams.outputDimensions.x,
    40.                 conversionParams.outputDimensions.y,
    41.                 conversionParams.outputFormat,
    42.                 false);
    43.  
    44.             m_Texture.LoadRawTextureData(buffer);
    45.             m_Texture.Apply();
    46.  
    47.         }
    48.     }
    Thanks!
     
    Last edited: Oct 21, 2019
    danyruibal likes this.
  2. danyruibal

    danyruibal

    Joined:
    Sep 13, 2018
    Posts:
    2
    I got exactly the same behaviour.

    With multithreaded rendering off it works just fine in many phones, but I got a black screen on Pocophone F1, Samsung Galaxy Tab S6 and Sony Xperia XZ2.

    As soon as I call TryGetLatestImage for the first time on the frameUpdated callback, the ARCameraBackground scripts stops painting the camera background and draws a black screen. If I rotate the device changing its orientation, the screen starts to paint the camera background again. All the retrieved images are valid, but the camera background keeps black all the time after the first call.
     
  3. pawlusmall

    pawlusmall

    Joined:
    Oct 21, 2016
    Posts:
    2
    Same problem here with ARFoundation 2.2.0-preview3

    On Samsung Galaxy Tab S6 and Xperia XZ2 it displays a black screen when I call TryGetLatestImage() method. That black screen keeps there unless I rotate the phone (change its orientation).

    In other phones (Pixel 2XL, Samsung Galaxy S9, Samsung Galaxy S9 plus, Asus Zenfone AR, LG G6 ..) it just works fine.
     
    danyruibal likes this.