public void UpdateSortOrder() { int maxMat = MyRenderVoxelMaterials.GetMaterialsCount() + 1; int matCount = MyRenderVoxelMaterials.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; }
private unsafe void AddBatch(MyClipmapCellBatch batch) { // This will just preload textures used by this material - so they are ready in memory when first time drawn MyRenderVoxelMaterials.Get((byte)batch.Material0).GetTextures(); MyRenderVoxelBatch newBatch = new MyRenderVoxelBatch(); string debugName = "VoxelBatchSingle"; newBatch.Type = MyRenderVoxelBatchType.SINGLE_MATERIAL; newBatch.Lod = m_metadata.Cell.Lod; newBatch.Material0 = (byte)batch.Material0; newBatch.Material1 = batch.Material1 == -1 ? (byte?)null : (byte)batch.Material1; newBatch.Material2 = batch.Material2 == -1 ? (byte?)null : (byte)batch.Material2; if (newBatch.Material1 != null || newBatch.Material2 != null) { newBatch.Type = MyRenderVoxelBatchType.MULTI_MATERIAL; debugName = "VoxelBatchMulti"; } // Vertex buffer int vbSize = sizeof(MyVertexFormatVoxelSingleData) * batch.Vertices.Length; newBatch.VertexCount = batch.Vertices.Length; // When Usage.Dynamic was not there, it crashed on nVidia cards newBatch.VertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, vbSize, Usage.WriteOnly | Usage.Dynamic, VertexFormat.None, Pool.Default); newBatch.VertexBuffer.SetData(batch.Vertices, LockFlags.Discard); newBatch.VertexBuffer.Tag = newBatch; newBatch.VertexBuffer.DebugName = debugName; MyPerformanceCounter.PerAppLifetime.VoxelVertexBuffersSize += vbSize; // Index buffer int ibSize = sizeof(short) * batch.Indices.Length; newBatch.IndexCount = batch.Indices.Length; // When Usage.Dynamic was not there, it crashed on nVidia cards newBatch.IndexBuffer = new IndexBuffer(MyRender.GraphicsDevice, ibSize, Usage.WriteOnly | Usage.Dynamic, Pool.Default, true); newBatch.IndexBuffer.SetData(batch.Indices, LockFlags.Discard); newBatch.IndexBuffer.DebugName = debugName; MyPerformanceCounter.PerAppLifetime.VoxelIndexBuffersSize += ibSize; newBatch.UpdateSortOrder(); m_batches.Add(newBatch); }