示例#1
0
    public static VTile VoxToTile(byte[] vox)
    {
        VTile tile = new VTile();

        VPalette pal = tile.GetPalette();

        for (int j = 0; j < 256; j++)
        {
            byte[] bits = System.BitConverter.GetBytes(vox_default_palette[j]);
            VColor c    = new VColor(bits[0], bits[1], bits[2], bits[3]);
            if (j >= pal.GetCount())
            {
                pal.AddColor(c);
            }
            else
            {
                pal.SetColor(j, c);
            }
        }

        int    i     = 0;
        string chunk = "" + (char)vox[i + 0] + (char)vox[i + 1] + (char)vox[i + 2] + (char)vox[i + 3];

        i += 4;
        if (chunk != "VOX ")
        {
            throw new System.Exception("Invalid VOX file");
        }
        int version = System.BitConverter.ToInt32(vox, i);

        i += 4;
        if (version != 150)
        {
            throw new System.Exception("Unsupported VOX version (expected 150, got " + version + ")");
        }

        while (i < vox.Length)
        {
            chunk = "" + (char)vox[i + 0] + (char)vox[i + 1] + (char)vox[i + 2] + (char)vox[i + 3];
            i    += 4;
            int contentLength = System.BitConverter.ToInt32(vox, i);
            i += 4;
            //int childrenLength = System.BitConverter.ToInt32(vox, i);
            i += 4;
            if (chunk == "MAIN")
            {
            }
            else if (chunk == "PACK")
            {
                int numModels = System.BitConverter.ToInt32(vox, i);
                i += 4;
                if (numModels > 1)
                {
                    throw new System.Exception("Unsupported VOX feature (cannot read multi-model pack files)");
                }
            }
            else if (chunk == "SIZE")
            {
                int x = System.BitConverter.ToInt32(vox, i);
                i += 4;
                int y = System.BitConverter.ToInt32(vox, i);
                i += 4;
                int z = System.BitConverter.ToInt32(vox, i);
                i += 4;
                tile.Resize(x, z, y);
            }
            else if (chunk == "XYZI")
            {
                int count = System.BitConverter.ToInt32(vox, i);
                i += 4;
                for (int j = 0; j < count; j++)
                {
                    byte x = vox[i + 0];
                    byte y = vox[i + 1];
                    byte z = vox[i + 2];
                    byte c = vox[i + 3];
                    tile.GetChunk(0, 0, 0).SetPaletteIndexAt(x, z, tile.GetDepth() - y - 1, (byte)(255 - c + 1));
                    i += 4;
                }
            }
            else if (chunk == "RGBA")
            {
                for (int j = 0; j < 256; j++)
                {
                    VColor c = new VColor(vox[i + 0], vox[i + 1], vox[i + 2], vox[i + 3]);
                    i += 4;
                    // Palette color 0 is always transparent
                    if (j == 255)
                    {
                        c.a = 0;
                    }
                    if (j >= pal.GetCount())
                    {
                        pal.AddColor(c);
                    }
                    else
                    {
                        pal.SetColor(255 - j, c);
                    }
                }
            }
            else
            {
                i += contentLength;
            }
        }

        return(tile);
    }