public static GeometryData GetGeometryData(SvgMeshData data, bool instanced) { var result = new GeometryData() { VertexCount = data.Vertices.Count, Indices = data.Indices.ToArray(), Attribs = instanced ? MeshHelper.PositionColorInstancedVertex.VertexAttribs : MeshHelper.PositionColorVertex.VertexAttribs }; using (var stream = new System.IO.MemoryStream()) using (var writer = new System.IO.BinaryWriter(stream)) { for (int i = 0; i < data.Vertices.Count; i++) { writer.Write(data.Vertices[i].PosX); writer.Write(data.Vertices[i].PosY); writer.Write(data.Vertices[i].PosZ); writer.Write(data.Vertices[i].R); writer.Write(data.Vertices[i].G); writer.Write(data.Vertices[i].B); writer.Write(data.Vertices[i].A); } result.Data = stream.ToArray(); } return(result); }
/// <summary> /// Applies translation and scaling transforms to the vertices so you don't need to keep track of them later on /// </summary> /// <param name="data">Mesh data</param> /// <param name="normalize">Gets the document width as the unit and normalizes the scaling given this new unit, if the document has 512px width, 512px becomes 1 unit</param> /// <returns></returns> public static SvgMeshData ApplyTransforms(SvgMeshData data, bool normalize = true) { var translate = new float[] { data.Translation?.X() ?? 0f, data.Translation?.Y() ?? 0, 0f }; var scale = new float[] { data.Scaling?.X() ?? 1f, data.Scaling?.Y() ?? 1f, 1f }; if (normalize) { if (!(data.Size is null)) { scale = scale.VectorScale(1 / data.Size[0]); translate = translate.VectorScale(1 / data.Size[0]); translate[1] += 1; } } var transform = translate.MatrixCompose(translate, new float[] { 0, 0, 0, 1f }, scale); data.Vertices = data.Vertices.Select(point => { var transformed = new float[] { point.PosX, point.PosY, point.PosZ }.VectorTransform(transform); point.PosX = transformed.X(); point.PosY = transformed.Y(); point.PosZ = transform.Z(); return(point); }).ToList(); return(data); }
public static SvgMeshData GetMesh(GraphicElement item, Style parentStyle, Definitions definitions) { var style = StyleHelper.GetStyle(item, parentStyle, definitions); var mesh = new SvgMeshData() { Scaling = new float[] { style.Transform.Scale[0], -style.Transform.Scale[1], 1f }, Translation = new float[] { style.Transform.Translate[0], -style.Transform.Translate[1], 1f } }; if (item is Circle) { var points = GetPoints((Circle)item); var temp = GetMeshData((Circle)item, points, style.Fill); mesh.Indices = temp.Indices; mesh.Vertices = temp.Vertices; var border = GetBorderMesh(points, (float)style.StrokeWidth, style.Stroke); mesh.Indices.AddRange(border.Indices.Select(a => a + mesh.Vertices.Count)); mesh.Vertices.AddRange(border.Vertices); } else if (item is CoreSvg.Models.Rectangle) { var points = GetPoints((CoreSvg.Models.Rectangle)item); var temp = GetMeshData((CoreSvg.Models.Rectangle)item, points, style.Fill); mesh.Indices = temp.Indices; mesh.Vertices = temp.Vertices; var border = GetBorderMesh(points, (float)style.StrokeWidth, style.Stroke); mesh.Indices.AddRange(border.Indices.Select(a => a + mesh.Vertices.Count)); mesh.Vertices.AddRange(border.Vertices); } else if (item is CoreSvg.Models.Path) { var points = GetPoints((Path)item); var temp = GetMeshData((Path)item, points, style); mesh.Indices = temp.Indices; mesh.Vertices = temp.Vertices; var border = GetBorderMesh(points, (float)style.StrokeWidth, style.Stroke); mesh.Indices.AddRange(border.Indices.Select(a => a + mesh.Vertices.Count)); mesh.Vertices.AddRange(border.Vertices); } else if (item is Ellipse) { var points = GetPoints((Ellipse)item); var temp = GetMeshData((Ellipse)item, points, style.Fill); mesh.Indices = temp.Indices; mesh.Vertices = temp.Vertices; var border = GetBorderMesh(points, (float)style.StrokeWidth, style.Stroke); mesh.Indices.AddRange(border.Indices.Select(a => a + mesh.Vertices.Count)); mesh.Vertices.AddRange(border.Vertices); } return(mesh); }