示例#1
0
        static void RenderTriangles(VoxelData voxelData, int size, SKCanvas canvas, MeshSettings settings)
        {
            var matrix = GetMatrix(voxelData, size);

            settings.MeshType = MeshType.Triangles;
            var triangles = new MeshBuilder(voxelData, settings);

            using (var fill = new SKPaint()) {
                var vertices = triangles.Vertices
                               .Select(v => matrix.MapScalars(v.X, v.Z, -v.Y, 1f))
                               .Select(v => new SKPoint(v[0], v[1]))
                               .ToArray();
                var colors    = triangles.Colors;
                var occlusion = triangles.Occlusion;
                var indices   = triangles.Faces;

                // 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 => AmbientOcclusion.CombineColorOcclusion(colors[indices[j]], occlusion[indices[j]])).Select(ToSKColor).ToArray();
                    var _indices  = batch.Select(j => (ushort)(j - i)).ToArray();
                    canvas.DrawVertices(SKVertexMode.Triangles, _vertices, null, _colors, _indices, fill);
                }
            }
        }
示例#2
0
    public static void VoxelsToUnity(GameObject go, Mesh mesh, VoxelData voxelData, MeshSettings settings)
    {
        // Recenter the .vox model so the bottom is at the origin
        var matrix = Matrix4x4.Translate(new Vector3(-voxelData.size.X * 0.5f, 0, -voxelData.size.Y * 0.5f));

        // Convert a Voxel mesh to a Unity mesh
        var meshBuilder = new MeshBuilder(voxelData, settings);

        mesh.Clear();
        mesh.vertices  = meshBuilder.Vertices.Select(v => matrix.MultiplyPoint(new Vector3(v.X, v.Z, v.Y))).ToArray();
        mesh.normals   = meshBuilder.Normals.Select(n => matrix.MultiplyVector(new Vector3(n.X, n.Z, n.Y))).ToArray();
        mesh.triangles = meshBuilder.Faces.Select(f => (int)f).ToArray();

        // Combine occlusion with vertex colors
        var colors    = meshBuilder.Colors;
        var occlusion = meshBuilder.Occlusion;

        mesh.colors32 = Enumerable.Range(0, meshBuilder.Colors.Length)
                        .Select(i => AmbientOcclusion.CombineColorOcclusion(colors[i], occlusion[i]))
                        .Select(c => new Color32(c.R, c.G, c.B, c.A)).ToArray();

        // Set mesh filter mesh
        var meshFilter = go.GetComponent <MeshFilter>();

        if (meshFilter == null)
        {
            meshFilter = go.AddComponent <MeshFilter>();
        }
        meshFilter.sharedMesh = mesh;

        // Set mesh render material
        var meshRenderer = go.GetComponent <MeshRenderer>();

        if (meshRenderer == null)
        {
            meshRenderer = go.AddComponent <MeshRenderer>();
        }
        meshRenderer.sharedMaterial = Resources.Load <Material>(settings.FloorShadow ? "StandardTransparent" : "Standard");
    }