Using GetPixel() on MagicaVoxel model, not working on Android

Hi,

I’m making a 3D game using voxel graphics, all of them being made with MagicaVoxel.

I’m trying to retrieve a pixel color with a raycast, in order to « analyze » the environment of the player.

It works fine when running the game in the editor.
But it fails to read the pixel color in the Android build ; the color read is always dark grey. More precisely, it fails on objects imported from MagicaVoxel.

I made a test with a regular cube created in Unity, and using regular Material and texture. In this case, GetPixel() works fine, even in Android.

Could this problem occur because of the way MagicaVoxel meshes are textured ?

As you know, MV does not generate UVs on its meshes. It generates a 256*1 pixel PNG texture, each pixel corresponding to a palette color, and each voxel using one of these colors.

Sorry if my explanations are not that clear, but MV users know what I’m talking about.

So, if the problem has to do with UV mapping, why does it work at Unity runtime but not in Android ?

Or is it a problem of texture compression in Android, as I read here : Android returning white in Texture2D.GetPixel(x,y) - Questions & Answers - Unity Discussions ?

Tried to override texture settings for Android as suggested, but it failed (by the way I couldn’t find any RGBA 3 bits, tried with 32 bits).

A few precisions :

  • both of my objects (MV mesh and Unity cube) have a Mesh Colider ;
  • both textures (including the PNG palette generated by MV) are read/write enabled ;
  • I use .OBJ format to export my models from MV to Unity.

Here is my code (this method is called when the raycast hits something) :

void ReadRaycastedColor(RaycastHit hit) {
    
            
        Renderer rend = hit.collider.GetComponent< MeshRenderer>();
           
        Texture2D texture2D = rend.material.mainTexture as Texture2D;
            
        Vector2 pCoord = hit.textureCoord;
            
        pCoord.x *= texture2D.width;
            
        pCoord.y *= texture2D.height;
            
        Vector2 tiling = rend.material.mainTextureScale;
        Color raycastedColor = texture2D.GetPixel(Mathf.FloorToInt(pCoord.x * tiling.x), Mathf.FloorToInt(pCoord.y * tiling.y));
        
    }

Hope there is a solution without having to remesh and generate UVs in Blender…

Thanx in advance for any help.

Finally found the issue, and it was quite simple.
All you have to do is to enable the Read/Write permission not only for the texture, but also for the mesh.