public void UpdateSortOrder()
        {
            int maxMat   = MyVoxelMaterials.GetMaterialsCount() + 1;
            int matCount = MyVoxelMaterials.GetMaterialsCount() + 2;

            int mats0 = (int)Material0;
            int mats1 = (int)(Material1.HasValue ? (int)Material1.Value : maxMat);
            int mats2 = (int)(Material2.HasValue ? (int)Material2.Value : maxMat);

            //  Important is type and material/texture. Order of type is defined by enum values
            SortOrder  = ((int)Type * matCount * matCount * matCount) + mats2 * matCount * matCount + mats1 * matCount + mats0;
            MaterialId = mats2 * matCount * matCount + mats1 * matCount + mats0;
        }
        void EndMultiMaterial(MyMultiMaterialHelper helper)
        {
            if (helper.VertexCount > 0)
            {
                //  This will just preload textures used by this material - so they are ready in memory when first time drawn
                MyVoxelMaterials.Get(helper.Material0).GetTextures();
                MyVoxelMaterials.Get(helper.Material1).GetTextures();
                MyVoxelMaterials.Get(helper.Material2).GetTextures();

                MyVoxelCacheCellRenderBatch newBatch = new MyVoxelCacheCellRenderBatch();

                //  Vertex buffer
                newBatch.VertexBufferCount = helper.VertexCount;
                newBatch.VertexBuffer      = new VertexBuffer(MyMinerGame.Static.GraphicsDevice, MyVertexFormatVoxelSingleMaterial.Stride * newBatch.VertexBufferCount, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                newBatch.VertexBuffer.Lock(0, 0, LockFlags.None).WriteRange(helper.Vertices, 0, newBatch.VertexBufferCount);
                newBatch.VertexBuffer.Unlock();
                newBatch.VertexBuffer.Tag       = this;
                newBatch.VertexBuffer.DebugName = "VoxelBatchMulti";
                newBatch.VertexBufferSize       = helper.VertexCount * MyVertexFormatVoxelSingleMaterial.Stride;
                MyPerformanceCounter.PerAppLifetime.VoxelVertexBuffersSize += newBatch.VertexBufferSize;

                // Index buffer (because everything must have IB)
                newBatch.IndexBufferCount = helper.VertexCount;
                newBatch.IndexBuffer      = new IndexBuffer(MyMinerGame.Static.GraphicsDevice, newBatch.IndexBufferCount * sizeof(short), Usage.WriteOnly, Pool.Default, true);

                short[] indices = new short[helper.VertexCount];
                for (short i = 0; i < indices.Length; i++)
                {
                    indices[i] = i;
                }
                newBatch.IndexBuffer.Lock(0, 0, LockFlags.None).WriteRange(indices);
                newBatch.IndexBuffer.Unlock();
                newBatch.IndexBuffer.DebugName = "VoxelBatchMulti";
                newBatch.IndexBuffer.Tag       = this;
                newBatch.IndexBufferSize       = helper.VertexCount * sizeof(short);
                MyPerformanceCounter.PerAppLifetime.VoxelIndexBuffersSize += newBatch.IndexBufferSize;

                newBatch.Type      = MyVoxelCacheCellRenderBatchType.MULTI_MATERIAL;
                newBatch.Material0 = helper.Material0;
                newBatch.Material1 = helper.Material1;
                newBatch.Material2 = helper.Material2;
                newBatch.UpdateSortOrder();

                Batches.Add(newBatch);
            }

            //  Reset helper arrays, so we can start adding triangles to them again
            helper.VertexCount = 0;
        }
        void EndSingleMaterial(MySingleMaterialHelper materialHelper)
        {
            if (materialHelper.IndexCount > 0 && materialHelper.VertexCount > 0)
            {
                //  This will just preload textures used by this material - so they are ready in memory when first time drawn
                MyVoxelMaterials.Get(materialHelper.Material).GetTextures();

                MyVoxelCacheCellRenderBatch newBatch = new MyVoxelCacheCellRenderBatch();
                //  Vertex buffer
                newBatch.VertexBufferCount = materialHelper.VertexCount;
                newBatch.VertexBuffer      = new VertexBuffer(MyMinerGame.Static.GraphicsDevice, MyVertexFormatVoxelSingleMaterial.Stride * newBatch.VertexBufferCount, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                newBatch.VertexBuffer.Lock(0, 0, LockFlags.None).WriteRange(materialHelper.Vertices, 0, newBatch.VertexBufferCount);
                newBatch.VertexBuffer.Unlock();
                newBatch.VertexBuffer.Tag       = newBatch;
                newBatch.VertexBuffer.DebugName = "VoxelBatchSingle";
                newBatch.VertexBufferSize       = materialHelper.VertexCount * MyVertexFormatVoxelSingleMaterial.Stride;
                MyPerformanceCounter.PerAppLifetime.VoxelVertexBuffersSize += newBatch.VertexBufferSize;

                //  Index buffer
                newBatch.IndexBufferCount = materialHelper.IndexCount;
                newBatch.IndexBuffer      = new IndexBuffer(MyMinerGame.Static.GraphicsDevice, newBatch.IndexBufferCount * sizeof(short), Usage.WriteOnly, Pool.Default, true);
                newBatch.IndexBuffer.Lock(0, 0, LockFlags.None).WriteRange(materialHelper.Indices, 0, newBatch.IndexBufferCount);
                newBatch.IndexBuffer.Unlock();
                newBatch.IndexBuffer.DebugName = "VoxelBatchSingle";
                newBatch.IndexBufferSize       = materialHelper.IndexCount * sizeof(short);
                MyPerformanceCounter.PerAppLifetime.VoxelIndexBuffersSize += newBatch.IndexBufferSize;

                newBatch.Type      = MyVoxelCacheCellRenderBatchType.SINGLE_MATERIAL;
                newBatch.Material0 = materialHelper.Material;
                newBatch.Material1 = null;
                newBatch.Material2 = null;
                newBatch.UpdateSortOrder();

                Batches.Add(newBatch);
            }
            //  Reset helper arrays, so we can start adding triangles to them again
            materialHelper.IndexCount  = 0;
            materialHelper.VertexCount = 0;
            MyVoxelCacheCellRenderHelper.SingleMaterialIndicesLookupCount[(int)materialHelper.Material]++;
        }