Search Unity

Enumeration of asset's colours, imported from MagicaVoxel .obj

Discussion in 'External Tools' started by jdcrane, Oct 10, 2019.

  1. jdcrane

    jdcrane

    Joined:
    Oct 3, 2019
    Posts:
    9
    Hi, I'm trying to programmatically enumerate the 3 colours used in a simple .obj asset I imported from MagicaVoxel but am not yielding any sensible results.

    I wondered if I could persuade someone to slap a brief example together to show me how, tested against the attached files? I could really use the help, chr
     

    Attached Files:

    Last edited: Oct 10, 2019
  2. jdcrane

    jdcrane

    Joined:
    Oct 3, 2019
    Posts:
    9
    In case anyone is interested, here's some sample code, the key being to ensure you enable read/write on the .png palette generated alongside the .obj file exported by MagicaVoxel:

    Code (CSharp):
    1. MeshRenderer mr = this.go.GetComponentInChildren<MeshRenderer>();
    2. MeshFilter mf = this.go.GetComponentInChildren<MeshFilter>();
    3. Vector2[] uv = mf.mesh.uv;
    4. Texture2D t2d = (Texture2D)mr.material.mainTexture;
    5.  
    6. // iterate the uv
    7. for (int i = 0; i < uv.Length; i++)
    8. {
    9.     Vector2 v2 = uv[i];
    10.     int ix = (int)(v2.x * t2d.width);
    11.     int iy = (int)(v2.y * t2d.height);
    12.  
    13.     ...
    14. }