Skip to content

Commit

Permalink
Render triangles in batches of up to 20000
Browse files Browse the repository at this point in the history
This allows large voxel files with more than 65536 vertices to still render correctly.
The test case is from mmmm scene_parade.vox.
  • Loading branch information
Arlorean committed Aug 14, 2017
1 parent 3bc93cd commit c90f417
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Voxels.SkiaSharp/Renderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SkiaSharp;
using System;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -79,9 +80,18 @@ static void RenderTriangles(VoxelData voxelData, int size, SKCanvas canvas, Mesh
.Select(v => matrix.MapScalars(v.X, v.Z, -v.Y, 1f))
.Select(v => new SKPoint(v[0], v[1]))
.ToArray();
var colors = triangles.Colors.Select(ToSKColor).ToArray();
var colors = triangles.Colors;
var indices = triangles.Faces;
canvas.DrawVertices(SKVertexMode.Triangles, vertices, null, colors, indices, fill);

// Render triangles in batches since SkiaSharp DrawVertices indices are 16 bit which fails for large files
var batchSize = 3*20000; // up to 20000 triangles per batch
for (var i = 0; i < indices.Length; i += batchSize) {
var batch = Enumerable.Range(i, Math.Min(batchSize, indices.Length - i));
var _vertices = batch.Select(j => vertices[indices[j]]).ToArray();
var _colors = batch.Select(j => colors[indices[j]]).Select(ToSKColor).ToArray();
var _indices = batch.Select(j => (ushort)(j-i)).ToArray();
canvas.DrawVertices(SKVertexMode.Triangles, _vertices, null, _colors, _indices, fill);
}
}
}

Expand Down

0 comments on commit c90f417

Please sign in to comment.