Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How can I get pixels from a Cubemap created by AREnvironmentProbe?

Discussion in 'AR' started by Ulven2, May 12, 2021.

  1. Ulven2


    Apr 23, 2012
    How can I get the pixels from a Cubemap that is created by an AREnvironmentProbe / AR Environment Probe Manager in AR Foundation?
    Using GetPixels / GetPixelData on the ReflectionProbe.customBakedTexture
    doesn't seem to work and result in a UnityException: Texture '' is not configured correctly to allow GetPixels.
    If I use ARCore directly instead of ARFoundation, I do seem to be able to access the environment probe.
    The texture is created from a native pointer and so it is already in memory, (i.e. I don't think it should be something I have to read back from the GPU like a render texture)

    What is the correct way of getting the pixels of the AR Environment Probe?

    Unity version: 2020.3.6
    ARFoundation 4.1.7
  2. davidmo_unity


    Unity Technologies

    Jun 18, 2019
    The underlying cubemap texture is externally produced and modified by ARCore and therefore not a readable texture. In order to read the pixels of the texture you will need to copy the cubemap to another texture. You can use Graphics.CopyTexture to accomplish this if your platform has copy texture support. If not you can setup your own texture Blit for the Cubemap.
    rainbowbl666d likes this.
  3. Deleted User

    Deleted User


    if anyone gets to this issue in the future, I just wanted to say I was able to do this on iOS.

    For some reason, "CopyTexture" will only work on iOS if the dest texture is a RenderTexture, and not another Cubemap. If dest is a cubemap, it works only on the editor (on windows 10 at least), but not on iOS.

    Code (CSharp):
    1.   private RenderTexture blitTextureCubemap;
    2.         private void InitRTX(Cubemap tx)
    3.         {
    4.             if (blitTextureCubemap == null) {
    5.                 blitTextureCubemap = new RenderTexture(tx.width, tx.height, 16 , tx.graphicsFormat, tx.mipmapCount);
    6.                 blitTextureCubemap.dimension = TextureDimension.Cube;
    7.                 blitTextureCubemap.useMipMap = true;
    8.             }
    9.         }
    11.      public byte[] convertProbeTOPNG(Cubemap tx)
    12. {
    13.             Texture2D finalTex = new Texture2D(tx.width * 6, tx.height, TextureFormat.RGBAFloat, false);
    15.             InitRTX(tx);
    17.             for (int i = 0; i < 6; i++)
    18.             {
    19.                 Graphics.CopyTexture(tx, i, blitTextureCubemap, i);
    20.                 Graphics.SetRenderTarget(blitTextureCubemap, 0, (CubemapFace)i);
    21.                 finalTex.ReadPixels(new Rect(0, 0, tx.width, tx.height), i * tx.width, 0);
    22.             }
    24.             finalTex.Apply();
    26.             return finalTex.EncodeToPNG();
    27. }