Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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

    Ulven2

    Joined:
    Apr 23, 2012
    Posts:
    64
    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

    davidmo_unity

    Unity Technologies

    Joined:
    Jun 18, 2019
    Posts:
    99
    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

    Guest

    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.         }
    10.  
    11.      public byte[] convertProbeTOPNG(Cubemap tx)
    12. {
    13.             Texture2D finalTex = new Texture2D(tx.width * 6, tx.height, TextureFormat.RGBAFloat, false);
    14.  
    15.             InitRTX(tx);
    16.  
    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.             }
    23.  
    24.             finalTex.Apply();
    25.  
    26.             return finalTex.EncodeToPNG();
    27. }