Example #1
0
    public static byte[] TileToVox(VTile tile)
    {
        List <byte> data = new List <byte>();

        Add(data, (byte)'V', (byte)'O', (byte)'X', (byte)' ');
        Add(data, System.BitConverter.GetBytes(150));
        Add(data, (byte)'M', (byte)'A', (byte)'I', (byte)'N');
        Add(data, System.BitConverter.GetBytes(0));
        int mainChildSizeIndex = data.Count;

        Add(data, (byte)'S', (byte)'I', (byte)'Z', (byte)'E');
        Add(data, System.BitConverter.GetBytes(12));
        Add(data, System.BitConverter.GetBytes(0));
        Add(data, System.BitConverter.GetBytes(tile.GetWidth()));
        Add(data, System.BitConverter.GetBytes(tile.GetHeight()));
        Add(data, System.BitConverter.GetBytes(tile.GetDepth()));
        Add(data, (byte)'X', (byte)'Y', (byte)'Z', (byte)'I');
        int xyziContentSizeIndex = data.Count;

        Add(data, System.BitConverter.GetBytes(0));
        int numVoxelsIndex = data.Count;
        int numVoxels      = 0;

        for (int x = 0; x < tile.GetWidth(); x++)
        {
            for (int y = 0; y < tile.GetHeight(); y++)
            {
                for (int z = 0; z < tile.GetDepth(); z++)
                {
                    int index = GetIndex(tile, x, y, z);
                    if (index != 0)
                    {
                        Add(data, (byte)x, (byte)(tile.GetHeight() - z - 1), (byte)y, (byte)(255 - index + 1));
                        numVoxels++;
                    }
                }
            }
        }
        data.InsertRange(numVoxelsIndex, System.BitConverter.GetBytes(numVoxels));
        data.InsertRange(xyziContentSizeIndex, System.BitConverter.GetBytes(data.Count - xyziContentSizeIndex - 4));
        Add(data, (byte)'R', (byte)'G', (byte)'B', (byte)'A');
        Add(data, System.BitConverter.GetBytes(256 * 4));
        Add(data, System.BitConverter.GetBytes(0));
        for (int i = 255; i >= 0; i--)
        {
            VColor c;
            if (i >= tile.GetPalette().GetCount())
            {
                c = new VColor(255, 0, 255, 255, 0, 0, 255, 0);
            }
            else
            {
                c = tile.GetPalette().GetColor(i);
            }
            Add(data, c.r, c.g, c.b, c.a);
        }
        data.InsertRange(mainChildSizeIndex, System.BitConverter.GetBytes(data.Count - mainChildSizeIndex));
        return(data.ToArray());
    }
Example #2
0
    void Awake()
    {
        use        = this;
        tile       = new VTile();
        refPalette = new VPalette();

        width  = tile.GetWidth();
        height = tile.GetHeight();
        depth  = tile.GetDepth();
    }
Example #3
0
    void LateUpdate()
    {
        VTile t = tile.GetTile();

        cachedVChunk = chunk;

        if (chunk.GetWidth() != t.GetWidth() ||
            chunk.GetHeight() != t.GetHeight() ||
            chunk.GetDepth() != t.GetDepth())
        {
            chunk.Resize(t.GetWidth(), t.GetHeight(), t.GetDepth());
        }

        if (GetChunk().IsDirty() || t.GetPalette().IsDirty())
        {
            Refresh();
        }

        mr.enabled = Tool.editing;
    }
Example #4
0
    public static Mesh TileToMesh(VTile tile)
    {
        Mesh mesh = new Mesh();

        v = 0;

        for (int x = 0; x < tile.GetWidth(); x++)
        {
            for (int y = 0; y < tile.GetHeight(); y++)
            {
                for (int z = 0; z < tile.GetDepth(); z++)
                {
                    VColor c = GetColor(tile, x, y, z);
                    if (GetColor(tile, x, y, z).a == 0)
                    {
                        continue;
                    }
                    Vector3 p = new Vector3(x, y, z);

                    bool useSubMesh = c.a < 255;

                    if (x == 0 || (GetColor(tile, x - 1, y, z).a < 255 && GetColor(tile, x - 1, y, z) != c))
                    {
                        verts.Add(p + new Vector3(0f, 0f, 1f));
                        verts.Add(p + new Vector3(0f, 1f, 1f));
                        verts.Add(p + new Vector3(0f, 1f, 0f));
                        verts.Add(p + new Vector3(0f, 0f, 0f));

                        AddFace(tile, x, y, z, Vector3.left, useSubMesh);
                    }
                    if (x == tile.GetWidth() - 1 || (GetColor(tile, x + 1, y, z).a < 255 && GetColor(tile, x + 1, y, z) != c))
                    {
                        verts.Add(p + new Vector3(1f, 0f, 0f));
                        verts.Add(p + new Vector3(1f, 1f, 0f));
                        verts.Add(p + new Vector3(1f, 1f, 1f));
                        verts.Add(p + new Vector3(1f, 0f, 1f));

                        AddFace(tile, x, y, z, Vector3.right, useSubMesh);
                    }
                    if (y == 0 || (GetColor(tile, x, y - 1, z).a < 255 && GetColor(tile, x, y - 1, z) != c))
                    {
                        verts.Add(p + new Vector3(1f, 0f, 0f));
                        verts.Add(p + new Vector3(1f, 0f, 1f));
                        verts.Add(p + new Vector3(0f, 0f, 1f));
                        verts.Add(p + new Vector3(0f, 0f, 0f));

                        AddFace(tile, x, y, z, Vector3.down, useSubMesh);
                    }
                    if (y == tile.GetHeight() - 1 || (GetColor(tile, x, y + 1, z).a < 255 && GetColor(tile, x, y + 1, z) != c))
                    {
                        verts.Add(p + new Vector3(0f, 1f, 0f));
                        verts.Add(p + new Vector3(0f, 1f, 1f));
                        verts.Add(p + new Vector3(1f, 1f, 1f));
                        verts.Add(p + new Vector3(1f, 1f, 0f));

                        AddFace(tile, x, y, z, Vector3.up, useSubMesh);
                    }
                    if (z == 0 || (GetColor(tile, x, y, z - 1).a < 255 && GetColor(tile, x, y, z - 1) != c))
                    {
                        verts.Add(p + new Vector3(0f, 0f, 0f));
                        verts.Add(p + new Vector3(0f, 1f, 0f));
                        verts.Add(p + new Vector3(1f, 1f, 0f));
                        verts.Add(p + new Vector3(1f, 0f, 0f));

                        AddFace(tile, x, y, z, Vector3.back, useSubMesh);
                    }
                    if (z == tile.GetDepth() - 1 || (GetColor(tile, x, y, z + 1).a < 255 && GetColor(tile, x, y, z + 1) != c))
                    {
                        verts.Add(p + new Vector3(1f, 0f, 1f));
                        verts.Add(p + new Vector3(1f, 1f, 1f));
                        verts.Add(p + new Vector3(0f, 1f, 1f));
                        verts.Add(p + new Vector3(0f, 0f, 1f));

                        AddFace(tile, x, y, z, Vector3.forward, useSubMesh);
                    }
                }
            }
        }

        mesh.vertices  = verts.ToArray();
        mesh.normals   = normals.ToArray();
        mesh.uv        = uvs.ToArray();
        mesh.triangles = tris.ToArray();

        mesh.RecalculateBounds();

        verts.Clear();
        normals.Clear();
        uvs.Clear();
        tris.Clear();

        return(mesh);
    }
Example #5
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);
    }